nextcloud-desktop/src/gui/settingsdialog.cpp

302 lines
10 KiB
C++
Raw Normal View History

/*
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
*
* 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; version 2 of the License.
*
* 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.
*/
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "folderman.h"
#include "theme.h"
#include "generalsettings.h"
#include "networksettings.h"
#include "accountsettings.h"
2014-11-10 01:25:57 +03:00
#include "configfile.h"
#include "progressdispatcher.h"
#include "owncloudgui.h"
#include "activitywidget.h"
2015-04-17 18:56:17 +03:00
#include "accountmanager.h"
#include "protocolwidget.h"
#include <QLabel>
#include <QStandardItemModel>
#include <QStackedWidget>
#include <QPushButton>
#include <QDebug>
#include <QSettings>
#include <QToolBar>
2015-08-10 11:50:57 +03:00
#include <QToolButton>
#include <QLayout>
2015-08-10 11:50:57 +03:00
#include <QVBoxLayout>
#include <QPixmap>
#include <QImage>
#include <QWidgetAction>
namespace {
const char TOOLBAR_CSS[] =
2015-08-11 09:43:36 +03:00
"QToolBar { background: %1; margin: 0; padding: 0; border: none; border-bottom: 1px solid %2; spacing: 0; } "
"QToolBar QToolButton { background: %1; border: none; border-bottom: 1px solid %2; margin: 0; padding: 5px; } "
"QToolBar QToolButton:checked { background: %3; color: %4; }";
}
2014-11-10 00:34:07 +03:00
namespace OCC {
//
// Whenever you change something here check both settingsdialog.cpp and settingsdialogmac.cpp !
//
SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent) :
QDialog(parent)
2015-04-17 18:56:17 +03:00
, _ui(new Ui::SettingsDialog), _gui(gui)
{
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
_ui->setupUi(this);
_toolBar = new QToolBar;
_toolBar->setIconSize(QSize(32, 32));
_toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
layout()->setMenuBar(_toolBar);
// People perceive this as a Window, so also make Ctrl+W work
QAction *closeWindowAction = new QAction(this);
closeWindowAction->setShortcut(QKeySequence("Ctrl+W"));
connect(closeWindowAction, SIGNAL(triggered()), SLOT(accept()));
addAction(closeWindowAction);
setObjectName("Settings"); // required as group for saveGeometry call
setWindowTitle(Theme::instance()->appNameGUI());
_actionGroup = new QActionGroup(this);
_actionGroup->setExclusive(true);
// Note: all the actions have a '\n' because the account name is in two lines and
// all buttons must have the same size in order to keep a good layout
_activityAction = createColorAwareAction(QLatin1String(":/client/resources/activity.png"), tr("Activity"));
_actionGroup->addAction(_activityAction);
addActionToToolBar(_activityAction);
// FIXME: Put this QTabWidget into its own class to be used here.
_activityWidget = new ActivityWidget;
_activityWidget->setParent(this);
_ui->stack->addWidget(_activityWidget);
_activityWidget->tabWidget()->setTabIcon(0, Theme::instance()->applicationIcon());
ProtocolWidget *pw = new ProtocolWidget(this);
_activityWidget->tabWidget()->addTab(pw, Theme::instance()->syncStateIcon(SyncResult::Success), tr("Sync Protocol"));
_activityWidget->tabWidget()->addTab(pw->issueWidget(), Theme::instance()->syncStateIcon(SyncResult::Problem), tr("Not Synced"));
QAction *generalAction = createColorAwareAction(QLatin1String(":/client/resources/settings.png"), tr("General"));
_actionGroup->addAction(generalAction);
addActionToToolBar(generalAction);
GeneralSettings *generalSettings = new GeneralSettings;
_ui->stack->addWidget(generalSettings);
QAction *networkAction = createColorAwareAction(QLatin1String(":/client/resources/network.png"), tr("Network"));
_actionGroup->addAction(networkAction);
addActionToToolBar(networkAction);
NetworkSettings *networkSettings = new NetworkSettings;
_ui->stack->addWidget(networkSettings);
_actionGroupWidgets.insert(_activityAction, _activityWidget);
_actionGroupWidgets.insert(generalAction, generalSettings);
_actionGroupWidgets.insert(networkAction, networkSettings);
connect(_actionGroup, SIGNAL(triggered(QAction*)), SLOT(slotSwitchPage(QAction*)));
2015-04-17 18:56:17 +03:00
connect(AccountManager::instance(), SIGNAL(accountAdded(AccountState*)),
this, SLOT(accountAdded(AccountState*)));
connect(AccountManager::instance(), SIGNAL(accountRemoved(AccountState*)),
this, SLOT(accountRemoved(AccountState*)));
foreach (auto ai , AccountManager::instance()->accounts()) {
accountAdded(ai.data());
}
QTimer::singleShot(1, this, SLOT(showFirstPage()));
QAction *showLogWindow = new QAction(this);
showLogWindow->setShortcut(QKeySequence("F12"));
connect(showLogWindow, SIGNAL(triggered()), gui, SLOT(slotToggleLogBrowser()));
addAction(showLogWindow);
customizeStyle();
2014-11-10 00:30:29 +03:00
ConfigFile cfg;
cfg.restoreGeometry(this);
}
SettingsDialog::~SettingsDialog()
{
delete _ui;
}
// close event is not being called here
void SettingsDialog::reject() {
2014-11-10 00:30:29 +03:00
ConfigFile cfg;
cfg.saveGeometry(this);
QDialog::reject();
}
void SettingsDialog::accept() {
2014-11-10 00:30:29 +03:00
ConfigFile cfg;
cfg.saveGeometry(this);
QDialog::accept();
}
void SettingsDialog::changeEvent(QEvent *e)
{
switch (e->type()) {
case QEvent::StyleChange:
case QEvent::PaletteChange:
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
case QEvent::ThemeChange:
#endif
customizeStyle();
break;
default:
break;
}
QDialog::changeEvent(e);
}
void SettingsDialog::slotSwitchPage(QAction *action)
{
_ui->stack->setCurrentWidget(_actionGroupWidgets.value(action));
}
void SettingsDialog::showFirstPage()
{
Q_FOREACH(QAction *action, _toolBar->actions()) {
if (QWidgetAction *wa = qobject_cast<QWidgetAction*>(action)) {
if (QToolButton *qtb = qobject_cast<QToolButton*>(wa->defaultWidget())) {
if (QAction *a2 = qtb->defaultAction()) {
a2->trigger();
break;
}
}
}
}
}
void SettingsDialog::showActivityPage()
{
if (_activityAction) {
slotSwitchPage(_activityAction);
}
}
2015-04-17 18:56:17 +03:00
void SettingsDialog::accountAdded(AccountState *s)
{
auto height = _toolBar->sizeHint().height();
auto accountAction = createColorAwareAction(QLatin1String(":/client/resources/account.png"),
s->shortDisplayNameForSettings(height * 1.618)); // Golden ratio
accountAction->setToolTip(s->account()->displayName());
2015-08-10 11:50:57 +03:00
QToolButton* accountButton = new QToolButton;
accountButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
accountButton->setDefaultAction(accountAction);
2015-08-10 11:50:57 +03:00
accountButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
accountButton->setMinimumWidth(height * 1.3);
QAction* toolbarAction = _toolBar->insertWidget(_toolBar->actions().at(0), accountButton);
_toolbarAccountActions.insert(accountAction, toolbarAction);
2015-08-10 11:50:57 +03:00
2015-04-17 18:56:17 +03:00
auto accountSettings = new AccountSettings(s, this);
_ui->stack->insertWidget(0 , accountSettings);
_actionGroup->addAction(accountAction);
_actionGroupWidgets.insert(accountAction, accountSettings);
2015-04-17 18:56:17 +03:00
connect( accountSettings, SIGNAL(folderChanged()), _gui, SLOT(slotFoldersChanged()));
connect( accountSettings, SIGNAL(openFolderAlias(const QString&)),
_gui, SLOT(slotFolderOpenAction(QString)));
2015-04-17 18:56:17 +03:00
}
void SettingsDialog::accountRemoved(AccountState *s)
{
for (auto it = _actionGroupWidgets.begin(); it != _actionGroupWidgets.end(); ++it) {
2015-04-17 18:56:17 +03:00
auto as = qobject_cast<AccountSettings *>(*it);
if (!as) {
continue;
}
if (as->accountsState() == s) {
_toolBar->removeAction(_toolbarAccountActions.value(it.key()));
_toolbarAccountActions.remove(it.key());
2015-04-17 18:56:17 +03:00
delete it.key();
delete it.value();
_actionGroupWidgets.erase(it);
2015-04-17 18:56:17 +03:00
break;
}
}
}
void SettingsDialog::customizeStyle()
{
QString highlightColor(palette().highlight().color().name());
QString altBase(palette().alternateBase().color().name());
QString dark(palette().dark().color().name());
QString background(palette().base().color().name());
_toolBar->setStyleSheet(QString::fromAscii(TOOLBAR_CSS).arg(background).arg(dark).arg(highlightColor).arg(altBase));
Q_FOREACH(QAction *a, _actionGroup->actions()) {
QIcon icon = createColorAwareIcon(a->property("iconPath").toString());
a->setIcon(icon);
QToolButton *btn = qobject_cast<QToolButton*>(_toolBar->widgetForAction(a));
if (btn) {
btn->setIcon(icon);
}
}
}
QIcon SettingsDialog::createColorAwareIcon(const QString &name)
{
QColor bg(palette().base().color());
QImage img(name);
// account for different sensitivty of the human eye to certain colors
double treshold = 1.0 - ( 0.299 * bg.red() + 0.587 * bg.green() + 0.114 * bg.blue())/255.0;
if (treshold > 0.5) {
img.invertPixels(QImage::InvertRgb);
}
return QIcon(QPixmap::fromImage(img));
}
QAction *SettingsDialog::createColorAwareAction(const QString &iconPath, const QString &text)
{
// all buttons must have the same size in order to keep a good layout
QIcon coloredIcon = createColorAwareIcon(iconPath);
QAction *action = new QAction(coloredIcon, text, this);
action->setCheckable(true);
action->setProperty("iconPath", iconPath);
return action;
}
2015-04-17 18:56:17 +03:00
void SettingsDialog::addActionToToolBar(QAction *action) {
QToolButton* btn = new QToolButton;
btn->setDefaultAction(action);
btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
_toolBar->addWidget(btn);
btn->setMinimumWidth(_toolBar->sizeHint().height() * 1.3);
}
void SettingsDialog::slotRefreshActivity( AccountState* accountState )
{
if (accountState) {
qDebug() << "Refreshing Activity list for " << accountState->account()->displayName();
_activityWidget->slotRefresh(accountState);
}
}
2014-11-10 00:34:07 +03:00
} // namespace OCC