PosTab
[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
21 MintingView::MintingView(QWidget *parent) :
22     QWidget(parent), model(0), mintingView(0)
23 {
24     QHBoxLayout *hlayout = new QHBoxLayout();
25     hlayout->setContentsMargins(0,0,0,0);
26
27     QString legendBoxStyle = "background-color: rgb(%1,%2,%3); border: 1px solid black;";
28
29     QLabel *youngColor = new QLabel(" ");
30     youngColor->setMaximumHeight(15);
31     youngColor->setMaximumWidth(10);
32     youngColor->setStyleSheet(legendBoxStyle.arg(COLOR_MINT_YOUNG.red()).arg(COLOR_MINT_YOUNG.green()).arg(COLOR_MINT_YOUNG.blue()));
33     QLabel *youngLegend = new QLabel(tr("transaction is too young"));
34     youngLegend->setContentsMargins(5,0,15,0);
35
36     QLabel *matureColor = new QLabel(" ");
37     matureColor->setMaximumHeight(15);
38     matureColor->setMaximumWidth(10);
39     matureColor->setStyleSheet(legendBoxStyle.arg(COLOR_MINT_MATURE.red()).arg(COLOR_MINT_MATURE.green()).arg(COLOR_MINT_MATURE.blue()));
40     QLabel *matureLegend = new QLabel(tr("transaction is mature"));
41     matureLegend->setContentsMargins(5,0,15,0);
42
43     QLabel *oldColor = new QLabel(" ");
44     oldColor->setMaximumHeight(15);
45     oldColor->setMaximumWidth(10);
46     oldColor->setStyleSheet(legendBoxStyle.arg(COLOR_MINT_OLD.red()).arg(COLOR_MINT_OLD.green()).arg(COLOR_MINT_OLD.blue()));
47     QLabel *oldLegend = new QLabel(tr("transaction has reached maximum probability"));
48     oldLegend->setContentsMargins(5,0,15,0);
49
50     QHBoxLayout *legendLayout = new QHBoxLayout();
51     legendLayout->setContentsMargins(10,10,0,0);
52     legendLayout->addWidget(youngColor);
53     legendLayout->addWidget(youngLegend);
54     legendLayout->addWidget(matureColor);
55     legendLayout->addWidget(matureLegend);
56     legendLayout->addWidget(oldColor);
57     legendLayout->addWidget(oldLegend);
58     legendLayout->insertStretch(-1);
59
60     QLabel *mintingLabel = new QLabel(tr("Display minting probability within : "));
61     mintingCombo = new QComboBox();
62     mintingCombo->addItem(tr("10 min"), Minting10min);
63     mintingCombo->addItem(tr("24 hours"), Minting1day);
64     mintingCombo->addItem(tr("30 days"), Minting30days);
65     mintingCombo->addItem(tr("90 days"), Minting90days);
66     mintingCombo->setFixedWidth(120);
67
68
69     hlayout->insertStretch(0);
70     hlayout->addWidget(mintingLabel);
71     hlayout->addWidget(mintingCombo);
72
73     QVBoxLayout *vlayout = new QVBoxLayout(this);
74     vlayout->setContentsMargins(0,0,0,0);
75     vlayout->setSpacing(0);
76
77     QTableView *view = new QTableView(this);
78     vlayout->addLayout(hlayout);
79     vlayout->addWidget(view);
80     vlayout->addLayout(legendLayout);
81
82     vlayout->setSpacing(0);
83     int width = view->verticalScrollBar()->sizeHint().width();
84     // Cover scroll bar width with spacing
85 #ifdef Q_WS_MAC
86     hlayout->addSpacing(width+2);
87 #else
88     hlayout->addSpacing(width);
89 #endif
90     // Always show scroll bar
91     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
92     view->setTabKeyNavigation(false);
93     view->setContextMenuPolicy(Qt::CustomContextMenu);
94
95     mintingView = view;
96
97     connect(mintingCombo, SIGNAL(activated(int)), this, SLOT(chooseMintingInterval(int)));
98
99 }
100
101
102 void MintingView::setModel(WalletModel *model)
103 {
104     this->model = model;
105     if(model)
106     {
107         mintingProxyModel = new MintingFilterProxy(this);
108         mintingProxyModel->setSourceModel(model->getMintingTableModel());
109         mintingProxyModel->setDynamicSortFilter(true);
110         mintingProxyModel->setSortRole(Qt::EditRole);
111
112         mintingView->setModel(mintingProxyModel);
113         mintingView->setAlternatingRowColors(true);
114         mintingView->setSelectionBehavior(QAbstractItemView::SelectRows);
115         mintingView->setSelectionMode(QAbstractItemView::ExtendedSelection);
116         mintingView->setSortingEnabled(true);
117         mintingView->sortByColumn(MintingTableModel::CoinDay, Qt::DescendingOrder);
118         mintingView->verticalHeader()->hide();
119
120         mintingView->horizontalHeader()->resizeSection(
121                 MintingTableModel::Address, 420);
122 #if QT_VERSION < 0x050000
123         mintingView->horizontalHeader()->setResizeMode(
124                 MintingTableModel::TxHash, QHeaderView::Stretch);
125 #else
126         mintingView->horizontalHeader()->setSectionResizeMode(
127                 MintingTableModel::TxHash, QHeaderView::Stretch);
128 #endif
129         mintingView->horizontalHeader()->resizeSection(
130                 MintingTableModel::Age, 120);
131         mintingView->horizontalHeader()->resizeSection(
132                 MintingTableModel::Balance, 120);
133         mintingView->horizontalHeader()->resizeSection(
134                 MintingTableModel::CoinDay,120);
135         mintingView->horizontalHeader()->resizeSection(
136                 MintingTableModel::MintProbability, 160);
137     }
138 }
139
140 void MintingView::chooseMintingInterval(int idx)
141 {
142     int interval = 10;
143     switch(mintingCombo->itemData(idx).toInt())
144     {
145         case Minting10min:
146             interval = 10;
147             break;
148         case Minting1day:
149             interval = 60*24;
150             break;
151         case Minting30days:
152             interval = 60*24*30;
153             break;
154         case Minting90days:
155             interval = 60*24*90;
156             break;
157     }
158     model->getMintingTableModel()->setMintingInterval(interval);
159     mintingProxyModel->invalidate();
160 }
161
162 void MintingView::exportClicked()
163 {
164     // CSV is currently the only supported format
165     QString filename = GUIUtil::getSaveFileName(
166             this,
167             tr("Export Minting Data"), QString(),
168             tr("Comma separated file (*.csv)"));
169
170     if (filename.isNull()) return;
171
172     CSVModelWriter writer(filename);
173
174     // name, column, role
175     writer.setModel(mintingProxyModel);
176     writer.addColumn(tr("Address"),0, MintingTableModel::Address);
177     writer.addColumn(tr("Transaction"), 0, MintingTableModel::TxHash);
178     writer.addColumn(tr("Age"), 0, MintingTableModel::Age);
179     writer.addColumn(tr("CoinDay"), 0, MintingTableModel::CoinDay);
180     writer.addColumn(tr("Balance"), 0, MintingTableModel::Balance);
181     writer.addColumn(tr("MintingProbability"), 0, MintingTableModel::MintProbability);
182
183     if(!writer.write())
184     {
185         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
186                               QMessageBox::Abort, QMessageBox::Abort);
187     }
188 }