Merge branch 'targets'
[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 QStringList icons = QStringList(iconNames) << "application-x-executable";
63 new QListWidgetItem(XdgIcon::fromTheme(icons), name, ui->moduleList);
64 mIcons.append(icons);
65 ui->stackedWidget->addWidget(page);
66 if(ui->stackedWidget->count() > 1)
67 {
68 ui->moduleList->setVisible(true);
69 ui->moduleList->setCurrentRow(0);
70 mMaxSize = QSize(qMax(page->geometry().width() + ui->moduleList->geometry().width(),
71 mMaxSize.width()),
72 qMax(page->geometry().height() + ui->buttons->geometry().height(),
73 mMaxSize.height()));
74 }
75 else
76 {
77 mMaxSize = page->geometry().size();
78 }
79 resize(mMaxSize);
80 }
81
82 void ConfigDialog::showPage(QWidget* page)
83 {
84 int index = ui->stackedWidget->indexOf(page);
85 if (index < 0)
86 return;
87
88 ui->stackedWidget->setCurrentIndex(index);
89 ui->moduleList->setCurrentRow(index);
90 }
91
92 void ConfigDialog::closeEvent(QCloseEvent* event)
93 {
94 emit save();
95 mSettings->sync();
96 }
97
98 void ConfigDialog::dialogButtonsAction(QAbstractButton* button)
99 {
100 QDialogButtonBox::StandardButton standardButton = ui->buttons->standardButton(button);
101 emit clicked(standardButton);
102 if (standardButton == QDialogButtonBox::Reset)
103 {
104 mCache->loadToSettings();
105 emit reset();
106 }
107 else if(standardButton == QDialogButtonBox::Close)
108 {
109 close();
110 }
111 }
112
113 void ConfigDialog::updateIcons()
114 {
115 for (int ix = 0; ix < mIcons.size(); ix++)
116 ui->moduleList->item(ix)->setIcon(XdgIcon::fromTheme(mIcons.at(ix)));
117 update();
118 }
119
120 ConfigDialog::~ConfigDialog()
121 {
122 delete ui;
123 delete mCache;
124 }