deleted address and txid columns, added context menu
[novacoin.git] / src / qt / mintingview.cpp
1 #include "mintingview.h"
2 #include "mintingfilterproxy.h"
3 #include "transactionrecord.h"
4 #include "mintingtablemodel.h"
5 #include "walletmodel.h"
6 #include "guiconstants.h"
7 #include "guiutil.h"
8 #include "csvmodelwriter.h"
9
10
11 #include <QHBoxLayout>
12 #include <QHeaderView>
13 #include <QVBoxLayout>
14 #include <QTableView>
15 #include <QScrollBar>
16 #include <QLabel>
17 #include <QLineEdit>
18 #include <QComboBox>
19 #include <QMessageBox>
20 #include <QMenu>
21
22 MintingView::MintingView(QWidget *parent) :
23     QWidget(parent), model(0), mintingView(0)
24 {
25     QHBoxLayout *hlayout = new QHBoxLayout();
26     hlayout->setContentsMargins(0,0,0,0);
27
28     QString legendBoxStyle = "background-color: rgb(%1,%2,%3); border: 1px solid black;";
29
30     QLabel *youngColor = new QLabel(" ");
31     youngColor->setMaximumHeight(15);
32     youngColor->setMaximumWidth(10);
33     youngColor->setStyleSheet(legendBoxStyle.arg(COLOR_MINT_YOUNG.red()).arg(COLOR_MINT_YOUNG.green()).arg(COLOR_MINT_YOUNG.blue()));
34     QLabel *youngLegend = new QLabel(tr("transaction is too young"));
35     youngLegend->setContentsMargins(5,0,15,0);
36
37     QLabel *matureColor = new QLabel(" ");
38     matureColor->setMaximumHeight(15);
39     matureColor->setMaximumWidth(10);
40     matureColor->setStyleSheet(legendBoxStyle.arg(COLOR_MINT_MATURE.red()).arg(COLOR_MINT_MATURE.green()).arg(COLOR_MINT_MATURE.blue()));
41     QLabel *matureLegend = new QLabel(tr("transaction is mature"));
42     matureLegend->setContentsMargins(5,0,15,0);
43
44     QLabel *oldColor = new QLabel(" ");
45     oldColor->setMaximumHeight(15);
46     oldColor->setMaximumWidth(10);
47     oldColor->setStyleSheet(legendBoxStyle.arg(COLOR_MINT_OLD.red()).arg(COLOR_MINT_OLD.green()).arg(COLOR_MINT_OLD.blue()));
48     QLabel *oldLegend = new QLabel(tr("transaction has reached maximum probability"));
49     oldLegend->setContentsMargins(5,0,15,0);
50
51     QHBoxLayout *legendLayout = new QHBoxLayout();
52     legendLayout->setContentsMargins(10,10,0,0);
53     legendLayout->addWidget(youngColor);
54     legendLayout->addWidget(youngLegend);
55     legendLayout->addWidget(matureColor);
56     legendLayout->addWidget(matureLegend);
57     legendLayout->addWidget(oldColor);
58     legendLayout->addWidget(oldLegend);
59     legendLayout->insertStretch(-1);
60
61     QLabel *mintingLabel = new QLabel(tr("Display minting probability within : "));
62     mintingCombo = new QComboBox();
63     mintingCombo->addItem(tr("10 min"), Minting10min);
64     mintingCombo->addItem(tr("24 hours"), Minting1day);
65     mintingCombo->addItem(tr("30 days"), Minting30days);
66     mintingCombo->addItem(tr("90 days"), Minting90days);
67     mintingCombo->setFixedWidth(120);
68
69
70     hlayout->insertStretch(0);
71     hlayout->addWidget(mintingLabel);
72     hlayout->addWidget(mintingCombo);
73
74     QVBoxLayout *vlayout = new QVBoxLayout(this);
75     vlayout->setContentsMargins(0,0,0,0);
76     vlayout->setSpacing(0);
77
78     QTableView *view = new QTableView(this);
79     vlayout->addLayout(hlayout);
80     vlayout->addWidget(view);
81     vlayout->addLayout(legendLayout);
82
83     vlayout->setSpacing(0);
84     int width = view->verticalScrollBar()->sizeHint().width();
85     // Cover scroll bar width with spacing
86 #ifdef Q_WS_MAC
87     hlayout->addSpacing(width+2);
88 #else
89     hlayout->addSpacing(width);
90 #endif
91     // Always show scroll bar
92     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
93     view->setTabKeyNavigation(false);
94     view->setContextMenuPolicy(Qt::CustomContextMenu);
95
96     mintingView = view;
97
98     connect(mintingCombo, SIGNAL(activated(int)), this, SLOT(chooseMintingInterval(int)));
99
100     // Actions
101     QAction *copyTxIDAction = new QAction(tr("Copy transaction ID of input"), this);
102     QAction *copyAddressAction = new QAction(tr("Copy address of input"), this);
103
104     contextMenu = new QMenu();
105     contextMenu->addAction(copyAddressAction);
106     contextMenu->addAction(copyTxIDAction);
107
108     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
109     connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
110
111     connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
112 }
113
114
115 void MintingView::setModel(WalletModel *model)
116 {
117     this->model = model;
118     if(model)
119     {
120         mintingProxyModel = new MintingFilterProxy(this);
121         mintingProxyModel->setSourceModel(model->getMintingTableModel());
122         mintingProxyModel->setDynamicSortFilter(true);
123         mintingProxyModel->setSortRole(Qt::EditRole);
124
125         mintingView->setModel(mintingProxyModel);
126         mintingView->setAlternatingRowColors(true);
127         mintingView->setSelectionBehavior(QAbstractItemView::SelectRows);
128         mintingView->setSelectionMode(QAbstractItemView::ExtendedSelection);
129         mintingView->setSortingEnabled(true);
130         mintingView->sortByColumn(MintingTableModel::CoinDay, Qt::DescendingOrder);
131         mintingView->verticalHeader()->hide();
132
133         mintingView->horizontalHeader()->resizeSection(
134                 MintingTableModel::Age, 120);
135         mintingView->horizontalHeader()->resizeSection(
136                 MintingTableModel::Balance, 120);
137         mintingView->horizontalHeader()->resizeSection(
138                 MintingTableModel::CoinDay,120);
139         mintingView->horizontalHeader()->resizeSection(
140                 MintingTableModel::MintProbability, 120);
141 #if QT_VERSION < 0x050000
142         mintingView->horizontalHeader()->setResizeMode(
143                 MintingTableModel::MintReward, QHeaderView::Stretch);
144 #else
145         mintingView->horizontalHeader()->setSectionResizeMode(MintingTableModel::MintReward, QHeaderView::Stretch);
146 #endif
147         mintingView->horizontalHeader()->resizeSection(
148             MintingTableModel::Address, 0);
149         mintingView->horizontalHeader()->resizeSection(
150             MintingTableModel::TxHash, 0);
151     }
152 }
153
154 void MintingView::chooseMintingInterval(int idx)
155 {
156     int interval = 10;
157     switch(mintingCombo->itemData(idx).toInt())
158     {
159         case Minting10min:
160             interval = 10;
161             break;
162         case Minting1day:
163             interval = 60*24;
164             break;
165         case Minting30days:
166             interval = 60*24*30;
167             break;
168         case Minting90days:
169             interval = 60*24*90;
170             break;
171     }
172     model->getMintingTableModel()->setMintingInterval(interval);
173     mintingProxyModel->invalidate();
174 }
175
176 void MintingView::exportClicked()
177 {
178     // CSV is currently the only supported format
179     QString filename = GUIUtil::getSaveFileName(
180             this,
181             tr("Export Minting Data"), QString(),
182             tr("Comma separated file (*.csv)"));
183
184     if (filename.isNull()) return;
185
186     CSVModelWriter writer(filename);
187
188     // name, column, role
189     writer.setModel(mintingProxyModel);
190     writer.addColumn(tr("Address"),0, MintingTableModel::Address);
191     writer.addColumn(tr("Transaction"), 0, MintingTableModel::TxHash);
192     writer.addColumn(tr("Age"), 0, MintingTableModel::Age);
193     writer.addColumn(tr("CoinDay"), 0, MintingTableModel::CoinDay);
194     writer.addColumn(tr("Balance"), 0, MintingTableModel::Balance);
195     writer.addColumn(tr("MintingProbability"), 0, MintingTableModel::MintProbability);
196     writer.addColumn(tr("MintingReward"), 0, MintingTableModel::MintReward);
197
198     if(!writer.write())
199     {
200         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
201                               QMessageBox::Abort, QMessageBox::Abort);
202     }
203 }
204
205 void MintingView::copyTxID()
206 {
207     GUIUtil::copyEntryData(mintingView, MintingTableModel::TxHash, 0);
208 }
209
210 void MintingView::copyAddress()
211 {
212     GUIUtil::copyEntryData(mintingView, MintingTableModel::Address, 0 );
213 }
214
215 void MintingView::contextualMenu(const QPoint &point)
216 {
217     QModelIndex index = mintingView->indexAt(point);
218     if(index.isValid())
219     {
220         contextMenu->exec(QCursor::pos());
221     }
222 }