Skip to content

Commit

Permalink
Merge pull request #62 from caarmen/add-theme-setting
Browse files Browse the repository at this point in the history
Issue #56: Add a setting fot the theme
  • Loading branch information
caarmen authored Feb 23, 2022
2 parents 76bd44e + 83e2543 commit becdd7c
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 55 deletions.
3 changes: 1 addition & 2 deletions app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ SOURCE_DIR=src
SOURCES += \
$${SOURCE_DIR}/main/app/appcomponents.cpp \
$${SOURCE_DIR}/main/app/main.cpp \
$${SOURCE_DIR}/main/app/style.cpp \
$${SOURCE_DIR}/main/debug/refcounter.cpp \
$${SOURCE_DIR}/main/repository/appearancerepository.cpp \
$${SOURCE_DIR}/main/repository/db.cpp \
Expand Down Expand Up @@ -86,7 +85,6 @@ RESOURCES += \

HEADERS += \
$${SOURCE_DIR}/main/app/appcomponents.h \
$${SOURCE_DIR}/main/app/style.h \
$${SOURCE_DIR}/main/debug/refcounter.h \
$${SOURCE_DIR}/main/repository/appearancerepository.h \
$${SOURCE_DIR}/main/repository/db.h \
Expand Down Expand Up @@ -115,6 +113,7 @@ HEADERS += \
$${SOURCE_DIR}/main/viewmodel/rhymeentitymapper.h \
$${SOURCE_DIR}/main/viewmodel/rhymelistmodel.h \
$${SOURCE_DIR}/main/viewmodel/rhymeviewmodel.h \
$${SOURCE_DIR}/main/viewmodel/styleenum.h \
$${SOURCE_DIR}/main/viewmodel/suggestiondisplaydata.h \
$${SOURCE_DIR}/main/viewmodel/suggestionentitymapper.h \
$${SOURCE_DIR}/main/viewmodel/suggestionlistmodel.h \
Expand Down
15 changes: 15 additions & 0 deletions app/resources/i18n/PoetAssistant_en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@
<message id="preferences_label_night_mode_night">
<translation>Dark</translation>
</message>
<message id="preferences_label_style">
<translation>Theme (restarts the app)</translation>
</message>
<message id="preferences_label_style_basic">
<translation>Basic</translation>
</message>
<message id="preferences_label_style_fusion">
<translation>Fusion</translation>
</message>
<message id="preferences_label_style_material">
<translation>Material</translation>
</message>
<message id="preferences_label_style_universal">
<translation>Universal</translation>
</message>
<message id="rhymer_no_matches">
<translation>No rhymes for %1</translation>
</message>
Expand Down
35 changes: 34 additions & 1 deletion app/resources/qml/PreferencesWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import NightMode
import Theme

Dialog {
id: dlgPreference
Expand Down Expand Up @@ -85,7 +86,39 @@ Dialog {
]

Component.onCompleted: currentIndex = indexOfValue(preferencesViewModel.nightMode)
onActivated: preferencesViewModel.nightMode = currentIndex
onActivated: preferencesViewModel.nightMode = currentValue
}

Text {
text: qsTrId("preferences_label_style")
Accessible.name: text
color: Style.primaryText
}

StyledComboBox {
textRole: "text"
valueRole: "value"
model: [
{
text: qsTrId("preferences_label_style_basic"),
value: Theme.Basic
},
{
text: qsTrId("preferences_label_style_fusion"),
value: Theme.Fusion
},
{
text: qsTrId("preferences_label_style_material"),
value: Theme.Material
},
{
text: qsTrId("preferences_label_style_universal"),
value: Theme.Universal
},
]

Component.onCompleted: currentIndex = indexOfValue(preferencesViewModel.style)
onActivated: preferencesViewModel.style = currentValue
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/resources/qml/TtsControlsView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ RowLayout{
AnnotatedToolButton {
iconsource: ttsViewModel.playButtonIcon
label: qsTrId(ttsViewModel.playButtonLabel)
icon.height: 32
icon.width: 32
icon.height: 48
icon.width: 48
anchors.centerIn: parent
onClicked: {
if (taPoem.selectedText) {
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/app/appcomponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ class AppComponents : public QObject
SuggestionViewModel suggestionViewModel;
public:
SuggestionListModel suggestionListModel;
private:
AppearanceRepository appearanceRepository;
public:
PreferencesViewModel preferencesViewModel;
MainViewModel mainViewModel;
};
Expand Down
27 changes: 23 additions & 4 deletions app/src/main/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
#include "appcomponents.h"
#include "colortypeenum.h"
#include "nightmodeenum.h"
#include "style.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>
Expand All @@ -31,10 +30,13 @@ along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.

void setupEngine(QQmlApplicationEngine &engine, AppComponents &components)
{
qmlClearTypeRegistrations();
qmlRegisterUncreatableType<ColorTypeEnum>("ColorType", 1, 0, "ColorType",
"Not creatable as it is an enum type");
qmlRegisterUncreatableType<NightModeEnum>("NightMode", 1, 0, "NightMode",
"Not creatable as it is an enum type");
qmlRegisterUncreatableType<StyleEnum>("Theme", 1, 0, "Theme",
"Not creatable as it is an enum type");
engine.rootContext()->setContextProperty("mainViewModel",
QVariant::fromValue(&components.mainViewModel));
engine.rootContext()->setContextProperty("rhymeListModel",
Expand All @@ -53,7 +55,9 @@ void setupEngine(QQmlApplicationEngine &engine, AppComponents &components)
QVariant::fromValue(&components.suggestionListModel));
engine.rootContext()->setContextProperty("preferencesViewModel",
QVariant::fromValue(&components.preferencesViewModel));
engine.rootContext()->setContextProperty("theme", Style::setStyle());
QString style = AppearanceRepository::getName(components.appearanceRepository.getStyle());
QQuickStyle::setStyle(style);
engine.rootContext()->setContextProperty("theme", style);
engine.load(QUrl("qrc:/qml/main.qml"));
}

Expand All @@ -74,7 +78,22 @@ int main(int argc, char *argv[])
QFuture<void> future = db.openDb();
future.waitForFinished(); // TODO
AppComponents components(&db);
QQmlApplicationEngine engine;
setupEngine(engine, components);
QQmlApplicationEngine *engine = new QQmlApplicationEngine();
setupEngine(*engine, components);
QObject context;
QObject::connect(&components.appearanceRepository, &AppearanceRepository::styleChanged,
&context, [ &context, &engine, &components] {
engine->clearComponentCache();
engine->deleteLater();
QObject::connect(engine, &QObject::destroyed, &context, [&engine, &components]{
engine = new QQmlApplicationEngine();
setupEngine(*engine, components);
});
});
// Prevent QML errors when closing the app
// https://bugreports.qt.io/browse/QTBUG-81247?page=com.googlecode.jira-suite-utilities%3Atransitions-summary-tabpanel
QObject::connect(&a, &QGuiApplication::aboutToQuit, &context, [&engine] {
engine->deleteLater();
});
return a.exec();
}
35 changes: 0 additions & 35 deletions app/src/main/app/style.cpp

This file was deleted.

24 changes: 24 additions & 0 deletions app/src/main/repository/appearancerepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ AppearanceRepository::NightMode AppearanceRepository::getNightMode()
{
return static_cast<NightMode>(settings.value(nightModeSetting, NightMode::Auto).toInt());
}

void AppearanceRepository::setStyle(AppearanceRepository::Style style)
{
settings.setValue(styleSetting, style);
emit styleChanged();
}

AppearanceRepository::Style AppearanceRepository::getStyle()
{
return static_cast<Style>(settings.value(styleSetting, Style::Basic).toInt());
}
QString AppearanceRepository::getName(Style style)
{
switch (style) {
case Fusion:
return "Fusion";
case Material:
return "Material";
case Universal:
return "Universal";
default:
return "Basic";
}
}
7 changes: 7 additions & 0 deletions app/src/main/repository/appearancerepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ class AppearanceRepository : public QObject
Q_OBJECT
public:
enum NightMode { Day, Night, Auto };
enum Style { Basic, Fusion, Material, Universal};

explicit AppearanceRepository(QObject *parent = nullptr);
void setNightMode(NightMode nightMode);
NightMode getNightMode();
void setStyle(Style style);
Style getStyle();
static QString getName(Style style);
signals:
void nightModeChanged();
void styleChanged();
private:
static inline QString nightModeSetting = "nightMode";
static inline QString styleSetting = "style";
QSettings settings;
};

Expand Down
29 changes: 29 additions & 0 deletions app/src/main/viewmodel/appearancemapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,32 @@ AppearanceRepository::NightMode AppearanceMapper::map(NightMode nightMode)
return AppearanceRepository::Auto;
}
}


Style AppearanceMapper::map(AppearanceRepository::Style style)
{
switch (style) {
case AppearanceRepository::Fusion:
return Style::Fusion;
case AppearanceRepository::Material:
return Style::Material;
case AppearanceRepository::Universal:
return Style::Universal;
default:
return Style::Basic;
}
}

AppearanceRepository::Style AppearanceMapper::map(Style style)
{
switch (style) {
case Style::Fusion:
return AppearanceRepository::Fusion;
case Style::Material:
return AppearanceRepository::Material;
case Style::Universal:
return AppearanceRepository::Universal;
default:
return AppearanceRepository::Basic;
}
}
3 changes: 3 additions & 0 deletions app/src/main/viewmodel/appearancemapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
#define APPEARANCEMAPPER_H

#include "nightmodeenum.h"
#include "styleenum.h"
#include "appearancerepository.h"
#include <QObject>

Expand All @@ -30,6 +31,8 @@ class AppearanceMapper : public QObject
public:
static NightMode map(AppearanceRepository::NightMode nightMode);
static AppearanceRepository::NightMode map(NightMode nightMode);
static Style map(AppearanceRepository::Style style);
static AppearanceRepository::Style map(Style style);
private:
explicit AppearanceMapper(QObject *parent = nullptr);
};
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/viewmodel/preferencesviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ void PreferencesViewModel::setNightMode(NightMode nightMode)
{
appearanceRepository->setNightMode(AppearanceMapper::map(nightMode));
}

Style PreferencesViewModel::getStyle()
{
return AppearanceMapper::map(appearanceRepository->getStyle());
}

void PreferencesViewModel::setStyle(Style style)
{
appearanceRepository->setStyle(AppearanceMapper::map(style));
}
6 changes: 6 additions & 0 deletions app/src/main/viewmodel/preferencesviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "suggestionrepository.h"
#include "appearancerepository.h"
#include "nightmodeenum.h"
#include "styleenum.h"
#include <QObject>

class PreferencesViewModel : public QObject
Expand All @@ -12,6 +13,7 @@ class PreferencesViewModel : public QObject
Q_PROPERTY(bool historyEnabled READ getSettingUseSearchHistory WRITE setSettingUseSearchHistory
NOTIFY historyEnabledChanged)
Q_PROPERTY(NightMode nightMode READ getNightMode WRITE setNightMode NOTIFY nightModeChanged)
Q_PROPERTY(Style style READ getStyle WRITE setStyle NOTIFY styleChanged)

public:
explicit PreferencesViewModel(SuggestionRepository *suggestionRepository,
Expand All @@ -21,11 +23,15 @@ class PreferencesViewModel : public QObject
signals:
void historyEnabledChanged();
void nightModeChanged();
void styleChanged();
private:
bool getSettingUseSearchHistory();
void setSettingUseSearchHistory(bool enabled);
NightMode getNightMode();
void setNightMode(NightMode nightMode);
Style getStyle();
void setStyle(Style style);

SuggestionRepository *suggestionRepository;
AppearanceRepository *appearanceRepository;

Expand Down
23 changes: 14 additions & 9 deletions app/src/main/app/style.h → app/src/main/viewmodel/styleenum.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef STYLEENUM_H
#define STYLEENUM_H

#ifndef STYLE_H
#define STYLE_H
#include <QObject>

#include <QStringList>

class Style
class StyleEnum: public QObject
{
Q_OBJECT
public:
static QString setStyle();
private:
Style();
static inline QStringList supportedStyles = {"Basic", "Material", "Universal", "Fusion"};
enum Value {
Basic,
Fusion,
Material,
Universal
};
Q_ENUM(Value)
};

#endif // STYLE_H
typedef StyleEnum::Value Style;
#endif // STYLEENUM_H

0 comments on commit becdd7c

Please sign in to comment.