Update qt5 porting
[lxde/liblxqt.git] / configdialog / lxqtconfigdialog.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXDE-Qt - 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 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
29 #include <qt5xdg/XdgIcon>
30 #include <QtWidgets>
31 #else
32 #include <qtxdg/XdgIcon>
33 #include <QtGui/QPushButton>
34 #endif
35
36 using namespace LxQt;
37
38 ConfigDialog::ConfigDialog(const QString& title, Settings* settings, QWidget* parent) :
39 QDialog(parent),
40 mSettings(settings),
41 mCache(new SettingsCache(settings)),
42 ui(new Ui::ConfigDialog)
43 {
44 ui->setupUi(this);
45 setWindowTitle(title);
46 connect(ui->buttons, SIGNAL(clicked(QAbstractButton*)), SLOT(dialogButtonsAction(QAbstractButton*)));
47 ui->moduleList->setVisible(false);
48 connect(Settings::globalSettings(), SIGNAL(settingsChanged()), this, SLOT(updateIcons()));
49 foreach(QPushButton* button, ui->buttons->findChildren<QPushButton*>())
50 button->setAutoDefault(false);
51 }
52
53 void ConfigDialog::addPage(QWidget* page, const QString& name, const QString& iconName)
54 {
55 addPage(page, name, QStringList() << iconName);
56 }
57
58 void ConfigDialog::addPage(QWidget* page, const QString& name, const QStringList& iconNames)
59 {
60 QStringList icons = QStringList(iconNames) << "application-x-executable";
61 new QListWidgetItem(XdgIcon::fromTheme(icons), name, ui->moduleList);
62 mIcons.append(icons);
63 ui->stackedWidget->addWidget(page);
64 if(ui->stackedWidget->count() > 1)
65 {
66 ui->moduleList->setVisible(true);
67 ui->moduleList->setCurrentRow(0);
68 mMaxSize = QSize(qMax(page->geometry().width() + ui->moduleList->geometry().width(), mMaxSize.width()),
69 qMax(page->geometry().height() + ui->buttons->geometry().height(), mMaxSize.height()));
70 }
71 else
72 {
73 mMaxSize = page->geometry().size();
74 }
75 resize(mMaxSize);
76 }
77
78 void ConfigDialog::closeEvent(QCloseEvent* event)
79 {
80 emit save();
81 mSettings->sync();
82 }
83
84 void ConfigDialog::dialogButtonsAction(QAbstractButton* button)
85 {
86 QDialogButtonBox::ButtonRole role = ui->buttons->buttonRole(button);
87 if (role == QDialogButtonBox::ResetRole)
88 {
89 mCache->loadToSettings();
90 emit reset();
91 }
92 else
93 {
94 close();
95 }
96 }
97
98 void ConfigDialog::updateIcons()
99 {
100 for (int ix = 0; ix < mIcons.size(); ix++)
101 ui->moduleList->item(ix)->setIcon(XdgIcon::fromTheme(mIcons.at(ix)));
102 update();
103 }
104
105 ConfigDialog::~ConfigDialog()
106 {
107 delete ui;
108 delete mCache;
109 }