Move "Appearance" to "Interface" in settingspages
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
index 23fdb8a..ab39344 100644 (file)
 #include "chatviewsettings.h"
 #include "qtui.h"
 #include "qtuisettings.h"
+#include "qtuistyle.h"
 #include "util.h"
 
+#include <QCheckBox>
 #include <QDir>
+#include <QFileDialog>
+#include <QFontDialog>
+#include <QSignalMapper>
 #include <QStyleFactory>
 
 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
-  : SettingsPage(tr("Appearance"), tr("General"), parent) {
+  : SettingsPage(tr("Interface"), QString(), parent),
+  _fontsChanged(false)
+{
   ui.setupUi(this);
+  initAutoWidgets();
   initStyleComboBox();
   initLanguageComboBox();
 
@@ -41,6 +49,15 @@ AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
   }
+
+  mapper = new QSignalMapper(this);
+  connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
+
+  connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
+
+  mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
+
+  connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
 }
 
 void AppearanceSettingsPage::initStyleComboBox() {
@@ -52,7 +69,7 @@ void AppearanceSettingsPage::initStyleComboBox() {
 }
 
 void AppearanceSettingsPage::initLanguageComboBox() {
-  QDir i18nDir(":/i18n", "quassel_*.qm");
+  QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
 
   foreach(QString translationFile, i18nDir.entryList()) {
     QString localeName(translationFile.mid(8));
@@ -61,12 +78,15 @@ void AppearanceSettingsPage::initLanguageComboBox() {
     _locales << locale;
     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
   }
-
 }
 
 void AppearanceSettingsPage::defaults() {
   ui.styleComboBox->setCurrentIndex(0);
 
+  loadFonts(Settings::Default);
+  _fontsChanged = true;
+
+  SettingsPage::defaults();
   widgetHasChanged();
 }
 
@@ -74,13 +94,14 @@ void AppearanceSettingsPage::load() {
   QtUiSettings uiSettings;
 
   // Gui Style
-  settings["Style"] = uiSettings.value("Style", QString(""));
-  if(settings["Style"].toString() == "") {
+  QString style = uiSettings.value("Style", QString("")).toString();
+  if(style.isEmpty()) {
     ui.styleComboBox->setCurrentIndex(0);
   } else {
-    ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(settings["Style"].toString(), Qt::MatchExactly));
-    QApplication::setStyle(settings["Style"].toString());
+    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>();
@@ -90,17 +111,26 @@ void AppearanceSettingsPage::load() {
     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);
 
+  SettingsPage::load();
   setChangedState(false);
 }
 
+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);
+
+  _fontsChanged = false;
+}
+
 void AppearanceSettingsPage::save() {
   QtUiSettings uiSettings;
 
@@ -116,14 +146,23 @@ void AppearanceSettingsPage::save() {
     uiSettings.setValue("Locale", selectedLocale());
   }
 
-  ChatViewSettings chatViewSettings;
-  chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
+  // Fonts
+  QtUiStyleSettings fontSettings("Fonts");
+  if(ui.demoInputLine->font() != QApplication::font())
+    fontSettings.setValue("InputLine", ui.demoInputLine->font());
+  else
+    fontSettings.setValue("InputLine", "");
+
+  _fontsChanged = false;
 
-  BufferSettings bufferSettings;
-  bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
+  bool needsStyleReload =
+        ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
+    || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
 
-  load();
+  SettingsPage::save();
   setChangedState(false);
+  if(needsStyleReload)
+    QtUi::style()->reload();
 }
 
 QLocale AppearanceSettingsPage::selectedLocale() const {
@@ -139,20 +178,42 @@ QLocale AppearanceSettingsPage::selectedLocale() const {
   return locale;
 }
 
-void AppearanceSettingsPage::widgetHasChanged() {
-  setChangedState(testHasChanged());
+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();
 }
 
-bool AppearanceSettingsPage::testHasChanged() {
-  if(settings["Style"].toString() != ui.styleComboBox->currentText()) return true;
-  if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
+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);
+  }
+}
 
-  if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
-  if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
+void AppearanceSettingsPage::chooseStyleSheet() {
+  QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
+  if(!name.isEmpty())
+    ui.customStyleSheetPath->setText(name);
+}
 
-  return false;
+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;
 
+  if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
 
+  return false;
+}