Rename LxQt to LXQt everywhere
[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 /* We set the layout margin to 0. In the default configuration, one page
63 * only, it aligns buttons with the page. In multi-page it saves a little
64 * bit of space, without clutter.
65 */
66 if (page->layout())
67 {
68 page->layout()->setMargin(0);
69 }
70
71 QStringList icons = QStringList(iconNames) << "application-x-executable";
72 new QListWidgetItem(XdgIcon::fromTheme(icons), name, ui->moduleList);
73 mIcons.append(icons);
74 ui->stackedWidget->addWidget(page);
75 if(ui->stackedWidget->count() > 1)
76 {
77 ui->moduleList->setVisible(true);
78 ui->moduleList->setCurrentRow(0);
79 mMaxSize = QSize(qMax(page->geometry().width() + ui->moduleList->geometry().width(),
80 mMaxSize.width()),
81 qMax(page->geometry().height() + ui->buttons->geometry().height(),
82 mMaxSize.height()));
83 }
84 else
85 {
86 mMaxSize = page->geometry().size();
87 }
88 resize(mMaxSize);
89 }
90
91 void ConfigDialog::showPage(QWidget* page)
92 {
93 int index = ui->stackedWidget->indexOf(page);
94 if (index < 0)
95 return;
96
97 ui->stackedWidget->setCurrentIndex(index);
98 ui->moduleList->setCurrentRow(index);
99 }
100
101 void ConfigDialog::closeEvent(QCloseEvent* event)
102 {
103 emit save();
104 mSettings->sync();
105 }
106
107 void ConfigDialog::dialogButtonsAction(QAbstractButton* button)
108 {
109 QDialogButtonBox::StandardButton standardButton = ui->buttons->standardButton(button);
110 emit clicked(standardButton);
111 if (standardButton == QDialogButtonBox::Reset)
112 {
113 mCache->loadToSettings();
114 emit reset();
115 }
116 else if(standardButton == QDialogButtonBox::Close)
117 {
118 close();
119 }
120 }
121
122 void ConfigDialog::updateIcons()
123 {
124 for (int ix = 0; ix < mIcons.size(); ix++)
125 ui->moduleList->item(ix)->setIcon(XdgIcon::fromTheme(mIcons.at(ix)));
126 update();
127 }
128
129 ConfigDialog::~ConfigDialog()
130 {
131 delete ui;
132 delete mCache;
133 }