minor language/text updates
[novacoin.git] / src / qt / transactionview.cpp
1 #include "transactionview.h"
2
3 // Temp includes for filtering prototype
4 // Move to TransactionFilterRow class
5 #include "transactionfilterproxy.h"
6 #include "transactionrecord.h"
7 #include "transactiontablemodel.h"
8 #include "guiutil.h"
9
10 #include <QScrollBar>
11 #include <QComboBox>
12 #include <QDoubleValidator>
13 #include <QHBoxLayout>
14 #include <QVBoxLayout>
15 #include <QLineEdit>
16 #include <QTableView>
17 #include <QHeaderView>
18
19 #include <QDebug>
20
21 TransactionView::TransactionView(QWidget *parent) :
22     QWidget(parent), model(0), transactionProxyModel(0),
23     transactionView(0)
24 {
25     // Build filter row
26     setContentsMargins(0,0,0,0);
27
28     QHBoxLayout *hlayout = new QHBoxLayout();
29     hlayout->setContentsMargins(0,0,0,0);
30     hlayout->setSpacing(0);
31
32     hlayout->addSpacing(23);
33
34     dateWidget = new QComboBox(this);
35     dateWidget->setMaximumWidth(120);
36     dateWidget->setMinimumWidth(120);
37     dateWidget->addItem(tr("All"), All);
38     dateWidget->addItem(tr("Today"), Today);
39     dateWidget->addItem(tr("This week"), ThisWeek);
40     dateWidget->addItem(tr("This month"), ThisMonth);
41     dateWidget->addItem(tr("Last month"), LastMonth);
42     dateWidget->addItem(tr("This year"), ThisYear);
43     dateWidget->addItem(tr("Range..."), Range);
44     hlayout->addWidget(dateWidget);
45
46     typeWidget = new QComboBox(this);
47     typeWidget->setMaximumWidth(120);
48     typeWidget->setMinimumWidth(120);
49
50     typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
51     typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
52                                         TransactionFilterProxy::TYPE(TransactionRecord::RecvFromIP));
53     typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
54                                   TransactionFilterProxy::TYPE(TransactionRecord::SendToIP));
55     typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
56     typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
57     typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
58
59     hlayout->addWidget(typeWidget);
60
61     addressWidget = new QLineEdit(this);
62 #if QT_VERSION >= 0x040700
63     addressWidget->setPlaceholderText("Enter address or label to search");
64 #endif
65     hlayout->addWidget(addressWidget);
66
67     amountWidget = new QLineEdit(this);
68 #if QT_VERSION >= 0x040700
69     amountWidget->setPlaceholderText("Min amount");
70 #endif
71     amountWidget->setMaximumWidth(100);
72     amountWidget->setMinimumWidth(100);
73     amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
74     hlayout->addWidget(amountWidget);
75
76     QVBoxLayout *vlayout = new QVBoxLayout(this);
77     vlayout->setContentsMargins(0,0,0,0);
78     vlayout->setSpacing(0);
79
80     QTableView *view = new QTableView(this);
81     vlayout->addLayout(hlayout);
82     vlayout->addWidget(view);
83     vlayout->setSpacing(0);
84     int width = view->verticalScrollBar()->sizeHint().width();
85     // Cover scroll bar width with spacing
86     hlayout->addSpacing(width);
87     // Always show scroll bar
88     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
89
90     transactionView = view;
91
92     connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
93     connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
94     connect(addressWidget, SIGNAL(textChanged(const QString&)), this, SLOT(changedPrefix(const QString&)));
95     connect(amountWidget, SIGNAL(textChanged(const QString&)), this, SLOT(changedAmount(const QString&)));
96
97     connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SIGNAL(doubleClicked(const QModelIndex&)));
98 }
99
100 void TransactionView::setModel(TransactionTableModel *model)
101 {
102     this->model = model;
103
104     transactionProxyModel = new TransactionFilterProxy(this);
105     transactionProxyModel->setSourceModel(model);
106     transactionProxyModel->setDynamicSortFilter(true);
107
108     transactionProxyModel->setSortRole(Qt::EditRole);
109
110     transactionView->setModel(transactionProxyModel);
111     transactionView->setAlternatingRowColors(true);
112     transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
113     transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
114     transactionView->setSortingEnabled(true);
115     transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
116     transactionView->verticalHeader()->hide();
117
118     transactionView->horizontalHeader()->resizeSection(
119             TransactionTableModel::Status, 23);
120     transactionView->horizontalHeader()->resizeSection(
121             TransactionTableModel::Date, 120);
122     transactionView->horizontalHeader()->resizeSection(
123             TransactionTableModel::Type, 120);
124     transactionView->horizontalHeader()->setResizeMode(
125             TransactionTableModel::ToAddress, QHeaderView::Stretch);
126     transactionView->horizontalHeader()->resizeSection(
127             TransactionTableModel::Amount, 100);
128
129 }
130
131 void TransactionView::chooseDate(int idx)
132 {
133     QDate current = QDate::currentDate();
134     switch(dateWidget->itemData(idx).toInt())
135     {
136     case All:
137         transactionProxyModel->setDateRange(
138                 TransactionFilterProxy::MIN_DATE,
139                 TransactionFilterProxy::MAX_DATE);
140         break;
141     case Today:
142         transactionProxyModel->setDateRange(
143                 QDateTime(current),
144                 TransactionFilterProxy::MAX_DATE);
145         break;
146     case ThisWeek: {
147         // Find last monday
148         QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
149         transactionProxyModel->setDateRange(
150                 QDateTime(startOfWeek),
151                 TransactionFilterProxy::MAX_DATE);
152
153         } break;
154     case ThisMonth:
155         transactionProxyModel->setDateRange(
156                 QDateTime(QDate(current.year(), current.month(), 1)),
157                 TransactionFilterProxy::MAX_DATE);
158         break;
159     case LastMonth:
160         transactionProxyModel->setDateRange(
161                 QDateTime(QDate(current.year(), current.month()-1, 1)),
162                 QDateTime(QDate(current.year(), current.month(), 1)));
163         break;
164     case ThisYear:
165         transactionProxyModel->setDateRange(
166                 QDateTime(QDate(current.year(), 1, 1)),
167                 TransactionFilterProxy::MAX_DATE);
168         break;
169     case Range:
170         // TODO ask specific range
171         break;
172     }
173
174 }
175
176 void TransactionView::chooseType(int idx)
177 {
178     transactionProxyModel->setTypeFilter(
179         typeWidget->itemData(idx).toInt());
180 }
181
182 void TransactionView::changedPrefix(const QString &prefix)
183 {
184     transactionProxyModel->setAddressPrefix(prefix);
185 }
186
187 void TransactionView::changedAmount(const QString &amount)
188 {
189     qint64 amount_parsed = 0;
190     if(GUIUtil::parseMoney(amount, &amount_parsed))
191     {
192         transactionProxyModel->setMinAmount(amount_parsed);
193     }
194     else
195     {
196         transactionProxyModel->setMinAmount(0);
197     }
198 }