configdialog: Update icons in ThemeChanged event
[lxde/liblxqt.git] / configdialog / lxqtconfigdialog.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.org
6 *
7 * Copyright (C) 2012 Alec Moskvin <alecm@gmx.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * END_COMMON_COPYRIGHT_HEADER */
24
25 #include "lxqtconfigdialog.h"
26 #include "ui_lxqtconfigdialog.h"
27
28 #include <XdgIcon>
29 #include <QPushButton>
30
31 using namespace LXQt;
32
33 ConfigDialog::ConfigDialog(const QString& title, Settings* settings, QWidget* parent) :
34 QDialog(parent),
35 mSettings(settings),
36 mCache(new SettingsCache(settings)),
37 ui(new Ui::ConfigDialog)
38 {
39 ui->setupUi(this);
40 setWindowTitle(title);
41 connect(ui->buttons, SIGNAL(clicked(QAbstractButton*)), SLOT(dialogButtonsAction(QAbstractButton*)));
42 ui->moduleList->setVisible(false);
43 foreach(QPushButton* button, ui->buttons->findChildren<QPushButton*>())
44 button->setAutoDefault(false);
45 }
46
47 void ConfigDialog::setButtons(QDialogButtonBox::StandardButtons buttons)
48 {
49 ui->buttons->setStandardButtons(buttons);
50 foreach(QPushButton* button, ui->buttons->findChildren<QPushButton*>())
51 button->setAutoDefault(false);
52 }
53
54 void ConfigDialog::addPage(QWidget* page, const QString& name, const QString& iconName)
55 {
56 addPage(page, name, QStringList() << iconName);
57 }
58
59 void ConfigDialog::addPage(QWidget* page, const QString& name, const QStringList& iconNames)
60 {
61 Q_ASSERT(page);
62 if (!page)
63 {
64 return;
65 }
66
67 /* We set the layout margin to 0. In the default configuration, one page
68 * only, it aligns buttons with the page. In multi-page it saves a little
69 * bit of space, without clutter.
70 */
71 if (page->layout())
72 {
73 page->layout()->setMargin(0);
74 }
75
76 QStringList icons = QStringList(iconNames) << "application-x-executable";
77 new QListWidgetItem(XdgIcon::fromTheme(icons), name, ui->moduleList);
78 mIcons.append(icons);
79 ui->stackedWidget->addWidget(page);
80 if(ui->stackedWidget->count() > 1)
81 {
82 ui->moduleList->setVisible(true);
83 ui->moduleList->setCurrentRow(0);
84 mMaxSize = QSize(qMax(page->geometry().width() + ui->moduleList->geometry().width(),
85 mMaxSize.width()),
86 qMax(page->geometry().height() + ui->buttons->geometry().height(),
87 mMaxSize.height()));
88 }
89 else
90 {
91 mMaxSize = page->geometry().size();
92 }
93 resize(mMaxSize);
94 }
95
96 void ConfigDialog::showPage(QWidget* page)
97 {
98 int index = ui->stackedWidget->indexOf(page);
99 if (index < 0)
100 return;
101
102 ui->stackedWidget->setCurrentIndex(index);
103 ui->moduleList->setCurrentRow(index);
104 }
105
106 bool ConfigDialog::event(QEvent * event)
107 {
108 if (QEvent::ThemeChange == event->type())
109 updateIcons();
110 return QDialog::event(event);
111 }
112
113 void ConfigDialog::closeEvent(QCloseEvent* event)
114 {
115 emit save();
116 mSettings->sync();
117 }
118
119 void ConfigDialog::dialogButtonsAction(QAbstractButton* button)
120 {
121 QDialogButtonBox::StandardButton standardButton = ui->buttons->standardButton(button);
122 emit clicked(standardButton);
123 if (standardButton == QDialogButtonBox::Reset)
124 {
125 mCache->loadToSettings();
126 emit reset();
127 }
128 else if(standardButton == QDialogButtonBox::Close)
129 {
130 close();
131 }
132 }
133
134 void ConfigDialog::updateIcons()
135 {
136 for (int ix = 0; ix < mIcons.size(); ix++)
137 ui->moduleList->item(ix)->setIcon(XdgIcon::fromTheme(mIcons.at(ix)));
138 update();
139 }
140
141 ConfigDialog::~ConfigDialog()
142 {
143 delete ui;
144 delete mCache;
145 }