Update CMakeLists.txt - play with openssl
[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("7 days"), Minting7days);
66     mintingCombo->addItem(tr("30 days"), Minting30days);
67     mintingCombo->addItem(tr("60 days"), Minting60days);
68     mintingCombo->addItem(tr("90 days"), Minting90days);
69     mintingCombo->setFixedWidth(120);
70
71
72     hlayout->insertStretch(0);
73     hlayout->addWidget(mintingLabel);
74     hlayout->addWidget(mintingCombo);
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->addLayout(legendLayout);
84
85     vlayout->setSpacing(0);
86     int width = view->verticalScrollBar()->sizeHint().width();
87     // Cover scroll bar width with spacing
88 #ifdef Q_WS_MAC
89     hlayout->addSpacing(width+2);
90 #else
91     hlayout->addSpacing(width);
92 #endif
93     // Always show scroll bar
94     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
95     view->setTabKeyNavigation(false);
96     view->setContextMenuPolicy(Qt::CustomContextMenu);
97
98     mintingView = view;
99
100     connect(mintingCombo, SIGNAL(activated(int)), this, SLOT(chooseMintingInterval(int)));
101
102     // Actions
103     QAction *copyTxIDAction = new QAction(tr("Copy transaction ID of input"), this);
104     QAction *copyAddressAction = new QAction(tr("Copy address of input"), this);
105     QAction *showHideAddressAction = new QAction(tr("Show/hide 'Address' column"), this);
106     QAction *showHideTxIDAction = new QAction(tr("Show/hide 'Transaction' column"), this);
107
108     contextMenu = new QMenu();
109     contextMenu->addAction(copyAddressAction);
110     contextMenu->addAction(copyTxIDAction);
111     contextMenu->addAction(showHideAddressAction);
112     contextMenu->addAction(showHideTxIDAction);
113
114     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
115     connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
116     connect(showHideAddressAction, SIGNAL(triggered()), this, SLOT(showHideAddress()));
117     connect(showHideTxIDAction, SIGNAL(triggered()), this, SLOT(showHideTxID()));
118
119     connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
120 }
121
122
123 void MintingView::setModel(WalletModel *model)
124 {
125     this->model = model;
126     if(model)
127     {
128         mintingProxyModel = new MintingFilterProxy(this);
129         mintingProxyModel->setSourceModel(model->getMintingTableModel());
130         mintingProxyModel->setDynamicSortFilter(true);
131         mintingProxyModel->setSortRole(Qt::EditRole);
132         model->getMintingTableModel()->setMintingProxyModel(mintingProxyModel);
133
134         mintingView->setModel(mintingProxyModel);
135         mintingView->setAlternatingRowColors(true);
136         mintingView->setSelectionBehavior(QAbstractItemView::SelectRows);
137         mintingView->setSelectionMode(QAbstractItemView::ExtendedSelection);
138         mintingView->setSortingEnabled(true);
139         mintingView->sortByColumn(MintingTableModel::CoinDay, Qt::DescendingOrder);
140         mintingView->verticalHeader()->hide();
141
142         mintingView->horizontalHeader()->resizeSection(
143                 MintingTableModel::Age, 60);
144         mintingView->horizontalHeader()->resizeSection(
145                 MintingTableModel::Balance, 80);
146         mintingView->horizontalHeader()->resizeSection(
147                 MintingTableModel::CoinDay,60);
148         mintingView->horizontalHeader()->resizeSection(
149                 MintingTableModel::MintProbability, 105);
150 #if QT_VERSION < 0x050000
151         mintingView->horizontalHeader()->setResizeMode(
152                 MintingTableModel::MintReward, QHeaderView::Stretch);
153 #else
154         mintingView->horizontalHeader()->setSectionResizeMode(
155                 MintingTableModel::MintReward, QHeaderView::Stretch);
156 #endif
157         mintingView->horizontalHeader()->resizeSection(
158             MintingTableModel::Address, 245);
159         mintingView->horizontalHeader()->resizeSection(
160             MintingTableModel::TxHash, 75);
161     }
162 }
163
164 void MintingView::chooseMintingInterval(int idx)
165 {
166     int interval = 10;
167     switch(mintingCombo->itemData(idx).toInt())
168     {
169         case Minting10min:
170             interval = 10;
171             break;
172         case Minting1day:
173             interval = 60*24;
174             break;
175         case Minting7days:
176             interval = 60*24*7;
177             break;
178         case Minting30days:
179             interval = 60*24*30;
180             break;
181         case Minting60days:
182             interval = 60*24*60;
183             break;
184         case Minting90days:
185             interval = 60*24*90;
186             break;
187     }
188     model->getMintingTableModel()->setMintingInterval(interval);
189     mintingProxyModel->invalidate();
190 }
191
192 void MintingView::exportClicked()
193 {
194     // CSV is currently the only supported format
195     QString filename = GUIUtil::getSaveFileName(
196             this,
197             tr("Export Minting Data"), QString(),
198             tr("Comma separated file (*.csv)"));
199
200     if (filename.isNull()) return;
201
202     CSVModelWriter writer(filename);
203
204     // name, column, role
205     writer.setModel(mintingProxyModel);
206     writer.addColumn(tr("Address"),MintingTableModel::Address,0);
207     writer.addColumn(tr("Transaction"),MintingTableModel::TxHash,0);
208     writer.addColumn(tr("Age"), MintingTableModel::Age,0);
209     writer.addColumn(tr("CoinDay"), MintingTableModel::CoinDay,0);
210     writer.addColumn(tr("Balance"), MintingTableModel::Balance,0);
211     writer.addColumn(tr("MintingProbability"), MintingTableModel::MintProbability,0);
212     writer.addColumn(tr("MintingReward"), MintingTableModel::MintReward,0);
213
214     if(!writer.write())
215     {
216         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
217                               QMessageBox::Abort, QMessageBox::Abort);
218     }
219 }
220
221 void MintingView::copyTxID()
222 {
223     GUIUtil::copyEntryData(mintingView, MintingTableModel::TxHash, 0);
224 }
225
226 void MintingView::copyAddress()
227 {
228     GUIUtil::copyEntryData(mintingView, MintingTableModel::Address, 0 );
229 }
230
231 void MintingView::showHideAddress()
232 {
233     mintingView->horizontalHeader()->setSectionHidden(MintingTableModel::Address, 
234         !(mintingView->horizontalHeader()->isSectionHidden(MintingTableModel::Address)));
235 }
236
237 void MintingView::showHideTxID()
238 {
239     mintingView->horizontalHeader()->setSectionHidden(MintingTableModel::TxHash, 
240         !(mintingView->horizontalHeader()->isSectionHidden(MintingTableModel::TxHash)));
241 }
242
243 void MintingView::contextualMenu(const QPoint &point)
244 {
245     QModelIndex index = mintingView->indexAt(point);
246     if(index.isValid())
247     {
248         contextMenu->exec(QCursor::pos());
249     }
250 }