2012-02-29 18:33:50 +01:00
|
|
|
/* ============================================================
|
2017-02-07 15:47:41 +01:00
|
|
|
* QupZilla - Qt web browser
|
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2012-02-29 18:33:50 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2012-02-25 12:34:16 +01:00
|
|
|
#include "pluginlistdelegate.h"
|
|
|
|
|
2012-03-05 23:34:00 +01:00
|
|
|
#include <QPainter>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QListWidget>
|
|
|
|
#include <QApplication>
|
|
|
|
|
2012-02-25 12:34:16 +01:00
|
|
|
PluginListDelegate::PluginListDelegate(QListWidget* parent)
|
2012-04-19 18:07:00 +02:00
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
, m_rowHeight(0)
|
2012-07-08 17:57:06 +02:00
|
|
|
, m_padding(0)
|
2012-02-25 12:34:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-19 18:07:00 +02:00
|
|
|
void PluginListDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
2012-02-25 12:34:16 +01:00
|
|
|
{
|
2016-04-24 10:17:13 +02:00
|
|
|
QStyleOptionViewItem opt = option;
|
2012-04-19 18:07:00 +02:00
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
const QWidget* w = opt.widget;
|
|
|
|
const QStyle* style = w ? w->style() : QApplication::style();
|
|
|
|
const int height = opt.rect.height();
|
|
|
|
const int center = height / 2 + opt.rect.top();
|
|
|
|
|
2013-07-11 18:18:32 +02:00
|
|
|
painter->setLayoutDirection(Qt::LeftToRight);
|
|
|
|
|
2012-04-19 18:07:00 +02:00
|
|
|
// Prepare title font
|
|
|
|
QFont titleFont = opt.font;
|
|
|
|
titleFont.setBold(true);
|
|
|
|
titleFont.setPointSize(titleFont.pointSize() + 1);
|
|
|
|
|
|
|
|
const QFontMetrics titleMetrics(titleFont);
|
|
|
|
const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
|
2016-12-26 19:40:55 +01:00
|
|
|
|
|
|
|
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
|
|
|
|
if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) {
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
}
|
2017-02-07 15:47:41 +01:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
opt.palette.setColor(QPalette::All, QPalette::HighlightedText, opt.palette.color(QPalette::Active, QPalette::Text));
|
|
|
|
opt.palette.setColor(QPalette::All, QPalette::Highlight, opt.palette.base().color().darker(108));
|
|
|
|
#endif
|
|
|
|
|
2016-12-26 19:40:55 +01:00
|
|
|
QPalette textPalette = opt.palette;
|
|
|
|
textPalette.setCurrentColorGroup(cg);
|
2012-04-19 18:07:00 +02:00
|
|
|
|
|
|
|
int leftPosition = m_padding;
|
|
|
|
int rightPosition = opt.rect.right() - m_padding;
|
|
|
|
|
|
|
|
// Draw background
|
|
|
|
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
|
|
|
|
|
|
|
|
// Draw checkbox
|
|
|
|
const int checkboxSize = 18;
|
|
|
|
const int checkboxYPos = center - (checkboxSize / 2);
|
2016-04-24 10:17:13 +02:00
|
|
|
QStyleOptionViewItem opt2 = opt;
|
2012-04-19 18:07:00 +02:00
|
|
|
opt2.checkState == Qt::Checked ? opt2.state |= QStyle::State_On : opt2.state |= QStyle::State_Off;
|
2012-04-20 14:03:08 +02:00
|
|
|
QRect styleCheckBoxRect = style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt2, w);
|
2013-07-11 18:18:32 +02:00
|
|
|
opt2.rect = QRect(leftPosition, checkboxYPos, styleCheckBoxRect.width(), styleCheckBoxRect.height());
|
2012-04-19 18:07:00 +02:00
|
|
|
style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt2, painter, w);
|
2013-07-11 18:18:32 +02:00
|
|
|
leftPosition = opt2.rect.right() + m_padding;
|
2012-04-19 18:07:00 +02:00
|
|
|
|
|
|
|
// Draw icon
|
|
|
|
const int iconSize = 32;
|
|
|
|
const int iconYPos = center - (iconSize / 2);
|
|
|
|
QRect iconRect(leftPosition, iconYPos, iconSize, iconSize);
|
|
|
|
QPixmap pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(iconSize);
|
2013-07-11 18:18:32 +02:00
|
|
|
painter->drawPixmap(iconRect, pixmap);
|
2012-04-19 18:07:00 +02:00
|
|
|
leftPosition = iconRect.right() + m_padding;
|
|
|
|
|
|
|
|
// Draw plugin name
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString name = index.data(Qt::DisplayRole).toString();
|
2012-04-19 18:07:00 +02:00
|
|
|
const int leftTitleEdge = leftPosition + 2;
|
|
|
|
const int rightTitleEdge = rightPosition - m_padding;
|
|
|
|
const int leftPosForVersion = titleMetrics.width(name) + m_padding;
|
|
|
|
QRect nameRect(leftTitleEdge, opt.rect.top() + m_padding, rightTitleEdge - leftTitleEdge, titleMetrics.height());
|
|
|
|
painter->setFont(titleFont);
|
2016-12-26 19:40:55 +01:00
|
|
|
style->drawItemText(painter, nameRect, Qt::AlignLeft, textPalette, true, name, colorRole);
|
2012-04-19 18:07:00 +02:00
|
|
|
|
|
|
|
// Draw version
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString version = index.data(Qt::UserRole).toString();
|
2012-04-19 18:07:00 +02:00
|
|
|
QRect versionRect(nameRect.x() + leftPosForVersion, nameRect.y(), rightTitleEdge - leftPosForVersion, titleMetrics.height());
|
|
|
|
QFont versionFont = titleFont;
|
|
|
|
versionFont.setBold(false);
|
|
|
|
painter->setFont(versionFont);
|
2016-12-26 19:40:55 +01:00
|
|
|
style->drawItemText(painter, versionRect, Qt::AlignLeft, textPalette, true, version, colorRole);
|
2012-04-19 18:07:00 +02:00
|
|
|
|
|
|
|
// Draw info
|
|
|
|
const int infoYPos = nameRect.bottom() + opt.fontMetrics.leading();
|
2013-07-11 18:18:32 +02:00
|
|
|
QRect infoRect(nameRect.x(), infoYPos, nameRect.width(), opt.fontMetrics.height());
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString info = opt.fontMetrics.elidedText(index.data(Qt::UserRole + 1).toString(), Qt::ElideRight, infoRect.width());
|
2012-04-19 18:07:00 +02:00
|
|
|
painter->setFont(opt.font);
|
2016-12-26 19:40:55 +01:00
|
|
|
style->drawItemText(painter, infoRect, Qt::TextSingleLine | Qt::AlignLeft, textPalette, true, info, colorRole);
|
2012-04-19 18:07:00 +02:00
|
|
|
|
|
|
|
// Draw description
|
|
|
|
const int descriptionYPos = infoRect.bottom() + opt.fontMetrics.leading();
|
|
|
|
QRect descriptionRect(infoRect.x(), descriptionYPos, infoRect.width(), opt.fontMetrics.height());
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString description = opt.fontMetrics.elidedText(index.data(Qt::UserRole + 2).toString(), Qt::ElideRight, descriptionRect.width());
|
2016-12-26 19:40:55 +01:00
|
|
|
style->drawItemText(painter, descriptionRect, Qt::TextSingleLine | Qt::AlignLeft, textPalette, true, description, colorRole);
|
2012-02-25 12:34:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize PluginListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
2012-03-07 21:28:31 +01:00
|
|
|
Q_UNUSED(index)
|
|
|
|
|
2012-04-19 18:07:00 +02:00
|
|
|
if (!m_rowHeight) {
|
2016-04-24 10:17:13 +02:00
|
|
|
QStyleOptionViewItem opt(option);
|
2012-04-19 18:07:00 +02:00
|
|
|
initStyleOption(&opt, index);
|
2012-03-05 23:34:00 +01:00
|
|
|
|
2012-04-19 18:07:00 +02:00
|
|
|
const QWidget* w = opt.widget;
|
|
|
|
const QStyle* style = w ? w->style() : QApplication::style();
|
|
|
|
const int padding = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1;
|
2012-03-05 23:34:00 +01:00
|
|
|
|
2012-04-19 18:07:00 +02:00
|
|
|
QFont titleFont = opt.font;
|
|
|
|
titleFont.setBold(true);
|
|
|
|
titleFont.setPointSize(titleFont.pointSize() + 1);
|
|
|
|
|
|
|
|
m_padding = padding > 5 ? padding : 5;
|
|
|
|
|
|
|
|
const QFontMetrics titleMetrics(titleFont);
|
|
|
|
|
|
|
|
m_rowHeight = 2 * m_padding + 2 * opt.fontMetrics.leading() + 2 * opt.fontMetrics.height() + titleMetrics.height();
|
|
|
|
}
|
2012-02-25 12:34:16 +01:00
|
|
|
|
2012-04-19 18:07:00 +02:00
|
|
|
return QSize(200, m_rowHeight);
|
2012-02-25 12:34:16 +01:00
|
|
|
}
|