2018-10-10 09:37:24 +00:00
|
|
|
#include <QDir>
|
2019-05-01 16:15:33 +00:00
|
|
|
#include <QFile>
|
2017-12-14 15:14:33 +00:00
|
|
|
#include <QLabel>
|
2019-05-01 16:15:33 +00:00
|
|
|
#include <QPainter>
|
2017-12-14 15:14:33 +00:00
|
|
|
#include <QFontDialog>
|
2019-05-01 16:15:33 +00:00
|
|
|
#include <QFileDialog>
|
2018-10-31 16:07:53 +00:00
|
|
|
#include <QTranslator>
|
2018-10-10 09:37:24 +00:00
|
|
|
#include <QInputDialog>
|
2018-11-11 12:11:50 +00:00
|
|
|
#include <QSignalBlocker>
|
2019-05-01 16:15:33 +00:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QtSvg/QSvgRenderer>
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-02-26 22:25:23 +00:00
|
|
|
#include <QComboBox>
|
2019-10-12 05:50:10 +00:00
|
|
|
#include <QtWidgets/QSpinBox>
|
2017-12-14 15:14:33 +00:00
|
|
|
#include "PreferencesDialog.h"
|
2018-10-17 07:30:56 +00:00
|
|
|
#include "AppearanceOptionsWidget.h"
|
|
|
|
#include "ui_AppearanceOptionsWidget.h"
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
|
|
|
#include "common/Configuration.h"
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2019-05-01 16:15:33 +00:00
|
|
|
#include "common/ColorThemeWorker.h"
|
|
|
|
#include "dialogs/preferences/ColorThemeEditDialog.h"
|
|
|
|
#include "widgets/ColorPicker.h"
|
2018-11-11 12:11:50 +00:00
|
|
|
|
2019-03-16 12:41:45 +00:00
|
|
|
AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog)
|
|
|
|
: QDialog(dialog),
|
2018-10-17 07:30:56 +00:00
|
|
|
ui(new Ui::AppearanceOptionsWidget)
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2020-01-10 22:02:28 +00:00
|
|
|
updateFromConfig();
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2019-01-19 20:54:02 +00:00
|
|
|
QStringList langs = Config()->getAvailableTranslations();
|
2018-10-31 16:07:53 +00:00
|
|
|
ui->languageComboBox->addItems(langs);
|
|
|
|
|
|
|
|
QString curr = Config()->getCurrLocale().nativeLanguageName();
|
|
|
|
curr = curr.at(0).toUpper() + curr.right(curr.length() - 1);
|
|
|
|
if (!langs.contains(curr)) {
|
|
|
|
curr = "English";
|
|
|
|
}
|
|
|
|
ui->languageComboBox->setCurrentText(curr);
|
2019-05-01 16:15:33 +00:00
|
|
|
|
|
|
|
auto setIcons = [this]() {
|
|
|
|
QColor textColor = palette().text().color();
|
|
|
|
ui->editButton->setIcon(getIconFromSvg(":/img/icons/pencil_thin.svg", textColor));
|
|
|
|
ui->deleteButton->setIcon(getIconFromSvg(":/img/icons/trash_bin.svg", textColor));
|
|
|
|
ui->copyButton->setIcon(getIconFromSvg(":/img/icons/copy.svg", textColor));
|
|
|
|
ui->importButton->setIcon(getIconFromSvg(":/img/icons/download_black.svg", textColor));
|
|
|
|
ui->exportButton->setIcon(getIconFromSvg(":/img/icons/upload_black.svg", textColor));
|
|
|
|
ui->renameButton->setIcon(getIconFromSvg(":/img/icons/rename.svg", textColor));
|
|
|
|
};
|
|
|
|
setIcons();
|
|
|
|
connect(Config(), &Configuration::interfaceThemeChanged, this, setIcons);
|
|
|
|
|
2018-10-31 16:07:53 +00:00
|
|
|
connect(ui->languageComboBox,
|
|
|
|
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
|
|
|
this,
|
|
|
|
&AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged);
|
|
|
|
|
|
|
|
connect(Config(), &Configuration::fontsUpdated, this,
|
|
|
|
&AppearanceOptionsWidget::updateFontFromConfig);
|
2019-05-01 16:15:33 +00:00
|
|
|
|
|
|
|
connect(ui->colorComboBox, &QComboBox::currentTextChanged,
|
2019-06-21 15:27:21 +00:00
|
|
|
this, &AppearanceOptionsWidget::updateModificationButtons);
|
2019-10-12 05:50:10 +00:00
|
|
|
|
|
|
|
connect(ui->fontZoomBox,
|
|
|
|
static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
|
|
|
|
this,
|
|
|
|
&AppearanceOptionsWidget::onFontZoomBoxValueChanged);
|
2017-12-14 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
AppearanceOptionsWidget::~AppearanceOptionsWidget() {}
|
2017-12-14 15:14:33 +00:00
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::updateFontFromConfig()
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2019-10-12 05:50:10 +00:00
|
|
|
QFont currentFont = Config()->getBaseFont();
|
2017-12-14 15:14:33 +00:00
|
|
|
ui->fontSelectionLabel->setText(currentFont.toString());
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:15:33 +00:00
|
|
|
void AppearanceOptionsWidget::updateThemeFromConfig(bool interfaceThemeChanged)
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2018-03-01 15:30:31 +00:00
|
|
|
// Disconnect currentIndexChanged because clearing the comboxBox and refiling it causes its index to change.
|
2018-11-11 12:11:50 +00:00
|
|
|
QSignalBlocker signalBlockerThemeBox(ui->themeComboBox);
|
2018-11-29 07:39:58 +00:00
|
|
|
|
|
|
|
ui->themeComboBox->clear();
|
2019-06-12 17:46:07 +00:00
|
|
|
for (auto &it : Configuration::cutterInterfaceThemesList()) {
|
2018-11-11 12:11:50 +00:00
|
|
|
ui->themeComboBox->addItem(it.name);
|
|
|
|
}
|
2019-05-01 16:15:33 +00:00
|
|
|
int currInterfaceThemeIndex = Config()->getInterfaceTheme();
|
2019-06-12 17:46:07 +00:00
|
|
|
if (currInterfaceThemeIndex >= Configuration::cutterInterfaceThemesList().size()) {
|
2019-05-01 16:15:33 +00:00
|
|
|
currInterfaceThemeIndex = 0;
|
|
|
|
Config()->setInterfaceTheme(currInterfaceThemeIndex);
|
2018-11-11 12:11:50 +00:00
|
|
|
}
|
2019-05-01 16:15:33 +00:00
|
|
|
ui->themeComboBox->setCurrentIndex(currInterfaceThemeIndex);
|
|
|
|
ui->colorComboBox->updateFromConfig(interfaceThemeChanged);
|
2019-06-21 15:27:21 +00:00
|
|
|
updateModificationButtons(ui->colorComboBox->currentText());
|
2017-12-14 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 05:50:10 +00:00
|
|
|
void AppearanceOptionsWidget::onFontZoomBoxValueChanged(int zoom)
|
|
|
|
{
|
|
|
|
qreal zoomFactor = zoom / 100.0;
|
|
|
|
Config()->setZoomFactor(zoomFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_fontSelectionButton_clicked()
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2019-10-12 05:50:10 +00:00
|
|
|
QFont currentFont = Config()->getBaseFont();
|
2017-12-14 15:14:33 +00:00
|
|
|
bool ok;
|
2018-10-10 09:37:24 +00:00
|
|
|
QFont newFont = QFontDialog::getFont(&ok, currentFont, this, QString(),
|
|
|
|
QFontDialog::DontUseNativeDialog);
|
2017-12-14 15:14:33 +00:00
|
|
|
if (ok) {
|
|
|
|
Config()->setFont(newFont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_themeComboBox_currentIndexChanged(int index)
|
2017-12-14 15:14:33 +00:00
|
|
|
{
|
2019-05-01 16:15:33 +00:00
|
|
|
Config()->setInterfaceTheme(index);
|
2018-11-11 12:11:50 +00:00
|
|
|
updateThemeFromConfig();
|
2018-02-26 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 16:15:33 +00:00
|
|
|
void AppearanceOptionsWidget::on_editButton_clicked()
|
2018-02-26 22:25:23 +00:00
|
|
|
{
|
2019-05-01 16:15:33 +00:00
|
|
|
ColorThemeEditDialog dial;
|
|
|
|
dial.setWindowTitle(tr("Theme Editor - <%1>").arg(ui->colorComboBox->currentText()));
|
|
|
|
dial.exec();
|
|
|
|
ui->colorComboBox->updateFromConfig(false);
|
2017-12-14 15:14:33 +00:00
|
|
|
}
|
2018-10-10 09:37:24 +00:00
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_copyButton_clicked()
|
2018-10-10 09:37:24 +00:00
|
|
|
{
|
2019-05-01 16:15:33 +00:00
|
|
|
QString currColorTheme = ui->colorComboBox->currentText();
|
|
|
|
|
|
|
|
QString newThemeName;
|
2018-10-10 09:37:24 +00:00
|
|
|
do {
|
2019-05-01 16:15:33 +00:00
|
|
|
newThemeName = QInputDialog::getText(this, tr("Enter theme name"),
|
|
|
|
tr("Name:"), QLineEdit::Normal,
|
2019-05-19 17:29:01 +00:00
|
|
|
currColorTheme + tr(" - copy"))
|
|
|
|
.trimmed();
|
|
|
|
if (newThemeName.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ThemeWorker().isThemeExist(newThemeName)) {
|
|
|
|
QMessageBox::information(this, tr("Theme Copy"),
|
|
|
|
tr("Theme named %1 already exists.").arg(newThemeName));
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
2019-05-01 16:15:33 +00:00
|
|
|
|
|
|
|
ThemeWorker().copy(currColorTheme, newThemeName);
|
|
|
|
Config()->setColorTheme(newThemeName);
|
2018-11-29 07:39:58 +00:00
|
|
|
updateThemeFromConfig(false);
|
2018-10-10 09:37:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 07:30:56 +00:00
|
|
|
void AppearanceOptionsWidget::on_deleteButton_clicked()
|
2018-10-10 09:37:24 +00:00
|
|
|
{
|
2019-05-01 16:15:33 +00:00
|
|
|
QString currTheme = ui->colorComboBox->currentText();
|
|
|
|
if (!ThemeWorker().isCustomTheme(currTheme)) {
|
|
|
|
QMessageBox::critical(nullptr, tr("Error"), ThemeWorker().deleteTheme(currTheme));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int ret = QMessageBox::question(nullptr,
|
|
|
|
tr("Delete"),
|
|
|
|
tr("Are you sure you want to delete <b>%1</b>?")
|
|
|
|
.arg(currTheme));
|
|
|
|
if (ret == QMessageBox::Yes) {
|
|
|
|
QString err = ThemeWorker().deleteTheme(currTheme);
|
|
|
|
updateThemeFromConfig(false);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
QMessageBox::critical(nullptr, tr("Error"), err);
|
2018-11-11 12:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 09:37:24 +00:00
|
|
|
}
|
2018-10-31 16:07:53 +00:00
|
|
|
|
2019-05-01 16:15:33 +00:00
|
|
|
void AppearanceOptionsWidget::on_importButton_clicked()
|
|
|
|
{
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(this,
|
|
|
|
"",
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString err = ThemeWorker().importTheme(fileName);
|
|
|
|
QString themeName = QFileInfo(fileName).fileName();
|
|
|
|
if (err.isEmpty()) {
|
|
|
|
QMessageBox::information(this,
|
|
|
|
tr("Success"),
|
|
|
|
tr("Color theme <b>%1</b> was successfully imported.").arg(themeName));
|
|
|
|
Config()->setColorTheme(themeName);
|
|
|
|
updateThemeFromConfig(false);
|
|
|
|
} else {
|
|
|
|
QMessageBox::critical(this, tr("Error"), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppearanceOptionsWidget::on_exportButton_clicked()
|
|
|
|
{
|
|
|
|
QString theme = ui->colorComboBox->currentText();
|
|
|
|
QString file = QFileDialog::getSaveFileName(this,
|
|
|
|
"",
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
|
|
|
|
+ QDir::separator() + theme);
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// User already gave his consent for this in QFileDialog::getSaveFileName()
|
|
|
|
if (QFileInfo(file).exists()) {
|
|
|
|
QFile(file).remove();
|
|
|
|
}
|
|
|
|
QString err = ThemeWorker().save(ThemeWorker().getTheme(theme), file);
|
|
|
|
if (err.isEmpty()) {
|
|
|
|
QMessageBox::information(this,
|
|
|
|
tr("Success"),
|
|
|
|
tr("Color theme <b>%1</b> was successfully exported.").arg(theme));
|
|
|
|
} else {
|
|
|
|
QMessageBox::critical(this, tr("Error"), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppearanceOptionsWidget::on_renameButton_clicked()
|
|
|
|
{
|
|
|
|
QString currColorTheme = Config()->getColorTheme();
|
|
|
|
QString newName = QInputDialog::getText(this,
|
|
|
|
tr("Enter new theme name"),
|
|
|
|
tr("Name:"),
|
|
|
|
QLineEdit::Normal,
|
|
|
|
currColorTheme);
|
|
|
|
if (newName.isEmpty() || newName == currColorTheme) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString err = ThemeWorker().renameTheme(currColorTheme, newName);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
QMessageBox::critical(this, tr("Error"), err);
|
|
|
|
} else {
|
|
|
|
Config()->setColorTheme(newName);
|
|
|
|
updateThemeFromConfig(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:07:53 +00:00
|
|
|
void AppearanceOptionsWidget::onLanguageComboBoxCurrentIndexChanged(int index)
|
|
|
|
{
|
|
|
|
QString language = ui->languageComboBox->itemText(index).toLower();
|
2019-01-20 17:00:23 +00:00
|
|
|
if (Config()->setLocaleByName(language)) {
|
|
|
|
QMessageBox::information(this,
|
2019-05-01 16:15:33 +00:00
|
|
|
tr("Language settings"),
|
|
|
|
tr("Language will be changed after next application start."));
|
2019-01-20 17:00:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qWarning() << tr("Cannot set language, not found in available ones");
|
2019-05-01 16:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:27:21 +00:00
|
|
|
void AppearanceOptionsWidget::updateModificationButtons(const QString& theme)
|
|
|
|
{
|
|
|
|
bool editable = ThemeWorker().isCustomTheme(theme);
|
|
|
|
ui->editButton->setEnabled(editable);
|
|
|
|
ui->deleteButton->setEnabled(editable);
|
|
|
|
ui->renameButton->setEnabled(editable);
|
|
|
|
}
|
|
|
|
|
2020-01-10 22:02:28 +00:00
|
|
|
void AppearanceOptionsWidget::updateFromConfig()
|
|
|
|
{
|
|
|
|
updateFontFromConfig();
|
|
|
|
updateThemeFromConfig(false);
|
|
|
|
ui->fontZoomBox->setValue(qRound(Config()->getZoomFactor() * 100));
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:15:33 +00:00
|
|
|
QIcon AppearanceOptionsWidget::getIconFromSvg(const QString& fileName, const QColor& after, const QColor& before)
|
|
|
|
{
|
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
return QIcon();
|
|
|
|
}
|
|
|
|
QString data = file.readAll();
|
2019-10-13 14:59:12 +00:00
|
|
|
data.replace(QRegularExpression(QString("#%1").arg(before.isValid() ? before.name().remove(0, 1) : "[0-9a-fA-F]{6}")),
|
2019-05-01 16:15:33 +00:00
|
|
|
QString("%1").arg(after.name()));
|
|
|
|
|
|
|
|
QSvgRenderer svgRenderer(data.toUtf8());
|
|
|
|
QPixmap pix(svgRenderer.defaultSize());
|
|
|
|
pix.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter pixPainter(&pix);
|
|
|
|
svgRenderer.render(&pixPainter);
|
|
|
|
|
|
|
|
return pix;
|
|
|
|
}
|