From a53af3d0fdedb8528b59f4c20a5b57b02ad2f522 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 1 Jan 2019 22:40:29 -0500 Subject: [PATCH] lots of refactors and fixes on extra window --- extensions/extrawindow.cpp | 55 +++++++++++++++++++++----------------- include/text.h | 5 ++++ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/extensions/extrawindow.cpp b/extensions/extrawindow.cpp index e03f48e..14daef3 100644 --- a/extensions/extrawindow.cpp +++ b/extensions/extrawindow.cpp @@ -16,10 +16,12 @@ std::mutex m; struct : QDialog { +public: void launch() { 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); setAttribute(Qt::WA_TranslucentBackground); setSizeGripEnabled(true); @@ -31,7 +33,7 @@ struct : QDialog if (color.alpha() == 0) color.setAlpha(1); bgColor = color; repaint(); - settings->setValue("BG Color", color); + settings->setValue(BG_COLOR, color); }; auto setTextColor = [=](QColor color) { @@ -39,58 +41,61 @@ struct : QDialog auto newPalette = display->palette(); newPalette.setColor(QPalette::WindowText, color); display->setPalette(newPalette); - settings->setValue("Text Color", color); + settings->setValue(TEXT_COLOR, color); }; auto setFontSize = [=](int pt) { QFont newFont = display->font(); newFont.setPointSize(pt); display->setFont(newFont); - settings->setValue("Font Size", pt); + settings->setValue(FONT_SIZE, pt); }; auto setTopmost = [=](bool topmost) { 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()); - setTopmost(settings->value("Topmost", false).toBool()); - setFontSize(settings->value("Font Size", 16).toInt()); - setBackgroundColor(settings->value("BG Color", palette().window().color()).value()); - setTextColor(settings->value("Text Color", display->palette().windowText().color()).value()); + setGeometry(settings->value(WINDOW, geometry()).toRect()); + setTopmost(settings->value(TOPMOST, false).toBool()); + setFontSize(settings->value(FONT_SIZE, 16).toInt()); + setBackgroundColor(settings->value(BG_COLOR, palette().window().color()).value()); + setTextColor(settings->value(TEXT_COLOR, display->palette().windowText().color()).value()); auto menu = new QMenu(this); - menu->addAction("Topmost", setTopmost)->setCheckable(true); - menu->addAction("BG Color", [=] { setBackgroundColor(QColorDialog::getColor(palette().window().color(), this, "BG Color", QColorDialog::ShowAlphaChannel)); }); - menu->addAction("Text Color", [=] { setTextColor(QColorDialog::getColor(display->palette().windowText().color(), this, "Text Color")); }); - menu->addAction("Font Size", [=] { setFontSize(QInputDialog::getInt(this, "Font Size", "", display->font().pointSize(), 0, INT_MAX, 1, nullptr, Qt::WindowCloseButtonHint)); }); + auto topmost = menu->addAction(TOPMOST, setTopmost); + topmost->setCheckable(true); + topmost->setChecked(settings->value(TOPMOST).toBool()); + 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); 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 { 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()); - oldPos = evt->globalPos(); + oldPos = event->globalPos(); } - QColor bgColor = QPalette().window().color(); + QColor bgColor; QPoint oldPos; - - QSettings* settings = new QSettings(CONFIG_FILE, QSettings::IniFormat, this); - QLabel* display; }*window = nullptr; 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: { 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 { delete window; diff --git a/include/text.h b/include/text.h index 9b2be69..a792278 100644 --- a/include/text.h +++ b/include/text.h @@ -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 TOO_MANY_TRANS_REQUESTS = L"\r\nToo many translation requests: refuse to make more"; 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 REGEX_FILTER = u8"Regex Filter"; constexpr auto INVALID_REGEX = u8"Invalid regex";