1 #include "optionsdialog.h"
2 #include "optionsmodel.h"
3 #include "bitcoinamountfield.h"
4 #include "monitoreddatamapper.h"
6 #include "bitcoinunits.h"
7 #include "qvaluecombobox.h"
10 #include <QVBoxLayout>
11 #include <QPushButton>
12 #include <QListWidget>
13 #include <QStackedWidget>
18 #include <QIntValidator>
19 #include <QDoubleValidator>
20 #include <QRegExpValidator>
21 #include <QDialogButtonBox>
23 #include <QMessageBox>
25 class OptionsPage: public QWidget
29 explicit OptionsPage(QWidget *parent=0): QWidget(parent) {}
31 virtual void setMapper(MonitoredDataMapper *mapper) = 0;
34 class MainOptionsPage: public OptionsPage
38 explicit MainOptionsPage(QWidget *parent=0);
40 virtual void setMapper(MonitoredDataMapper *mapper);
42 BitcoinAmountField *fee_edit;
43 QCheckBox *bitcoin_at_startup;
44 QCheckBox *detach_database;
47 class WindowOptionsPage: public OptionsPage
51 explicit WindowOptionsPage(QWidget *parent=0);
53 virtual void setMapper(MonitoredDataMapper *mapper);
56 QCheckBox *minimize_to_tray;
57 QCheckBox *minimize_on_close;
61 class DisplayOptionsPage: public OptionsPage
65 explicit DisplayOptionsPage(QWidget *parent=0);
67 virtual void setMapper(MonitoredDataMapper *mapper);
71 QCheckBox *display_addresses;
72 bool restart_warning_displayed;
74 void showRestartWarning();
77 class NetworkOptionsPage: public OptionsPage
81 explicit NetworkOptionsPage(QWidget *parent=0);
83 virtual void setMapper(MonitoredDataMapper *mapper);
85 QCheckBox *map_port_upnp;
86 QCheckBox *connect_socks4;
88 QLineEdit *proxy_port;
92 #include "optionsdialog.moc"
94 OptionsDialog::OptionsDialog(QWidget *parent):
95 QDialog(parent), contents_widget(0), pages_widget(0),
98 contents_widget = new QListWidget();
99 contents_widget->setMaximumWidth(128);
101 pages_widget = new QStackedWidget();
102 pages_widget->setMinimumWidth(500);
103 pages_widget->setMinimumHeight(300);
105 pages.append(new MainOptionsPage(this));
106 pages.append(new NetworkOptionsPage(this));
108 /* Hide Window options on Mac as there are currently none available */
109 pages.append(new WindowOptionsPage(this));
111 pages.append(new DisplayOptionsPage(this));
113 foreach(OptionsPage *page, pages)
115 QListWidgetItem *item = new QListWidgetItem(page->windowTitle());
116 contents_widget->addItem(item);
117 pages_widget->addWidget(page);
120 contents_widget->setCurrentRow(0);
122 QHBoxLayout *main_layout = new QHBoxLayout();
123 main_layout->addWidget(contents_widget);
124 main_layout->addWidget(pages_widget, 1);
126 QVBoxLayout *layout = new QVBoxLayout();
127 layout->addLayout(main_layout);
129 QDialogButtonBox *buttonbox = new QDialogButtonBox();
130 buttonbox->setStandardButtons(QDialogButtonBox::Apply|QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
131 apply_button = buttonbox->button(QDialogButtonBox::Apply);
132 layout->addWidget(buttonbox);
135 setWindowTitle(tr("Options"));
137 /* Widget-to-option mapper */
138 mapper = new MonitoredDataMapper(this);
139 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
140 mapper->setOrientation(Qt::Vertical);
141 /* enable apply button when data modified */
142 connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApply()));
143 /* disable apply button when new data loaded */
144 connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApply()));
147 connect(contents_widget, SIGNAL(currentRowChanged(int)), this, SLOT(changePage(int)));
148 connect(buttonbox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(okClicked()));
149 connect(buttonbox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(cancelClicked()));
150 connect(buttonbox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(applyClicked()));
153 void OptionsDialog::setModel(OptionsModel *model)
157 mapper->setModel(model);
159 foreach(OptionsPage *page, pages)
161 page->setMapper(mapper);
167 void OptionsDialog::changePage(int index)
169 pages_widget->setCurrentIndex(index);
172 void OptionsDialog::okClicked()
178 void OptionsDialog::cancelClicked()
183 void OptionsDialog::applyClicked()
186 apply_button->setEnabled(false);
189 void OptionsDialog::enableApply()
191 apply_button->setEnabled(true);
194 void OptionsDialog::disableApply()
196 apply_button->setEnabled(false);
200 MainOptionsPage::MainOptionsPage(QWidget *parent):
203 QVBoxLayout *layout = new QVBoxLayout();
204 setWindowTitle(tr("Main"));
206 QLabel *fee_help = new QLabel(tr("Optional transaction fee per kB that helps make sure your transactions are processed quickly. Most transactions are 1 kB. Fee 0.01 recommended."));
207 fee_help->setWordWrap(true);
208 layout->addWidget(fee_help);
210 QHBoxLayout *fee_hbox = new QHBoxLayout();
211 fee_hbox->addSpacing(18);
212 QLabel *fee_label = new QLabel(tr("Pay transaction &fee"));
213 fee_hbox->addWidget(fee_label);
214 fee_edit = new BitcoinAmountField();
216 fee_label->setBuddy(fee_edit);
217 fee_hbox->addWidget(fee_edit);
218 fee_hbox->addStretch(1);
220 layout->addLayout(fee_hbox);
222 bitcoin_at_startup = new QCheckBox(tr("&Start Bitcoin on system login"));
223 bitcoin_at_startup->setToolTip(tr("Automatically start Bitcoin after logging in to the system"));
224 layout->addWidget(bitcoin_at_startup);
226 detach_database = new QCheckBox(tr("&Detach databases at shutdown"));
227 detach_database->setToolTip(tr("Detach block and address databases at shutdown. This means they can be moved to another data directory, but it slows down shutdown. The wallet is always detached."));
228 layout->addWidget(detach_database);
230 layout->addStretch(1); // Extra space at bottom
234 void MainOptionsPage::setMapper(MonitoredDataMapper *mapper)
236 // Map model to widgets
237 mapper->addMapping(fee_edit, OptionsModel::Fee);
238 mapper->addMapping(bitcoin_at_startup, OptionsModel::StartAtStartup);
239 mapper->addMapping(detach_database, OptionsModel::DetachDatabases);
242 /* Display options */
243 DisplayOptionsPage::DisplayOptionsPage(QWidget *parent):
244 OptionsPage(parent), restart_warning_displayed(false)
246 setWindowTitle(tr("Display"));
248 QVBoxLayout *layout = new QVBoxLayout();
250 QHBoxLayout *lang_hbox = new QHBoxLayout();
251 lang_hbox->addSpacing(18);
252 QLabel *lang_label = new QLabel(tr("User Interface &Language:"));
253 lang_hbox->addWidget(lang_label);
254 lang = new QValueComboBox(this);
255 // Make list of languages
256 QDir translations(":translations");
257 lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
258 foreach(const QString &langStr, translations.entryList())
260 lang->addItem(langStr, QVariant(langStr));
263 lang->setToolTip(tr("The user interface language can be set here. This setting will only take effect after restarting Bitcoin."));
264 connect(lang, SIGNAL(activated(int)), this, SLOT(showRestartWarning()));
266 lang_label->setBuddy(lang);
267 lang_hbox->addWidget(lang);
269 layout->addLayout(lang_hbox);
271 QHBoxLayout *unit_hbox = new QHBoxLayout();
272 unit_hbox->addSpacing(18);
273 QLabel *unit_label = new QLabel(tr("&Unit to show amounts in:"));
274 unit_hbox->addWidget(unit_label);
275 unit = new QValueComboBox(this);
276 unit->setModel(new BitcoinUnits(this));
277 unit->setToolTip(tr("Choose the default subdivision unit to show in the interface, and when sending coins"));
279 unit_label->setBuddy(unit);
280 unit_hbox->addWidget(unit);
282 layout->addLayout(unit_hbox);
284 display_addresses = new QCheckBox(tr("&Display addresses in transaction list"), this);
285 display_addresses->setToolTip(tr("Whether to show Bitcoin addresses in the transaction list"));
286 layout->addWidget(display_addresses);
288 layout->addStretch();
292 void DisplayOptionsPage::setMapper(MonitoredDataMapper *mapper)
294 mapper->addMapping(lang, OptionsModel::Language);
295 mapper->addMapping(unit, OptionsModel::DisplayUnit);
296 mapper->addMapping(display_addresses, OptionsModel::DisplayAddresses);
299 void DisplayOptionsPage::showRestartWarning()
301 if(!restart_warning_displayed)
303 QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting Bitcoin."), QMessageBox::Ok);
304 restart_warning_displayed = true;
309 WindowOptionsPage::WindowOptionsPage(QWidget *parent):
312 QVBoxLayout *layout = new QVBoxLayout();
313 setWindowTitle(tr("Window"));
316 minimize_to_tray = new QCheckBox(tr("&Minimize to the tray instead of the taskbar"));
317 minimize_to_tray->setToolTip(tr("Show only a tray icon after minimizing the window"));
318 layout->addWidget(minimize_to_tray);
320 minimize_on_close = new QCheckBox(tr("M&inimize on close"));
321 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."));
322 layout->addWidget(minimize_on_close);
325 layout->addStretch(1); // Extra space at bottom
329 void WindowOptionsPage::setMapper(MonitoredDataMapper *mapper)
331 // Map model to widgets
333 mapper->addMapping(minimize_to_tray, OptionsModel::MinimizeToTray);
336 mapper->addMapping(minimize_on_close, OptionsModel::MinimizeOnClose);
340 /* Network options */
341 NetworkOptionsPage::NetworkOptionsPage(QWidget *parent):
344 QVBoxLayout *layout = new QVBoxLayout();
345 setWindowTitle(tr("Network"));
347 map_port_upnp = new QCheckBox(tr("Map port using &UPnP"));
348 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."));
349 layout->addWidget(map_port_upnp);
351 connect_socks4 = new QCheckBox(tr("&Connect through SOCKS4 proxy:"));
352 connect_socks4->setToolTip(tr("Connect to the Bitcon network through a SOCKS4 proxy (e.g. when connecting through Tor)"));
353 layout->addWidget(connect_socks4);
355 QHBoxLayout *proxy_hbox = new QHBoxLayout();
356 proxy_hbox->addSpacing(18);
357 QLabel *proxy_ip_label = new QLabel(tr("Proxy &IP:"));
358 proxy_hbox->addWidget(proxy_ip_label);
359 proxy_ip = new QLineEdit();
360 proxy_ip->setMaximumWidth(140);
361 proxy_ip->setEnabled(false);
362 proxy_ip->setValidator(new QRegExpValidator(QRegExp("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"), this));
363 proxy_ip->setToolTip(tr("IP address of the proxy (e.g. 127.0.0.1)"));
364 proxy_ip_label->setBuddy(proxy_ip);
365 proxy_hbox->addWidget(proxy_ip);
366 QLabel *proxy_port_label = new QLabel(tr("&Port:"));
367 proxy_hbox->addWidget(proxy_port_label);
368 proxy_port = new QLineEdit();
369 proxy_port->setMaximumWidth(55);
370 proxy_port->setValidator(new QIntValidator(0, 65535, this));
371 proxy_port->setEnabled(false);
372 proxy_port->setToolTip(tr("Port of the proxy (e.g. 1234)"));
373 proxy_port_label->setBuddy(proxy_port);
374 proxy_hbox->addWidget(proxy_port);
375 proxy_hbox->addStretch(1);
376 layout->addLayout(proxy_hbox);
378 layout->addStretch(1); // Extra space at bottom
381 connect(connect_socks4, SIGNAL(toggled(bool)), proxy_ip, SLOT(setEnabled(bool)));
382 connect(connect_socks4, SIGNAL(toggled(bool)), proxy_port, SLOT(setEnabled(bool)));
385 map_port_upnp->setDisabled(true);
389 void NetworkOptionsPage::setMapper(MonitoredDataMapper *mapper)
391 // Map model to widgets
392 mapper->addMapping(map_port_upnp, OptionsModel::MapPortUPnP);
393 mapper->addMapping(connect_socks4, OptionsModel::ConnectSOCKS4);
394 mapper->addMapping(proxy_ip, OptionsModel::ProxyIP);
395 mapper->addMapping(proxy_port, OptionsModel::ProxyPort);