From 32b3ed0a1362a4b0798ad71fac3450fb77cb7e41 Mon Sep 17 00:00:00 2001 From: Thomas Groman Date: Thu, 19 Sep 2019 00:41:48 -0700 Subject: merged from 0.6.7 codebase --- application/widgets/VersionListView.cpp | 185 +++++++++++++++----------------- 1 file changed, 85 insertions(+), 100 deletions(-) (limited to 'application/widgets/VersionListView.cpp') diff --git a/application/widgets/VersionListView.cpp b/application/widgets/VersionListView.cpp index 8c80ecf3..09df75b7 100644 --- a/application/widgets/VersionListView.cpp +++ b/application/widgets/VersionListView.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,155 +22,140 @@ #include "Common.h" VersionListView::VersionListView(QWidget *parent) - :QTreeView ( parent ) + :QTreeView ( parent ) { - m_emptyString = tr("No versions are currently available."); + m_emptyString = tr("No versions are currently available."); } void VersionListView::rowsInserted(const QModelIndex &parent, int start, int end) { - if(!m_itemCount) - viewport()->update(); - m_itemCount += end-start+1; - QTreeView::rowsInserted(parent, start, end); + m_itemCount += end-start+1; + updateEmptyViewPort(); + QTreeView::rowsInserted(parent, start, end); } void VersionListView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) { - m_itemCount -= end-start+1; - if(!m_itemCount) - viewport()->update(); - QTreeView::rowsInserted(parent, start, end); + m_itemCount -= end-start+1; + updateEmptyViewPort(); + QTreeView::rowsInserted(parent, start, end); } void VersionListView::setModel(QAbstractItemModel *model) { - m_itemCount = model->rowCount(); - if(!m_itemCount) - viewport()->update(); - QTreeView::setModel(model); + m_itemCount = model->rowCount(); + updateEmptyViewPort(); + QTreeView::setModel(model); } void VersionListView::reset() { - if(model()) - { - m_itemCount = model()->rowCount(); - } - viewport()->update(); - QTreeView::reset(); + if(model()) + { + m_itemCount = model()->rowCount(); + } + else { + m_itemCount = 0; + } + updateEmptyViewPort(); + QTreeView::reset(); } void VersionListView::setEmptyString(QString emptyString) { - m_emptyString = emptyString; - updateEmptyViewPort(); + m_emptyString = emptyString; + updateEmptyViewPort(); } void VersionListView::setEmptyErrorString(QString emptyErrorString) { - m_emptyErrorString = emptyErrorString; - updateEmptyViewPort(); + m_emptyErrorString = emptyErrorString; + updateEmptyViewPort(); } void VersionListView::setEmptyMode(VersionListView::EmptyMode mode) { - m_emptyMode = mode; - updateEmptyViewPort(); + m_emptyMode = mode; + updateEmptyViewPort(); } void VersionListView::updateEmptyViewPort() { - if(!m_itemCount) - { - viewport()->update(); - } + setAccessibleDescription(currentEmptyString()); + + if(!m_itemCount) + { + viewport()->update(); + } } void VersionListView::paintEvent(QPaintEvent *event) { - if(m_itemCount) - { - QTreeView::paintEvent(event); - } - else - { - paintInfoLabel(event); - } + if(m_itemCount) + { + QTreeView::paintEvent(event); + } + else + { + paintInfoLabel(event); + } } -void VersionListView::paintInfoLabel(QPaintEvent *event) +QString VersionListView::currentEmptyString() const { - QString emptyString; - switch(m_emptyMode) - { - case VersionListView::Empty: - return; - case VersionListView::String: - emptyString = m_emptyString; - break; - case VersionListView::ErrorString: - emptyString = m_emptyErrorString; - break; - } + if(m_itemCount) { + return QString(); + } + switch(m_emptyMode) + { + default: + case VersionListView::Empty: + return QString(); + case VersionListView::String: + return m_emptyString; + case VersionListView::ErrorString: + return m_emptyErrorString; + } +} + + +void VersionListView::paintInfoLabel(QPaintEvent *event) const +{ + QString emptyString = currentEmptyString(); + //calculate the rect for the overlay QPainter painter(viewport()); painter.setRenderHint(QPainter::Antialiasing, true); - QFont font("sans", 20); + QFont font("sans", 20); font.setBold(true); - - QRect bounds = viewport()->geometry(); - bounds.moveTop(0); - QTextLayout layout(emptyString, font); - qreal height = 0.0; - qreal widthUsed = 0.0; - QStringList lines = viewItemTextLayout(layout, bounds.width() - 20, height, widthUsed); - QRect rect (0,0, widthUsed, height); - rect.setWidth(rect.width()+20); - rect.setHeight(rect.height()+20); - rect.moveCenter(bounds.center()); + + QRect bounds = viewport()->geometry(); + bounds.moveTop(0); + auto innerBounds = bounds; + innerBounds.adjust(10, 10, -10, -10); + + QColor background = QApplication::palette().color(QPalette::Foreground); + QColor foreground = QApplication::palette().color(QPalette::Base); + foreground.setAlpha(190); + painter.setFont(font); + auto fontMetrics = painter.fontMetrics(); + auto textRect = fontMetrics.boundingRect(innerBounds, Qt::AlignHCenter | Qt::TextWordWrap, emptyString); + textRect.moveCenter(bounds.center()); + + auto wrapRect = textRect; + wrapRect.adjust(-10, -10, 10, 10); + //check if we are allowed to draw in our area - if (!event->rect().intersects(rect)) { + if (!event->rect().intersects(wrapRect)) { return; } - //draw the letter of the topmost item semitransparent in the middle - QColor background = QApplication::palette().color(QPalette::Foreground); - QColor foreground = QApplication::palette().color(QPalette::Base); - /* - background.setAlpha(128 - scrollFade); - foreground.setAlpha(128 - scrollFade); - */ + painter.setBrush(QBrush(background)); painter.setPen(foreground); - painter.drawRoundedRect(rect, 5.0, 5.0); - foreground.setAlpha(190); + painter.drawRoundedRect(wrapRect, 5.0, 5.0); + painter.setPen(foreground); painter.setFont(font); - painter.drawText(rect, Qt::AlignCenter, lines.join("\n")); - -} - -/* -void ModListView::setModel ( QAbstractItemModel* model ) -{ - QTreeView::setModel ( model ); - auto head = header(); - head->setStretchLastSection(false); - // HACK: this is true for the checkbox column of mod lists - auto string = model->headerData(0,head->orientation()).toString(); - if(!string.size()) - { - head->setSectionResizeMode(0, QHeaderView::ResizeToContents); - head->setSectionResizeMode(1, QHeaderView::Stretch); - for(int i = 2; i < head->count(); i++) - head->setSectionResizeMode(i, QHeaderView::ResizeToContents); - } - else - { - head->setSectionResizeMode(0, QHeaderView::Stretch); - for(int i = 1; i < head->count(); i++) - head->setSectionResizeMode(i, QHeaderView::ResizeToContents); - } + painter.drawText(textRect, Qt::AlignHCenter | Qt::TextWordWrap, emptyString); } -*/ -- cgit v1.2.3