diff --git a/src/represent/resultwidget.cpp b/src/represent/resultwidget.cpp index b2e82ac..1f75fe2 100644 --- a/src/represent/resultwidget.cpp +++ b/src/represent/resultwidget.cpp @@ -18,6 +18,7 @@ ResultWidget::ResultWidget(Representer &representer, const Settings &settings, , settings_(settings) , image_(new QLabel(this)) , recognized_(new QLabel(this)) + , separator_(new QLabel(this)) , translated_(new QLabel(this)) , contextMenu_(new QMenu(this)) { @@ -30,11 +31,12 @@ ResultWidget::ResultWidget(Representer &representer, const Settings &settings, image_->setAlignment(Qt::AlignCenter); - recognized_->setObjectName("recognizeLabel"); recognized_->setAlignment(Qt::AlignCenter); recognized_->setWordWrap(true); - translated_->setObjectName("translateLabel"); + separator_->setFixedHeight(1); + separator_->setAutoFillBackground(true); + translated_->setAlignment(Qt::AlignCenter); translated_->setWordWrap(true); @@ -50,23 +52,18 @@ ResultWidget::ResultWidget(Representer &representer, const Settings &settings, this, &ResultWidget::edit); } - const auto styleSheet = - "#recognizeLabel, #translateLabel {" - "color: black;" - "background: qlineargradient(x1:0, y1:0, x2:1, y2:1," - "stop:0 darkGray, stop: 0.5 lightGray, stop:1 darkGray);" - "}"; - setStyleSheet(styleSheet); - installEventFilter(this); auto layout = new QVBoxLayout(this); layout->addWidget(image_); layout->addWidget(recognized_); + layout->addWidget(separator_); layout->addWidget(translated_); layout->setMargin(0); - layout->setSpacing(1); + layout->setSpacing(0); + + updateSettings(); } const TaskPtr &ResultWidget::task() const @@ -90,6 +87,7 @@ void ResultWidget::show(const TaskPtr &task) const auto gotTranslation = !task->translated.isEmpty(); translated_->setVisible(gotTranslation); + separator_->setVisible(gotTranslation); const auto mustShowRecognized = settings_.showRecognized || !gotTranslation; recognized_->setVisible(mustShowRecognized); @@ -97,6 +95,10 @@ void ResultWidget::show(const TaskPtr &task) show(); adjustSize(); + if (!image_->isVisible()) + resize(std::max(width(), task->captured.width()), + std::max(height(), task->captured.height())); + QDesktopWidget *desktop = QApplication::desktop(); Q_CHECK_PTR(desktop); const auto correction = @@ -113,9 +115,12 @@ void ResultWidget::show(const TaskPtr &task) const auto isTextOnTop = layout->indexOf(recognized_) == 0; if (isTextOnTop != shouldTextOnTop) { layout->removeWidget(recognized_); + layout->removeWidget(separator_); layout->removeWidget(translated_); - layout->insertWidget(shouldTextOnTop ? 0 : 1, recognized_); - layout->insertWidget(shouldTextOnTop ? 1 : 2, translated_); + auto index = shouldTextOnTop ? 0 : 1; + layout->insertWidget(index, recognized_); + layout->insertWidget(++index, separator_); + layout->insertWidget(++index, translated_); } move(rect.topLeft()); @@ -125,10 +130,20 @@ void ResultWidget::show(const TaskPtr &task) void ResultWidget::updateSettings() { - // explicit font change because of stylesheet QFont font(settings_.fontFamily, settings_.fontSize); - recognized_->setFont(font); - translated_->setFont(font); + setFont(font); + + auto palette = this->palette(); + const auto &backgroundColor = settings_.backgroundColor; + palette.setColor(QPalette::Window, backgroundColor); + palette.setColor(QPalette::WindowText, settings_.fontColor); + setPalette(palette); + + const auto separatorColor = backgroundColor.lightness() > 150 + ? backgroundColor.darker() + : backgroundColor.lighter(); + palette.setColor(QPalette::Window, separatorColor); + separator_->setPalette(palette); image_->setVisible(settings_.showCaptured); } diff --git a/src/represent/resultwidget.h b/src/represent/resultwidget.h index f4212e8..6f645a3 100644 --- a/src/represent/resultwidget.h +++ b/src/represent/resultwidget.h @@ -31,6 +31,7 @@ private: TaskPtr task_; QLabel* image_; QLabel* recognized_; + QLabel* separator_; QLabel* translated_; QMenu* contextMenu_; }; diff --git a/src/settings.cpp b/src/settings.cpp index 2c2b4e8..3213d3a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -45,6 +45,8 @@ const QString qs_translators = "translators"; const QString qs_representationGroup = "Representation"; const QString qs_fontFamily = "fontFamily"; const QString qs_fontSize = "fontSize"; +const QString qs_fontColor = "fontColor"; +const QString qs_backgroundColor = "backgroundColor"; const QString qs_showRecognized = "showRecognized"; const QString qs_showCaptured = "showCaptured"; @@ -188,6 +190,8 @@ void Settings::save() const settings.setValue(qs_fontFamily, fontFamily); settings.setValue(qs_fontSize, fontSize); + settings.setValue(qs_fontColor, fontColor.name()); + settings.setValue(qs_backgroundColor, backgroundColor.name()); settings.setValue(qs_showRecognized, showRecognized); settings.setValue(qs_showCaptured, showCaptured); @@ -281,6 +285,9 @@ void Settings::load() const auto defaultFont = QApplication::font().family(); fontFamily = settings.value(qs_fontFamily, defaultFont).toString(); fontSize = std::clamp(settings.value(qs_fontSize, fontSize).toInt(), 6, 24); + fontColor = QColor(settings.value(qs_fontColor, fontColor.name()).toString()); + backgroundColor = QColor( + settings.value(qs_backgroundColor, backgroundColor.name()).toString()); showRecognized = settings.value(qs_showRecognized, showRecognized).toBool(); showCaptured = settings.value(qs_showCaptured, showCaptured).toBool(); diff --git a/src/settings.h b/src/settings.h index c1005bb..5f41e39 100644 --- a/src/settings.h +++ b/src/settings.h @@ -2,6 +2,7 @@ #include "stfwd.h" +#include #include #include @@ -66,6 +67,8 @@ public: ResultMode resultShowType{ResultMode::Widget}; // dialog QString fontFamily; int fontSize{11}; + QColor fontColor{Qt::black}; + QColor backgroundColor{Qt::lightGray}; bool showRecognized{true}; bool showCaptured{true}; diff --git a/src/settingseditor.cpp b/src/settingseditor.cpp index db8b4c3..62dea0b 100644 --- a/src/settingseditor.cpp +++ b/src/settingseditor.cpp @@ -8,6 +8,7 @@ #include "updates.h" #include "widgetstate.h" +#include #include #include #include @@ -68,12 +69,19 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater) ui->translateLangCombo->setModel(models_.targetLanguageModel()); // representation + ui->fontColor->setAutoFillBackground(true); + ui->backgroundColor->setAutoFillBackground(true); + ui->backgroundColor->setText(tr("Sample text")); connect(ui->dialogRadio, &QRadioButton::toggled, // ui->resultWindow, &QTableWidget::setEnabled); connect(ui->resultFont, &QFontComboBox::currentFontChanged, // this, &SettingsEditor::updateResultFont); connect(ui->resultFontSize, qOverload(&QSpinBox::valueChanged), // this, &SettingsEditor::updateResultFont); + connect(ui->fontColor, &QPushButton::clicked, // + this, [this] { pickColor(ColorContext::Font); }); + connect(ui->backgroundColor, &QPushButton::clicked, // + this, [this] { pickColor(ColorContext::Bagkround); }); // updates auto updatesProxy = new QSortFilterProxyModel(this); @@ -149,6 +157,9 @@ Settings SettingsEditor::settings() const ui->trayRadio->isChecked() ? ResultMode::Tooltip : ResultMode::Widget; settings.fontFamily = ui->resultFont->currentFont().family(); settings.fontSize = ui->resultFontSize->value(); + settings.fontColor = ui->fontColor->palette().color(QPalette::Button); + settings.backgroundColor = + ui->backgroundColor->palette().color(QPalette::Button); settings.showRecognized = ui->showRecognized->isChecked(); settings.showCaptured = ui->showCaptured->isChecked(); @@ -202,6 +213,14 @@ void SettingsEditor::setSettings(const Settings &settings) ui->dialogRadio->setChecked(settings.resultShowType == ResultMode::Widget); ui->resultFont->setCurrentFont(QFont(settings.fontFamily)); ui->resultFontSize->setValue(settings.fontSize); + { + QPalette palette(this->palette()); + palette.setColor(QPalette::Button, settings.fontColor); + ui->fontColor->setPalette(palette); + palette.setColor(QPalette::ButtonText, settings.fontColor); + palette.setColor(QPalette::Button, settings.backgroundColor); + ui->backgroundColor->setPalette(palette); + } ui->showRecognized->setChecked(settings.showRecognized); ui->showCaptured->setChecked(settings.showCaptured); @@ -296,3 +315,26 @@ void SettingsEditor::updateModels(const QString &tessdataPath) models_.update(tessdataPath); ui->tesseractLangCombo->setCurrentText(source); } + +void SettingsEditor::pickColor(ColorContext context) +{ + const auto widget = + context == ColorContext::Font ? ui->fontColor : ui->backgroundColor; + const auto original = widget->palette().color(QPalette::Button); + const auto color = QColorDialog::getColor(original, this); + + if (!color.isValid()) + return; + + QPalette palette(widget->palette()); + palette.setColor(QPalette::Button, color); + widget->setPalette(palette); + + if (context == ColorContext::Bagkround) + return; + + palette = ui->backgroundColor->palette(); + palette.setColor(QPalette::ButtonText, color); + ui->backgroundColor->setPalette(palette); + ui->backgroundColor->update(); +} diff --git a/src/settingseditor.h b/src/settingseditor.h index 882c01b..0d41399 100644 --- a/src/settingseditor.h +++ b/src/settingseditor.h @@ -23,6 +23,7 @@ public: void setSettings(const Settings &settings); private: + enum ColorContext { Font, Bagkround }; void updateCurrentPage(); void updateTranslators(); void adjustUpdatesView(); @@ -30,6 +31,7 @@ private: void handlePortableChanged(); void updateResultFont(); void updateModels(const QString &tessdataPath); + void pickColor(ColorContext context); Ui::SettingsEditor *ui; Manager &manager_; diff --git a/src/settingseditor.ui b/src/settingseditor.ui index cb29a99..e5e70e2 100644 --- a/src/settingseditor.ui +++ b/src/settingseditor.ui @@ -436,6 +436,23 @@ Result window + + + + Font: + + + + + + + + + + Font size: + + + @@ -446,31 +463,48 @@ - - - - - + + - Font: + Font color: - - + + - Font size: + + + + true - + + + + Background: + + + + + + + + + + true + + + + Show image - + Show recognized