d64fdfc897aa8a67f6b42022c0b65bee51631e74
[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   load();
51
52 }
53
54 bool FontsSettingsPage::hasChanged() const {
55
56   return false;
57 }
58
59 void FontsSettingsPage::defaults() {
60   load(Settings::Default);
61
62 }
63
64 void FontsSettingsPage::load() {
65   load(Settings::Custom);
66   changeState(false);
67 }
68
69 void FontsSettingsPage::load(Settings::Mode mode) {
70   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None, mode);
71   setFont(ui.demoChatMessages, chatFormat.font());
72   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender, mode);
73   if(nicksFormat.hasProperty(QTextFormat::FontFamily)) {
74     setFont(ui.demoNicks, nicksFormat.font());
75     ui.checkNicks->setChecked(true);
76   } else {
77     setFont(ui.demoNicks, chatFormat.font());
78     ui.checkNicks->setChecked(false);
79   }
80   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp, mode);
81   if(timestampFormat.hasProperty(QTextFormat::FontFamily)) {
82     setFont(ui.demoTimestamp, timestampFormat.font());
83     ui.checkTimestamp->setChecked(true);
84   } else {
85     setFont(ui.demoTimestamp, chatFormat.font());
86     ui.checkTimestamp->setChecked(false);
87   }
88
89 }
90
91 void FontsSettingsPage::save() {
92   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
93   chatFormat.setFont(ui.demoChatMessages->font());
94   QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
95
96   //FIXME: actually remove font properties from the formats
97   QTextCharFormat nicksFormat = QtUi::style()->format(UiStyle::Sender);
98   if(ui.checkNicks->checkState() == Qt::Checked) nicksFormat.setFont(ui.demoNicks->font());
99   else nicksFormat.setFont(chatFormat.font());
100   QtUi::style()->setFormat(UiStyle::Sender, nicksFormat, Settings::Custom);
101
102   QTextCharFormat timestampFormat = QtUi::style()->format(UiStyle::Timestamp);
103   if(ui.checkTimestamp->checkState() == Qt::Checked) timestampFormat.setFont(ui.demoTimestamp->font());
104   else timestampFormat.setFont(chatFormat.font());
105   QtUi::style()->setFormat(UiStyle::Timestamp, timestampFormat, Settings::Custom);
106
107   changeState(false);
108 }
109
110 void FontsSettingsPage::setFont(QLabel *label, const QFont &font) {
111   QFontInfo fontInfo(font);
112   label->setFont(font);
113   label->setText(QString("%1 %2").arg(fontInfo.family()).arg(fontInfo.pointSize()));
114 }
115
116 void FontsSettingsPage::chooseFont(QWidget *widget) {
117   QLabel *label = qobject_cast<QLabel *>(widget);
118   Q_ASSERT(label);
119   bool ok;
120   QFont font = QFontDialog::getFont(&ok, label->font());
121   if(ok) {
122     setFont(label, font);
123   }
124 }