621776171b19500e62b0d69d2c5822f912a04854
[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 connect(Settings::globalSettings(), SIGNAL(settingsChanged()), this, SLOT(updateIcons()));
44 foreach(QPushButton* button, ui->buttons->findChildren<QPushButton*>())
45 button->setAutoDefault(false);
46 }
47
48 void ConfigDialog::setButtons(QDialogButtonBox::StandardButtons buttons)
49 {
50 ui->buttons->setStandardButtons(buttons);
51 foreach(QPushButton* button, ui->buttons->findChildren<QPushButton*>())
52 button->setAutoDefault(false);
53 }
54
55 void ConfigDialog::addPage(QWidget* page, const QString& name, const QString& iconName)
56 {
57 addPage(page, name, QStringList() << iconName);
58 }
59
60 void ConfigDialog::addPage(QWidget* page, const QString& name, const QStringList& iconNames)
61 {
62 Q_ASSERT(page);
63 if (!page)
64 {
65 return;
66 }
67
68 /* We set the layout margin to 0. In the default configuration, one page
69 * only, it aligns buttons with the page. In multi-page it saves a little
70 * bit of space, without clutter.
71 */
72 if (page->layout())
73 {
74 page->layout()->setMargin(0);
75 }
76
77 QStringList icons = QStringList(iconNames) << "application-x-executable";
78 new QListWidgetItem(XdgIcon::fromTheme(icons), name, ui->moduleList);
79 mIcons.append(icons);
80 ui->stackedWidget->addWidget(page);
81 if(ui->stackedWidget->count() > 1)
82 {
83 ui->moduleList->setVisible(true);
84 ui->moduleList->setCurrentRow(0);
85 mMaxSize = QSize(qMax(page->geometry().width() + ui->moduleList->geometry().width(),
86 mMaxSize.width()),
87 qMax(page->geometry().height() + ui->buttons->geometry().height(),
88 mMaxSize.height()));
89 }
90 else
91 {
92 mMaxSize = page->geometry().size();
93 }
94 resize(mMaxSize);
95 }
96
97 void ConfigDialog::showPage(QWidget* page)
98 {
99 int index = ui->stackedWidget->indexOf(page);
100 if (index < 0)
101 return;
102
103 ui->stackedWidget->setCurrentIndex(index);
104 ui->moduleList->setCurrentRow(index);
105 }
106
107 void ConfigDialog::closeEvent(QCloseEvent* event)
108 {
109 emit save();
110 mSettings->sync();
111 }
112
113 void ConfigDialog::dialogButtonsAction(QAbstractButton* button)
114 {
115 QDialogButtonBox::StandardButton standardButton = ui->buttons->standardButton(button);
116 emit clicked(standardButton);
117 if (standardButton == QDialogButtonBox::Reset)
118 {
119 mCache->loadToSettings();
120 emit reset();
121 }
122 else if(standardButton == QDialogButtonBox::Close)
123 {
124 close();
125 }
126 }
127
128 void ConfigDialog::updateIcons()
129 {
130 for (int ix = 0; ix < mIcons.size(); ix++)
131 ui->moduleList->item(ix)->setIcon(XdgIcon::fromTheme(mIcons.at(ix)));
132 update();
133 }
134
135 ConfigDialog::~ConfigDialog()
136 {
137 delete ui;
138 delete mCache;
139 }