Full support for other units, add configuration option for default unit (used when...
[novacoin.git] / src / qt / optionsdialog.cpp
1 #include "optionsdialog.h"
2 #include "optionsmodel.h"
3 #include "bitcoinamountfield.h"
4 #include "monitoreddatamapper.h"
5 #include "guiutil.h"
6 #include "bitcoinunits.h"
7 #include "qvaluecombobox.h"
8
9 #include <QHBoxLayout>
10 #include <QVBoxLayout>
11 #include <QPushButton>
12 #include <QListWidget>
13 #include <QStackedWidget>
14
15 #include <QCheckBox>
16 #include <QLabel>
17 #include <QLineEdit>
18 #include <QIntValidator>
19 #include <QDoubleValidator>
20 #include <QRegExpValidator>
21 #include <QDialogButtonBox>
22 #include <QDebug>
23
24 /* First page of options */
25 class MainOptionsPage : public QWidget
26 {
27 public:
28     explicit MainOptionsPage(QWidget *parent=0);
29
30     void setMapper(MonitoredDataMapper *mapper);
31 private:
32     QCheckBox *bitcoin_at_startup;
33     QCheckBox *minimize_to_tray;
34     QCheckBox *map_port_upnp;
35     QCheckBox *minimize_on_close;
36     QCheckBox *connect_socks4;
37     QLineEdit *proxy_ip;
38     QLineEdit *proxy_port;
39     BitcoinAmountField *fee_edit;
40
41 signals:
42
43 public slots:
44
45 };
46
47 class DisplayOptionsPage : public QWidget
48 {
49 public:
50     explicit DisplayOptionsPage(QWidget *parent=0);
51
52     void setMapper(MonitoredDataMapper *mapper);
53 private:
54     QValueComboBox *unit;
55 signals:
56
57 public slots:
58
59 };
60
61 OptionsDialog::OptionsDialog(QWidget *parent):
62     QDialog(parent), contents_widget(0), pages_widget(0),
63     model(0), main_page(0), display_page(0)
64 {
65     contents_widget = new QListWidget();
66     contents_widget->setMaximumWidth(128);
67
68     pages_widget = new QStackedWidget();
69     pages_widget->setMinimumWidth(300);
70
71     QListWidgetItem *item_main = new QListWidgetItem(tr("Main"));
72     contents_widget->addItem(item_main);
73     main_page = new MainOptionsPage(this);
74     pages_widget->addWidget(main_page);
75
76     QListWidgetItem *item_display = new QListWidgetItem(tr("Display"));
77     contents_widget->addItem(item_display);
78     display_page = new DisplayOptionsPage(this);
79     pages_widget->addWidget(display_page);
80
81     contents_widget->setCurrentRow(0);
82
83     QHBoxLayout *main_layout = new QHBoxLayout();
84     main_layout->addWidget(contents_widget);
85     main_layout->addWidget(pages_widget, 1);
86
87     QVBoxLayout *layout = new QVBoxLayout();
88     layout->addLayout(main_layout);
89
90     QDialogButtonBox *buttonbox = new QDialogButtonBox();
91     buttonbox->setStandardButtons(QDialogButtonBox::Apply|QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
92     apply_button = buttonbox->button(QDialogButtonBox::Apply);
93     layout->addWidget(buttonbox);
94
95     setLayout(layout);
96     setWindowTitle(tr("Options"));
97
98     /* Widget-to-option mapper */
99     mapper = new MonitoredDataMapper(this);
100     mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
101     mapper->setOrientation(Qt::Vertical);
102     /* enable apply button when data modified */
103     connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApply()));
104     /* disable apply button when new data loaded */
105     connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApply()));
106
107     /* Event bindings */
108     connect(contents_widget, SIGNAL(currentRowChanged(int)), this, SLOT(changePage(int)));
109     connect(buttonbox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(okClicked()));
110     connect(buttonbox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(cancelClicked()));
111     connect(buttonbox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(applyClicked()));
112 }
113
114 void OptionsDialog::setModel(OptionsModel *model)
115 {
116     this->model = model;
117
118     mapper->setModel(model);
119     main_page->setMapper(mapper);
120     display_page->setMapper(mapper);
121
122     mapper->toFirst();
123 }
124
125 void OptionsDialog::changePage(int index)
126 {
127     pages_widget->setCurrentIndex(index);
128 }
129
130 void OptionsDialog::okClicked()
131 {
132     mapper->submit();
133     accept();
134 }
135
136 void OptionsDialog::cancelClicked()
137 {
138     reject();
139 }
140
141 void OptionsDialog::applyClicked()
142 {
143     mapper->submit();
144     apply_button->setEnabled(false);
145 }
146
147 void OptionsDialog::enableApply()
148 {
149     apply_button->setEnabled(true);
150 }
151
152 void OptionsDialog::disableApply()
153 {
154     apply_button->setEnabled(false);
155 }
156
157 MainOptionsPage::MainOptionsPage(QWidget *parent):
158         QWidget(parent)
159 {
160     QVBoxLayout *layout = new QVBoxLayout();
161
162     bitcoin_at_startup = new QCheckBox(tr("&Start Bitcoin on window system startup"));
163     bitcoin_at_startup->setToolTip(tr("Automatically start Bitcoin after the computer is turned on"));
164     layout->addWidget(bitcoin_at_startup);
165
166     minimize_to_tray = new QCheckBox(tr("&Minimize to the tray instead of the taskbar"));
167     minimize_to_tray->setToolTip(tr("Show only a tray icon after minimizing the window"));
168     layout->addWidget(minimize_to_tray);
169
170     map_port_upnp = new QCheckBox(tr("Map port using &UPnP"));
171     map_port_upnp->setToolTip(tr("Automatically open the Bitcoin client port on the router. This only works when your router supports UPnP and it is enabled."));
172     layout->addWidget(map_port_upnp);
173
174     minimize_on_close = new QCheckBox(tr("M&inimize on close"));
175     minimize_on_close->setToolTip(tr("Minimize instead of exit the application when the window is closed. When this option is enabled, the application will be closed only after selecting Quit in the menu."));
176     layout->addWidget(minimize_on_close);
177
178     connect_socks4 = new QCheckBox(tr("&Connect through SOCKS4 proxy:"));
179     connect_socks4->setToolTip(tr("Connect to the Bitcon network through a SOCKS4 proxy (e.g. when connecting through Tor)"));
180     layout->addWidget(connect_socks4);
181
182     QHBoxLayout *proxy_hbox = new QHBoxLayout();
183     proxy_hbox->addSpacing(18);
184     QLabel *proxy_ip_label = new QLabel(tr("Proxy &IP: "));
185     proxy_hbox->addWidget(proxy_ip_label);
186     proxy_ip = new QLineEdit();
187     proxy_ip->setMaximumWidth(140);
188     proxy_ip->setEnabled(false);
189     proxy_ip->setValidator(new QRegExpValidator(QRegExp("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"), this));
190     proxy_ip->setToolTip(tr("IP address of the proxy (e.g. 127.0.0.1)"));
191     proxy_ip_label->setBuddy(proxy_ip);
192     proxy_hbox->addWidget(proxy_ip);
193     QLabel *proxy_port_label = new QLabel(tr("&Port: "));
194     proxy_hbox->addWidget(proxy_port_label);
195     proxy_port = new QLineEdit();
196     proxy_port->setMaximumWidth(55);
197     proxy_port->setValidator(new QIntValidator(0, 65535, this));
198     proxy_port->setEnabled(false);
199     proxy_port->setToolTip(tr("Port of the proxy (e.g. 1234)"));
200     proxy_port_label->setBuddy(proxy_port);
201     proxy_hbox->addWidget(proxy_port);
202     proxy_hbox->addStretch(1);
203
204     layout->addLayout(proxy_hbox);
205     QLabel *fee_help = new QLabel(tr("Optional transaction fee per KB that helps make sure your transactions are processed quickly.  Most transactions are 1KB.  Fee 0.01 recommended."));
206     fee_help->setWordWrap(true);
207     layout->addWidget(fee_help);
208
209     QHBoxLayout *fee_hbox = new QHBoxLayout();
210     fee_hbox->addSpacing(18);
211     QLabel *fee_label = new QLabel(tr("Pay transaction &fee"));
212     fee_hbox->addWidget(fee_label);
213     fee_edit = new BitcoinAmountField();
214     fee_edit->setToolTip(tr("Optional transaction fee per KB that helps make sure your transactions are processed quickly. Most transactions are 1KB. Fee 0.01 recommended."));
215
216     fee_label->setBuddy(fee_edit);
217     fee_hbox->addWidget(fee_edit);
218     fee_hbox->addStretch(1);
219
220     layout->addLayout(fee_hbox);
221
222     layout->addStretch(1); // Extra space at bottom
223
224     setLayout(layout);
225
226     connect(connect_socks4, SIGNAL(toggled(bool)), proxy_ip, SLOT(setEnabled(bool)));
227     connect(connect_socks4, SIGNAL(toggled(bool)), proxy_port, SLOT(setEnabled(bool)));
228
229 #ifndef USE_UPNP
230     map_port_upnp->setDisabled(true);
231 #endif
232 }
233
234 void MainOptionsPage::setMapper(MonitoredDataMapper *mapper)
235 {
236     // Map model to widgets
237     mapper->addMapping(bitcoin_at_startup, OptionsModel::StartAtStartup);
238     mapper->addMapping(minimize_to_tray, OptionsModel::MinimizeToTray);
239     mapper->addMapping(map_port_upnp, OptionsModel::MapPortUPnP);
240     mapper->addMapping(minimize_on_close, OptionsModel::MinimizeOnClose);
241     mapper->addMapping(connect_socks4, OptionsModel::ConnectSOCKS4);
242     mapper->addMapping(proxy_ip, OptionsModel::ProxyIP);
243     mapper->addMapping(proxy_port, OptionsModel::ProxyPort);
244     mapper->addMapping(fee_edit, OptionsModel::Fee);
245 }
246
247 DisplayOptionsPage::DisplayOptionsPage(QWidget *parent):
248         QWidget(parent)
249 {
250     QVBoxLayout *layout = new QVBoxLayout();
251     QHBoxLayout *unit_hbox = new QHBoxLayout();
252     unit_hbox->addSpacing(18);
253     QLabel *unit_label = new QLabel(tr("&Unit to show amounts in: "));
254     unit_hbox->addWidget(unit_label);
255     unit = new QValueComboBox(this);
256     unit->setModel(new BitcoinUnits(this));
257     unit->setToolTip(tr("Choose the default subdivision unit to show in the interface, and when sending coins"));
258
259     unit_label->setBuddy(unit);
260     unit_hbox->addWidget(unit);
261
262     layout->addLayout(unit_hbox);
263     layout->addStretch();
264
265     setLayout(layout);
266 }
267
268 void DisplayOptionsPage::setMapper(MonitoredDataMapper *mapper)
269 {
270     mapper->addMapping(unit, OptionsModel::DisplayUnit);
271 }