Skip to content

Commit

Permalink
don't store huge binary blobs in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolcha committed Sep 7, 2024
1 parent b78f89c commit 0c410ce
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 74 deletions.
48 changes: 48 additions & 0 deletions app/config/config_sections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,56 @@

#include "config_sections.hpp"

#include <QFile>

// complex default values should be implemneted here

QBrush SectionAppearance::getTexture() const
{
switch (getTextureType()) {
case None:
return Qt::NoBrush;
case SolidColor:
return getTextureColor();
case Gradient:
return getTextureGradient();
case Pattern:
return getTexturePattern();
}
Q_UNREACHABLE();
return Qt::NoBrush;
}

QPixmap SectionAppearance::getTexturePattern() const
{
if (QFile::exists(getTexturePatternFile()))
return QPixmap(getTexturePatternFile());
return sample_pattern();
}

QBrush SectionAppearance::getBackground() const
{
switch (getBackgroundType()) {
case None:
return Qt::NoBrush;
case SolidColor:
return getBackgroundColor();
case Gradient:
return getBackgroundGradient();
case Pattern:
return getBackgroundPattern();
}
Q_UNREACHABLE();
return Qt::NoBrush;
}

QPixmap SectionAppearance::getBackgroundPattern() const
{
if (QFile::exists(getBackgroundPatternFile()))
return QPixmap(getBackgroundPatternFile());
return sample_pattern();
}

QFont SectionAppearance::default_font()
{
#ifdef Q_OS_WINDOWS
Expand Down
25 changes: 23 additions & 2 deletions app/config/config_sections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#pragma once

#include <QFont>
#include <QObject>

#include "config/settings_storage.hpp"
#include "config/sample_brushes.hpp"

// even it may look as unnecessary and unused header,
// it is requeired due to some templates behind macro
Expand Down Expand Up @@ -52,7 +54,16 @@ class SectionLimits : public SettingsStorageClient {
// per-window settings

class SectionAppearance : public SettingsStorageClient {
Q_GADGET
public:
enum CustomizationType {
None,
SolidColor,
Gradient,
Pattern,
};
Q_ENUM(CustomizationType)

using SettingsStorageClient::SettingsStorageClient;

CONFIG_OPTION_Q(int, ScalingH, 100)
Expand All @@ -79,11 +90,21 @@ class SectionAppearance : public SettingsStorageClient {
CONFIG_OPTION_Q(QString, LayoutConfig, QString())
CONFIG_OPTION_Q(int, SecondsScaleFactor, 100)

CONFIG_OPTION_Q(QBrush, Texture, QColor(112, 96, 240))
QBrush getTexture() const;
CONFIG_OPTION_Q(CustomizationType, TextureType, SolidColor)
CONFIG_OPTION_Q(QColor, TextureColor, QColor(112, 96, 240))
CONFIG_OPTION_Q(QGradient, TextureGradient, sample_conical_gradient())
QPixmap getTexturePattern() const;
CONFIG_OPTION_Q(QString, TexturePatternFile, QString())
CONFIG_OPTION_Q(bool, TextureStretch, false)
CONFIG_OPTION_Q(bool, TexturePerCharacter, true)

CONFIG_OPTION_Q(QBrush, Background, Qt::NoBrush)
QBrush getBackground() const;
CONFIG_OPTION_Q(CustomizationType, BackgroundType, None)
CONFIG_OPTION_Q(QColor, BackgroundColor, QColor(0, 0, 0, 160))
CONFIG_OPTION_Q(QGradient, BackgroundGradient, sample_linear_gradient())
QPixmap getBackgroundPattern() const;
CONFIG_OPTION_Q(QString, BackgroundPatternFile, QString())
CONFIG_OPTION_Q(bool, BackgroundStretch, false)
CONFIG_OPTION_Q(bool, BackgroundPerCharacter, false)

Expand Down
116 changes: 46 additions & 70 deletions app/gui/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@
#include "core/application.hpp"
#include "platform/autostart.h"
#include "plugin_list_item_widget.hpp"
#include "common_appearance_state.hpp"

namespace {

class SettingsDialogState : public CommonAppearanceState {
public:
using CommonAppearanceState::CommonAppearanceState;
};

int setIndexByValue(QComboBox* box, const QVariant& value)
{
auto idx = box->findData(value);
Expand Down Expand Up @@ -413,46 +407,41 @@ void SettingsDialog::on_colorization_strength_edit_valueChanged(int arg1)

void SettingsDialog::on_texture_group_clicked(bool checked)
{
SettingsDialogState state(app->config().window(_curr_idx).state());

QBrush brush(Qt::NoBrush);
auto& acfg = app->config().window(_curr_idx).appearance();

if (checked) {
if (ui->tx_options_box->currentIndex() == 0)
brush = QBrush(state.getTextureColor());
acfg.setTextureType(SectionAppearance::SolidColor);

if (ui->tx_options_box->currentIndex() == 1)
brush = QBrush(state.getTextureGradient());
acfg.setTextureType(SectionAppearance::Gradient);

if (ui->tx_options_box->currentIndex() == 2)
brush = QBrush(state.getTexturePattern());
acfg.setTextureType(SectionAppearance::Pattern);
} else {
acfg.setTextureType(SectionAppearance::None);
}

app->config().window(_curr_idx).appearance().setTexture(brush);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), brush);
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, brush);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), acfg.getTexture());
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, acfg.getTexture());
}

void SettingsDialog::on_tx_options_box_activated(int index)
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto& acfg = app->config().window(_curr_idx).appearance();
switch (index) {
case 0: // color
app->config().window(_curr_idx).appearance().setTexture(state.getTextureColor());
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), state.getTextureColor());
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, state.getTextureColor());
acfg.setTextureType(SectionAppearance::SolidColor);
break;
case 1: // gradient
app->config().window(_curr_idx).appearance().setTexture(state.getTextureGradient());
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), state.getTextureGradient());
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, state.getTextureGradient());
acfg.setTextureType(SectionAppearance::Gradient);
break;
case 2: // pattern
app->config().window(_curr_idx).appearance().setTexture(state.getTexturePattern());
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), state.getTexturePattern());
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, state.getTexturePattern());
acfg.setTextureType(SectionAppearance::Pattern);
break;
}
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), acfg.getTexture());
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, acfg.getTexture());
}

void SettingsDialog::on_tx_options_box_currentIndexChanged(int index)
Expand Down Expand Up @@ -483,45 +472,41 @@ void SettingsDialog::on_tx_options_box_currentIndexChanged(int index)

void SettingsDialog::tx_select_color()
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto color = QColorDialog::getColor(state.getTextureColor(),
auto& acfg = app->config().window(_curr_idx).appearance();
auto color = QColorDialog::getColor(acfg.getTextureColor(),
this,
QString(),
QColorDialog::ShowAlphaChannel);
if (!color.isValid()) return;
app->config().window(_curr_idx).appearance().setTexture(color);
state.setTextureColor(color);
acfg.setTextureColor(color);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), color);
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, color);
}

void SettingsDialog::tx_select_gradient()
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto& acfg = app->config().window(_curr_idx).appearance();
bool ok = false;
auto gradient = GradientDialog::getGradient(&ok,
state.getTextureGradient(),
acfg.getTextureGradient(),
this);
if (!ok) return;
gradient.setCoordinateMode(QGradient::ObjectMode);
app->config().window(_curr_idx).appearance().setTexture(gradient);
state.setTextureGradient(gradient);
acfg.setTextureGradient(gradient);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), gradient);
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, gradient);
}

void SettingsDialog::tx_select_pattern()
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto& acfg = app->config().window(_curr_idx).appearance();
auto file = QFileDialog::getOpenFileName(this,
QString(),
_last_path,
acfg.getTexturePatternFile(),
tr("Images (*.png *.bmp *.jpg)"));
if (file.isEmpty()) return;
_last_path = file;
QPixmap pxm(file);
app->config().window(_curr_idx).appearance().setTexture(pxm);
state.setTexturePattern(pxm);
acfg.setTexturePatternFile(file);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setTexture), pxm);
notifyOptionChanged(&SettingsChangeTransmitter::setTexture, pxm);
}
Expand All @@ -542,46 +527,41 @@ void SettingsDialog::on_tx_per_element_cb_clicked(bool checked)

void SettingsDialog::on_background_group_clicked(bool checked)
{
SettingsDialogState state(app->config().window(_curr_idx).state());

QBrush brush(Qt::NoBrush);
auto& acfg = app->config().window(_curr_idx).appearance();

if (checked) {
if (ui->bg_options_box->currentIndex() == 0)
brush = QBrush(state.getBackgroundColor());
acfg.setBackgroundType(SectionAppearance::SolidColor);

if (ui->bg_options_box->currentIndex() == 1)
brush = QBrush(state.getBackgroundGradient());
acfg.setBackgroundType(SectionAppearance::Gradient);

if (ui->bg_options_box->currentIndex() == 2)
brush = QBrush(state.getBackgroundPattern());
acfg.setBackgroundType(SectionAppearance::Pattern);
} else {
acfg.setBackgroundType(SectionAppearance::None);
}

app->config().window(_curr_idx).appearance().setBackground(brush);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), brush);
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, brush);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), acfg.getBackground());
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, acfg.getBackground());
}

void SettingsDialog::on_bg_options_box_activated(int index)
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto& acfg = app->config().window(_curr_idx).appearance();
switch (index) {
case 0: // color
app->config().window(_curr_idx).appearance().setBackground(state.getBackgroundColor());
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), state.getBackgroundColor());
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, state.getBackgroundColor());
acfg.setBackgroundType(SectionAppearance::SolidColor);
break;
case 1: // gradient
app->config().window(_curr_idx).appearance().setBackground(state.getBackgroundGradient());
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), state.getBackgroundGradient());
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, state.getBackgroundGradient());
acfg.setBackgroundType(SectionAppearance::Gradient);
break;
case 2: // pattern
app->config().window(_curr_idx).appearance().setBackground(state.getBackgroundPattern());
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), state.getBackgroundPattern());
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, state.getBackgroundPattern());
acfg.setBackgroundType(SectionAppearance::Pattern);
break;
}
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), acfg.getBackground());
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, acfg.getBackground());
}

void SettingsDialog::on_bg_options_box_currentIndexChanged(int index)
Expand Down Expand Up @@ -612,45 +592,41 @@ void SettingsDialog::on_bg_options_box_currentIndexChanged(int index)

void SettingsDialog::bg_select_color()
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto color = QColorDialog::getColor(state.getBackgroundColor(),
auto& acfg = app->config().window(_curr_idx).appearance();
auto color = QColorDialog::getColor(acfg.getBackgroundColor(),
this,
QString(),
QColorDialog::ShowAlphaChannel);
if (!color.isValid()) return;
app->config().window(_curr_idx).appearance().setBackground(color);
state.setBackgroundColor(color);
acfg.setBackgroundColor(color);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), color);
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, color);
}

void SettingsDialog::bg_select_gradient()
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto& acfg = app->config().window(_curr_idx).appearance();
bool ok = false;
auto gradient = GradientDialog::getGradient(&ok,
state.getBackgroundGradient(),
acfg.getBackgroundGradient(),
this);
if (!ok) return;
gradient.setCoordinateMode(QGradient::ObjectMode);
app->config().window(_curr_idx).appearance().setBackground(gradient);
state.setBackgroundGradient(gradient);
acfg.setBackgroundGradient(gradient);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), gradient);
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, gradient);
}

void SettingsDialog::bg_select_pattern()
{
SettingsDialogState state(app->config().window(_curr_idx).state());
auto& acfg = app->config().window(_curr_idx).appearance();
auto file = QFileDialog::getOpenFileName(this,
QString(),
_last_path,
acfg.getBackgroundPatternFile(),
tr("Images (*.png *.bmp *.jpg)"));
if (file.isEmpty()) return;
_last_path = file;
QPixmap pxm(file);
app->config().window(_curr_idx).appearance().setBackground(pxm);
state.setBackgroundPattern(pxm);
acfg.setBackgroundPatternFile(file);
applyClockOption(qOverload<QBrush>(&GraphicsDateTimeWidget::setBackground), pxm);
notifyOptionChanged(&SettingsChangeTransmitter::setBackground, pxm);
}
Expand Down
1 change: 0 additions & 1 deletion app/gui/settings_dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,4 @@ private slots:
Ui::SettingsDialog* ui;
Application* app;
int _curr_idx;
QString _last_path;
};
4 changes: 3 additions & 1 deletion clock_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

project(ClockCommon VERSION 2.0.0 LANGUAGES CXX)
project(ClockCommon VERSION 2.0.1 LANGUAGES CXX)

qt_add_library(${PROJECT_NAME} SHARED
config/settings_storage.hpp
config/settings_storage.cpp
config/custom_converters.hpp
config/sample_brushes.cpp
config/sample_brushes.hpp
clock_common_global.hpp
state.hpp
common_appearance_state.cpp
Expand Down
Loading

0 comments on commit 0c410ce

Please sign in to comment.