Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings UI proposal #684

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added resources/MaterialIconsOutlined-Regular.otf
Binary file not shown.
1 change: 1 addition & 0 deletions resources/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<qresource prefix="/fonts">
<file>Lato-Light.ttf</file>
<file>Lato-Regular.ttf</file>
<file>MaterialIconsOutlined-Regular.otf</file>
</qresource>
<qresource prefix="/images">
<file>checkmark.png</file>
Expand Down
5 changes: 2 additions & 3 deletions src/qvaboutdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "qvapplication.h"

#include <QFontDatabase>
#include <QJsonDocument>

QVAboutDialog::QVAboutDialog(double givenLatestVersionNum, QWidget *parent) :
Expand All @@ -25,8 +24,8 @@ QVAboutDialog::QVAboutDialog(double givenLatestVersionNum, QWidget *parent) :
#endif

// add fonts
QFontDatabase::addApplicationFont(":/fonts/Lato-Light.ttf");
QFontDatabase::addApplicationFont(":/fonts/Lato-Regular.ttf");
qvApp->ensureFontLoaded(":/fonts/Lato-Light.ttf");
qvApp->ensureFontLoaded(":/fonts/Lato-Regular.ttf");

int modifier = 0;
//set main title font
Expand Down
34 changes: 34 additions & 0 deletions src/qvapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QSettings>
#include <QTimer>
#include <QFileDialog>
#include <QFontDatabase>

QVApplication::QVApplication(int &argc, char **argv) : QApplication(argc, argv)
{
Expand Down Expand Up @@ -390,3 +391,36 @@ void QVApplication::defineFilterLists()
nameFilterList << filterString;
nameFilterList << tr("All Files") + " (*)";
}

void QVApplication::ensureFontLoaded(const QString &path)
{
if (loadedFontPaths.contains(path))
return;

QFontDatabase::addApplicationFont(path);
loadedFontPaths.insert(path);
}

QIcon QVApplication::iconFromFont(const QString &fontFamily, const QChar &codePoint, const int pixelSize, const qreal pixelRatio)
{
const int scaledPixelSize = qRound(pixelSize * pixelRatio);

QFont font(fontFamily);
font.setPixelSize(pixelSize);

QPixmap pixmap(scaledPixelSize, scaledPixelSize);
pixmap.fill(Qt::transparent);
pixmap.setDevicePixelRatio(pixelRatio);

QPainter painter(&pixmap);
painter.setFont(font);
painter.setPen(QApplication::palette().color(QPalette::WindowText));
painter.drawText(pixmap.rect(), codePoint);

return QIcon(pixmap);
}

qreal QVApplication::getPerceivedBrightness(const QColor &color)
{
return (color.red() * 0.299 + color.green() * 0.587 + color.blue() * 0.114) / 255.0;
}
8 changes: 8 additions & 0 deletions src/qvapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ class QVApplication : public QApplication

ActionManager &getActionManager() { return actionManager; }

void ensureFontLoaded(const QString &path);

static QIcon iconFromFont(const QString &fontFamily, const QChar &codePoint, const int pixelSize, const qreal pixelRatio);

static qreal getPerceivedBrightness(const QColor &color);

private:

QList<MainWindow*> lastActiveWindows;
Expand All @@ -102,6 +108,8 @@ class QVApplication : public QApplication
#ifndef QV_DISABLE_ONLINE_VERSION_CHECK
UpdateChecker updateChecker;
#endif //QV_DISABLE_ONLINE_VERSION_CHECK

QSet<QString> loadedFontPaths;
};

#endif // QVAPPLICATION_H
50 changes: 46 additions & 4 deletions src/qvoptionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ QVOptionsDialog::QVOptionsDialog(QWidget *parent) :
setAttribute(Qt::WA_DeleteOnClose);
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint | Qt::CustomizeWindowHint));

resize(640, 530);

qvApp->ensureFontLoaded(":/fonts/MaterialIconsOutlined-Regular.otf");

connect(ui->categoryList, &QListWidget::currentRowChanged, this, [this](int currentRow) { ui->stackedWidget->setCurrentIndex(currentRow); });
connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &QVOptionsDialog::buttonBoxClicked);
connect(ui->shortcutsTable, &QTableWidget::cellDoubleClicked, this, &QVOptionsDialog::shortcutCellDoubleClicked);
connect(ui->bgColorCheckbox, &QCheckBox::stateChanged, this, &QVOptionsDialog::bgColorCheckboxStateChanged);
connect(ui->scalingCheckbox, &QCheckBox::stateChanged, this, &QVOptionsDialog::scalingCheckboxStateChanged);

populateLanguages();

QSettings settings;
ui->tabWidget->setCurrentIndex(settings.value("optionstab", 1).toInt());

populateCategories(settings.value("optionstab", 1).toInt());
populateLanguages();

// On macOS, the dialog should not be dependent on any window
#ifndef Q_OS_MACOS
Expand Down Expand Up @@ -79,12 +84,21 @@ QVOptionsDialog::~QVOptionsDialog()
delete ui;
}

void QVOptionsDialog::changeEvent(QEvent *event)
{
if (event->type() == QEvent::PaletteChange)
{
populateCategories(ui->categoryList->currentRow());
}
QDialog::changeEvent(event);
}

void QVOptionsDialog::done(int r)
{
// Save window geometry
QSettings settings;
settings.setValue("optionsgeometry", saveGeometry());
settings.setValue("optionstab", ui->tabWidget->currentIndex());
settings.setValue("optionstab", ui->categoryList->currentRow());

QDialog::done(r);
}
Expand Down Expand Up @@ -452,6 +466,34 @@ void QVOptionsDialog::windowResizeComboBoxCurrentIndexChanged(int index)
ui->maxWindowResizeSpinBox->setEnabled(enableRelatedControls);
}

void QVOptionsDialog::populateCategories(int selectedRow)
{
const int iconSize = 24;
const int listRightPadding = 3;
auto addItem = [&](const QChar &iconChar, const QString &text) {
ui->categoryList->addItem(new QListWidgetItem(qvApp->iconFromFont("Material Icons Outlined", iconChar, iconSize, devicePixelRatioF()), text));
};
ui->categoryList->setIconSize(QSize(iconSize, iconSize));
ui->categoryList->setFont(QApplication::font());
const QString currentStyle = qApp->style()->objectName();
if (currentStyle.compare("fusion", Qt::CaseInsensitive) == 0 ||
currentStyle.compare("macos", Qt::CaseInsensitive) == 0)
{
const QColor textColor = QApplication::palette().color(QPalette::WindowText);
QPalette palette = ui->categoryList->palette();
palette.setColor(QPalette::HighlightedText, textColor);
palette.setColor(QPalette::Highlight, qvApp->getPerceivedBrightness(textColor) > 0.5 ? QColor(0, 65, 127) : QColor(75, 166, 255));
ui->categoryList->setPalette(palette);
}
ui->categoryList->clear();
addItem(u'\ue069', tr("Window"));
addItem(u'\ue3f4', tr("Image"));
addItem(u'\ue429', tr("Miscellaneous"));
addItem(u'\ue312', tr("Shortcuts"));
ui->categoryList->setCurrentRow(selectedRow);
ui->categoryList->setFixedWidth(ui->categoryList->sizeHintForColumn(0) + ui->categoryList->frameWidth() + listRightPadding);
}

void QVOptionsDialog::populateLanguages()
{
ui->langComboBox->clear();
Expand Down
2 changes: 2 additions & 0 deletions src/qvoptionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class QVOptionsDialog : public QDialog
~QVOptionsDialog() override;

protected:
void changeEvent(QEvent *event) override;
void done(int r) override;

void modifySetting(QString key, QVariant value);
Expand All @@ -40,6 +41,7 @@ class QVOptionsDialog : public QDialog
void updateButtonBox();
void bgColorButtonClicked();
void updateBgColorButton();
void populateCategories(int selectedRow);
void populateLanguages();

private slots:
Expand Down
Loading