Skip to content

Commit

Permalink
qt: Fix TxViewDelegate layout
Browse files Browse the repository at this point in the history
This change (1) prevents overlapping date and amount strings,
and (2) guaranties that "eye" sign at the end of the watch-only
address/label is always visible.

Github-Pull: bitcoin-core/gui#176
Rebased-From: f0d0479
(cherry picked from commit 7bc4498)
  • Loading branch information
hebasto authored and apoelstra committed Sep 21, 2021
1 parent e640918 commit 4630083
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/qt/forms/overviewpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,9 @@
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
Expand Down
35 changes: 29 additions & 6 deletions src/qt/overviewpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <QPainter>
#include <QStatusTipEvent>

#include <algorithm>
#include <map>

#define DECORATION_SIZE 54
#define NUM_ITEMS 5

Expand All @@ -36,7 +39,7 @@ class TxViewDelegate : public QAbstractItemDelegate
QAbstractItemDelegate(parent), unit(BitcoinUnits::BTC),
platformStyle(_platformStyle)
{

connect(this, &TxViewDelegate::width_changed, this, &TxViewDelegate::sizeHintChanged);
}

inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
Expand Down Expand Up @@ -69,13 +72,15 @@ class TxViewDelegate : public QAbstractItemDelegate

painter->setPen(foreground);
QRect boundingRect;
painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address, &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)
Expand All @@ -92,23 +97,41 @@ class TxViewDelegate : public QAbstractItemDelegate
}
painter->setPen(foreground);
QString amountText = index.sibling(index.row(), TransactionTableModel::Amount).data(Qt::DisplayRole).toString();
painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
QRect amount_bounding_rect;
painter->drawText(amountRect, Qt::AlignRight | Qt::AlignVCenter, amountText, &amount_bounding_rect);

painter->setPen(option.palette.color(QPalette::Text));
painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
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
{
return QSize(DECORATION_SIZE, DECORATION_SIZE);
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;
const PlatformStyle *platformStyle;

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) :
Expand Down

0 comments on commit 4630083

Please sign in to comment.