c3f2117e2090c6c9871e024ed541fd82faefe13c
[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 #include "qtuistyle.h"
26
27 #include <QFontDialog>
28
29 FontsSettingsPage::FontsSettingsPage(QWidget *parent)
30   : SettingsPage(tr("Appearance"), tr("Fonts"), parent) {
31
32   ui.setupUi(this);
33   mapper = new QSignalMapper(this);
34   connect(ui.chooseGeneral, SIGNAL(clicked()), mapper, SLOT(map()));
35   connect(ui.chooseTopic, SIGNAL(clicked()), mapper, SLOT(map()));
36   connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
37   connect(ui.chooseNickList, SIGNAL(clicked()), mapper, SLOT(map()));
38   connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
39   connect(ui.chooseChatMessages, SIGNAL(clicked()), mapper, SLOT(map()));
40   connect(ui.chooseNicks, SIGNAL(clicked()), mapper, SLOT(map()));
41   connect(ui.chooseTimestamp, SIGNAL(clicked()), mapper, SLOT(map()));
42
43   mapper->setMapping(ui.chooseGeneral, ui.demoGeneral);
44   mapper->setMapping(ui.chooseTopic, ui.demoTopic);
45   mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
46   mapper->setMapping(ui.chooseNickList, ui.demoNickList);
47   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
48   mapper->setMapping(ui.chooseChatMessages, ui.demoChatMessages);
49   mapper->setMapping(ui.chooseNicks, ui.demoNicks);
50   mapper->setMapping(ui.chooseTimestamp, ui.demoTimestamp);
51
52   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
53
54   //connect(ui.customAppFonts, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55   connect(ui.checkTopic, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56   connect(ui.checkBufferView, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
57   connect(ui.checkNickList, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
58   connect(ui.checkInputLine, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
59   connect(ui.checkNicks, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
60   connect(ui.checkTimestamp, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
61
62   load();
63
64 }
65
66 bool FontsSettingsPage::hasDefaults() const {
67   return true;
68 }
69
70 void FontsSettingsPage::defaults() {
71   load(Settings::Default);
72   widgetHasChanged();
73 }
74
75 void FontsSettingsPage::load() {
76   load(Settings::Custom);
77   setChangedState(false);
78 }
79
80 void FontsSettingsPage::load(Settings::Mode mode) {
81   QtUiSettings s;
82   bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
83   QFont inputLineFont;
84   if(useInputLineFont) {
85     ui.checkInputLine->setChecked(true);
86     inputLineFont = s.value("InputLineFont").value<QFont>();
87   } else {
88     inputLineFont = qApp->font();
89   }
90   initLabel(ui.demoInputLine, inputLineFont);
91
92   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None, mode);
93   initLabel(ui.demoChatMessages, chatFormat.font());
94   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender, mode);
95   if(nicksFormat.hasProperty(QTextFormat::FontFamily)) {
96     initLabel(ui.demoNicks, nicksFormat.font());
97     ui.checkNicks->setChecked(true);
98   } else {
99     initLabel(ui.demoNicks, chatFormat.font());
100     ui.checkNicks->setChecked(false);
101   }
102
103   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp, mode);
104   if(timestampFormat.hasProperty(QTextFormat::FontFamily)) {
105     initLabel(ui.demoTimestamp, timestampFormat.font());
106     ui.checkTimestamp->setChecked(true);
107   } else {
108     initLabel(ui.demoTimestamp, chatFormat.font());
109     ui.checkTimestamp->setChecked(false);
110   }
111
112   setChangedState(false);
113 }
114
115 void FontsSettingsPage::save() {
116   QtUiSettings s;
117   s.setValue("UseInputLineFont", (ui.checkInputLine->checkState() == Qt::Checked));
118   s.setValue("InputLineFont", ui.demoInputLine->font());
119
120   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
121   chatFormat.setFont(ui.demoChatMessages->font());
122   QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
123
124   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender);
125   if(ui.checkNicks->checkState() == Qt::Checked)
126     nicksFormat.setFont(ui.demoNicks->font());
127   else
128     clearFontFromFormat(nicksFormat);
129   QtUi::style()->setFormat(UiStyle::Sender, nicksFormat, Settings::Custom);
130
131   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp);
132   if(ui.checkTimestamp->checkState() == Qt::Checked)
133     timestampFormat.setFont(ui.demoTimestamp->font());
134   else
135     clearFontFromFormat(timestampFormat);
136   QtUi::style()->setFormat(UiStyle::Timestamp, timestampFormat, Settings::Custom);
137
138   setChangedState(false);
139 }
140
141 void FontsSettingsPage::widgetHasChanged() {
142   if(!hasChanged()) setChangedState(true);
143 }
144
145 void FontsSettingsPage::initLabel(QLabel *label, const QFont &font) {
146   setFont(label, font);
147 }
148
149 void FontsSettingsPage::setFont(QLabel *label, const QFont &font) {
150   label->setFont(font);
151   label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
152   widgetHasChanged();
153 }
154
155 void FontsSettingsPage::chooseFont(QWidget *widget) {
156   QLabel *label = qobject_cast<QLabel *>(widget);
157   Q_ASSERT(label);
158   bool ok;
159   QFont font = QFontDialog::getFont(&ok, label->font());
160   if(ok) {
161     setFont(label, font);
162   }
163 }
164
165 void FontsSettingsPage::clearFontFromFormat(QTextCharFormat &fmt) {
166   fmt.clearProperty(QTextFormat::FontFamily);
167   fmt.clearProperty(QTextFormat::FontPointSize);
168   fmt.clearProperty(QTextFormat::FontPixelSize);
169   fmt.clearProperty(QTextFormat::FontWeight);
170   fmt.clearProperty(QTextFormat::FontItalic);
171   fmt.clearProperty(QTextFormat::TextUnderlineStyle);
172   fmt.clearProperty(QTextFormat::FontOverline);
173   fmt.clearProperty(QTextFormat::FontStrikeOut);
174   fmt.clearProperty(QTextFormat::FontFixedPitch);
175   fmt.clearProperty(QTextFormat::FontCapitalization);
176   fmt.clearProperty(QTextFormat::FontWordSpacing);
177   fmt.clearProperty(QTextFormat::FontLetterSpacing);
178 }