Finally! The new identities plus a nice shiny settingspage for editing them are done!
[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
25 #include <QFontDialog>
26
27 FontsSettingsPage::FontsSettingsPage(QWidget *parent)
28   : SettingsPage(tr("Appearance"), tr("Fonts"), parent) {
29
30   ui.setupUi(this);
31   mapper = new QSignalMapper(this);
32   connect(ui.chooseGeneral, SIGNAL(clicked()), mapper, SLOT(map()));
33   connect(ui.chooseTopic, SIGNAL(clicked()), mapper, SLOT(map()));
34   connect(ui.chooseNickList, SIGNAL(clicked()), mapper, SLOT(map()));
35   connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
36   connect(ui.chooseChatMessages, SIGNAL(clicked()), mapper, SLOT(map()));
37   connect(ui.chooseNicks, SIGNAL(clicked()), mapper, SLOT(map()));
38   connect(ui.chooseTimestamp, SIGNAL(clicked()), mapper, SLOT(map()));
39
40   mapper->setMapping(ui.chooseGeneral, ui.demoGeneral);
41   mapper->setMapping(ui.chooseTopic, ui.demoTopic);
42   mapper->setMapping(ui.chooseNickList, ui.demoNickList);
43   mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
44   mapper->setMapping(ui.chooseChatMessages, ui.demoChatMessages);
45   mapper->setMapping(ui.chooseNicks, ui.demoNicks);
46   mapper->setMapping(ui.chooseTimestamp, ui.demoTimestamp);
47
48   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
49
50   connect(ui.customAppFonts, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
51   connect(ui.checkTopic, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
52   connect(ui.checkNickList, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
53   connect(ui.checkBufferView, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
54   connect(ui.checkNicks, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55   connect(ui.checkTimestamp, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56
57   load();
58
59 }
60
61 bool FontsSettingsPage::hasDefaults() const {
62   return true;
63 }
64
65 void FontsSettingsPage::defaults() {
66   load(Settings::Default);
67   widgetHasChanged();
68 }
69
70 void FontsSettingsPage::load() {
71   load(Settings::Custom);
72   changeState(false);
73 }
74
75 void FontsSettingsPage::load(Settings::Mode mode) {
76   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None, mode);
77   initLabel(ui.demoChatMessages, chatFormat.font());
78   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender, mode);
79   if(nicksFormat.hasProperty(QTextFormat::FontFamily)) {
80     initLabel(ui.demoNicks, nicksFormat.font());
81     ui.checkNicks->setChecked(true);
82   } else {
83     initLabel(ui.demoNicks, chatFormat.font());
84     ui.checkNicks->setChecked(false);
85   }
86   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp, mode);
87   if(timestampFormat.hasProperty(QTextFormat::FontFamily)) {
88     initLabel(ui.demoTimestamp, timestampFormat.font());
89     ui.checkTimestamp->setChecked(true);
90   } else {
91     initLabel(ui.demoTimestamp, chatFormat.font());
92     ui.checkTimestamp->setChecked(false);
93   }
94
95   changeState(false);
96 }
97
98 void FontsSettingsPage::save() {
99   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
100   chatFormat.setFont(ui.demoChatMessages->font());
101   QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
102
103   //FIXME: actually remove font properties from the formats
104   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender);
105   if(ui.checkNicks->checkState() == Qt::Checked) nicksFormat.setFont(ui.demoNicks->font());
106   else nicksFormat.setFont(chatFormat.font());
107   QtUi::style()->setFormat(UiStyle::Sender, nicksFormat, Settings::Custom);
108
109   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp);
110   if(ui.checkTimestamp->checkState() == Qt::Checked) timestampFormat.setFont(ui.demoTimestamp->font());
111   else timestampFormat.setFont(chatFormat.font());
112   QtUi::style()->setFormat(UiStyle::Timestamp, timestampFormat, Settings::Custom);
113
114   changeState(false);
115 }
116
117 void FontsSettingsPage::widgetHasChanged() {
118   if(!hasChanged()) changeState(true);
119 }
120
121 void FontsSettingsPage::initLabel(QLabel *label, const QFont &font) {
122   setFont(label, font);
123 }
124
125 void FontsSettingsPage::setFont(QLabel *label, const QFont &font) {
126   label->setFont(font);
127   label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
128   widgetHasChanged();
129 }
130
131 void FontsSettingsPage::chooseFont(QWidget *widget) {
132   QLabel *label = qobject_cast<QLabel *>(widget);
133   Q_ASSERT(label);
134   bool ok;
135   QFont font = QFontDialog::getFont(&ok, label->font());
136   if(ok) {
137     setFont(label, font);
138   }
139 }