lots of refactors and fixes on extra window

This commit is contained in:
Akash Mozumdar 2019-01-01 22:40:29 -05:00
parent 479c73d029
commit a53af3d0fd
2 changed files with 35 additions and 25 deletions

View File

@ -16,10 +16,12 @@ std::mutex m;
struct : QDialog struct : QDialog
{ {
public:
void launch() void launch()
{ {
settings->beginGroup("Extra Window"); settings->beginGroup("Extra Window");
(new QHBoxLayout(this))->addWidget(display = new QLabel("Right click to change settings", this)); (new QHBoxLayout(this))->addWidget(display = new QLabel(EXTRA_WINDOW_INFO, this));
display->setAlignment(Qt::AlignTop);
setWindowFlags(Qt::FramelessWindowHint); setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground); setAttribute(Qt::WA_TranslucentBackground);
setSizeGripEnabled(true); setSizeGripEnabled(true);
@ -31,7 +33,7 @@ struct : QDialog
if (color.alpha() == 0) color.setAlpha(1); if (color.alpha() == 0) color.setAlpha(1);
bgColor = color; bgColor = color;
repaint(); repaint();
settings->setValue("BG Color", color); settings->setValue(BG_COLOR, color);
}; };
auto setTextColor = [=](QColor color) auto setTextColor = [=](QColor color)
{ {
@ -39,58 +41,61 @@ struct : QDialog
auto newPalette = display->palette(); auto newPalette = display->palette();
newPalette.setColor(QPalette::WindowText, color); newPalette.setColor(QPalette::WindowText, color);
display->setPalette(newPalette); display->setPalette(newPalette);
settings->setValue("Text Color", color); settings->setValue(TEXT_COLOR, color);
}; };
auto setFontSize = [=](int pt) auto setFontSize = [=](int pt)
{ {
QFont newFont = display->font(); QFont newFont = display->font();
newFont.setPointSize(pt); newFont.setPointSize(pt);
display->setFont(newFont); display->setFont(newFont);
settings->setValue("Font Size", pt); settings->setValue(FONT_SIZE, pt);
}; };
auto setTopmost = [=](bool topmost) auto setTopmost = [=](bool topmost)
{ {
SetWindowPos((HWND)winId(), topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); SetWindowPos((HWND)winId(), topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
settings->setValue("Topmost", topmost); settings->setValue(TOPMOST, topmost);
}; };
setGeometry(settings->value("Window", geometry()).toRect()); setGeometry(settings->value(WINDOW, geometry()).toRect());
setTopmost(settings->value("Topmost", false).toBool()); setTopmost(settings->value(TOPMOST, false).toBool());
setFontSize(settings->value("Font Size", 16).toInt()); setFontSize(settings->value(FONT_SIZE, 16).toInt());
setBackgroundColor(settings->value("BG Color", palette().window().color()).value<QColor>()); setBackgroundColor(settings->value(BG_COLOR, palette().window().color()).value<QColor>());
setTextColor(settings->value("Text Color", display->palette().windowText().color()).value<QColor>()); setTextColor(settings->value(TEXT_COLOR, display->palette().windowText().color()).value<QColor>());
auto menu = new QMenu(this); auto menu = new QMenu(this);
menu->addAction("Topmost", setTopmost)->setCheckable(true); auto topmost = menu->addAction(TOPMOST, setTopmost);
menu->addAction("BG Color", [=] { setBackgroundColor(QColorDialog::getColor(palette().window().color(), this, "BG Color", QColorDialog::ShowAlphaChannel)); }); topmost->setCheckable(true);
menu->addAction("Text Color", [=] { setTextColor(QColorDialog::getColor(display->palette().windowText().color(), this, "Text Color")); }); topmost->setChecked(settings->value(TOPMOST).toBool());
menu->addAction("Font Size", [=] { setFontSize(QInputDialog::getInt(this, "Font Size", "", display->font().pointSize(), 0, INT_MAX, 1, nullptr, Qt::WindowCloseButtonHint)); }); menu->addAction(BG_COLOR, [=] { setBackgroundColor(QColorDialog::getColor(bgColor, this, BG_COLOR, QColorDialog::ShowAlphaChannel)); });
menu->addAction(TEXT_COLOR, [=] { setTextColor(QColorDialog::getColor(display->palette().windowText().color(), this, TEXT_COLOR, QColorDialog::ShowAlphaChannel)); });
menu->addAction(FONT_SIZE, [=] { setFontSize(QInputDialog::getInt(this, FONT_SIZE, "", display->font().pointSize(), 0, INT_MAX, 1, nullptr, Qt::WindowCloseButtonHint)); });
setContextMenuPolicy(Qt::CustomContextMenu); setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QDialog::customContextMenuRequested, menu, [=](QPoint point) { menu->exec(mapToGlobal(point)); }); connect(this, &QDialog::customContextMenuRequested, menu, [=](QPoint point) { menu->exec(mapToGlobal(point)); });
connect(this, &QDialog::destroyed, [=] { settings->setValue("Window", geometry()); }); connect(this, &QDialog::destroyed, [=] { settings->setValue(WINDOW, geometry()); });
} }
QSettings* settings = new QSettings(CONFIG_FILE, QSettings::IniFormat, this);
QLabel* display;
private:
void paintEvent(QPaintEvent*) override void paintEvent(QPaintEvent*) override
{ {
QPainter(this).fillRect(rect(), bgColor); QPainter(this).fillRect(rect(), bgColor);
} }
void mousePressEvent(QMouseEvent* evt) void mousePressEvent(QMouseEvent* event)
{ {
oldPos = evt->globalPos(); oldPos = event->globalPos();
} }
void mouseMoveEvent(QMouseEvent* evt) void mouseMoveEvent(QMouseEvent* event)
{ {
const QPoint delta = evt->globalPos() - oldPos; const QPoint delta = event->globalPos() - oldPos;
move(x() + delta.x(), y() + delta.y()); move(x() + delta.x(), y() + delta.y());
oldPos = evt->globalPos(); oldPos = event->globalPos();
} }
QColor bgColor = QPalette().window().color(); QColor bgColor;
QPoint oldPos; QPoint oldPos;
QSettings* settings = new QSettings(CONFIG_FILE, QSettings::IniFormat, this);
QLabel* display;
}*window = nullptr; }*window = nullptr;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
@ -109,7 +114,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
{ {
std::lock_guard l(m); std::lock_guard l(m);
if (window != nullptr) window->settings->setValue("Window", window->geometry()); if (window != nullptr) window->settings->setValue(WINDOW, window->geometry());
if (lpReserved == NULL) // https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683 if (lpReserved == NULL) // https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683
{ {
delete window; delete window;

View File

@ -61,6 +61,11 @@ constexpr auto BING_PROMPT = u8"What language should Bing translate to?";
constexpr auto GOOGLE_PROMPT = u8"What language should Google translate to?"; constexpr auto GOOGLE_PROMPT = u8"What language should Google translate to?";
constexpr auto TOO_MANY_TRANS_REQUESTS = L"\r\nToo many translation requests: refuse to make more"; constexpr auto TOO_MANY_TRANS_REQUESTS = L"\r\nToo many translation requests: refuse to make more";
constexpr auto TRANSLATION_ERROR = L"Error while translating"; constexpr auto TRANSLATION_ERROR = L"Error while translating";
constexpr auto EXTRA_WINDOW_INFO = u8"Right click to change settings";
constexpr auto BG_COLOR = u8"Background Color";
constexpr auto TEXT_COLOR = u8"Text Color";
constexpr auto FONT_SIZE = u8"Font Size";
constexpr auto TOPMOST = u8"Always on Top";
constexpr auto ALWAYS_ON_TOP = u8"Keep this window on top"; constexpr auto ALWAYS_ON_TOP = u8"Keep this window on top";
constexpr auto REGEX_FILTER = u8"Regex Filter"; constexpr auto REGEX_FILTER = u8"Regex Filter";
constexpr auto INVALID_REGEX = u8"Invalid regex"; constexpr auto INVALID_REGEX = u8"Invalid regex";