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