git / moonbite-core / src/qt/overviewpage.cpp · raw
// Copyright (c) 2011-2019 The Bitcoin Core developers// Distributed under the MIT software license, see the accompanying// file COPYING or http://www.opensource.org/licenses/mit-license.php.#include <qt/overviewpage.h>#include <qt/forms/ui_overviewpage.h>#include <qt/bitcoinunits.h>#include <qt/clientmodel.h>#include <qt/guiconstants.h>#include <qt/guiutil.h>#include <qt/optionsmodel.h>#include <qt/platformstyle.h>#include <qt/transactionfilterproxy.h>#include <qt/transactionoverviewwidget.h>#include <qt/transactiontablemodel.h>#include <qt/walletmodel.h>#include <QAbstractItemDelegate>#include <QApplication>#include <QColor>#include <QFrame>#include <QGraphicsDropShadowEffect>#include <QHBoxLayout>#include <QLabel>#include <QLocale>#include <QPainter>#include <QPushButton>#include <QStatusTipEvent>#include <QVBoxLayout>#include <algorithm>#include <map>#define DECORATION_SIZE 54#define NUM_ITEMS 5Q_DECLARE_METATYPE(interfaces::WalletBalances)class TxViewDelegate : public QAbstractItemDelegate{ Q_OBJECTpublic: explicit TxViewDelegate(const PlatformStyle *_platformStyle, QObject *parent=nullptr): QAbstractItemDelegate(parent), unit(BitcoinUnits::BTC), platformStyle(_platformStyle) { connect(this, &TxViewDelegate::width_changed, this, &TxViewDelegate::sizeHintChanged); } inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override { painter->save(); QIcon icon = qvariant_cast<QIcon>(index.data(TransactionTableModel::RawDecorationRole)); QRect mainRect = option.rect; QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE)); int xspace = DECORATION_SIZE + 8; int ypad = 6; int halfheight = (mainRect.height() - 2*ypad)/2; QRect amountRect(mainRect.left() + xspace, mainRect.top()+ypad, mainRect.width() - xspace, halfheight); QRect addressRect(mainRect.left() + xspace, mainRect.top()+ypad+halfheight, mainRect.width() - xspace, halfheight); icon = platformStyle->SingleColorIcon(icon); icon.paint(painter, decorationRect); QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime(); QString address = index.data(Qt::DisplayRole).toString(); qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong(); bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool(); QVariant value = index.data(Qt::ForegroundRole); QColor foreground = option.palette.color(QPalette::Text); if(value.canConvert<QBrush>()) { QBrush brush = qvariant_cast<QBrush>(value); foreground = brush.color(); } painter->setPen(foreground); QRect boundingRect; painter->drawText(addressRect, Qt::AlignLeft | Qt::AlignVCenter, address, &boundingRect); int address_rect_min_width = boundingRect.width(); if (index.data(TransactionTableModel::WatchonlyRole).toBool()) { QIcon iconWatchonly = qvariant_cast<QIcon>(index.data(TransactionTableModel::WatchonlyDecorationRole)); QRect watchonlyRect(boundingRect.right() + 5, mainRect.top()+ypad+halfheight, 16, halfheight); iconWatchonly.paint(painter, watchonlyRect); address_rect_min_width += 5 + watchonlyRect.width(); } if(amount < 0) { foreground = COLOR_NEGATIVE; } else if(!confirmed) { foreground = COLOR_UNCONFIRMED; } else { foreground = option.palette.color(QPalette::Text); } painter->setPen(foreground); QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true, BitcoinUnits::SeparatorStyle::ALWAYS); if(!confirmed) { amountText = QString("[") + amountText + QString("]"); } QRect amount_bounding_rect; painter->drawText(amountRect, Qt::AlignRight | Qt::AlignVCenter, amountText, &amount_bounding_rect); painter->setPen(option.palette.color(QPalette::Text)); QRect date_bounding_rect; painter->drawText(amountRect, Qt::AlignLeft | Qt::AlignVCenter, GUIUtil::dateTimeStr(date), &date_bounding_rect); const int minimum_width = std::max(address_rect_min_width, amount_bounding_rect.width() + date_bounding_rect.width()); const auto search = m_minimum_width.find(index.row()); if (search == m_minimum_width.end() || search->second != minimum_width) { m_minimum_width[index.row()] = minimum_width; Q_EMIT width_changed(index); } painter->restore(); } inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override { const auto search = m_minimum_width.find(index.row()); const int minimum_text_width = search == m_minimum_width.end() ? 0 : search->second; return {DECORATION_SIZE + 8 + minimum_text_width, DECORATION_SIZE}; } int unit;Q_SIGNALS: //! An intermediate signal for emitting from the `paint() const` member function. void width_changed(const QModelIndex& index) const;private: const PlatformStyle* platformStyle; mutable std::map<int, int> m_minimum_width;};#include <qt/overviewpage.moc>OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent) : QWidget(parent), ui(new Ui::OverviewPage), clientModel(nullptr), walletModel(nullptr), txdelegate(new TxViewDelegate(platformStyle, this)){ ui->setupUi(this); m_balances.balance = -1; // use a SingleColorIcon for the "out of sync warning" icon QIcon icon = platformStyle->SingleColorIcon(":/icons/warning"); icon.addPixmap(icon.pixmap(QSize(64,64), QIcon::Normal), QIcon::Disabled); // also set the disabled icon because we are using a disabled QPushButton to work around missing HiDPI support of QLabel (https://bugreports.qt.io/browse/QTBUG-42503) ui->labelTransactionsStatus->setIcon(icon); ui->labelWalletStatus->setIcon(icon); // Recent transactions ui->listTransactions->setItemDelegate(txdelegate); ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE)); ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2)); ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false); connect(ui->listTransactions, &TransactionOverviewWidget::clicked, this, &OverviewPage::handleTransactionClicked); // start with displaying the "out of sync" warnings showOutOfSyncWarning(true); connect(ui->labelWalletStatus, &QPushButton::clicked, this, &OverviewPage::handleOutOfSyncWarningClicks); connect(ui->labelTransactionsStatus, &QPushButton::clicked, this, &OverviewPage::handleOutOfSyncWarningClicks); // MoonBite: premium "wealth card" hero + quick-action row at the top createHeroCard(); createActionRow();}void OverviewPage::createHeroCard(){ m_heroCard = new QFrame(this); m_heroCard->setObjectName("moonHeroCard"); m_heroCard->setMinimumHeight(188); m_heroCard->setStyleSheet( "#moonHeroCard {" " border-radius: 22px;" " background: qlineargradient(x1:0, y1:0, x2:1, y2:1," " stop:0 #2a1a5e, stop:0.45 #3d2a8c, stop:0.8 #1e2a6e, stop:1 #0e3a6e);" " border: 1px solid rgba(255,255,255,0.14);" "}" "#moonHeroCard QLabel { background: transparent; }" ); // soft outer glow to lift the card off the page auto *shadow = new QGraphicsDropShadowEffect(m_heroCard); shadow->setBlurRadius(40); shadow->setColor(QColor(124, 92, 255, 150)); shadow->setOffset(0, 10); m_heroCard->setGraphicsEffect(shadow); auto *outer = new QVBoxLayout(m_heroCard); outer->setContentsMargins(28, 22, 28, 22); outer->setSpacing(0); // top row: wordmark + PREMIUM chip auto *topRow = new QHBoxLayout(); auto *brand = new QLabel("\xE2\x97\x90 M O O N B I T E", m_heroCard); brand->setStyleSheet("color:#ffffff; font-size:15px; font-weight:800; letter-spacing:3px;"); auto *chip = new QLabel("PREMIUM", m_heroCard); chip->setAlignment(Qt::AlignCenter); chip->setStyleSheet( "color:#3a2a00; font-size:10px; font-weight:800; letter-spacing:2px;" "padding:4px 12px; border-radius:9px;" "background: qlineargradient(x1:0,y1:0,x2:1,y2:0, stop:0 #ffe8a3, stop:1 #f2c14e);"); topRow->addWidget(brand); topRow->addStretch(); topRow->addWidget(chip); outer->addLayout(topRow); outer->addSpacing(18); // available-balance caption auto *cap = new QLabel("AVAILABLE BALANCE", m_heroCard); cap->setStyleSheet("color:rgba(255,255,255,0.60); font-size:11px; font-weight:700; letter-spacing:2px;"); outer->addWidget(cap); outer->addSpacing(4); // hero balance (big gold) + MBITE ticker auto *balRow = new QHBoxLayout(); balRow->setSpacing(8); m_heroBalance = new QLabel("0.00", m_heroCard); m_heroBalance->setStyleSheet("color:#ffe8a3; font-size:38px; font-weight:800;"); auto *ticker = new QLabel("MBITE", m_heroCard); ticker->setStyleSheet("color:rgba(255,232,163,0.75); font-size:16px; font-weight:800;"); balRow->addWidget(m_heroBalance, 0, Qt::AlignBottom); balRow->addWidget(ticker, 0, Qt::AlignBottom); balRow->addStretch(); outer->addLayout(balRow); outer->addStretch(); // bottom row: payment-card flourish + network tag (no fiat price) auto *botRow = new QHBoxLayout(); auto *cardNo = new QLabel("\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2 \xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2 \xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2 MOON", m_heroCard); cardNo->setStyleSheet("color:rgba(255,255,255,0.45); font-size:14px; font-weight:700; letter-spacing:2px; font-family:'JetBrains Mono','Consolas',monospace;"); auto *netTag = new QLabel("MOONBITE NETWORK", m_heroCard); netTag->setStyleSheet("color:rgba(255,255,255,0.55); font-size:11px; font-weight:700; letter-spacing:1px;"); botRow->addWidget(cardNo); botRow->addStretch(); botRow->addWidget(netTag, 0, Qt::AlignBottom); outer->addLayout(botRow); // insert the card at the very top of the page, above the balance columns ui->topLayout->insertWidget(1, m_heroCard);}void OverviewPage::createActionRow(){ // A premium row of circular quick-action buttons (Send / Receive / History) // sitting just under the hero card. Each only NAVIGATES to an existing page // (UI layer only) — no wallet/crypto logic here. auto *row = new QWidget(this); row->setObjectName("moonActionRow"); auto *rowLayout = new QHBoxLayout(row); rowLayout->setContentsMargins(4, 12, 4, 6); rowLayout->setSpacing(26); struct Action { const char *glyph; const char *caption; const char *grad0; const char *grad1; }; const Action actions[] = { { "\xE2\x86\x97", "Send", "#7c5cff", "#5b8bff" }, // up-right arrow { "\xE2\x86\x99", "Receive", "#2fb98a", "#34d3ff" }, // down-left arrow { "\xE2\x98\xB0", "History", "#8a6cff", "#c06cff" }, // list glyph }; QPushButton *buttons[3] = {nullptr, nullptr, nullptr}; rowLayout->addStretch(); for (int i = 0; i < 3; ++i) { auto *col = new QVBoxLayout(); col->setSpacing(9); col->setAlignment(Qt::AlignHCenter); auto *btn = new QPushButton(QString::fromUtf8(actions[i].glyph), row); btn->setCursor(Qt::PointingHandCursor); btn->setFixedSize(60, 60); btn->setStyleSheet(QString( "QPushButton {" " border: none; border-radius: 30px; color: #ffffff;" " font-size: 24px; font-weight: 800;" " background: qlineargradient(x1:0,y1:0,x2:1,y2:1, stop:0 %1, stop:1 %2);" "}" "QPushButton:hover { background: qlineargradient(x1:0,y1:0,x2:1,y2:1," " stop:0 %2, stop:1 %1); }" "QPushButton:pressed { padding-top: 2px; }") .arg(actions[i].grad0, actions[i].grad1)); auto *glow = new QGraphicsDropShadowEffect(btn); glow->setBlurRadius(24); glow->setColor(QColor(124, 92, 255, 130)); glow->setOffset(0, 6); btn->setGraphicsEffect(glow); auto *cap = new QLabel(QString::fromUtf8(actions[i].caption), row); cap->setAlignment(Qt::AlignHCenter); cap->setStyleSheet("color:#c7cdf0; font-size:12px; font-weight:700; letter-spacing:1px; background:transparent;"); col->addWidget(btn, 0, Qt::AlignHCenter); col->addWidget(cap, 0, Qt::AlignHCenter); rowLayout->addLayout(col); rowLayout->addStretch(); buttons[i] = btn; } connect(buttons[0], &QPushButton::clicked, this, &OverviewPage::sendClicked); connect(buttons[1], &QPushButton::clicked, this, &OverviewPage::receiveClicked); connect(buttons[2], &QPushButton::clicked, this, &OverviewPage::historyClicked); // place directly beneath the hero card ui->topLayout->insertWidget(2, row);}void OverviewPage::updateHeroCard(){ if (!m_heroBalance || !walletModel) return; if (m_privacy) { m_heroBalance->setText("\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2"); return; } const int unit = walletModel->getOptionsModel()->getDisplayUnit(); m_heroBalance->setText(BitcoinUnits::format(unit, m_balances.balance, false, BitcoinUnits::SeparatorStyle::ALWAYS));}void OverviewPage::handleTransactionClicked(const QModelIndex &index){ if(filter) Q_EMIT transactionClicked(filter->mapToSource(index));}void OverviewPage::handleOutOfSyncWarningClicks(){ Q_EMIT outOfSyncWarningClicked();}void OverviewPage::setPrivacy(bool privacy){ m_privacy = privacy; if (m_balances.balance != -1) { setBalance(m_balances); } ui->listTransactions->setVisible(!m_privacy); const QString status_tip = m_privacy ? tr("Privacy mode activated for the Overview tab. To unmask the values, uncheck Settings->Mask values.") : ""; setStatusTip(status_tip); QStatusTipEvent event(status_tip); QApplication::sendEvent(this, &event);}OverviewPage::~OverviewPage(){ delete ui;}void OverviewPage::setBalance(const interfaces::WalletBalances& balances){ int unit = walletModel->getOptionsModel()->getDisplayUnit(); m_balances = balances; if (walletModel->wallet().isLegacy()) { if (walletModel->wallet().privateKeysDisabled()) { ui->labelBalance->setText(BitcoinUnits::formatWithPrivacy(unit, balances.watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelUnconfirmed->setText(BitcoinUnits::formatWithPrivacy(unit, balances.unconfirmed_watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelImmature->setText(BitcoinUnits::formatWithPrivacy(unit, balances.immature_watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelTotal->setText(BitcoinUnits::formatWithPrivacy(unit, balances.watch_only_balance + balances.unconfirmed_watch_only_balance + balances.immature_watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); } else { ui->labelBalance->setText(BitcoinUnits::formatWithPrivacy(unit, balances.balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelUnconfirmed->setText(BitcoinUnits::formatWithPrivacy(unit, balances.unconfirmed_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelImmature->setText(BitcoinUnits::formatWithPrivacy(unit, balances.immature_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelTotal->setText(BitcoinUnits::formatWithPrivacy(unit, balances.balance + balances.unconfirmed_balance + balances.immature_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelWatchAvailable->setText(BitcoinUnits::formatWithPrivacy(unit, balances.watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelWatchPending->setText(BitcoinUnits::formatWithPrivacy(unit, balances.unconfirmed_watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelWatchImmature->setText(BitcoinUnits::formatWithPrivacy(unit, balances.immature_watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelWatchTotal->setText(BitcoinUnits::formatWithPrivacy(unit, balances.watch_only_balance + balances.unconfirmed_watch_only_balance + balances.immature_watch_only_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); } } else { ui->labelBalance->setText(BitcoinUnits::formatWithPrivacy(unit, balances.balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelUnconfirmed->setText(BitcoinUnits::formatWithPrivacy(unit, balances.unconfirmed_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelImmature->setText(BitcoinUnits::formatWithPrivacy(unit, balances.immature_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); ui->labelTotal->setText(BitcoinUnits::formatWithPrivacy(unit, balances.balance + balances.unconfirmed_balance + balances.immature_balance, BitcoinUnits::SeparatorStyle::ALWAYS, m_privacy)); } // only show immature (newly mined) balance if it's non-zero, so as not to complicate things // for the non-mining users bool showImmature = balances.immature_balance != 0; bool showWatchOnlyImmature = balances.immature_watch_only_balance != 0; // for symmetry reasons also show immature label when the watch-only one is shown ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature); ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature); ui->labelWatchImmature->setVisible(!walletModel->wallet().privateKeysDisabled() && showWatchOnlyImmature); // show watch-only immature balance // MoonBite: refresh the premium hero card with the latest balance + fiat value updateHeroCard();}// show/hide watch-only labelsvoid OverviewPage::updateWatchOnlyLabels(bool showWatchOnly){ ui->labelSpendable->setVisible(showWatchOnly); // show spendable label (only when watch-only is active) ui->labelWatchonly->setVisible(showWatchOnly); // show watch-only label ui->lineWatchBalance->setVisible(showWatchOnly); // show watch-only balance separator line ui->labelWatchAvailable->setVisible(showWatchOnly); // show watch-only available balance ui->labelWatchPending->setVisible(showWatchOnly); // show watch-only pending balance ui->labelWatchTotal->setVisible(showWatchOnly); // show watch-only total balance if (!showWatchOnly) ui->labelWatchImmature->hide();}void OverviewPage::setClientModel(ClientModel *model){ this->clientModel = model; if (model) { // Show warning, for example if this is a prerelease version connect(model, &ClientModel::alertsChanged, this, &OverviewPage::updateAlerts); updateAlerts(model->getStatusBarWarnings()); }}void OverviewPage::setWalletModel(WalletModel *model){ this->walletModel = model; if(model && model->getOptionsModel()) { // Set up transaction list filter.reset(new TransactionFilterProxy()); filter->setSourceModel(model->getTransactionTableModel()); filter->setLimit(NUM_ITEMS); filter->setDynamicSortFilter(true); filter->setSortRole(Qt::EditRole); filter->setShowInactive(false); filter->sort(TransactionTableModel::Date, Qt::DescendingOrder); ui->listTransactions->setModel(filter.get()); ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress); // Keep up to date with wallet interfaces::Wallet& wallet = model->wallet(); interfaces::WalletBalances balances = wallet.getBalances(); setBalance(balances); connect(model, &WalletModel::balanceChanged, this, &OverviewPage::setBalance); connect(model->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &OverviewPage::updateDisplayUnit); updateWatchOnlyLabels(wallet.haveWatchOnly() && !model->wallet().privateKeysDisabled()); connect(model, &WalletModel::notifyWatchonlyChanged, [this](bool showWatchOnly) { updateWatchOnlyLabels(showWatchOnly && !walletModel->wallet().privateKeysDisabled()); }); } // update the display unit, to not use the default ("BTC") updateDisplayUnit();}void OverviewPage::updateDisplayUnit(){ if(walletModel && walletModel->getOptionsModel()) { if (m_balances.balance != -1) { setBalance(m_balances); } // Update txdelegate->unit with the current unit txdelegate->unit = walletModel->getOptionsModel()->getDisplayUnit(); ui->listTransactions->update(); }}void OverviewPage::updateAlerts(const QString &warnings){ this->ui->labelAlerts->setVisible(!warnings.isEmpty()); this->ui->labelAlerts->setText(warnings);}void OverviewPage::showOutOfSyncWarning(bool fShow){ ui->labelWalletStatus->setVisible(fShow); ui->labelTransactionsStatus->setVisible(fShow);}