Improved Mac experience; QDoubleSpinBox for BitcoinAmountField
[novacoin.git] / src / qt / transactionview.cpp
index 037dfbb..92dda57 100644 (file)
@@ -2,9 +2,14 @@
 
 #include "transactionfilterproxy.h"
 #include "transactionrecord.h"
+#include "walletmodel.h"
+#include "addresstablemodel.h"
 #include "transactiontablemodel.h"
-#include "guiutil.h"
+#include "bitcoinunits.h"
 #include "csvmodelwriter.h"
+#include "transactiondescdialog.h"
+#include "editaddressdialog.h"
+#include "optionsmodel.h"
 
 #include <QScrollBar>
 #include <QComboBox>
 #include <QPushButton>
 #include <QFileDialog>
 #include <QMessageBox>
-
-#include <QDebug>
+#include <QPoint>
+#include <QMenu>
+#include <QApplication>
+#include <QClipboard>
+#include <QLabel>
+#include <QDateTimeEdit>
 
 TransactionView::TransactionView(QWidget *parent) :
     QWidget(parent), model(0), transactionProxyModel(0),
@@ -29,13 +38,20 @@ TransactionView::TransactionView(QWidget *parent) :
 
     QHBoxLayout *hlayout = new QHBoxLayout();
     hlayout->setContentsMargins(0,0,0,0);
+#ifdef Q_WS_MAC
+    hlayout->setSpacing(5);
+    hlayout->addSpacing(26);
+#else
     hlayout->setSpacing(0);
-
     hlayout->addSpacing(23);
+#endif
 
     dateWidget = new QComboBox(this);
-    dateWidget->setMaximumWidth(120);
-    dateWidget->setMinimumWidth(120);
+#ifdef Q_WS_MAC
+    dateWidget->setFixedWidth(121);
+#else
+    dateWidget->setFixedWidth(120);
+#endif
     dateWidget->addItem(tr("All"), All);
     dateWidget->addItem(tr("Today"), Today);
     dateWidget->addItem(tr("This week"), ThisWeek);
@@ -46,8 +62,11 @@ TransactionView::TransactionView(QWidget *parent) :
     hlayout->addWidget(dateWidget);
 
     typeWidget = new QComboBox(this);
-    typeWidget->setMaximumWidth(120);
-    typeWidget->setMinimumWidth(120);
+#ifdef Q_WS_MAC
+    typeWidget->setFixedWidth(121);
+#else
+    typeWidget->setFixedWidth(120);
+#endif
 
     typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
     typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
@@ -62,16 +81,19 @@ TransactionView::TransactionView(QWidget *parent) :
 
     addressWidget = new QLineEdit(this);
 #if QT_VERSION >= 0x040700
-    addressWidget->setPlaceholderText("Enter address or label to search");
+    addressWidget->setPlaceholderText(tr("Enter address or label to search"));
 #endif
     hlayout->addWidget(addressWidget);
 
     amountWidget = new QLineEdit(this);
 #if QT_VERSION >= 0x040700
-    amountWidget->setPlaceholderText("Min amount");
+    amountWidget->setPlaceholderText(tr("Min amount"));
+#endif
+#ifdef Q_WS_MAC
+    amountWidget->setFixedWidth(97);
+#else
+    amountWidget->setFixedWidth(100);
 #endif
-    amountWidget->setMaximumWidth(100);
-    amountWidget->setMinimumWidth(100);
     amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
     hlayout->addWidget(amountWidget);
 
@@ -82,30 +104,60 @@ TransactionView::TransactionView(QWidget *parent) :
 
     QTableView *view = new QTableView(this);
     vlayout->addLayout(hlayout);
+    vlayout->addWidget(createDateRangeWidget());
     vlayout->addWidget(view);
     vlayout->setSpacing(0);
     int width = view->verticalScrollBar()->sizeHint().width();
     // Cover scroll bar width with spacing
+#ifdef Q_WS_MAC
+    hlayout->addSpacing(width+2);
+#else
     hlayout->addSpacing(width);
+#endif
     // Always show scroll bar
     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+    view->setTabKeyNavigation(false);
+    view->setContextMenuPolicy(Qt::CustomContextMenu);
 
     transactionView = view;
 
+    // Actions
+    QAction *copyAddressAction = new QAction(tr("Copy address"), this);
+    QAction *copyLabelAction = new QAction(tr("Copy label"), this);
+    QAction *editLabelAction = new QAction(tr("Edit label"), this);
+    QAction *showDetailsAction = new QAction(tr("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(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
+    connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
+
+    connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
+
+    connect(view,
+            SIGNAL(customContextMenuRequested(QPoint)),
+            this,
+            SLOT(contextualMenu(QPoint)));
 
-    connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SIGNAL(doubleClicked(const QModelIndex&)));
+    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);
@@ -134,6 +186,7 @@ void TransactionView::setModel(TransactionTableModel *model)
 void TransactionView::chooseDate(int idx)
 {
     QDate current = QDate::currentDate();
+    dateRangeWidget->setVisible(false);
     switch(dateWidget->itemData(idx).toInt())
     {
     case All:
@@ -170,10 +223,10 @@ void TransactionView::chooseDate(int idx)
                 TransactionFilterProxy::MAX_DATE);
         break;
     case Range:
-        // TODO ask specific range
+        dateRangeWidget->setVisible(true);
+        dateRangeChanged();
         break;
     }
-
 }
 
 void TransactionView::chooseType(int idx)
@@ -190,7 +243,7 @@ void TransactionView::changedPrefix(const QString &prefix)
 void TransactionView::changedAmount(const QString &amount)
 {
     qint64 amount_parsed = 0;
-    if(GUIUtil::parseMoney(amount, &amount_parsed))
+    if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
     {
         transactionProxyModel->setMinAmount(amount_parsed);
     }
@@ -208,22 +261,20 @@ void TransactionView::exportClicked()
             tr("Export Transaction Data"),
             QDir::currentPath(),
             tr("Comma separated file (*.csv)"));
-    if(!filename.endsWith(".csv"))
-    {
-        filename += ".csv";
-    }
+
+    if (filename.isNull()) return;
 
     CSVModelWriter writer(filename);
 
     // name, column, role
     writer.setModel(transactionProxyModel);
-    writer.addColumn("Confirmed", 0, TransactionTableModel::ConfirmedRole);
-    writer.addColumn("Date", 0, TransactionTableModel::DateRole);
-    writer.addColumn("Type", TransactionTableModel::Type, Qt::EditRole);
-    writer.addColumn("Label", 0, TransactionTableModel::LabelRole);
-    writer.addColumn("Address", 0, TransactionTableModel::AddressRole);
-    writer.addColumn("Amount", 0, TransactionTableModel::FormattedAmountRole);
-    writer.addColumn("ID", 0, TransactionTableModel::TxIDRole);
+    writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
+    writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
+    writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
+    writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
+    writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
+    writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
+    writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
 
     if(!writer.write())
     {
@@ -232,3 +283,120 @@ 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();
+    }
+}
+
+QWidget *TransactionView::createDateRangeWidget()
+{
+    dateRangeWidget = new QFrame();
+    dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
+    dateRangeWidget->setContentsMargins(1,1,1,1);
+    QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
+    layout->setContentsMargins(0,0,0,0);
+    layout->addSpacing(23);
+    layout->addWidget(new QLabel(tr("Range:")));
+
+    dateFrom = new QDateTimeEdit(this);
+    dateFrom->setDisplayFormat("dd/MM/yy");
+    dateFrom->setCalendarPopup(true);
+    dateFrom->setMinimumWidth(100);
+    dateFrom->setDate(QDate::currentDate().addDays(-7));
+    layout->addWidget(dateFrom);
+    layout->addWidget(new QLabel(tr("to")));
+
+    dateTo = new QDateTimeEdit(this);
+    dateTo->setDisplayFormat("dd/MM/yy");
+    dateTo->setCalendarPopup(true);
+    dateTo->setMinimumWidth(100);
+    dateTo->setDate(QDate::currentDate());
+    layout->addWidget(dateTo);
+    layout->addStretch();
+
+    // Hide by default
+    dateRangeWidget->setVisible(false);
+
+    // Notify on change
+    connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
+    connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
+
+    return dateRangeWidget;
+}
+
+void TransactionView::dateRangeChanged()
+{
+    transactionProxyModel->setDateRange(
+            QDateTime(dateFrom->date()),
+            QDateTime(dateTo->date()).addDays(1));
+}