added preferences + dialog
[shopper.git] / src / ui / PreferencesDialog.cc
blob055cca88df0115859abea2272628b18a872e6e10
1 /* Shopper
2 * Copyright (C) 2008 David Greaves
4 * This software is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This software is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #define DEBUG_SHOPPER 1
21 #include "shopper.h" // automake, i8n, gettext
23 #include "PreferencesDialog.h"
25 namespace Shopper
27 ////////////////////////////////////////////////////////////////
28 // Public
29 PreferencesDialog::PreferencesDialog( QWidget * parent) :
30 QDialog(parent),
31 signalMapper(this)
33 _ENTER;
35 // UI pane and options
36 uiPane = new QWidget;
37 QGridLayout *uiLayout = new QGridLayout(uiPane);
38 addPref("ui/ShowCategoryHeadings", true, tr("Show Category Headings"), uiLayout, 0);
39 addPref("ui/ShowTwoColumns", false, tr("Show Two Columns"), uiLayout, 1);
41 // Data pane and options
42 dataPane = new QWidget;
43 QGridLayout *dataLayout = new QGridLayout(dataPane);
44 addPref("data/SaveOnExit", true, tr("Save On Exit"), dataLayout, 0);
46 // Mapped settings send the "setting" for every toggle
47 connect(&signalMapper, SIGNAL(mapped(const QString &)),
48 this, SLOT(toggleSetting(const QString &)));
50 // Add the panes to the tab widget
51 tabWidget = new QTabWidget;
52 tabWidget->addTab(uiPane, tr("View"));
53 tabWidget->addTab(dataPane, tr("Data"));
55 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
56 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
58 QVBoxLayout *mainLayout = new QVBoxLayout;
59 mainLayout->addWidget(tabWidget);
60 mainLayout->addWidget(buttonBox);
61 setLayout(mainLayout);
65 ////////////////////////////////////////////////////////////////
66 // Private
67 void PreferencesDialog::addPref(const QString setting, bool defaultVal, QString label, QGridLayout *layout, int row)
69 QSettings settings;
70 bool value = settings.value(setting, defaultVal).toBool();
71 settings.setValue(setting, value); // ensure that any default is saved
72 QLabel *L = new QLabel(label);
73 QCheckBox *Check = new QCheckBox();
74 layout->addWidget(L, row, 0);
75 layout->addWidget(Check, row, 1);
76 Check->setChecked(value);
77 signalMapper.setMapping(Check, setting);
78 connect(Check, SIGNAL(released()), // ugh - but signalMapper can't cope with anything other than signal()
79 &signalMapper, SLOT(map()));
82 void PreferencesDialog::toggleSetting(const QString setting)
84 _ENTER;
85 QSettings settings;
86 bool value = settings.value(setting).toBool();
87 settings.setValue(setting, ! value); // invert
88 DEBUG("Set " << setting << " from " << value << " to " << settings.value(setting).toBool());