mirror of https://github.com/Nheko-Reborn/nheko
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
324 lines
12 KiB
324 lines
12 KiB
7 years ago
|
/*
|
||
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
7 years ago
|
#include <QApplication>
|
||
7 years ago
|
#include <QComboBox>
|
||
|
#include <QDebug>
|
||
|
#include <QLabel>
|
||
|
#include <QPushButton>
|
||
7 years ago
|
#include <QScrollArea>
|
||
7 years ago
|
#include <QSettings>
|
||
|
|
||
|
#include "Config.h"
|
||
|
#include "UserSettingsPage.h"
|
||
6 years ago
|
#include "ui/FlatButton.h"
|
||
|
#include "ui/ToggleButton.h"
|
||
7 years ago
|
|
||
6 years ago
|
#include "version.h"
|
||
7 years ago
|
|
||
7 years ago
|
UserSettings::UserSettings() { load(); }
|
||
|
|
||
|
void
|
||
|
UserSettings::load()
|
||
|
{
|
||
|
QSettings settings;
|
||
7 years ago
|
isTrayEnabled_ = settings.value("user/window/tray", true).toBool();
|
||
7 years ago
|
isStartInTrayEnabled_ = settings.value("user/window/start_in_tray", false).toBool();
|
||
7 years ago
|
isOrderingEnabled_ = settings.value("user/room_ordering", true).toBool();
|
||
|
isGroupViewEnabled_ = settings.value("user/group_view", true).toBool();
|
||
|
isTypingNotificationsEnabled_ = settings.value("user/typing_notifications", true).toBool();
|
||
7 years ago
|
isReadReceiptsEnabled_ = settings.value("user/read_receipts", true).toBool();
|
||
7 years ago
|
theme_ = settings.value("user/theme", "light").toString();
|
||
7 years ago
|
|
||
|
applyTheme();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
UserSettings::setTheme(QString theme)
|
||
|
{
|
||
|
theme_ = theme;
|
||
|
save();
|
||
|
applyTheme();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
UserSettings::applyTheme()
|
||
|
{
|
||
|
QFile stylefile;
|
||
|
QPalette pal;
|
||
|
|
||
|
if (theme() == "light") {
|
||
|
stylefile.setFileName(":/styles/styles/nheko.qss");
|
||
|
pal.setColor(QPalette::Link, QColor("#333"));
|
||
|
} else if (theme() == "dark") {
|
||
|
stylefile.setFileName(":/styles/styles/nheko-dark.qss");
|
||
|
pal.setColor(QPalette::Link, QColor("#d7d9dc"));
|
||
|
} else {
|
||
|
stylefile.setFileName(":/styles/styles/system.qss");
|
||
|
}
|
||
|
|
||
|
stylefile.open(QFile::ReadOnly);
|
||
|
QString stylesheet = QString(stylefile.readAll());
|
||
|
|
||
|
QApplication::setPalette(pal);
|
||
|
qobject_cast<QApplication *>(QApplication::instance())->setStyleSheet(stylesheet);
|
||
7 years ago
|
}
|
||
|
|
||
|
void
|
||
|
UserSettings::save()
|
||
|
{
|
||
|
QSettings settings;
|
||
|
settings.beginGroup("user");
|
||
7 years ago
|
|
||
|
settings.beginGroup("window");
|
||
7 years ago
|
settings.setValue("tray", isTrayEnabled_);
|
||
7 years ago
|
settings.setValue("start_in_tray", isStartInTrayEnabled_);
|
||
7 years ago
|
settings.endGroup();
|
||
|
|
||
7 years ago
|
settings.setValue("room_ordering", isOrderingEnabled_);
|
||
7 years ago
|
settings.setValue("typing_notifications", isTypingNotificationsEnabled_);
|
||
7 years ago
|
settings.setValue("read_receipts", isReadReceiptsEnabled_);
|
||
7 years ago
|
settings.setValue("group_view", isGroupViewEnabled_);
|
||
7 years ago
|
settings.setValue("theme", theme());
|
||
|
settings.endGroup();
|
||
|
}
|
||
|
|
||
|
HorizontalLine::HorizontalLine(QWidget *parent)
|
||
7 years ago
|
: QFrame{parent}
|
||
7 years ago
|
{
|
||
|
setFrameShape(QFrame::HLine);
|
||
|
setFrameShadow(QFrame::Sunken);
|
||
|
}
|
||
|
|
||
|
UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidget *parent)
|
||
7 years ago
|
: QWidget{parent}
|
||
|
, settings_{settings}
|
||
7 years ago
|
{
|
||
|
topLayout_ = new QVBoxLayout(this);
|
||
|
|
||
|
QIcon icon;
|
||
|
icon.addFile(":/icons/icons/ui/angle-pointing-to-left.png");
|
||
|
|
||
|
auto backBtn_ = new FlatButton(this);
|
||
|
backBtn_->setMinimumSize(QSize(24, 24));
|
||
|
backBtn_->setIcon(icon);
|
||
|
backBtn_->setIconSize(QSize(24, 24));
|
||
|
|
||
|
auto heading_ = new QLabel(tr("User Settings"));
|
||
7 years ago
|
heading_->setStyleSheet("font-weight: bold; font-size: 22px;");
|
||
7 years ago
|
|
||
7 years ago
|
auto versionInfo = new QLabel(QString("%1 | %2").arg(nheko::version).arg(nheko::build_os));
|
||
|
versionInfo->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||
7 years ago
|
|
||
7 years ago
|
topBarLayout_ = new QHBoxLayout;
|
||
|
topBarLayout_->setSpacing(0);
|
||
|
topBarLayout_->setMargin(0);
|
||
|
topBarLayout_->addWidget(backBtn_, 1, Qt::AlignLeft | Qt::AlignVCenter);
|
||
|
topBarLayout_->addWidget(heading_, 0, Qt::AlignBottom);
|
||
|
topBarLayout_->addStretch(1);
|
||
|
|
||
|
auto trayOptionLayout_ = new QHBoxLayout;
|
||
|
trayOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
|
auto trayLabel = new QLabel(tr("Minimize to tray"), this);
|
||
|
trayToggle_ = new Toggle(this);
|
||
7 years ago
|
trayLabel->setStyleSheet("font-size: 15px;");
|
||
7 years ago
|
|
||
|
trayOptionLayout_->addWidget(trayLabel);
|
||
|
trayOptionLayout_->addWidget(trayToggle_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
7 years ago
|
auto startInTrayOptionLayout_ = new QHBoxLayout;
|
||
|
startInTrayOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
|
auto startInTrayLabel = new QLabel(tr("Start in tray"), this);
|
||
|
startInTrayToggle_ = new Toggle(this);
|
||
|
if (!settings_->isTrayEnabled())
|
||
|
startInTrayToggle_->setDisabled(true);
|
||
|
startInTrayLabel->setStyleSheet("font-size: 15px;");
|
||
|
|
||
|
startInTrayOptionLayout_->addWidget(startInTrayLabel);
|
||
|
startInTrayOptionLayout_->addWidget(
|
||
|
startInTrayToggle_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
7 years ago
|
auto orderRoomLayout = new QHBoxLayout;
|
||
|
orderRoomLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
|
auto orderLabel = new QLabel(tr("Re-order rooms based on activity"), this);
|
||
|
roomOrderToggle_ = new Toggle(this);
|
||
|
orderLabel->setStyleSheet("font-size: 15px;");
|
||
|
|
||
|
orderRoomLayout->addWidget(orderLabel);
|
||
|
orderRoomLayout->addWidget(roomOrderToggle_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
7 years ago
|
auto groupViewLayout = new QHBoxLayout;
|
||
|
groupViewLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
|
auto groupViewLabel = new QLabel(tr("Group's sidebar"), this);
|
||
|
groupViewToggle_ = new Toggle(this);
|
||
|
groupViewLabel->setStyleSheet("font-size: 15px;");
|
||
|
|
||
|
groupViewLayout->addWidget(groupViewLabel);
|
||
|
groupViewLayout->addWidget(groupViewToggle_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
7 years ago
|
auto typingLayout = new QHBoxLayout;
|
||
|
typingLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
|
auto typingLabel = new QLabel(tr("Typing notifications"), this);
|
||
|
typingNotifications_ = new Toggle(this);
|
||
|
typingLabel->setStyleSheet("font-size: 15px;");
|
||
|
|
||
|
typingLayout->addWidget(typingLabel);
|
||
|
typingLayout->addWidget(typingNotifications_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
7 years ago
|
auto receiptsLayout = new QHBoxLayout;
|
||
|
receiptsLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
|
auto receiptsLabel = new QLabel(tr("Read receipts"), this);
|
||
|
readReceipts_ = new Toggle(this);
|
||
|
receiptsLabel->setStyleSheet("font-size: 15px;");
|
||
|
|
||
|
receiptsLayout->addWidget(receiptsLabel);
|
||
|
receiptsLayout->addWidget(readReceipts_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
7 years ago
|
auto themeOptionLayout_ = new QHBoxLayout;
|
||
|
themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin);
|
||
7 years ago
|
auto themeLabel_ = new QLabel(tr("Theme"), this);
|
||
7 years ago
|
themeCombo_ = new QComboBox(this);
|
||
7 years ago
|
themeCombo_->addItem("Light");
|
||
|
themeCombo_->addItem("Dark");
|
||
7 years ago
|
themeCombo_->addItem("System");
|
||
7 years ago
|
themeLabel_->setStyleSheet("font-size: 15px;");
|
||
7 years ago
|
|
||
|
themeOptionLayout_->addWidget(themeLabel_);
|
||
|
themeOptionLayout_->addWidget(themeCombo_, 0, Qt::AlignBottom | Qt::AlignRight);
|
||
|
|
||
|
auto general_ = new QLabel(tr("GENERAL"), this);
|
||
7 years ago
|
general_->setStyleSheet("font-size: 17px");
|
||
7 years ago
|
|
||
7 years ago
|
mainLayout_ = new QVBoxLayout;
|
||
7 years ago
|
mainLayout_->setSpacing(7);
|
||
|
mainLayout_->setContentsMargins(
|
||
7 years ago
|
sideMargin_, LayoutTopMargin, sideMargin_, LayoutBottomMargin);
|
||
7 years ago
|
mainLayout_->addWidget(general_, 1, Qt::AlignLeft | Qt::AlignVCenter);
|
||
|
mainLayout_->addWidget(new HorizontalLine(this));
|
||
|
mainLayout_->addLayout(trayOptionLayout_);
|
||
7 years ago
|
mainLayout_->addLayout(startInTrayOptionLayout_);
|
||
|
mainLayout_->addWidget(new HorizontalLine(this));
|
||
7 years ago
|
mainLayout_->addLayout(orderRoomLayout);
|
||
|
mainLayout_->addWidget(new HorizontalLine(this));
|
||
7 years ago
|
mainLayout_->addLayout(groupViewLayout);
|
||
|
mainLayout_->addWidget(new HorizontalLine(this));
|
||
7 years ago
|
mainLayout_->addLayout(typingLayout);
|
||
7 years ago
|
mainLayout_->addLayout(receiptsLayout);
|
||
7 years ago
|
mainLayout_->addWidget(new HorizontalLine(this));
|
||
7 years ago
|
mainLayout_->addLayout(themeOptionLayout_);
|
||
|
mainLayout_->addWidget(new HorizontalLine(this));
|
||
|
|
||
7 years ago
|
auto scrollArea_ = new QScrollArea(this);
|
||
|
scrollArea_->setFrameShape(QFrame::NoFrame);
|
||
|
scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
|
scrollArea_->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
|
||
|
scrollArea_->setWidgetResizable(true);
|
||
|
scrollArea_->setAlignment(Qt::AlignTop | Qt::AlignVCenter);
|
||
|
|
||
|
auto scrollAreaContents_ = new QWidget(this);
|
||
|
scrollAreaContents_->setObjectName("UserSettingScrollWidget");
|
||
|
scrollAreaContents_->setLayout(mainLayout_);
|
||
|
|
||
|
scrollArea_->setWidget(scrollAreaContents_);
|
||
7 years ago
|
topLayout_->addLayout(topBarLayout_);
|
||
7 years ago
|
topLayout_->addWidget(scrollArea_);
|
||
7 years ago
|
topLayout_->addWidget(versionInfo);
|
||
7 years ago
|
|
||
|
connect(themeCombo_,
|
||
|
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
|
||
7 years ago
|
[this](const QString &text) { settings_->setTheme(text.toLower()); });
|
||
7 years ago
|
|
||
7 years ago
|
connect(trayToggle_, &Toggle::toggled, this, [this](bool isDisabled) {
|
||
7 years ago
|
settings_->setTray(!isDisabled);
|
||
7 years ago
|
if (isDisabled) {
|
||
|
startInTrayToggle_->setDisabled(true);
|
||
|
} else {
|
||
|
startInTrayToggle_->setEnabled(true);
|
||
|
}
|
||
7 years ago
|
emit trayOptionChanged(!isDisabled);
|
||
7 years ago
|
});
|
||
|
|
||
7 years ago
|
connect(startInTrayToggle_, &Toggle::toggled, this, [this](bool isDisabled) {
|
||
|
settings_->setStartInTray(!isDisabled);
|
||
|
});
|
||
|
|
||
7 years ago
|
connect(roomOrderToggle_, &Toggle::toggled, this, [this](bool isDisabled) {
|
||
7 years ago
|
settings_->setRoomOrdering(!isDisabled);
|
||
|
});
|
||
|
|
||
7 years ago
|
connect(groupViewToggle_, &Toggle::toggled, this, [this](bool isDisabled) {
|
||
7 years ago
|
settings_->setGroupView(!isDisabled);
|
||
|
});
|
||
|
|
||
7 years ago
|
connect(typingNotifications_, &Toggle::toggled, this, [this](bool isDisabled) {
|
||
7 years ago
|
settings_->setTypingNotifications(!isDisabled);
|
||
|
});
|
||
|
|
||
7 years ago
|
connect(readReceipts_, &Toggle::toggled, this, [this](bool isDisabled) {
|
||
|
settings_->setReadReceipts(!isDisabled);
|
||
|
});
|
||
|
|
||
7 years ago
|
connect(backBtn_, &QPushButton::clicked, this, [this]() {
|
||
7 years ago
|
settings_->save();
|
||
|
emit moveBack();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void
|
||
|
UserSettingsPage::showEvent(QShowEvent *)
|
||
|
{
|
||
7 years ago
|
restoreThemeCombo();
|
||
7 years ago
|
|
||
|
// FIXME: Toggle treats true as "off"
|
||
|
trayToggle_->setState(!settings_->isTrayEnabled());
|
||
7 years ago
|
startInTrayToggle_->setState(!settings_->isStartInTrayEnabled());
|
||
7 years ago
|
roomOrderToggle_->setState(!settings_->isOrderingEnabled());
|
||
|
groupViewToggle_->setState(!settings_->isGroupViewEnabled());
|
||
7 years ago
|
typingNotifications_->setState(!settings_->isTypingNotificationsEnabled());
|
||
7 years ago
|
readReceipts_->setState(!settings_->isReadReceiptsEnabled());
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
void
|
||
|
UserSettingsPage::resizeEvent(QResizeEvent *event)
|
||
|
{
|
||
|
sideMargin_ = width() * 0.2;
|
||
|
mainLayout_->setContentsMargins(
|
||
|
sideMargin_, LayoutTopMargin, sideMargin_, LayoutBottomMargin);
|
||
|
|
||
|
QWidget::resizeEvent(event);
|
||
|
}
|
||
7 years ago
|
|
||
|
void
|
||
|
UserSettingsPage::paintEvent(QPaintEvent *)
|
||
|
{
|
||
|
QStyleOption opt;
|
||
|
opt.init(this);
|
||
|
QPainter p(this);
|
||
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
||
|
}
|
||
7 years ago
|
|
||
|
void
|
||
|
UserSettingsPage::restoreThemeCombo() const
|
||
|
{
|
||
|
if (settings_->theme() == "light")
|
||
|
themeCombo_->setCurrentIndex(0);
|
||
|
else if (settings_->theme() == "dark")
|
||
|
themeCombo_->setCurrentIndex(1);
|
||
|
else
|
||
|
themeCombo_->setCurrentIndex(2);
|
||
|
}
|