]>
Commit | Line | Data |
---|---|---|
64c8b699 WL |
1 | #include "overviewpage.h" |
2 | #include "ui_overviewpage.h" | |
3 | ||
df5ccbd2 | 4 | #include "walletmodel.h" |
e285ffcd | 5 | #include "bitcoinunits.h" |
ee014e5b | 6 | #include "optionsmodel.h" |
a99ac8d3 WL |
7 | #include "transactiontablemodel.h" |
8 | #include "transactionfilterproxy.h" | |
9 | #include "guiutil.h" | |
10 | #include "guiconstants.h" | |
ee014e5b | 11 | |
2ccd4759 | 12 | #include <QAbstractItemDelegate> |
a99ac8d3 WL |
13 | #include <QPainter> |
14 | ||
15 | #define DECORATION_SIZE 64 | |
82303fc3 WL |
16 | #define NUM_ITEMS 3 |
17 | ||
2ccd4759 | 18 | class TxViewDelegate : public QAbstractItemDelegate |
a99ac8d3 | 19 | { |
9b490f71 | 20 | Q_OBJECT |
a99ac8d3 | 21 | public: |
2ccd4759 | 22 | TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::BTC) |
a99ac8d3 WL |
23 | { |
24 | ||
25 | } | |
26 | ||
27 | inline void paint(QPainter *painter, const QStyleOptionViewItem &option, | |
28 | const QModelIndex &index ) const | |
29 | { | |
a99ac8d3 WL |
30 | painter->save(); |
31 | ||
32 | QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole)); | |
33 | QRect mainRect = option.rect; | |
34 | QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE)); | |
35 | int xspace = DECORATION_SIZE + 8; | |
36 | int ypad = 6; | |
37 | int halfheight = (mainRect.height() - 2*ypad)/2; | |
38 | QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight); | |
39 | QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight); | |
40 | icon.paint(painter, decorationRect); | |
41 | ||
42 | QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime(); | |
43 | QString address = index.data(Qt::DisplayRole).toString(); | |
44 | qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong(); | |
45 | bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool(); | |
46 | QVariant value = index.data(Qt::ForegroundRole); | |
47 | QColor foreground = option.palette.color(QPalette::Text); | |
48 | if(qVariantCanConvert<QColor>(value)) | |
49 | { | |
50 | foreground = qvariant_cast<QColor>(value); | |
51 | } | |
52 | ||
53 | painter->setPen(foreground); | |
54 | painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address); | |
55 | ||
56 | if(amount < 0) | |
57 | { | |
58 | foreground = COLOR_NEGATIVE; | |
59 | } | |
82303fc3 WL |
60 | else if(!confirmed) |
61 | { | |
62 | foreground = COLOR_UNCONFIRMED; | |
63 | } | |
a99ac8d3 WL |
64 | else |
65 | { | |
66 | foreground = option.palette.color(QPalette::Text); | |
67 | } | |
68 | painter->setPen(foreground); | |
69 | QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true); | |
70 | if(!confirmed) | |
71 | { | |
72 | amountText = QString("[") + amountText + QString("]"); | |
73 | } | |
74 | painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText); | |
75 | ||
76 | painter->setPen(option.palette.color(QPalette::Text)); | |
b0849613 | 77 | painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date)); |
a99ac8d3 WL |
78 | |
79 | painter->restore(); | |
80 | } | |
81 | ||
2ccd4759 WL |
82 | inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
83 | { | |
84 | return QSize(DECORATION_SIZE, DECORATION_SIZE); | |
85 | } | |
86 | ||
a99ac8d3 WL |
87 | int unit; |
88 | ||
89 | }; | |
9b490f71 | 90 | #include "overviewpage.moc" |
64c8b699 WL |
91 | |
92 | OverviewPage::OverviewPage(QWidget *parent) : | |
93 | QWidget(parent), | |
ee014e5b WL |
94 | ui(new Ui::OverviewPage), |
95 | currentBalance(-1), | |
a99ac8d3 WL |
96 | currentUnconfirmedBalance(-1), |
97 | txdelegate(new TxViewDelegate()) | |
64c8b699 WL |
98 | { |
99 | ui->setupUi(this); | |
100 | ||
101 | // Balance: <balance> | |
102 | ui->labelBalance->setFont(QFont("Monospace", -1, QFont::Bold)); | |
103 | ui->labelBalance->setToolTip(tr("Your current balance")); | |
104 | ui->labelBalance->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard); | |
105 | ||
1b392019 | 106 | // Unconfirmed balance: <balance> |
df5ccbd2 | 107 | ui->labelUnconfirmed->setFont(QFont("Monospace", -1, QFont::Bold)); |
1b392019 | 108 | ui->labelUnconfirmed->setToolTip(tr("Total of transactions that have yet to be confirmed, and do not yet count toward the current balance")); |
df5ccbd2 WL |
109 | ui->labelUnconfirmed->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard); |
110 | ||
111 | ui->labelNumTransactions->setToolTip(tr("Total number of transactions in wallet")); | |
112 | ||
a99ac8d3 WL |
113 | // Recent transactions |
114 | ui->listTransactions->setStyleSheet("background:transparent"); | |
115 | ui->listTransactions->setItemDelegate(txdelegate); | |
116 | ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE)); | |
117 | ui->listTransactions->setSelectionMode(QAbstractItemView::NoSelection); | |
82303fc3 | 118 | ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2)); |
527137e3 | 119 | ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false); |
1b392019 WL |
120 | |
121 | connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SIGNAL(transactionClicked(QModelIndex))); | |
64c8b699 WL |
122 | } |
123 | ||
124 | OverviewPage::~OverviewPage() | |
125 | { | |
126 | delete ui; | |
127 | } | |
128 | ||
df5ccbd2 | 129 | void OverviewPage::setBalance(qint64 balance, qint64 unconfirmedBalance) |
64c8b699 | 130 | { |
ee014e5b WL |
131 | int unit = model->getOptionsModel()->getDisplayUnit(); |
132 | currentBalance = balance; | |
133 | currentUnconfirmedBalance = unconfirmedBalance; | |
134 | ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance)); | |
135 | ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance)); | |
64c8b699 | 136 | } |
d52a0f3b WL |
137 | |
138 | void OverviewPage::setNumTransactions(int count) | |
139 | { | |
140 | ui->labelNumTransactions->setText(QLocale::system().toString(count)); | |
141 | } | |
df5ccbd2 WL |
142 | |
143 | void OverviewPage::setModel(WalletModel *model) | |
144 | { | |
145 | this->model = model; | |
dead0ff8 WL |
146 | if(model) |
147 | { | |
148 | // Set up transaction list | |
149 | TransactionFilterProxy *filter = new TransactionFilterProxy(); | |
150 | filter->setSourceModel(model->getTransactionTableModel()); | |
151 | filter->setLimit(NUM_ITEMS); | |
152 | filter->setDynamicSortFilter(true); | |
153 | filter->setSortRole(Qt::EditRole); | |
154 | filter->sort(TransactionTableModel::Status, Qt::DescendingOrder); | |
df5ccbd2 | 155 | |
dead0ff8 WL |
156 | ui->listTransactions->setModel(filter); |
157 | ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress); | |
a99ac8d3 | 158 | |
dead0ff8 WL |
159 | // Keep up to date with wallet |
160 | setBalance(model->getBalance(), model->getUnconfirmedBalance()); | |
161 | connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64))); | |
df5ccbd2 | 162 | |
dead0ff8 WL |
163 | setNumTransactions(model->getNumTransactions()); |
164 | connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int))); | |
df5ccbd2 | 165 | |
dead0ff8 WL |
166 | connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(displayUnitChanged())); |
167 | } | |
ee014e5b WL |
168 | } |
169 | ||
170 | void OverviewPage::displayUnitChanged() | |
171 | { | |
dead0ff8 WL |
172 | if(!model || !model->getOptionsModel()) |
173 | return; | |
ee014e5b WL |
174 | if(currentBalance != -1) |
175 | setBalance(currentBalance, currentUnconfirmedBalance); | |
a99ac8d3 WL |
176 | |
177 | txdelegate->unit = model->getOptionsModel()->getDisplayUnit(); | |
178 | ui->listTransactions->update(); | |
df5ccbd2 | 179 | } |