PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/overviewpage.cpp

https://github.com/deadbrainviv/master
C++ | 240 lines | 189 code | 38 blank | 13 comment | 13 complexity | 3f62ea5d010c5468c162022143415ff5 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, 0BSD
  1. #include "overviewpage.h"
  2. #include "ui_overviewpage.h"
  3. #include "walletmodel.h"
  4. #include "bitcoinunits.h"
  5. #include "optionsmodel.h"
  6. #include "transactiontablemodel.h"
  7. #include "transactionfilterproxy.h"
  8. #include "guiutil.h"
  9. #include "guiconstants.h"
  10. #include <QAbstractItemDelegate>
  11. #include <QPainter>
  12. #include <QDesktopServices>
  13. #include <QUrl>
  14. #include <QWebView>
  15. #define DECORATION_SIZE 64
  16. #define NUM_ITEMS 3
  17. class TxViewDelegate : public QAbstractItemDelegate
  18. {
  19. Q_OBJECT
  20. public:
  21. TxViewDelegate(): QAbstractItemDelegate(), unit(BitcoinUnits::VRC)
  22. {
  23. }
  24. inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
  25. const QModelIndex &index ) const
  26. {
  27. painter->save();
  28. QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
  29. QRect mainRect = option.rect;
  30. QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
  31. int xspace = DECORATION_SIZE + 8;
  32. int ypad = 6;
  33. int halfheight = (mainRect.height() - 2*ypad)/2;
  34. QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight);
  35. QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight);
  36. icon.paint(painter, decorationRect);
  37. QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
  38. QString address = index.data(Qt::DisplayRole).toString();
  39. qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
  40. bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
  41. QVariant value = index.data(Qt::ForegroundRole);
  42. QColor foreground = option.palette.color(QPalette::Text);
  43. if(qVariantCanConvert<QColor>(value))
  44. {
  45. foreground = qvariant_cast<QColor>(value);
  46. }
  47. painter->setPen(foreground);
  48. painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address);
  49. if(amount < 0)
  50. {
  51. foreground = COLOR_NEGATIVE;
  52. }
  53. else if(!confirmed)
  54. {
  55. foreground = COLOR_UNCONFIRMED;
  56. }
  57. else
  58. {
  59. foreground = option.palette.color(QPalette::Text);
  60. }
  61. painter->setPen(foreground);
  62. QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true);
  63. if(!confirmed)
  64. {
  65. amountText = QString("[") + amountText + QString("]");
  66. }
  67. painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
  68. painter->setPen(option.palette.color(QPalette::Text));
  69. painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
  70. painter->restore();
  71. }
  72. inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
  73. {
  74. return QSize(DECORATION_SIZE, DECORATION_SIZE);
  75. }
  76. int unit;
  77. };
  78. #include "overviewpage.moc"
  79. OverviewPage::OverviewPage(QWidget *parent) :
  80. QWidget(parent),
  81. ui(new Ui::OverviewPage),
  82. currentBalance(-1),
  83. currentStake(0),
  84. currentUnconfirmedBalance(-1),
  85. currentImmatureBalance(-1),
  86. txdelegate(new TxViewDelegate()),
  87. filter(0)
  88. {
  89. ui->setupUi(this);
  90. //QPixmap pix("src/qt/res/icons/logo_emboss.png");
  91. //ui->logo_emboss->setPixmap(pix);
  92. // Recent transactions
  93. ui->listTransactions->setItemDelegate(txdelegate);
  94. ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
  95. ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
  96. ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
  97. connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
  98. // init "out of sync" warning labels
  99. ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
  100. ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
  101. // start with displaying the "out of sync" warnings
  102. showOutOfSyncWarning(true);
  103. }
  104. void OverviewPage::handleTransactionClicked(const QModelIndex &index)
  105. {
  106. if(filter)
  107. emit transactionClicked(filter->mapToSource(index));
  108. }
  109. OverviewPage::~OverviewPage()
  110. {
  111. delete ui;
  112. }
  113. void OverviewPage::setBalance(qint64 balance, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance)
  114. {
  115. int unit = model->getOptionsModel()->getDisplayUnit();
  116. currentBalance = balance;
  117. currentStake = stake;
  118. currentUnconfirmedBalance = unconfirmedBalance;
  119. currentImmatureBalance = immatureBalance;
  120. ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
  121. ui->labelStake->setText(BitcoinUnits::formatWithUnit(unit, stake));
  122. ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
  123. ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance));
  124. ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, balance + stake + unconfirmedBalance + immatureBalance));
  125. // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
  126. // for the non-mining users
  127. bool showImmature = immatureBalance != 0;
  128. ui->labelImmature->setVisible(showImmature);
  129. ui->labelImmatureText->setVisible(showImmature);
  130. }
  131. void OverviewPage::setNumTransactions(int count)
  132. {
  133. //ui->labelNumTransactions->setText(QLocale::system().toString(count));
  134. }
  135. void OverviewPage::setModel(WalletModel *model)
  136. {
  137. this->model = model;
  138. if(model && model->getOptionsModel())
  139. {
  140. // Set up transaction list
  141. filter = new TransactionFilterProxy();
  142. filter->setSourceModel(model->getTransactionTableModel());
  143. filter->setLimit(NUM_ITEMS);
  144. filter->setDynamicSortFilter(true);
  145. filter->setSortRole(Qt::EditRole);
  146. filter->setShowInactive(false);
  147. filter->sort(TransactionTableModel::Status, Qt::DescendingOrder);
  148. ui->listTransactions->setModel(filter);
  149. ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
  150. // Keep up to date with wallet
  151. setBalance(model->getBalance(), model->getStake(), model->getUnconfirmedBalance(), model->getImmatureBalance());
  152. connect(model, SIGNAL(balanceChanged(qint64, qint64, qint64, qint64)), this, SLOT(setBalance(qint64, qint64, qint64, qint64)));
  153. setNumTransactions(model->getNumTransactions());
  154. connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
  155. connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
  156. }
  157. // update the display unit, to not use the default ("VRC")
  158. updateDisplayUnit();
  159. }
  160. void OverviewPage::updateDisplayUnit()
  161. {
  162. if(model && model->getOptionsModel())
  163. {
  164. if(currentBalance != -1)
  165. setBalance(currentBalance, model->getStake(), currentUnconfirmedBalance, currentImmatureBalance);
  166. // Update txdelegate->unit with the current unit
  167. txdelegate->unit = model->getOptionsModel()->getDisplayUnit();
  168. ui->listTransactions->update();
  169. }
  170. }
  171. void OverviewPage::showOutOfSyncWarning(bool fShow)
  172. {
  173. ui->labelWalletStatus->setVisible(fShow);
  174. ui->labelTransactionsStatus->setVisible(fShow);
  175. }
  176. //Links button grid
  177. void OverviewPage::on_VRCtalkbutton_clicked()
  178. {
  179. QString link="http://tinyurl.com/bitcointalk-vericoin";
  180. QDesktopServices::openUrl(QUrl(link));
  181. }
  182. void OverviewPage::on_IRCbutton_clicked()
  183. {
  184. QString link="http://tinyurl.com/freenode-vericoin";
  185. QDesktopServices::openUrl(QUrl(link));
  186. }
  187. void OverviewPage::on_redditButton_clicked()
  188. {
  189. QString link="http://www.reddit.com/r/vericoin";
  190. QDesktopServices::openUrl(QUrl(link));
  191. }
  192. void OverviewPage::on_twitterButton_clicked()
  193. {
  194. QString link="https://twitter.com/vericoin";
  195. QDesktopServices::openUrl(QUrl(link));
  196. }
  197. void OverviewPage::on_buyVeriCoin_clicked()
  198. {
  199. QWebView *view = new QWebView();
  200. view->load(QUrl("http://www.vericoin.info/fiat.html"));
  201. view->show();
  202. }