introduce bitcoin amount field with split amount/decimals, to protect against mistake...
[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
20 /* First (currently only) page of options */
21 class MainOptionsPage : public QWidget
22 {
23 public:
24     explicit MainOptionsPage(QWidget *parent=0);
25
26     void setMapper(MonitoredDataMapper *mapper);
27 private:
28     QCheckBox *bitcoin_at_startup;
29     QCheckBox *minimize_to_tray;
30     QCheckBox *map_port_upnp;
31     QCheckBox *minimize_on_close;
32     QCheckBox *connect_socks4;
33     QLineEdit *proxy_ip;
34     QLineEdit *proxy_port;
35     BitcoinAmountField *fee_edit;
36
37 signals:
38
39 public slots:
40
41 };
42
43 OptionsDialog::OptionsDialog(QWidget *parent):
44     QDialog(parent), contents_widget(0), pages_widget(0),
45     main_options_page(0), model(0)
46 {
47     contents_widget = new QListWidget();
48     contents_widget->setMaximumWidth(128);
49
50     pages_widget = new QStackedWidget();
51     pages_widget->setMinimumWidth(300);
52
53     QListWidgetItem *item_main = new QListWidgetItem(tr("Main"));
54     contents_widget->addItem(item_main);
55     main_options_page = new MainOptionsPage(this);
56     pages_widget->addWidget(main_options_page);
57
58     contents_widget->setCurrentRow(0);
59
60     QHBoxLayout *main_layout = new QHBoxLayout();
61     main_layout->addWidget(contents_widget);
62     main_layout->addWidget(pages_widget, 1);
63
64     QVBoxLayout *layout = new QVBoxLayout();
65     layout->addLayout(main_layout);
66
67     QHBoxLayout *buttons = new QHBoxLayout();
68     buttons->addStretch(1);
69     QPushButton *ok_button = new QPushButton(tr("OK"));
70     buttons->addWidget(ok_button);
71     QPushButton *cancel_button = new QPushButton(tr("Cancel"));
72     buttons->addWidget(cancel_button);
73     apply_button = new QPushButton(tr("Apply"));
74     apply_button->setEnabled(false);
75     buttons->addWidget(apply_button);
76
77     layout->addLayout(buttons);
78
79     setLayout(layout);
80     setWindowTitle(tr("Options"));
81
82     /* Widget-to-option mapper */
83     mapper = new MonitoredDataMapper(this);
84     mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
85     mapper->setOrientation(Qt::Vertical);
86     /* enable apply button when data modified */
87     connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApply()));
88     /* disable apply button when new data loaded */
89     connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApply()));
90
91     /* Event bindings */
92     connect(ok_button, SIGNAL(clicked()), this, SLOT(okClicked()));
93     connect(cancel_button, SIGNAL(clicked()), this, SLOT(cancelClicked()));
94     connect(apply_button, SIGNAL(clicked()), this, SLOT(applyClicked()));
95 }
96
97 void OptionsDialog::setModel(OptionsModel *model)
98 {
99     this->model = model;
100
101     mapper->setModel(model);
102     main_options_page->setMapper(mapper);
103
104     mapper->toFirst();
105 }
106
107 void OptionsDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
108 {
109     Q_UNUSED(previous);
110     if(current)
111     {
112         pages_widget->setCurrentIndex(contents_widget->row(current));
113     }
114 }
115
116 void OptionsDialog::okClicked()
117 {
118     mapper->submit();
119     accept();
120 }
121
122 void OptionsDialog::cancelClicked()
123 {
124     reject();
125 }
126
127 void OptionsDialog::applyClicked()
128 {
129     mapper->submit();
130     apply_button->setEnabled(false);
131 }
132
133 void OptionsDialog::enableApply()
134 {
135     apply_button->setEnabled(true);
136 }
137
138 void OptionsDialog::disableApply()
139 {
140     apply_button->setEnabled(false);
141 }
142
143 MainOptionsPage::MainOptionsPage(QWidget *parent):
144         QWidget(parent)
145 {
146     QVBoxLayout *layout = new QVBoxLayout();
147
148     bitcoin_at_startup = new QCheckBox(tr("&Start Bitcoin on window system startup"));
149     bitcoin_at_startup->setToolTip(tr("Automatically start Bitcoin after the computer is turned on"));
150     layout->addWidget(bitcoin_at_startup);
151
152     minimize_to_tray = new QCheckBox(tr("&Minimize to the tray instead of the taskbar"));
153     minimize_to_tray->setToolTip(tr("Show only a tray icon after minimizing the window"));
154     layout->addWidget(minimize_to_tray);
155
156     map_port_upnp = new QCheckBox(tr("Map port using &UPnP"));
157     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."));
158     layout->addWidget(map_port_upnp);
159
160     minimize_on_close = new QCheckBox(tr("M&inimize on close"));
161     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."));
162     layout->addWidget(minimize_on_close);
163
164     connect_socks4 = new QCheckBox(tr("&Connect through SOCKS4 proxy:"));
165     connect_socks4->setToolTip(tr("Connect to the Bitcon network through a SOCKS4 proxy (e.g. when connecting through Tor)"));
166     layout->addWidget(connect_socks4);
167
168     QHBoxLayout *proxy_hbox = new QHBoxLayout();
169     proxy_hbox->addSpacing(18);
170     QLabel *proxy_ip_label = new QLabel(tr("Proxy &IP: "));
171     proxy_hbox->addWidget(proxy_ip_label);
172     proxy_ip = new QLineEdit();
173     proxy_ip->setMaximumWidth(140);
174     proxy_ip->setEnabled(false);
175     proxy_ip->setValidator(new QRegExpValidator(QRegExp("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"), this));
176     proxy_ip->setToolTip(tr("IP address of the proxy (e.g. 127.0.0.1)"));
177     proxy_ip_label->setBuddy(proxy_ip);
178     proxy_hbox->addWidget(proxy_ip);
179     QLabel *proxy_port_label = new QLabel(tr("&Port: "));
180     proxy_hbox->addWidget(proxy_port_label);
181     proxy_port = new QLineEdit();
182     proxy_port->setMaximumWidth(55);
183     proxy_port->setValidator(new QIntValidator(0, 65535, this));
184     proxy_port->setEnabled(false);
185     proxy_port->setToolTip(tr("Port of the proxy (e.g. 1234)"));
186     proxy_port_label->setBuddy(proxy_port);
187     proxy_hbox->addWidget(proxy_port);
188     proxy_hbox->addStretch(1);
189
190     layout->addLayout(proxy_hbox);
191     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."));
192     fee_help->setWordWrap(true);
193     layout->addWidget(fee_help);
194
195     QHBoxLayout *fee_hbox = new QHBoxLayout();
196     fee_hbox->addSpacing(18);
197     QLabel *fee_label = new QLabel(tr("Pay transaction &fee"));
198     fee_hbox->addWidget(fee_label);
199     fee_edit = new BitcoinAmountField();
200     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."));
201
202     fee_label->setBuddy(fee_edit);
203     fee_hbox->addWidget(fee_edit);
204     fee_hbox->addStretch(1);
205
206     layout->addLayout(fee_hbox);
207
208     layout->addStretch(1); // Extra space at bottom
209
210     setLayout(layout);
211
212     connect(connect_socks4, SIGNAL(toggled(bool)), proxy_ip, SLOT(setEnabled(bool)));
213     connect(connect_socks4, SIGNAL(toggled(bool)), proxy_port, SLOT(setEnabled(bool)));
214
215 #ifndef USE_UPNP
216     map_port_upnp->setDisabled(true);
217 #endif
218 }
219
220 void MainOptionsPage::setMapper(MonitoredDataMapper *mapper)
221 {
222     // Map model to widgets
223     mapper->addMapping(bitcoin_at_startup, OptionsModel::StartAtStartup);
224     mapper->addMapping(minimize_to_tray, OptionsModel::MinimizeToTray);
225     mapper->addMapping(map_port_upnp, OptionsModel::MapPortUPnP);
226     mapper->addMapping(minimize_on_close, OptionsModel::MinimizeOnClose);
227     mapper->addMapping(connect_socks4, OptionsModel::ConnectSOCKS4);
228     mapper->addMapping(proxy_ip, OptionsModel::ProxyIP);
229     mapper->addMapping(proxy_port, OptionsModel::ProxyPort);
230     mapper->addMapping(fee_edit, OptionsModel::Fee);
231 }
232