qtui: Fix icon loading and improve icon theme support
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
index 3346eeb..3156433 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   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 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
 #include "appearancesettingspage.h"
 
+#include <QCheckBox>
+#include <QDir>
+#include <QFile>
+#include <QFileDialog>
+#include <QStyleFactory>
+
 #include "buffersettings.h"
-#include "chatviewsettings.h"
 #include "qtui.h"
 #include "qtuisettings.h"
 #include "qtuistyle.h"
-#include "util.h"
 
-#include <QDir>
-#include <QFontDialog>
-#include <QSignalMapper>
-#include <QStyleFactory>
 
 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
-  : SettingsPage(tr("Appearance"), QString(), parent),
-  _fontsChanged(false)
+    : SettingsPage(tr("Interface"), QString(), parent)
 {
-  ui.setupUi(this);
-  initStyleComboBox();
-  initLanguageComboBox();
+    ui.setupUi(this);
 
-#ifndef HAVE_WEBKIT
-  ui.showWebPreview->hide();
-  ui.showWebPreview->setEnabled(false);
+#ifdef QT_NO_SYSTEMTRAYICON
+    ui.useSystemTrayIcon->hide();
+#endif
+#if QT_VERSION < 0x050000
+    // We don't support overriding the system icon theme with Qt4
+    ui.overrideSystemIconTheme->hide();
 #endif
 
-  foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
-    connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
-  }
-  foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
-    connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
-  }
+    initAutoWidgets();
+    initStyleComboBox();
+    initLanguageComboBox();
+    initIconThemeComboBox();
 
-  mapper = new QSignalMapper(this);
-  connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
+    foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
+        connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
+    }
+    foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
+        connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
+    }
 
-  connect(ui.chooseChatView, SIGNAL(clicked()), mapper, SLOT(map()));
-  connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
-  connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
+    connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
 
-  mapper->setMapping(ui.chooseChatView, ui.demoChatView);
-  mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
-  mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
-}
+    connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+    connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+    connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
 
-void AppearanceSettingsPage::initStyleComboBox() {
-  QStringList styleList = QStyleFactory::keys();
-  ui.styleComboBox->addItem(tr("<System Default>"));
-  foreach(QString style, styleList) {
-    ui.styleComboBox->addItem(style);
-  }
+    connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+    connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+    connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+
+    connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+    connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+    connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
 }
 
-void AppearanceSettingsPage::initLanguageComboBox() {
-  QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
 
-  foreach(QString translationFile, i18nDir.entryList()) {
-    QString localeName(translationFile.mid(8));
-    localeName.chop(3);
-    QLocale locale(localeName);
-    _locales << locale;
-    ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
-  }
+void AppearanceSettingsPage::initStyleComboBox()
+{
+    QStringList styleList = QStyleFactory::keys();
+    ui.styleComboBox->addItem(tr("<System Default>"));
+    foreach(QString style, styleList) {
+        ui.styleComboBox->addItem(style);
+    }
 }
 
-void AppearanceSettingsPage::defaults() {
-  ui.styleComboBox->setCurrentIndex(0);
 
-  loadFonts(Settings::Default);
-  _fontsChanged = true;
+void AppearanceSettingsPage::initLanguageComboBox()
+{
+    QDir i18nDir(Quassel::translationDirPath(), "*.qm");
+
+    QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
+    foreach(QString translationFile, i18nDir.entryList()) {
+        if (!rx.exactMatch(translationFile))
+            continue;
+        if (!rx.cap(1).isEmpty())
+            continue;
+        QLocale locale(rx.cap(2));
+        _locales[QLocale::languageToString(locale.language())] = locale;
+    }
+    foreach(QString language, _locales.keys()) {
+        ui.languageComboBox->addItem(language);
+    }
+}
 
-  ui.showWebPreview->setChecked(true);
-  ui.showUserStateIcons->setChecked(true);
+void AppearanceSettingsPage::initIconThemeComboBox()
+{
+    auto availableThemes = QtUi::instance()->availableIconThemes();
 
-  widgetHasChanged();
+    ui.iconThemeComboBox->addItem(tr("Automatic"), QString{});
+    for (auto &&p : QtUi::instance()->availableIconThemes()) {
+        ui.iconThemeComboBox->addItem(p.second, p.first);
+    }
 }
 
-void AppearanceSettingsPage::load() {
-  QtUiSettings uiSettings;
 
-  // Gui Style
-  QString style = uiSettings.value("Style", QString("")).toString();
-  if(style.isEmpty()) {
+void AppearanceSettingsPage::defaults()
+{
     ui.styleComboBox->setCurrentIndex(0);
-  } else {
-    ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
-    QApplication::setStyle(style);
-  }
-  ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
-
-  // Language
-  QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
-  if(locale == QLocale::system())
-    ui.languageComboBox->setCurrentIndex(0);
-  else if(locale.language() == QLocale::C)
     ui.languageComboBox->setCurrentIndex(1);
-  else
-    ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
-  ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
-  Quassel::loadTranslation(selectedLocale());
-
-  ChatViewSettings chatViewSettings;
-  SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
 
-  BufferSettings bufferSettings;
-  SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
-
-  loadFonts(Settings::Custom);
-
-  setChangedState(false);
+    SettingsPage::defaults();
+    widgetHasChanged();
 }
 
-void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
-  QtUiStyleSettings s("Fonts");
-
-  QFont inputLineFont;
-  if(mode == Settings::Custom)
-    inputLineFont = s.value("InputLine", QFont()).value<QFont>();
-  setFont(ui.demoInputLine, inputLineFont);
-
-  QFont bufferViewFont;
-  if(mode == Settings::Custom)
-    bufferViewFont = s.value("BufferView", QFont()).value<QFont>();
-  setFont(ui.demoBufferView, bufferViewFont);
 
-  //QTextCharFormat chatFormat = QtUi::style()->cachedFormat(UiStyle::None, 0); // FIXME
-  //setFont(ui.demoChatView, chatFormat.font());
-
-  _fontsChanged = false;
+void AppearanceSettingsPage::load()
+{
+    QtUiSettings uiSettings;
+
+    // Gui Style
+    QString style = uiSettings.value("Style", QString("")).toString();
+    if (style.isEmpty()) {
+        ui.styleComboBox->setCurrentIndex(0);
+    }
+    else {
+        ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
+    }
+    ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
+
+    // Language
+    QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
+    if (locale == QLocale::system())
+        ui.languageComboBox->setCurrentIndex(1);
+    else if (locale.language() == QLocale::C) // we use C for "untranslated"
+        ui.languageComboBox->setCurrentIndex(0);
+    else
+        ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
+    ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
+    Quassel::loadTranslation(selectedLocale());
+
+    // IconTheme
+    QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
+    if (icontheme.isEmpty()) {
+        ui.iconThemeComboBox->setCurrentIndex(0);
+    }
+    else {
+        auto idx = ui.iconThemeComboBox->findData(icontheme);
+        ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
+    }
+    ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
+
+    // bufferSettings:
+    BufferSettings bufferSettings;
+    int redirectTarget = bufferSettings.userNoticesTarget();
+    SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
+    SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
+    SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
+
+    redirectTarget = bufferSettings.serverNoticesTarget();
+    SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
+    SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
+    SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
+
+    redirectTarget = bufferSettings.errorMsgsTarget();
+    SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
+    SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
+    SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
+
+    SettingsPage::load();
+    setChangedState(false);
 }
 
-void AppearanceSettingsPage::save() {
-  QtUiSettings uiSettings;
-
-  if(ui.styleComboBox->currentIndex() < 1) {
-    uiSettings.setValue("Style", QString(""));
-  } else {
-    uiSettings.setValue("Style", ui.styleComboBox->currentText());
-  }
-
-  if(ui.languageComboBox->currentIndex() == 0) {
-    uiSettings.remove("Locale"); // force the default (QLocale::system())
-  } else {
-    uiSettings.setValue("Locale", selectedLocale());
-  }
-
-  ChatViewSettings chatViewSettings;
-  chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
 
-  BufferSettings bufferSettings;
-  bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
-
-  // Fonts
-  QtUiStyleSettings fontSettings("Fonts");
-  if(ui.demoInputLine->font() != QApplication::font())
-    fontSettings.setValue("InputLine", ui.demoInputLine->font());
-  else
-    fontSettings.setValue("InputLine", "");
+void AppearanceSettingsPage::save()
+{
+    QtUiSettings uiSettings;
+    UiStyleSettings styleSettings;
+
+    if (ui.styleComboBox->currentIndex() < 1) {
+        uiSettings.setValue("Style", QString(""));
+    }
+    else {
+        uiSettings.setValue("Style", ui.styleComboBox->currentText());
+        QApplication::setStyle(ui.styleComboBox->currentText());
+    }
+    ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
+
+    if (ui.languageComboBox->currentIndex() == 1) {
+        uiSettings.remove("Locale"); // force the default (QLocale::system())
+    }
+    else {
+        uiSettings.setValue("Locale", selectedLocale());
+    }
+    ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
+
+    bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
+                              || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
+
+    auto iconTheme = selectedIconTheme();
+    if (iconTheme.isEmpty()) {
+        styleSettings.remove("Icons/FallbackTheme");
+    }
+    else {
+        styleSettings.setValue("Icons/FallbackTheme", iconTheme);
+    }
+    ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
+
+    bool needsStyleReload =
+        ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
+        || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
+
+    BufferSettings bufferSettings;
+    int redirectTarget = 0;
+    if (ui.userNoticesInDefaultBuffer->isChecked())
+        redirectTarget |= BufferSettings::DefaultBuffer;
+    if (ui.userNoticesInStatusBuffer->isChecked())
+        redirectTarget |= BufferSettings::StatusBuffer;
+    if (ui.userNoticesInCurrentBuffer->isChecked())
+        redirectTarget |= BufferSettings::CurrentBuffer;
+    bufferSettings.setUserNoticesTarget(redirectTarget);
+
+    redirectTarget = 0;
+    if (ui.serverNoticesInDefaultBuffer->isChecked())
+        redirectTarget |= BufferSettings::DefaultBuffer;
+    if (ui.serverNoticesInStatusBuffer->isChecked())
+        redirectTarget |= BufferSettings::StatusBuffer;
+    if (ui.serverNoticesInCurrentBuffer->isChecked())
+        redirectTarget |= BufferSettings::CurrentBuffer;
+    bufferSettings.setServerNoticesTarget(redirectTarget);
+
+    redirectTarget = 0;
+    if (ui.errorMsgsInDefaultBuffer->isChecked())
+        redirectTarget |= BufferSettings::DefaultBuffer;
+    if (ui.errorMsgsInStatusBuffer->isChecked())
+        redirectTarget |= BufferSettings::StatusBuffer;
+    if (ui.errorMsgsInCurrentBuffer->isChecked())
+        redirectTarget |= BufferSettings::CurrentBuffer;
+    bufferSettings.setErrorMsgsTarget(redirectTarget);
+
+    SettingsPage::save();
+    setChangedState(false);
+    if (needsStyleReload)
+        QtUi::style()->reload();
+    if (needsIconThemeRefresh)
+        QtUi::instance()->refreshIconTheme();
+}
 
-  if(ui.demoBufferView->font() != QApplication::font())
-    fontSettings.setValue("BufferView", ui.demoBufferView->font());
-  else
-    fontSettings.setValue("BufferView", "");
 
-  //QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
-  //chatFormat.setFont(ui.demoChatView->font());
-  //QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
+QLocale AppearanceSettingsPage::selectedLocale() const
+{
+    QLocale locale;
+    int index = ui.languageComboBox->currentIndex();
+    if (index == 1)
+        locale = QLocale::system();
+    else if (index == 0)
+        locale = QLocale::c();
+    else if (index > 1)
+        locale = _locales.values()[index - 2];
+
+    return locale;
+}
 
-  _fontsChanged = false;
 
-  load();
-  setChangedState(false);
+QString AppearanceSettingsPage::selectedIconTheme() const
+{
+    return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
 }
 
-QLocale AppearanceSettingsPage::selectedLocale() const {
-  QLocale locale;
-  int index = ui.languageComboBox->currentIndex();
-  if(index == 0)
-    locale = QLocale::system();
-  else if(index == 1)
-    locale = QLocale::c();
-  else if(index > 1)
-    locale = _locales[index - 2];
-
-  return locale;
-}
 
-void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
-  QFont font = font_;
-  if(font.family().isEmpty())
-    font = QApplication::font();
-  label->setFont(font);
-  label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
-  widgetHasChanged();
+void AppearanceSettingsPage::chooseStyleSheet()
+{
+    QString dir = ui.customStyleSheetPath->property("storedValue").toString();
+    if (!dir.isEmpty() && QFile(dir).exists())
+        dir = QDir(dir).absolutePath();
+    else
+        dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
+
+    QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
+    if (!name.isEmpty())
+        ui.customStyleSheetPath->setText(name);
 }
 
-void AppearanceSettingsPage::chooseFont(QWidget *widget) {
-  QLabel *label = qobject_cast<QLabel *>(widget);
-  Q_ASSERT(label);
-  bool ok;
-  QFont font = QFontDialog::getFont(&ok, label->font());
-  if(ok) {
-    _fontsChanged = true;
-    setFont(label, font);
-  }
-}
 
-void AppearanceSettingsPage::widgetHasChanged() {
-  setChangedState(testHasChanged());
+void AppearanceSettingsPage::widgetHasChanged()
+{
+    setChangedState(testHasChanged());
 }
 
-bool AppearanceSettingsPage::testHasChanged() {
-  if(_fontsChanged) return true; // comparisons are nasty for now
 
-  if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
+bool AppearanceSettingsPage::testHasChanged()
+{
+    if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
+    if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
+    if (ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()) return true;
+
+    if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
+    if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
+    if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
 
-  if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
+    if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
+    if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
+    if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
 
-  if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
-  if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
+    if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
+    if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
+    if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
 
-  return false;
+    return false;
 }