1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
22 #include <QMessageBox>
23 #include <QPushButton>
25 #include "settingsdlg.h"
29 SettingsDlg::SettingsDlg(QWidget *parent)
35 setAttribute(Qt::WA_DeleteOnClose, true);
36 setWindowIcon(QIcon::fromTheme("configure"));
40 ui.settingsTree->setRootIsDecorated(false);
42 connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
43 connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
45 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), SLOT(coreConnectionStateChanged()));
49 // Some settings panes can take a good bit of space. To avoid squashing the settings tree, try
50 // to resize the dialog. If needed, it can always be resized by the user to take less space.
52 // Only try to resize if the sizes are valid. This shouldn't happen.. but better to be safe.
53 // See http://www.qtcentre.org/threads/3427-invalid-sizeHint()
54 if (ui.settingsTree->sizeHint().isValid() && ui.settingsTree->size().isValid()) {
55 // Find out how much width would make the settings tree happy
56 int wantedExtraWidth = ui.settingsTree->sizeHint().width()
57 - ui.settingsTree->size().width();
58 // If more space is needed, try to resize to allow for it. Qt should keep the dialog within
59 // the bounds of the screen.
60 if (wantedExtraWidth > 0) {
61 this->resize(this->width() + wantedExtraWidth, this->height());
67 void SettingsDlg::coreConnectionStateChanged()
69 for (int i = 0; i < ui.settingsTree->topLevelItemCount(); i++) {
70 QTreeWidgetItem *catItem = ui.settingsTree->topLevelItem(i);
71 for (int j = 0; j < catItem->childCount(); j++) {
72 QTreeWidgetItem *item = catItem->child(j);
75 setItemState(catItem);
80 void SettingsDlg::setItemState(QTreeWidgetItem *item)
82 SettingsPage *sp = qobject_cast<SettingsPage *>(item->data(0, SettingsPageRole).value<QObject *>());
84 bool disabledDueToConnection = !Client::isConnected() && sp->needsCoreConnection();
85 bool disabledDueToOwnChoice = !sp->isSelectable();
86 item->setDisabled(disabledDueToConnection || disabledDueToOwnChoice);
90 void SettingsDlg::registerSettingsPage(SettingsPage *sp)
92 sp->setParent(ui.settingsStack);
93 ui.settingsStack->addWidget(sp);
95 connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
98 QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
100 cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
101 cat->setExpanded(true);
102 cat->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
108 QTreeWidgetItem *item;
109 if (sp->title().isEmpty())
112 item = new QTreeWidgetItem(cat, QStringList(sp->title()));
114 item->setData(0, SettingsPageRole, QVariant::fromValue<QObject *>(sp));
115 ui.settingsTree->setMinimumWidth(ui.settingsTree->header()->sectionSizeHint(0) + 5);
116 pageIsLoaded[sp] = false;
117 if (!ui.settingsTree->selectedItems().count())
118 ui.settingsTree->setCurrentItem(item);
124 void SettingsDlg::selectPage(SettingsPage *sp)
128 ui.settingsStack->setCurrentIndex(0);
129 ui.pageTitle->setText(tr("Settings"));
133 if (!pageIsLoaded[sp]) {
135 pageIsLoaded[sp] = true;
138 if (sp != currentPage() && currentPage() != 0 && currentPage()->hasChanged()) {
139 int ret = QMessageBox::warning(this, tr("Save changes"),
140 tr("There are unsaved changes on the current configuration page. Would you like to apply your changes now?"),
141 QMessageBox::Discard|QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
142 if (ret == QMessageBox::Save) {
143 if (!applyChanges()) sp = currentPage();
145 else if (ret == QMessageBox::Discard) {
148 else sp = currentPage();
151 if (sp != currentPage()) {
152 if (sp->title().isEmpty()) {
153 ui.pageTitle->setText(sp->category());
154 setWindowTitle(tr("Configure %1").arg(sp->category()));
157 ui.pageTitle->setText(sp->title());
158 setWindowTitle(tr("Configure %1").arg(sp->title()));
161 ui.settingsStack->setCurrentWidget(sp);
168 void SettingsDlg::itemSelected()
170 QList<QTreeWidgetItem *> items = ui.settingsTree->selectedItems();
171 SettingsPage *sp = 0;
172 if (!items.isEmpty()) {
173 sp = qobject_cast<SettingsPage *>(items[0]->data(0, SettingsPageRole).value<QObject *>());
179 void SettingsDlg::setButtonStates()
181 SettingsPage *sp = currentPage();
182 ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(sp && sp->hasChanged());
183 ui.buttonBox->button(QDialogButtonBox::Reset)->setEnabled(sp && sp->hasChanged());
184 ui.buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(sp && sp->hasDefaults());
188 void SettingsDlg::buttonClicked(QAbstractButton *button)
190 switch (ui.buttonBox->standardButton(button)) {
191 case QDialogButtonBox::Ok:
192 if (currentPage() && currentPage()->hasChanged()) {
193 if (applyChanges()) accept();
197 case QDialogButtonBox::Apply:
200 case QDialogButtonBox::Cancel:
204 case QDialogButtonBox::Reset:
207 case QDialogButtonBox::RestoreDefaults:
216 bool SettingsDlg::applyChanges()
218 if (!currentPage()) return false;
219 if (currentPage()->aboutToSave()) {
220 currentPage()->save();
227 void SettingsDlg::undoChanges()
230 currentPage()->load();
235 void SettingsDlg::reload()
237 if (!currentPage()) return;
238 int ret = QMessageBox::question(this, tr("Reload Settings"), tr("Do you like to reload the settings, undoing your changes on this page?"),
239 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
240 if (ret == QMessageBox::Yes) {
241 currentPage()->load();
246 void SettingsDlg::loadDefaults()
248 if (!currentPage()) return;
249 int ret = QMessageBox::question(this, tr("Restore Defaults"), tr("Do you like to restore the default values for this page?"),
250 QMessageBox::RestoreDefaults|QMessageBox::Cancel, QMessageBox::Cancel);
251 if (ret == QMessageBox::RestoreDefaults) {
252 currentPage()->defaults();