#include "extension.h" #include "ui_extrawindow.h" #include "defs.h" #include #include #include #include #include #include #include #include #include #include #include extern const char* EXTRA_WINDOW_INFO; extern const char* TOPMOST; extern const char* SHOW_ORIGINAL; extern const char* SHOW_ORIGINAL_INFO; extern const char* SIZE_LOCK; extern const char* BG_COLOR; extern const char* TEXT_COLOR; extern const char* FONT; extern const char* FONT_SIZE; extern const char* FONT_FAMILY; extern const char* FONT_WEIGHT; extern const char* SAVE_SETTINGS; struct Window : QDialog { public: Window() { ui.setupUi(this); settings.beginGroup("Extra Window"); setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); QMetaObject::invokeMethod(this, [this] { show(); QFont font = ui.display->font(); if (font.fromString(settings.value(FONT, font.toString()).toString())) ui.display->setFont(font); setBackgroundColor(settings.value(BG_COLOR, palette().window().color()).value()); setTextColor(settings.value(TEXT_COLOR, ui.display->palette().windowText().color()).value()); setLock(settings.value(SIZE_LOCK, false).toBool()); setTopmost(settings.value(TOPMOST, false).toBool()); setGeometry(settings.value(WINDOW, geometry()).toRect()); menu.addAction(FONT, this, &Window::RequestFont); menu.addAction(BG_COLOR, [this] { setBackgroundColor(QColorDialog::getColor(bgColor, this, BG_COLOR, QColorDialog::ShowAlphaChannel)); }); menu.addAction(TEXT_COLOR, [this] { setTextColor(QColorDialog::getColor(ui.display->palette().windowText().color(), this, TEXT_COLOR, QColorDialog::ShowAlphaChannel)); }); for (auto [name, default, slot] : Array>{ { TOPMOST, false, &Window::setTopmost }, { SIZE_LOCK, false, &Window::setLock }, { SHOW_ORIGINAL, true, &Window::setShowOriginal } }) { auto action = menu.addAction(name, this, slot); action->setCheckable(true); action->setChecked(settings.value(name, default).toBool()); } connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); }); QMetaObject::invokeMethod(this, [this] { ui.display->setText(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection); }, Qt::QueuedConnection); } ~Window() { settings.setValue(WINDOW, geometry()); settings.sync(); } Ui::ExtraWindow ui; QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this }; private: void RequestFont() { QFont font = ui.display->font(); QDialog fontDialog(this, Qt::WindowCloseButtonHint); fontDialog.setWindowTitle(FONT); QFormLayout layout(&fontDialog); QLineEdit fontFamily(font.family(), &fontDialog); layout.addRow(FONT_FAMILY, &fontFamily); QSpinBox fontSize(&fontDialog); fontSize.setValue(font.pointSize()); layout.addRow(FONT_SIZE, &fontSize); QSpinBox fontWeight(&fontDialog); fontWeight.setValue(font.weight()); layout.addRow(FONT_WEIGHT, &fontWeight); QPushButton save(SAVE_SETTINGS, &fontDialog); layout.addWidget(&save); connect(&save, &QPushButton::clicked, &fontDialog, &QDialog::accept); if (!fontDialog.exec()) return; font = QFont(fontFamily.text(), fontSize.value(), fontWeight.value()); settings.setValue(FONT, font.toString()); ui.display->setFont(font); }; void setBackgroundColor(QColor color) { if (!color.isValid()) return; if (color.alpha() == 0) color.setAlpha(1); bgColor = color; repaint(); settings.setValue(BG_COLOR, color); }; void setTextColor(QColor color) { if (!color.isValid()) return; auto newPalette = ui.display->palette(); newPalette.setColor(QPalette::WindowText, color); ui.display->setPalette(newPalette); settings.setValue(TEXT_COLOR, color); }; void 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); }; void setLock(bool lock) { locked = lock; setSizeGripEnabled(!lock); settings.setValue(SIZE_LOCK, lock); }; void setShowOriginal(bool showOriginal) { if (!showOriginal) QMessageBox::information(this, SHOW_ORIGINAL, SHOW_ORIGINAL_INFO); settings.setValue(SHOW_ORIGINAL, showOriginal); }; void paintEvent(QPaintEvent*) override { QPainter(this).fillRect(rect(), bgColor); } void mousePressEvent(QMouseEvent* event) override { oldPos = event->globalPos(); } void mouseMoveEvent(QMouseEvent* event) override { const QPoint delta = event->globalPos() - oldPos; if (!locked) move(x() + delta.x(), y() + delta.y()); oldPos = event->globalPos(); } QMenu menu{ ui.display }; bool locked = true; QColor bgColor; QPoint oldPos; } window; bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { if (!sentenceInfo["current select"]) return false; QString qSentence = QString::fromStdWString(sentence); if (!window.settings.value(SHOW_ORIGINAL).toBool()) qSentence = qSentence.section('\n', qSentence.count('\n') / 2 + 1); QMetaObject::invokeMethod(&window, [=] { window.ui.display->setText(qSentence); }); return false; }