Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Fix marker generation on HiDPI displays #54

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions src/listview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ void ListViewDelegate::paintLog(QPainter* p, const QStyleOptionViewItem& opt,
}
QStyleOptionViewItem newOpt(opt); // we need a copy
if (pm) {
p->drawPixmap(newOpt.rect.x(), newOpt.rect.y() + 1, *pm); // +1 means leave a pixel spacing above the pixmap
newOpt.rect.adjust(pm->width(), 0, 0, 0);
p->drawPixmap(newOpt.rect.x(), newOpt.rect.y() + 1, *pm); // +1 means leave a pixel spacing above the pixmap
newOpt.rect.adjust(pm->width() / dpr(), 0, 0, 0);
delete pm;
}
if (isHighlighted)
Expand Down Expand Up @@ -874,36 +874,52 @@ QString ListView::refNameAt(const QPoint &pos)
return QString();
}

/*
* Return the device pixel ratio
*/
const qreal ListViewDelegate::dpr(void) const {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be a private method on the class. It can very well be a local function and releave a tiny bit of the burden from the compiler.

#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
return qApp->devicePixelRatio();
#else
return 1.0;
#endif
}

void ListViewDelegate::addTextPixmap(QPixmap** pp, SCRef txt, const QStyleOptionViewItem& opt) const {

QPixmap* pm = *pp;
int ofs = pm->isNull() ? 0 : pm->width() + 2;
int spacing = 4;
QFontMetrics fm(opt.font);
int pw = fm.boundingRect(txt).width() + 2 * spacing;
int ph = fm.height();

QSize pixmapSize(ofs + pw, ph);
const unsigned int mark_spacing = 2; // Space between markers in pixels
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is highly preferable to avoid mixing functional changes and whitespace or code cleanup (like changing variable names without changing their use).

unsigned int offset = pm->isNull() ? 0 : (pm->width() / dpr()) + mark_spacing; // Marker's offset in the base pixmap

QFontMetrics fm(opt.font);
const unsigned int text_spacing = 4;
unsigned int text_width = fm.boundingRect(txt).width() + 2 * text_spacing;
unsigned int text_height = fm.height();

// Define size of the new Pixmap
QSize pixmapSize((offset + text_width) * dpr(),
(text_height) * dpr());

QPixmap* newPm = new QPixmap(pixmapSize);
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
qreal dpr = qApp->devicePixelRatio();
QPixmap* newPm = new QPixmap(pixmapSize * dpr);
newPm->setDevicePixelRatio(dpr);
#else
QPixmap* newPm = new QPixmap(pixmapSize);
newPm->setDevicePixelRatio(dpr());
#endif

QPainter p;
p.begin(newPm);

if (!pm->isNull()) {
newPm->fill(opt.palette.base().color());
p.drawPixmap(0, 0, *pm);
}

p.setPen(opt.palette.color(QPalette::WindowText));
p.setBrush(opt.palette.color(QPalette::Window));
p.setFont(opt.font);
p.drawRect(ofs, 0, pw - 1, ph - 1);
p.drawText(ofs + spacing, fm.ascent(), txt);
p.setFont(opt.font);

p.drawRect(offset, 0, text_width - 1, text_height - 1);
p.drawText(offset + text_spacing, fm.ascent(), txt);
p.end();

delete pm;
Expand Down
1 change: 1 addition & 0 deletions src/listview.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public slots:
QPixmap* getTagMarks(SCRef sha, const QStyleOptionViewItem& opt) const;
void addTextPixmap(QPixmap** pp, SCRef txt, const QStyleOptionViewItem& opt) const;
bool changedFiles(SCRef sha) const;
const qreal dpr(void) const;

Git* git;
ListViewProxy* lp;
Expand Down