1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Plugin list is now painted with QStyledItemDelegate.

This commit is contained in:
nowrep 2012-04-19 18:07:00 +02:00
parent cdf73b01b5
commit 30f97be3b1
6 changed files with 108 additions and 54 deletions

View File

@ -26,65 +26,105 @@
#include <QScrollBar> #include <QScrollBar>
PluginListDelegate::PluginListDelegate(QListWidget* parent) PluginListDelegate::PluginListDelegate(QListWidget* parent)
: QItemDelegate(parent) : QStyledItemDelegate(parent)
, m_listWidget(parent) , m_rowHeight(0)
{ {
} }
void PluginListDelegate::drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const void PluginListDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) { const QWidget* w = opt.widget;
cg = QPalette::Inactive; const QStyle* style = w ? w->style() : QApplication::style();
} const int height = opt.rect.height();
const int center = height / 2 + opt.rect.top();
if (option.state & QStyle::State_Selected) { // Prepare title font
painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight)); QFont titleFont = opt.font;
painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); titleFont.setBold(true);
} titleFont.setPointSize(titleFont.pointSize() + 1);
else {
painter->setPen(option.palette.color(cg, QPalette::Text));
}
QTextDocument textDocument; const QFontMetrics titleMetrics(titleFont);
textDocument.setHtml(text); const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
QTextLayout textLayout(textDocument.begin()); int leftPosition = m_padding;
textLayout.setFont(option.font); int rightPosition = opt.rect.right() - m_padding;
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; // Draw background
QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
textLayout.beginLayout(); // Draw checkbox
qreal height = 0; const int checkboxSize = 18;
QTextLine line = textLayout.createLine(); const int checkboxYPos = center - (checkboxSize / 2);
QStyleOptionViewItemV4 opt2 = opt;
opt2.rect = QRect(leftPosition, checkboxYPos, checkboxSize, checkboxSize);
opt2.checkState == Qt::Checked ? opt2.state |= QStyle::State_On : opt2.state |= QStyle::State_Off;
style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt2, painter, w);
leftPosition = opt2.rect.right() + m_padding;
while (line.isValid()) { // Draw icon
line.setLineWidth(textRect.width()); const int iconSize = 32;
height += 3; const int iconYPos = center - (iconSize / 2);
line.setPosition(QPoint(0, height)); QRect iconRect(leftPosition, iconYPos, iconSize, iconSize);
height += line.height(); QPixmap pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(iconSize);
painter->drawPixmap(iconRect, pixmap);
leftPosition = iconRect.right() + m_padding;
line = textLayout.createLine(); // Draw plugin name
} const QString &name = index.data(Qt::DisplayRole).toString();
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);
style->drawItemText(painter, nameRect, Qt::AlignLeft, opt.palette, true, name, colorRole);
textLayout.endLayout(); // Draw version
textLayout.draw(painter, QPointF(textRect.left(), textRect.top())); const QString &version = index.data(Qt::UserRole).toString();
QRect versionRect(nameRect.x() + leftPosForVersion, nameRect.y(), rightTitleEdge - leftPosForVersion, titleMetrics.height());
QFont versionFont = titleFont;
versionFont.setBold(false);
painter->setFont(versionFont);
style->drawItemText(painter, versionRect, Qt::AlignLeft, opt.palette, true, version, colorRole);
// Draw info
const int infoYPos = nameRect.bottom() + opt.fontMetrics.leading();
QRect infoRect(nameRect.x(), infoYPos, nameRect.width(), opt.fontMetrics.height());
const QString &info = opt.fontMetrics.elidedText(index.data(Qt::UserRole + 1).toString(), Qt::ElideRight, infoRect.width());
painter->setFont(opt.font);
style->drawItemText(painter, infoRect, Qt::TextSingleLine | Qt::AlignLeft, opt.palette, true, info, colorRole);
// Draw description
const int descriptionYPos = infoRect.bottom() + opt.fontMetrics.leading();
QRect descriptionRect(infoRect.x(), descriptionYPos, infoRect.width(), opt.fontMetrics.height());
const QString &description = opt.fontMetrics.elidedText(index.data(Qt::UserRole + 2).toString(), Qt::ElideRight, descriptionRect.width());
style->drawItemText(painter, descriptionRect, Qt::TextSingleLine | Qt::AlignLeft, opt.palette, true, description, colorRole);
} }
QSize PluginListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const QSize PluginListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
Q_UNUSED(index) Q_UNUSED(index)
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; if (!m_rowHeight) {
const int scrollBarSize = m_listWidget->verticalScrollBar()->isVisible() ? m_listWidget->verticalScrollBar()->width() : 0; QStyleOptionViewItemV4 opt(option);
initStyleOption(&opt, index);
QSize size; const QWidget* w = opt.widget;
size.setWidth(m_listWidget->width() - 10 - scrollBarSize); const QStyle* style = w ? w->style() : QApplication::style();
const int padding = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1;
// ( height of font * 3 = 3 lines ) + ( text margins ) + ( 2 free lines = every line is 3px ) QFont titleFont = opt.font;
size.setHeight((option.fontMetrics.height() * 3) + (textMargin * 2) + (2 * 3)); titleFont.setBold(true);
titleFont.setPointSize(titleFont.pointSize() + 1);
return size; 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();
}
return QSize(200, m_rowHeight);
} }

View File

@ -18,22 +18,23 @@
#ifndef PLUGINLISTDELEGATE_H #ifndef PLUGINLISTDELEGATE_H
#define PLUGINLISTDELEGATE_H #define PLUGINLISTDELEGATE_H
#include <QItemDelegate> #include <QStyledItemDelegate>
#include "qz_namespace.h" #include "qz_namespace.h"
class QListWidget; class QListWidget;
class QT_QUPZILLA_EXPORT PluginListDelegate : public QItemDelegate class QT_QUPZILLA_EXPORT PluginListDelegate : public QStyledItemDelegate
{ {
public: public:
PluginListDelegate(QListWidget* parent); PluginListDelegate(QListWidget* parent);
void drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const; void paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
private: private:
QListWidget* m_listWidget; mutable int m_rowHeight;
mutable int m_padding;
}; };
#endif // PLUGINLISTDELEGATE_H #endif // PLUGINLISTDELEGATE_H

View File

@ -24,6 +24,7 @@
#include "settings.h" #include "settings.h"
#include <QInputDialog> #include <QInputDialog>
#include <QTextDocument>
#include <QMessageBox> #include <QMessageBox>
#include <QTimer> #include <QTimer>
@ -156,16 +157,20 @@ void PluginsList::refresh()
PluginSpec spec = plugin.pluginSpec; PluginSpec spec = plugin.pluginSpec;
QListWidgetItem* item = new QListWidgetItem(ui->list); QListWidgetItem* item = new QListWidgetItem(ui->list);
QString pluginInfo = QString("<b>%1</b> %2 (%3)<br/>\n%4<br/>\n%5\n").arg(spec.name, spec.version, spec.author, spec.info, spec.description);
item->setText(pluginInfo);
QIcon icon = QIcon(spec.icon); QIcon icon = QIcon(spec.icon);
if (icon.isNull()) { if (icon.isNull()) {
icon = QIcon(":/icons/preferences/extension.png"); icon = QIcon(":/icons/preferences/extension.png");
} }
item->setIcon(icon); item->setIcon(icon);
QString pluginInfo = QString("<b>%1</b> %2<br/><i>%3</i><br/>%4").arg(spec.name, spec.version, Qt::escape(spec.author), spec.info);
item->setToolTip(pluginInfo);
item->setText(spec.name);
item->setData(Qt::UserRole, spec.version);
item->setData(Qt::UserRole + 1, spec.info);
item->setData(Qt::UserRole + 2, spec.description);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(plugin.isLoaded() ? Qt::Checked : Qt::Unchecked); item->setCheckState(plugin.isLoaded() ? Qt::Checked : Qt::Unchecked);
item->setData(Qt::UserRole + 10, qVariantFromValue(plugin)); item->setData(Qt::UserRole + 10, qVariantFromValue(plugin));

View File

@ -14,6 +14,9 @@
<string notr="true"/> <string notr="true"/>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="margin">
<number>0</number>
</property>
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
@ -34,6 +37,9 @@
<item> <item>
<widget class="QFrame" name="verticalFrame"> <widget class="QFrame" name="verticalFrame">
<layout class="QVBoxLayout" name="appExtensionFrame"> <layout class="QVBoxLayout" name="appExtensionFrame">
<property name="margin">
<number>0</number>
</property>
<item> <item>
<widget class="QListWidget" name="list"> <widget class="QListWidget" name="list">
<property name="alternatingRowColors"> <property name="alternatingRowColors">

View File

@ -20,7 +20,9 @@
#include <QLineEdit> #include <QLineEdit>
class FocusSelectLineEdit : public QLineEdit #include "qz_namespace.h"
class QT_QUPZILLA_EXPORT FocusSelectLineEdit : public QLineEdit
{ {
Q_OBJECT Q_OBJECT
public: public: