]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewmodes/dolphinfontrequester.cpp
Simplified code for the viewmode settings
[dolphin.git] / src / settings / viewmodes / dolphinfontrequester.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphinfontrequester.h"
21
22 #include <KFontDialog>
23 #include <KGlobalSettings>
24 #include <KLocale>
25 #include <KComboBox>
26
27 #include <QEvent>
28 #include <QHBoxLayout>
29 #include <QPushButton>
30
31 DolphinFontRequester::DolphinFontRequester(QWidget* parent) :
32 QWidget(parent),
33 m_modeCombo(0),
34 m_chooseFontButton(0),
35 m_mode(SystemFont),
36 m_customFont()
37 {
38 QHBoxLayout* topLayout = new QHBoxLayout(this);
39 topLayout->setMargin(0);
40
41 m_modeCombo = new KComboBox(this);
42 m_modeCombo->addItem(i18nc("@item:inlistbox Font", "System Font"));
43 m_modeCombo->addItem(i18nc("@item:inlistbox Font", "Custom Font"));
44 connect(m_modeCombo, SIGNAL(activated(int)),
45 this, SLOT(changeMode(int)));
46
47 m_chooseFontButton = new QPushButton(i18nc("@action:button Choose font", "Choose..."), this);
48 connect(m_chooseFontButton, SIGNAL(clicked()),
49 this, SLOT(openFontDialog()));
50
51 changeMode(m_modeCombo->currentIndex());
52
53 topLayout->addWidget(m_modeCombo);
54 topLayout->addWidget(m_chooseFontButton);
55 }
56
57 DolphinFontRequester::~DolphinFontRequester()
58 {
59 }
60
61 void DolphinFontRequester::setMode(Mode mode)
62 {
63 m_mode = mode;
64 m_modeCombo->setCurrentIndex(m_mode);
65 m_modeCombo->setFont(customFont());
66 m_chooseFontButton->setEnabled(m_mode == CustomFont);
67 }
68
69 DolphinFontRequester::Mode DolphinFontRequester::mode() const
70 {
71 return m_mode;
72 }
73
74 QFont DolphinFontRequester::currentFont() const
75 {
76 return (m_mode == CustomFont) ? m_customFont : KGlobalSettings::generalFont();
77 }
78
79 void DolphinFontRequester::setCustomFont(const QFont& font)
80 {
81 m_customFont = font;
82 }
83
84 QFont DolphinFontRequester::customFont() const
85 {
86 return m_customFont;
87 }
88
89 bool DolphinFontRequester::event(QEvent* event)
90 {
91 if (event->type() == QEvent::Polish) {
92 m_modeCombo->setFont(customFont());
93 }
94 return QWidget::event(event);
95 }
96
97 void DolphinFontRequester::openFontDialog()
98 {
99 QFont font = m_customFont;
100 const int result = KFontDialog::getFont(font,
101 KFontChooser::NoDisplayFlags,
102 this);
103 if (result == KFontDialog::Accepted) {
104 m_customFont = font;
105 m_modeCombo->setFont(m_customFont);
106 emit changed();
107 }
108 }
109
110 void DolphinFontRequester::changeMode(int index)
111 {
112 setMode((index == CustomFont) ? CustomFont : SystemFont);
113 emit changed();
114 }
115
116 #include "dolphinfontrequester.moc"