preparations for multiple unit (uBTC, mBTC, BTC) support, fix amount entry issue
[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
7 #include <QHBoxLayout>
8 #include <QVBoxLayout>
9 #include <QPushButton>
10 #include <QListWidget>
11 #include <QStackedWidget>
12
13 #include <QCheckBox>
14 #include <QLabel>
15 #include <QLineEdit>
16 #include <QIntValidator>
17 #include <QDoubleValidator>
18 #include <QRegExpValidator>
19 #include <QDialogButtonBox>
20 #include <QDebug>
21
22 /* First page of options */
23 class MainOptionsPage : public QWidget
24 {
25 public:
26     explicit MainOptionsPage(QWidget *parent=0);
27
28     void setMapper(MonitoredDataMapper *mapper);
29 private:
30     QCheckBox *bitcoin_at_startup;
31     QCheckBox *minimize_to_tray;
32     QCheckBox *map_port_upnp;
33     QCheckBox *minimize_on_close;
34     QCheckBox *connect_socks4;
35     QLineEdit *proxy_ip;
36     QLineEdit *proxy_port;
37     BitcoinAmountField *fee_edit;
38
39 signals:
40
41 public slots:
42
43 };
44
45 class DisplayOptionsPage : public QWidget
46 {
47 public:
48     explicit DisplayOptionsPage(QWidget *parent=0);
49
50     void setMapper(MonitoredDataMapper *mapper);
51 private:
52     QLineEdit *unit;
53 signals:
54
55 public slots:
56
57 };
58
59 OptionsDialog::OptionsDialog(QWidget *parent):
60     QDialog(parent), contents_widget(0), pages_widget(0),
61     model(0), main_page(0), display_page(0)
62 {
63     contents_widget = new QListWidget();
64     contents_widget->setMaximumWidth(128);
65
66     pages_widget = new QStackedWidget();
67     pages_widget->setMinimumWidth(300);
68
69     QListWidgetItem *item_main = new QListWidgetItem(tr("Main"));
70     contents_widget->addItem(item_main);
71     main_page = new MainOptionsPage(this);
72     pages_widget->addWidget(main_page);
73
74     QListWidgetItem *item_display = new QListWidgetItem(tr("Display"));
75     //contents_widget->addItem(item_display);
76     display_page = new DisplayOptionsPage(this);
77     pages_widget->addWidget(display_page);
78
79     contents_widget->setCurrentRow(0);
80
81     QHBoxLayout *main_layout = new QHBoxLayout();
82     main_layout->addWidget(contents_widget);
83     main_layout->addWidget(pages_widget, 1);
84
85     QVBoxLayout *layout = new QVBoxLayout();
86     layout->addLayout(main_layout);
87
88     QDialogButtonBox *buttonbox = new QDialogButtonBox();
89     buttonbox->setStandardButtons(QDialogButtonBox::Apply|QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
90     apply_button = buttonbox->button(QDialogButtonBox::Apply);
91     layout->addWidget(buttonbox);
92
93     setLayout(layout);
94     setWindowTitle(tr("Options"));
95
96     /* Widget-to-option mapper */
97     mapper = new MonitoredDataMapper(this);
98     mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
99     mapper->setOrientation(Qt::Vertical);
100     /* enable apply button when data modified */
101     connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApply()));
102     /* disable apply button when new data loaded */
103     connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApply()));
104
105     /* Event bindings */
106     qDebug() << "setup";
107     connect(contents_widget, SIGNAL(currentRowChanged(int)), this, SLOT(changePage(int)));
108     connect(buttonbox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(okClicked()));
109     connect(buttonbox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(cancelClicked()));
110     connect(buttonbox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(applyClicked()));
111 }
112
113 void OptionsDialog::setModel(OptionsModel *model)
114 {
115     this->model = model;
116
117     mapper->setModel(model);
118     main_page->setMapper(mapper);
119     display_page->setMapper(mapper);
120
121     mapper->toFirst();
122 }
123
124 void OptionsDialog::changePage(int index)
125 {
126     qDebug() << "page" << index;
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: "));
254     unit_hbox->addWidget(unit_label);
255     unit = new QLineEdit();
256
257     unit_label->setBuddy(unit);
258     unit_hbox->addWidget(unit);
259
260     layout->addLayout(unit_hbox);
261     layout->addStretch();
262
263     setLayout(layout);
264 }
265
266 void DisplayOptionsPage::setMapper(MonitoredDataMapper *mapper)
267 {
268 }