Browse Source

implement filtering, action listeners

miguelfreitas
Wladimir J. van der Laan 14 years ago
parent
commit
af94377667
  1. 113
      BitcoinGUI.cpp
  2. 7
      BitcoinGUI.h
  3. 28
      TransactionTableModel.cpp

113
BitcoinGUI.cpp

@ -1,15 +1,20 @@
/* /*
* Qt4 bitcoin GUI.
*
* W.J. van der Laan 2011 * W.J. van der Laan 2011
*/ */
#include "BitcoinGUI.h" #include "BitcoinGUI.h"
#include "TransactionTableModel.h" #include "TransactionTableModel.h"
#include "AddressBookDialog.h"
#include "SettingsDialog.h"
#include "SendCoinsDialog.h"
#include <QApplication> #include <QApplication>
#include <QMainWindow> #include <QMainWindow>
#include <QMenuBar> #include <QMenuBar>
#include <QMenu> #include <QMenu>
#include <QIcon> #include <QIcon>
#include <QTabBar> #include <QTabWidget>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QWidget> #include <QWidget>
#include <QToolBar> #include <QToolBar>
@ -20,6 +25,9 @@
#include <QPushButton> #include <QPushButton>
#include <QHeaderView> #include <QHeaderView>
#include <QLocale> #include <QLocale>
#include <QSortFilterProxyModel>
#include <QDebug>
#include <iostream> #include <iostream>
@ -30,10 +38,12 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
setWindowTitle(tr("Bitcoin")); setWindowTitle(tr("Bitcoin"));
setWindowIcon(QIcon(":icons/bitcoin")); setWindowIcon(QIcon(":icons/bitcoin"));
QAction *quit = new QAction(QIcon(":/icons/quit"), "&Quit", this); QAction *quit = new QAction(QIcon(":/icons/quit"), tr("&Quit"), this);
QAction *sendcoins = new QAction(QIcon(":/icons/send"), "&Send coins", this); QAction *sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
QAction *addressbook = new QAction(QIcon(":/icons/address-book"), "&Address book", this); QAction *addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address book"), this);
QAction *about = new QAction(QIcon(":/icons/bitcoin"), "&About", this); QAction *about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
QAction *receiving_addresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
QAction *options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
/* Menus */ /* Menus */
QMenu *file = menuBar()->addMenu("&File"); QMenu *file = menuBar()->addMenu("&File");
@ -42,7 +52,8 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
file->addAction(quit); file->addAction(quit);
QMenu *settings = menuBar()->addMenu("&Settings"); QMenu *settings = menuBar()->addMenu("&Settings");
settings->addAction(addressbook); settings->addAction(receiving_addresses);
settings->addAction(options);
QMenu *help = menuBar()->addMenu("&Help"); QMenu *help = menuBar()->addMenu("&Help");
help->addAction(about); help->addAction(about);
@ -60,7 +71,7 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
edit_address->setReadOnly(true); edit_address->setReadOnly(true);
hbox_address->addWidget(edit_address); hbox_address->addWidget(edit_address);
QPushButton *button_new = new QPushButton(trUtf8("&New\u2026")); QPushButton *button_new = new QPushButton(tr("&New..."));
QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard")); QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
hbox_address->addWidget(button_new); hbox_address->addWidget(button_new);
hbox_address->addWidget(button_clipboard); hbox_address->addWidget(button_clipboard);
@ -80,16 +91,33 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
vbox->addLayout(hbox_address); vbox->addLayout(hbox_address);
vbox->addLayout(hbox_balance); vbox->addLayout(hbox_balance);
/* Transaction table:
* TransactionView
* TransactionModel
*/
QTableView *transaction_table = new QTableView(this);
TransactionTableModel *transaction_model = new TransactionTableModel(this); TransactionTableModel *transaction_model = new TransactionTableModel(this);
transaction_table->setModel(transaction_model);
/* setupTabs */
QStringList tab_filters, tab_labels;
tab_filters << "^."
<< "^[sr]"
<< "^[s]"
<< "^[r]";
tab_labels << tr("All transactions")
<< tr("Sent/Received")
<< tr("Sent")
<< tr("Received");
QTabWidget *tabs = new QTabWidget(this);
for(int i = 0; i < tab_labels.size(); ++i)
{
QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
proxy_model->setSourceModel(transaction_model);
proxy_model->setDynamicSortFilter(true);
proxy_model->setFilterRole(Qt::UserRole);
proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
QTableView *transaction_table = new QTableView(this);
transaction_table->setModel(proxy_model);
transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows); transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection); transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
transaction_table->verticalHeader()->hide();
transaction_table->horizontalHeader()->resizeSection( transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Status, 112); TransactionTableModel::Status, 112);
@ -102,24 +130,10 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
transaction_table->horizontalHeader()->resizeSection( transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Credit, 79); TransactionTableModel::Credit, 79);
/* setupTabs */ tabs->addTab(transaction_table, tab_labels.at(i));
QTabBar *tabs = new QTabBar(this); }
tabs->addTab(tr("All transactions"));
tabs->addTab(tr("Sent/Received"));
tabs->addTab(tr("Sent"));
tabs->addTab(tr("Received"));
/* QSortFilterProxyModel
setFilterRole : filter on user role
setFilterKeyColumn
setFilterRegExp / setFilterFixedString
"^."
"^[sr]"
"^[s]"
"^[r]"
*/
vbox->addWidget(tabs); vbox->addWidget(tabs);
vbox->addWidget(transaction_table);
QWidget *centralwidget = new QWidget(this); QWidget *centralwidget = new QWidget(this);
centralwidget->setLayout(vbox); centralwidget->setLayout(vbox);
@ -140,19 +154,46 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
label_transactions->setFrameStyle(QFrame::Panel | QFrame::Sunken); label_transactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
label_transactions->setMinimumWidth(100); label_transactions->setMinimumWidth(100);
statusBar()->addPermanentWidget(label_connections); statusBar()->addPermanentWidget(label_connections);
statusBar()->addPermanentWidget(label_blocks); statusBar()->addPermanentWidget(label_blocks);
statusBar()->addPermanentWidget(label_transactions); statusBar()->addPermanentWidget(label_transactions);
/* Action bindings */ /* Action bindings */
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit())); connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int))); connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
connect(receiving_addresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
connect(button_new, SIGNAL(triggered()), this, SLOT(newAddressClicked()));
connect(button_clipboard, SIGNAL(triggered()), this, SLOT(copyClipboardClicked()));
}
void BitcoinGUI::sendcoinsClicked()
{
qDebug() << "Send coins clicked";
}
void BitcoinGUI::addressbookClicked()
{
qDebug() << "Address book clicked";
}
void BitcoinGUI::optionsClicked()
{
qDebug() << "Options clicked";
}
void BitcoinGUI::receivingAddressesClicked()
{
qDebug() << "Receiving addresses clicked";
}
void BitcoinGUI::newAddressClicked()
{
qDebug() << "New address clicked";
} }
void BitcoinGUI::currentChanged(int tab) void BitcoinGUI::copyClipboardClicked()
{ {
std::cout << "Switched to tab: " << tab << std::endl; qDebug() << "Copy to clipboard";
} }

7
BitcoinGUI.h

@ -17,7 +17,12 @@ public:
Received = 3 Received = 3
} TabIndex; } TabIndex;
private slots: private slots:
void currentChanged(int tab); void sendcoinsClicked();
void addressbookClicked();
void optionsClicked();
void receivingAddressesClicked();
void newAddressClicked();
void copyClipboardClicked();
}; };
#endif #endif

28
TransactionTableModel.cpp

@ -1,12 +1,14 @@
#include "TransactionTableModel.h" #include "TransactionTableModel.h"
#include <QLocale>
/* Credit and Debit columns are right-aligned as they contain numbers */ /* Credit and Debit columns are right-aligned as they contain numbers */
static Qt::AlignmentFlag column_alignments[] = { static int column_alignments[] = {
Qt::AlignLeft, Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft, Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft, Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignRight, Qt::AlignRight|Qt::AlignVCenter,
Qt::AlignRight Qt::AlignRight|Qt::AlignVCenter
}; };
TransactionTableModel::TransactionTableModel(QObject *parent): TransactionTableModel::TransactionTableModel(QObject *parent):
@ -36,16 +38,24 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
{ {
/* index.row(), index.column() */ /* index.row(), index.column() */
/* Return QString */ /* Return QString */
return QString("test"); return QLocale::system().toString(index.row());
} else if (role == Qt::TextAlignmentRole) } else if (role == Qt::TextAlignmentRole)
{ {
return column_alignments[index.column()]; return column_alignments[index.column()];
} } else if (role == Qt::UserRole)
/* user role: transaction type {
/* user role: transaction type for filtering
"s" (sent) "s" (sent)
"r" (received) "r" (received)
"g" (generated) "g" (generated)
*/ */
switch(index.row() % 3)
{
case 0: return QString("s");
case 1: return QString("r");
case 2: return QString("o");
}
}
return QVariant(); return QVariant();
} }

Loading…
Cancel
Save