implement filtering, action listeners

This commit is contained in:
Wladimir J. van der Laan 2011-05-10 19:03:10 +02:00
parent 0522725754
commit af94377667
3 changed files with 119 additions and 63 deletions

View File

@ -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,46 +91,49 @@ 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);
transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Status, 112);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Date, 112);
transaction_table->horizontalHeader()->setResizeMode(
TransactionTableModel::Description, QHeaderView::Stretch);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Debit, 79);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Credit, 79);
/* setupTabs */ /* setupTabs */
QTabBar *tabs = new QTabBar(this); QStringList tab_filters, tab_labels;
tabs->addTab(tr("All transactions")); tab_filters << "^."
tabs->addTab(tr("Sent/Received")); << "^[sr]"
tabs->addTab(tr("Sent")); << "^[s]"
tabs->addTab(tr("Received")); << "^[r]";
/* QSortFilterProxyModel tab_labels << tr("All transactions")
setFilterRole : filter on user role << tr("Sent/Received")
setFilterKeyColumn << tr("Sent")
setFilterRegExp / setFilterFixedString << tr("Received");
"^." QTabWidget *tabs = new QTabWidget(this);
"^[sr]"
"^[s]" for(int i = 0; i < tab_labels.size(); ++i)
"^[r]" {
*/ 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->setSelectionMode(QAbstractItemView::ExtendedSelection);
transaction_table->verticalHeader()->hide();
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Status, 112);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Date, 112);
transaction_table->horizontalHeader()->setResizeMode(
TransactionTableModel::Description, QHeaderView::Stretch);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Debit, 79);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Credit, 79);
tabs->addTab(transaction_table, tab_labels.at(i));
}
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::currentChanged(int tab) void BitcoinGUI::sendcoinsClicked()
{ {
std::cout << "Switched to tab: " << tab << std::endl; 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::copyClipboardClicked()
{
qDebug() << "Copy to clipboard";
} }

View File

@ -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

View File

@ -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 for filtering
"s" (sent)
"r" (received)
"g" (generated)
*/
switch(index.row() % 3)
{
case 0: return QString("s");
case 1: return QString("r");
case 2: return QString("o");
}
} }
/* user role: transaction type
"s" (sent)
"r" (received)
"g" (generated)
*/
return QVariant(); return QVariant();
} }