bitcoingui.cpp 15.5 KB
Newer Older
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
1
/*
2
3
 * Qt4 bitcoin GUI.
 *
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
4
5
 * W.J. van der Laan 2011
 */
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
6
7
#include "bitcoingui.h"
#include "transactiontablemodel.h"
8
#include "addressbookpage.h"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
9
10
11
#include "sendcoinsdialog.h"
#include "optionsdialog.h"
#include "aboutdialog.h"
12
#include "clientmodel.h"
13
#include "walletmodel.h"
14
#include "guiutil.h"
15
#include "editaddressdialog.h"
16
#include "optionsmodel.h"
17
#include "transactiondescdialog.h"
18
#include "addresstablemodel.h"
19
#include "transactionview.h"
20
#include "overviewpage.h"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
21
22
23
24
25
26

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QIcon>
27
#include <QTabWidget>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
28
29
30
31
32
33
#include <QVBoxLayout>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
34
#include <QLocale>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
35
#include <QMessageBox>
36
#include <QProgressBar>
37
#include <QStackedWidget>
38
#include <QDateTime>
39
40

#include <QDebug>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
41
42
43
44

#include <iostream>

BitcoinGUI::BitcoinGUI(QWidget *parent):
45
46
47
48
    QMainWindow(parent),
    clientModel(0),
    walletModel(0),
    trayIcon(0)
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
49
50
{
    resize(850, 550);
51
    setWindowTitle(tr("Bitcoin Wallet"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
52
    setWindowIcon(QIcon(":icons/bitcoin"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
53
54

    createActions();
55

56
    // Menus
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
57
    QMenu *file = menuBar()->addMenu("&File");
58
59
    file->addAction(sendCoinsAction);
    file->addAction(receiveCoinsAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
60
    file->addSeparator();
61
    file->addAction(quitAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
62
63
    
    QMenu *settings = menuBar()->addMenu("&Settings");
64
    settings->addAction(optionsAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
65
66

    QMenu *help = menuBar()->addMenu("&Help");
67
    help->addAction(aboutAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
68
    
69
    // Toolbar
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
70
71
    QToolBar *toolbar = addToolBar("Main toolbar");
    toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
72
    toolbar->addAction(overviewAction);
73
74
    toolbar->addAction(sendCoinsAction);
    toolbar->addAction(receiveCoinsAction);
75
    toolbar->addAction(historyAction);
76
    toolbar->addAction(addressBookAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
77

78
79
80
    QToolBar *toolbar2 = addToolBar("Transactions toolbar");
    toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolbar2->addAction(exportAction);
81

82
83
    // Overview page
    overviewPage = new OverviewPage();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
84
    QVBoxLayout *vbox = new QVBoxLayout();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
85

86
    transactionView = new TransactionView(this);
87
    connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), transactionView, SLOT(transactionDetails()));
88
    vbox->addWidget(transactionView);
89

90
91
92
    transactionsPage = new QWidget(this);
    transactionsPage->setLayout(vbox);

93
94
95
96
97
98
    addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);

    receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);

    sendCoinsPage = new SendCoinsDialog(this);

99
100
101
    centralWidget = new QStackedWidget(this);
    centralWidget->addWidget(overviewPage);
    centralWidget->addWidget(transactionsPage);
102
103
104
    centralWidget->addWidget(addressBookPage);
    centralWidget->addWidget(receiveCoinsPage);
    centralWidget->addWidget(sendCoinsPage);
105
    setCentralWidget(centralWidget);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
106
    
107
    // Create status bar
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
108
    statusBar();
109

110
111
    labelConnections = new QLabel();
    labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
112
    labelConnections->setMinimumWidth(150);
113
    labelConnections->setMaximumWidth(150);
114
    labelConnections->setToolTip(tr("Number of connections to other clients"));
115

116
117
    labelBlocks = new QLabel();
    labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
118
119
    labelBlocks->setMinimumWidth(150);
    labelBlocks->setMaximumWidth(150);
120
121
    labelBlocks->setToolTip(tr("Number of blocks in the block chain"));

122
    // Progress bar for blocks download
123
    progressBarLabel = new QLabel(tr("Synchronizing with network..."));
124
125
    progressBarLabel->setVisible(false);
    progressBar = new QProgressBar();
126
    progressBar->setToolTip(tr("Block chain synchronization in progress"));
127
128
129
130
    progressBar->setVisible(false);

    statusBar()->addWidget(progressBarLabel);
    statusBar()->addWidget(progressBar);
131
132
    statusBar()->addPermanentWidget(labelConnections);
    statusBar()->addPermanentWidget(labelBlocks);
133

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
134
    createTrayIcon();
135

136
    gotoOverviewPage();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
137
138
139
140
}

void BitcoinGUI::createActions()
{
141
    QActionGroup *tabGroup = new QActionGroup(this);
142

143
144
145
    overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
    overviewAction->setCheckable(true);
    tabGroup->addAction(overviewAction);
146

147
    historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
148
149
150
    historyAction->setCheckable(true);
    tabGroup->addAction(historyAction);

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
    addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
    addressBookAction->setCheckable(true);
    tabGroup->addAction(addressBookAction);

    receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
    receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
    receiveCoinsAction->setCheckable(true);
    tabGroup->addAction(receiveCoinsAction);

    sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
    sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
    sendCoinsAction->setCheckable(true);
    tabGroup->addAction(sendCoinsAction);

    connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
    connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));

    quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
    quitAction->setToolTip(tr("Quit application"));
    aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
    aboutAction->setToolTip(tr("Show information about Bitcoin"));
    optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
    optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
    openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
    openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
180
181
    exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
    exportAction->setToolTip(tr("Export data in current view to a file"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
182

183
184
185
186
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
    connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
187
188
}

189
void BitcoinGUI::setClientModel(ClientModel *clientModel)
190
{
191
    this->clientModel = clientModel;
192

193
194
    if(clientModel->isTestNet())
    {
195
        QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
196
        setWindowTitle(title_testnet);
197
198
199
        setWindowIcon(QIcon(":icons/bitcoin_testnet"));
        if(trayIcon)
        {
200
            trayIcon->setToolTip(title_testnet);
201
202
203
204
            trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
        }
    }

205
    // Keep up to date with client
206
207
    setNumConnections(clientModel->getNumConnections());
    connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
208

209
210
    setNumBlocks(clientModel->getNumBlocks());
    connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
211

212
213
214
215
216
217
218
    // Report errors from network/worker thread
    connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
}

void BitcoinGUI::setWalletModel(WalletModel *walletModel)
{
    this->walletModel = walletModel;
219

220
221
    // Report errors from wallet thread
    connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
222
223

    // Put transaction list in tabs
224
    transactionView->setModel(walletModel);
225

226
    overviewPage->setModel(walletModel);
227
228
229
230
    addressBookPage->setModel(walletModel->getAddressTableModel());
    receiveCoinsPage->setModel(walletModel->getAddressTableModel());
    sendCoinsPage->setModel(walletModel);

231
    // Balloon popup for new transaction
232
    connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
233
            this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
234
235
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
236
237
238
void BitcoinGUI::createTrayIcon()
{
    QMenu *trayIconMenu = new QMenu(this);
239
240
241
    trayIconMenu->addAction(openBitcoinAction);
    trayIconMenu->addAction(sendCoinsAction);
    trayIconMenu->addAction(optionsAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
242
    trayIconMenu->addSeparator();
243
    trayIconMenu->addAction(quitAction);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
244
245
246

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
247
    trayIcon->setToolTip("Bitcoin client");
248
    trayIcon->setIcon(QIcon(":/icons/toolbar"));
249
250
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
251
252
253
    trayIcon->show();
}

254
255
256
257
258
void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
    if(reason == QSystemTrayIcon::DoubleClick)
    {
        // Doubleclick on system tray icon triggers "open bitcoin"
259
        openBitcoinAction->trigger();
260
261
262
    }
}

263
264
void BitcoinGUI::optionsClicked()
{
265
    OptionsDialog dlg;
266
    dlg.setModel(clientModel->getOptionsModel());
267
    dlg.exec();
268
269
}

270
void BitcoinGUI::aboutClicked()
271
{
272
    AboutDialog dlg;
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
273
    dlg.setModel(clientModel);
274
    dlg.exec();
275
276
}

277
278
void BitcoinGUI::setNumConnections(int count)
{
279
280
281
    QString icon;
    switch(count)
    {
282
283
284
285
286
    case 0: icon = ":/icons/connect_0"; break;
    case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
    case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
    case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
    default: icon = ":/icons/connect_4"; break;
287
288
    }
    labelConnections->setTextFormat(Qt::RichText);
289
    labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
290
291
292
293
}

void BitcoinGUI::setNumBlocks(int count)
{
294
    int total = clientModel->getTotalBlocksEstimate();
295
296
297
298
299
300
301
302
303
304
305
306
307
    if(count < total)
    {
        progressBarLabel->setVisible(true);
        progressBar->setVisible(true);
        progressBar->setMaximum(total);
        progressBar->setValue(count);
    }
    else
    {
        progressBarLabel->setVisible(false);
        progressBar->setVisible(false);
    }

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    QDateTime now = QDateTime::currentDateTime();
    QDateTime lastBlockDate = clientModel->getLastBlockDate();
    int secs = lastBlockDate.secsTo(now);
    QString text;
    QString icon = ":/icons/notsynced";

    // "Up to date" icon, and outdated icon
    if(secs < 30*60)
    {
        text = "Up to date";
        icon = ":/icons/synced";
    }
    else if(secs < 60*60)
    {
        text = tr("%n minute(s) ago","",secs/60);
    }
    else if(secs < 24*60*60)
    {
        text = tr("%n hour(s) ago","",secs/(60*60));
    }
    else
    {
        text = tr("%n day(s) ago","",secs/(60*60*24));
    }

    labelBlocks->setText("<img src=\""+icon+"\"> " + text);
334
    labelBlocks->setToolTip(tr("Downloaded %n block(s) of transaction history. Last block was generated %1.", "", count)
335
                            .arg(QLocale::system().toString(lastBlockDate)));
336
337
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
338
339
void BitcoinGUI::error(const QString &title, const QString &message)
{
340
    // Report errors from network/worker thread
341
342
343
344
    if(trayIcon->supportsMessages())
    {
        // Show as "balloon" message if possible
        trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
345
346
347
    }
    else
    {
348
349
350
351
352
        // Fall back to old fashioned popup dialog if not
        QMessageBox::critical(this, title,
            message,
            QMessageBox::Ok, QMessageBox::Ok);
    }
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
353
}
354
355
356
357
358

void BitcoinGUI::changeEvent(QEvent *e)
{
    if (e->type() == QEvent::WindowStateChange)
    {
359
        if(clientModel->getOptionsModel()->getMinimizeToTray())
360
361
362
363
364
        {
            if (isMinimized())
            {
                hide();
                e->ignore();
365
366
367
            }
            else
            {
368
369
370
371
372
373
374
375
376
                e->accept();
            }
        }
    }
    QMainWindow::changeEvent(e);
}

void BitcoinGUI::closeEvent(QCloseEvent *event)
{
377
378
    if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
       !clientModel->getOptionsModel()->getMinimizeOnClose())
379
380
381
382
383
    {
        qApp->quit();
    }
    QMainWindow::closeEvent(event);
}
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
384
385
386
387
388
389

void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
{
    QString strMessage =
        tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
          "which goes to the nodes that process your transaction and helps to support the network.  "
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
390
          "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
391
392
393
394
395
    QMessageBox::StandardButton retval = QMessageBox::question(
          this, tr("Sending..."), strMessage,
          QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
    *payFee = (retval == QMessageBox::Yes);
}
396

397
398
void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
{
399
    TransactionTableModel *ttm = walletModel->getTransactionTableModel();
400
    qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
401
                    .data(Qt::EditRole).toULongLong();
402
    if(amount>0 && !clientModel->inInitialBlockDownload())
403
    {
404
        // On incoming transaction, make an info balloon
405
        // Unless the initial block download is in progress, to prevent balloon-spam
406
407
        QString date = ttm->index(start, TransactionTableModel::Date, parent)
                        .data().toString();
408
409
410
        QString type = ttm->index(start, TransactionTableModel::Type, parent)
                        .data().toString();
        QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
411
412
413
                        .data().toString();

        trayIcon->showMessage(tr("Incoming transaction"),
414
                              tr("Date: ") + date + "\n" +
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
415
                              tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
416
417
                              tr("Type: ") + type + "\n" +
                              tr("Address: ") + address + "\n",
418
419
420
                              QSystemTrayIcon::Information);
    }
}
421

422
void BitcoinGUI::gotoOverviewPage()
423
424
425
{
    overviewAction->setChecked(true);
    centralWidget->setCurrentWidget(overviewPage);
426

427
    exportAction->setEnabled(false);
428
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
429
430
}

431
void BitcoinGUI::gotoHistoryPage()
432
433
434
{
    historyAction->setChecked(true);
    centralWidget->setCurrentWidget(transactionsPage);
435

436
    exportAction->setEnabled(true);
437
438
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
439
440
}

441
442
443
444
void BitcoinGUI::gotoAddressBookPage()
{
    addressBookAction->setChecked(true);
    centralWidget->setCurrentWidget(addressBookPage);
445
446
447
448

    exportAction->setEnabled(true);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
449
450
451
452
453
454
}

void BitcoinGUI::gotoReceiveCoinsPage()
{
    receiveCoinsAction->setChecked(true);
    centralWidget->setCurrentWidget(receiveCoinsPage);
455
456
457
458

    exportAction->setEnabled(true);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
459
460
461
462
463
}

void BitcoinGUI::gotoSendCoinsPage()
{
    sendCoinsAction->setChecked(true);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
464
    sendCoinsPage->clear();
465
466
    centralWidget->setCurrentWidget(sendCoinsPage);

467
468
    exportAction->setEnabled(false);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
469
}
470