+#include "client.h"
+
+SettingsDlg::SettingsDlg(QWidget *parent)
+ : QDialog(parent),
+ _currentPage(0)
+{
+ ui.setupUi(this);
+ setModal(true);
+ setAttribute(Qt::WA_DeleteOnClose, true);
+ setWindowIcon(QIcon::fromTheme("configure"));
+
+ updateGeometry();
+
+ ui.settingsTree->setRootIsDecorated(false);
+
+ connect(ui.settingsTree, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
+ connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
+
+ connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), SLOT(coreConnectionStateChanged()));
+
+ setButtonStates();
+
+ // Some settings panes can take a good bit of space. To avoid squashing the settings tree, try
+ // to resize the dialog. If needed, it can always be resized by the user to take less space.
+ //
+ // Only try to resize if the sizes are valid. This shouldn't happen.. but better to be safe.
+ // See http://www.qtcentre.org/threads/3427-invalid-sizeHint()
+ if (ui.settingsTree->sizeHint().isValid() && ui.settingsTree->size().isValid()) {
+ // Find out how much width would make the settings tree happy
+ int wantedExtraWidth = ui.settingsTree->sizeHint().width()
+ - ui.settingsTree->size().width();
+ // If more space is needed, try to resize to allow for it. Qt should keep the dialog within
+ // the bounds of the screen.
+ if (wantedExtraWidth > 0) {
+ this->resize(this->width() + wantedExtraWidth, this->height());
+ }
+ }
+}
+
+
+void SettingsDlg::coreConnectionStateChanged()
+{
+ for (int i = 0; i < ui.settingsTree->topLevelItemCount(); i++) {
+ QTreeWidgetItem *catItem = ui.settingsTree->topLevelItem(i);
+ for (int j = 0; j < catItem->childCount(); j++) {
+ QTreeWidgetItem *item = catItem->child(j);
+ setItemState(item);
+ }
+ setItemState(catItem);
+ }
+}
+
+
+void SettingsDlg::setItemState(QTreeWidgetItem *item)
+{
+ SettingsPage *sp = qobject_cast<SettingsPage *>(item->data(0, SettingsPageRole).value<QObject *>());
+ Q_ASSERT(sp);
+ item->setDisabled(!Client::isConnected() && sp->needsCoreConnection());
+}
+
+
+void SettingsDlg::registerSettingsPage(SettingsPage *sp)
+{
+ sp->setParent(ui.settingsStack);
+ ui.settingsStack->addWidget(sp);
+
+ connect(sp, SIGNAL(changed(bool)), this, SLOT(setButtonStates()));
+
+ QTreeWidgetItem *cat;
+ QList<QTreeWidgetItem *> cats = ui.settingsTree->findItems(sp->category(), Qt::MatchExactly);
+ if (!cats.count()) {
+ cat = new QTreeWidgetItem(ui.settingsTree, QStringList(sp->category()));
+ cat->setExpanded(true);
+ cat->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
+ }
+ else {
+ cat = cats[0];
+ }