Skip to content

Commit 2ccf382

Browse files
hebastoapoelstra
authored andcommitted
qt: Fix TxViewDelegate layout
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)
1 parent 5823cc3 commit 2ccf382

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/qt/forms/overviewpage.ui

+3
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@
623623
<property name="horizontalScrollBarPolicy">
624624
<enum>Qt::ScrollBarAlwaysOff</enum>
625625
</property>
626+
<property name="sizeAdjustPolicy">
627+
<enum>QAbstractScrollArea::AdjustToContents</enum>
628+
</property>
626629
<property name="selectionMode">
627630
<enum>QAbstractItemView::NoSelection</enum>
628631
</property>

src/qt/overviewpage.cpp

+29-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <QPainter>
2424
#include <QStatusTipEvent>
2525

26+
#include <algorithm>
27+
#include <map>
28+
2629
#define DECORATION_SIZE 54
2730
#define NUM_ITEMS 5
2831

@@ -36,7 +39,7 @@ class TxViewDelegate : public QAbstractItemDelegate
3639
QAbstractItemDelegate(parent), unit(BitcoinUnits::BTC),
3740
platformStyle(_platformStyle)
3841
{
39-
42+
connect(this, &TxViewDelegate::width_changed, this, &TxViewDelegate::sizeHintChanged);
4043
}
4144

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

7073
painter->setPen(foreground);
7174
QRect boundingRect;
72-
painter->drawText(addressRect, Qt::AlignLeft|Qt::AlignVCenter, address, &boundingRect);
75+
painter->drawText(addressRect, Qt::AlignLeft | Qt::AlignVCenter, address, &boundingRect);
76+
int address_rect_min_width = boundingRect.width();
7377

7478
if (index.data(TransactionTableModel::WatchonlyRole).toBool())
7579
{
7680
QIcon iconWatchonly = qvariant_cast<QIcon>(index.data(TransactionTableModel::WatchonlyDecorationRole));
7781
QRect watchonlyRect(boundingRect.right() + 5, mainRect.top()+ypad+halfheight, 16, halfheight);
7882
iconWatchonly.paint(painter, watchonlyRect);
83+
address_rect_min_width += 5 + watchonlyRect.width();
7984
}
8085

8186
if(amount < 0)
@@ -92,23 +97,41 @@ class TxViewDelegate : public QAbstractItemDelegate
9297
}
9398
painter->setPen(foreground);
9499
QString amountText = index.sibling(index.row(), TransactionTableModel::Amount).data(Qt::DisplayRole).toString();
95-
painter->drawText(amountRect, Qt::AlignRight|Qt::AlignVCenter, amountText);
100+
QRect amount_bounding_rect;
101+
painter->drawText(amountRect, Qt::AlignRight | Qt::AlignVCenter, amountText, &amount_bounding_rect);
96102

97103
painter->setPen(option.palette.color(QPalette::Text));
98-
painter->drawText(amountRect, Qt::AlignLeft|Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
104+
QRect date_bounding_rect;
105+
painter->drawText(amountRect, Qt::AlignLeft | Qt::AlignVCenter, GUIUtil::dateTimeStr(date), &date_bounding_rect);
106+
107+
const int minimum_width = std::max(address_rect_min_width, amount_bounding_rect.width() + date_bounding_rect.width());
108+
const auto search = m_minimum_width.find(index.row());
109+
if (search == m_minimum_width.end() || search->second != minimum_width) {
110+
m_minimum_width[index.row()] = minimum_width;
111+
Q_EMIT width_changed(index);
112+
}
99113

100114
painter->restore();
101115
}
102116

103117
inline QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
104118
{
105-
return QSize(DECORATION_SIZE, DECORATION_SIZE);
119+
const auto search = m_minimum_width.find(index.row());
120+
const int minimum_text_width = search == m_minimum_width.end() ? 0 : search->second;
121+
return {DECORATION_SIZE + 8 + minimum_text_width, DECORATION_SIZE};
106122
}
107123

108124
int unit;
109-
const PlatformStyle *platformStyle;
110125

126+
Q_SIGNALS:
127+
//! An intermediate signal for emitting from the `paint() const` member function.
128+
void width_changed(const QModelIndex& index) const;
129+
130+
private:
131+
const PlatformStyle* platformStyle;
132+
mutable std::map<int, int> m_minimum_width;
111133
};
134+
112135
#include <qt/overviewpage.moc>
113136

114137
OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent) :

0 commit comments

Comments
 (0)