added and to predefined alias variables. (needs core restart)
[quassel.git] / src / qtui / settingspages / fontssettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "fontssettingspage.h"
22
23 #include "qtui.h"
24 #include "qtuisettings.h"
25
26 #include <QFontDialog>
27
28 FontsSettingsPage::FontsSettingsPage(QWidget *parent)
29   : SettingsPage(tr("Appearance"), tr("Fonts"), parent) {
30
31   ui.setupUi(this);
32   mapper = new QSignalMapper(this);
33   connect(ui.chooseGeneral, SIGNAL(clicked()), mapper, SLOT(map()));
34   connect(ui.chooseTopic, SIGNAL(clicked()), mapper, SLOT(map()));
35   connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
36   connect(ui.chooseNickList, SIGNAL(clicked()), mapper, SLOT(map()));
37   connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
38   connect(ui.chooseChatMessages, SIGNAL(clicked()), mapper, SLOT(map()));
39   connect(ui.chooseNicks, SIGNAL(clicked()), mapper, SLOT(map()));
40   connect(ui.chooseTimestamp, SIGNAL(clicked()), mapper, SLOT(map()));
41
42   mapper->setMapping(ui.chooseGeneral, ui.demoGeneral);
43   mapper->setMapping(ui.chooseTopic, ui.demoTopic);
44   mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
45   mapper->setMapping(ui.chooseNickList, ui.demoNickList);
46   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
47   mapper->setMapping(ui.chooseChatMessages, ui.demoChatMessages);
48   mapper->setMapping(ui.chooseNicks, ui.demoNicks);
49   mapper->setMapping(ui.chooseTimestamp, ui.demoTimestamp);
50
51   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
52   
53   //connect(ui.customAppFonts, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
54   connect(ui.checkTopic, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55   connect(ui.checkBufferView, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56   connect(ui.checkNickList, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
57   connect(ui.checkInputLine, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
58   connect(ui.checkNicks, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
59   connect(ui.checkTimestamp, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
60
61   load();
62
63 }
64
65 bool FontsSettingsPage::hasDefaults() const {
66   return true;
67 }
68
69 void FontsSettingsPage::defaults() {
70   load(Settings::Default);
71   widgetHasChanged();
72 }
73
74 void FontsSettingsPage::load() {
75   load(Settings::Custom);
76   setChangedState(false);
77 }
78
79 void FontsSettingsPage::load(Settings::Mode mode) {
80   QtUiSettings s;
81   bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
82   QFont inputLineFont;
83   if(useInputLineFont) {
84     ui.checkInputLine->setChecked(true);
85     inputLineFont = s.value("InputLineFont").value<QFont>();
86   } else {
87     inputLineFont = qApp->font();
88   }
89   initLabel(ui.demoInputLine, inputLineFont);
90   
91   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None, mode);
92   initLabel(ui.demoChatMessages, chatFormat.font());
93   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender, mode);
94   if(nicksFormat.hasProperty(QTextFormat::FontFamily)) {
95     initLabel(ui.demoNicks, nicksFormat.font());
96     ui.checkNicks->setChecked(true);
97   } else {
98     initLabel(ui.demoNicks, chatFormat.font());
99     ui.checkNicks->setChecked(false);
100   }
101   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp, mode);
102   if(timestampFormat.hasProperty(QTextFormat::FontFamily)) {
103     initLabel(ui.demoTimestamp, timestampFormat.font());
104     ui.checkTimestamp->setChecked(true);
105   } else {
106     initLabel(ui.demoTimestamp, chatFormat.font());
107     ui.checkTimestamp->setChecked(false);
108   }
109
110   setChangedState(false);
111 }
112
113 void FontsSettingsPage::save() {
114   QtUiSettings s;
115   s.setValue("UseInputLineFont", (ui.checkInputLine->checkState() == Qt::Checked));
116   s.setValue("InputLineFont", ui.demoInputLine->font());
117   
118   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
119   chatFormat.setFont(ui.demoChatMessages->font());
120   QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
121
122   //FIXME: actually remove font properties from the formats
123   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender);
124   if(ui.checkNicks->checkState() == Qt::Checked) nicksFormat.setFont(ui.demoNicks->font());
125   else nicksFormat.setFont(chatFormat.font());
126   QtUi::style()->setFormat(UiStyle::Sender, nicksFormat, Settings::Custom);
127
128   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp);
129   if(ui.checkTimestamp->checkState() == Qt::Checked) timestampFormat.setFont(ui.demoTimestamp->font());
130   else timestampFormat.setFont(chatFormat.font());
131   QtUi::style()->setFormat(UiStyle::Timestamp, timestampFormat, Settings::Custom);
132
133   setChangedState(false);
134 }
135
136 void FontsSettingsPage::widgetHasChanged() {
137   if(!hasChanged()) setChangedState(true);
138 }
139
140 void FontsSettingsPage::initLabel(QLabel *label, const QFont &font) {
141   setFont(label, font);
142 }
143
144 void FontsSettingsPage::setFont(QLabel *label, const QFont &font) {
145   label->setFont(font);
146   label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
147   widgetHasChanged();
148 }
149
150 void FontsSettingsPage::chooseFont(QWidget *widget) {
151   QLabel *label = qobject_cast<QLabel *>(widget);
152   Q_ASSERT(label);
153   bool ok;
154   QFont font = QFontDialog::getFont(&ok, label->font());
155   if(ok) {
156     setFont(label, font);
157   }
158 }