Add ability to change result font and background colors
This commit is contained in:
parent
ac1267c88d
commit
e70f5c21a5
@ -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);
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ private:
|
||||
TaskPtr task_;
|
||||
QLabel* image_;
|
||||
QLabel* recognized_;
|
||||
QLabel* separator_;
|
||||
QLabel* translated_;
|
||||
QMenu* contextMenu_;
|
||||
};
|
||||
|
@ -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();
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include "stfwd.h"
|
||||
|
||||
#include <QColor>
|
||||
#include <QDateTime>
|
||||
#include <QStringList>
|
||||
|
||||
@ -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};
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "updates.h"
|
||||
#include "widgetstate.h"
|
||||
|
||||
#include <QColorDialog>
|
||||
#include <QFileDialog>
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QStringListModel>
|
||||
@ -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<int>(&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();
|
||||
}
|
||||
|
@ -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_;
|
||||
|
@ -436,6 +436,23 @@
|
||||
<string>Result window</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_8">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label_18">
|
||||
<property name="text">
|
||||
<string>Font:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QFontComboBox" name="resultFont"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_19">
|
||||
<property name="text">
|
||||
<string>Font size:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QSpinBox" name="resultFontSize">
|
||||
<property name="minimum">
|
||||
@ -446,31 +463,48 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QFontComboBox" name="resultFont"/>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label_18">
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_20">
|
||||
<property name="text">
|
||||
<string>Font:</string>
|
||||
<string>Font color:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_19">
|
||||
<item row="2" column="1">
|
||||
<widget class="QPushButton" name="fontColor">
|
||||
<property name="text">
|
||||
<string>Font size:</string>
|
||||
<string/>
|
||||
</property>
|
||||
<property name="flat">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_21">
|
||||
<property name="text">
|
||||
<string>Background:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QPushButton" name="backgroundColor">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="flat">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="2">
|
||||
<widget class="QCheckBox" name="showCaptured">
|
||||
<property name="text">
|
||||
<string>Show image</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<item row="5" column="0" colspan="2">
|
||||
<widget class="QCheckBox" name="showRecognized">
|
||||
<property name="text">
|
||||
<string>Show recognized</string>
|
||||
|
Loading…
Reference in New Issue
Block a user