Add context menu on transaction list: copy label, copy address, edit label, show details

This commit is contained in:
Wladimir J. van der Laan 2011-07-08 22:27:36 +02:00
parent 35105534e7
commit 51d7cc07f1
10 changed files with 168 additions and 37 deletions

View File

@ -296,3 +296,33 @@ bool AddressTableModel::validateAddress(const QString &address)
return AddressToHash160(address.toStdString(), hash160);
}
/* Look up label for address in address book, if not found return empty string.
*/
QString AddressTableModel::labelForAddress(const QString &address) const
{
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString());
if (mi != wallet->mapAddressBook.end())
{
return QString::fromStdString(mi->second);
}
}
return QString();
}
int AddressTableModel::lookupAddress(const QString &address) const
{
QModelIndexList lst = match(index(0, Address, QModelIndex()),
Qt::EditRole, address, 1, Qt::MatchExactly);
if(lst.isEmpty())
{
return -1;
}
else
{
return lst.at(0).row();
}
}

View File

@ -49,6 +49,15 @@ public:
*/
bool validateAddress(const QString &address);
/* Look up label for address in address book, if not found return empty string.
*/
QString labelForAddress(const QString &address) const;
/* Look up row index of an address in the model.
Return -1 if not found.
*/
int lookupAddress(const QString &address) const;
private:
CWallet *wallet;
AddressTablePriv *priv;

View File

@ -84,7 +84,7 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
QVBoxLayout *vbox = new QVBoxLayout();
transactionView = new TransactionView(this);
connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), transactionView, SLOT(transactionDetails()));
vbox->addWidget(transactionView);
transactionsPage = new QWidget(this);
@ -229,7 +229,7 @@ void BitcoinGUI::setWalletModel(WalletModel *walletModel)
connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
// Put transaction list in tabs
transactionView->setModel(walletModel->getTransactionTableModel());
transactionView->setModel(walletModel);
addressBookPage->setModel(walletModel->getAddressTableModel());
receiveCoinsPage->setModel(walletModel->getAddressTableModel());
@ -298,7 +298,7 @@ void BitcoinGUI::setNumConnections(int count)
default: icon = ":/icons/connect_4"; break;
}
labelConnections->setTextFormat(Qt::RichText);
labelConnections->setText("<img src=\""+icon+"\">" + tr("%n connection(s)", "", count));
labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
}
void BitcoinGUI::setNumBlocks(int count)
@ -411,13 +411,6 @@ void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
*payFee = (retval == QMessageBox::Yes);
}
void BitcoinGUI::transactionDetails(const QModelIndex& idx)
{
// A transaction is doubleclicked
TransactionDescDialog dlg(idx);
dlg.exec();
}
void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
{
TransactionTableModel *ttm = walletModel->getTransactionTableModel();

View File

@ -102,7 +102,6 @@ private slots:
void optionsClicked();
void aboutClicked();
void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
void transactionDetails(const QModelIndex& idx);
void incomingTransaction(const QModelIndex & parent, int start, int end);
void exportClicked();
};

View File

@ -1,6 +1,7 @@
#include "sendcoinsdialog.h"
#include "ui_sendcoinsdialog.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
#include "guiutil.h"
#include "addressbookpage.h"
@ -131,7 +132,7 @@ void SendCoinsDialog::on_buttonBox_rejected()
void SendCoinsDialog::on_payTo_textChanged(const QString &address)
{
ui->addAsLabel->setText(model->labelForAddress(address));
ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));
}
void SendCoinsDialog::clear()

View File

@ -4,6 +4,7 @@
#include "guiconstants.h"
#include "transactiondesc.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
#include "headers.h"
@ -325,7 +326,7 @@ QVariant TransactionTableModel::formatTxDate(const TransactionRecord *wtx) const
*/
QString TransactionTableModel::lookupAddress(const std::string &address) const
{
QString label = walletModel->labelForAddress(QString::fromStdString(address));
QString label = walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(address));
QString description;
if(label.isEmpty())
{
@ -538,7 +539,7 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
}
else if (role == LabelRole)
{
return walletModel->labelForAddress(QString::fromStdString(rec->address));
return walletModel->getAddressTableModel()->labelForAddress(QString::fromStdString(rec->address));
}
else if (role == AbsoluteAmountRole)
{

View File

@ -2,9 +2,13 @@
#include "transactionfilterproxy.h"
#include "transactionrecord.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
#include "transactiontablemodel.h"
#include "guiutil.h"
#include "csvmodelwriter.h"
#include "transactiondescdialog.h"
#include "editaddressdialog.h"
#include <QScrollBar>
#include <QComboBox>
@ -17,6 +21,10 @@
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QPoint>
#include <QMenu>
#include <QApplication>
#include <QClipboard>
#include <QDebug>
@ -90,23 +98,47 @@ TransactionView::TransactionView(QWidget *parent) :
// Always show scroll bar
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
view->setTabKeyNavigation(false);
view->setContextMenuPolicy(Qt::CustomContextMenu);
transactionView = view;
// Actions
QAction *copyAddressAction = new QAction("Copy address", this);
QAction *copyLabelAction = new QAction("Copy label", this);
QAction *editLabelAction = new QAction("Edit label", this);
QAction *showDetailsAction = new QAction("Show details...", this);
contextMenu = new QMenu();
contextMenu->addAction(copyAddressAction);
contextMenu->addAction(copyLabelAction);
contextMenu->addAction(editLabelAction);
contextMenu->addAction(showDetailsAction);
// Connect actions
connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
connect(addressWidget, SIGNAL(textChanged(const QString&)), this, SLOT(changedPrefix(const QString&)));
connect(amountWidget, SIGNAL(textChanged(const QString&)), this, SLOT(changedAmount(const QString&)));
connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SIGNAL(doubleClicked(const QModelIndex&)));
connect(view,
SIGNAL(customContextMenuRequested(const QPoint &)),
this,
SLOT(contextualMenu(const QPoint &)));
connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
}
void TransactionView::setModel(TransactionTableModel *model)
void TransactionView::setModel(WalletModel *model)
{
this->model = model;
transactionProxyModel = new TransactionFilterProxy(this);
transactionProxyModel->setSourceModel(model);
transactionProxyModel->setSourceModel(model->getTransactionTableModel());
transactionProxyModel->setDynamicSortFilter(true);
transactionProxyModel->setSortRole(Qt::EditRole);
@ -233,3 +265,77 @@ void TransactionView::exportClicked()
}
}
void TransactionView::contextualMenu(const QPoint &point)
{
QModelIndex index = transactionView->indexAt(point);
if(index.isValid())
{
contextMenu->exec(QCursor::pos());
}
}
void TransactionView::copyAddress()
{
QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty())
{
QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::AddressRole).toString());
}
}
void TransactionView::copyLabel()
{
QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty())
{
QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::LabelRole).toString());
}
}
void TransactionView::editLabel()
{
QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty())
{
AddressTableModel *addressBook = model->getAddressTableModel();
QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
if(address.isEmpty())
{
// If this transaction has no associated address, exit
return;
}
int idx = addressBook->lookupAddress(address);
if(idx != -1)
{
// Edit sending / receiving address
QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
// Determine type of address, launch appropriate editor dialog type
QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
EditAddressDialog dlg(type==AddressTableModel::Receive
? EditAddressDialog::EditReceivingAddress
: EditAddressDialog::EditSendingAddress,
this);
dlg.setModel(addressBook);
dlg.loadRow(idx);
dlg.exec();
}
else
{
// Add sending address
EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
this);
dlg.exec();
}
}
}
void TransactionView::showDetails()
{
QModelIndexList selection = transactionView->selectionModel()->selectedRows();
if(!selection.isEmpty())
{
TransactionDescDialog dlg(selection.at(0));
dlg.exec();
}
}

View File

@ -3,7 +3,7 @@
#include <QWidget>
class TransactionTableModel;
class WalletModel;
class TransactionFilterProxy;
QT_BEGIN_NAMESPACE
@ -11,6 +11,7 @@ class QTableView;
class QComboBox;
class QLineEdit;
class QModelIndex;
class QMenu;
QT_END_NAMESPACE
class TransactionView : public QWidget
@ -19,7 +20,7 @@ class TransactionView : public QWidget
public:
explicit TransactionView(QWidget *parent = 0);
void setModel(TransactionTableModel *model);
void setModel(WalletModel *model);
enum DateEnum
{
@ -33,7 +34,7 @@ public:
};
private:
TransactionTableModel *model;
WalletModel *model;
TransactionFilterProxy *transactionProxyModel;
QTableView *transactionView;
@ -42,6 +43,11 @@ private:
QLineEdit *addressWidget;
QLineEdit *amountWidget;
QMenu *contextMenu;
private slots:
void contextualMenu(const QPoint &);
signals:
void doubleClicked(const QModelIndex&);
@ -51,6 +57,10 @@ public slots:
void changedPrefix(const QString &prefix);
void changedAmount(const QString &amount);
void exportClicked();
void showDetails();
void copyAddress();
void editLabel();
void copyLabel();
};

View File

@ -123,18 +123,4 @@ TransactionTableModel *WalletModel::getTransactionTableModel()
return transactionTableModel;
}
/* Look up label for address in address book, if not found return empty string.
*/
QString WalletModel::labelForAddress(const QString &address) const
{
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{
std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString());
if (mi != wallet->mapAddressBook.end())
{
return QString::fromStdString(mi->second);
}
}
return QString();
}

View File

@ -33,10 +33,6 @@ public:
qint64 getBalance() const;
int getNumTransactions() const;
/* Look up label for address in address book, if not found return empty string.
*/
QString labelForAddress(const QString &address) const;
/* Send coins */
StatusCode sendCoins(const QString &payTo, qint64 payAmount, const QString &addToAddressBookAs=QString());
private: