2012-02-29 18:33:50 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <QTextLayout>
|
|
|
|
#include <QTextDocument>
|
|
|
|
#include <QTextBlock>
|
|
|
|
#include <QApplication>
|
|
|
|
|
2012-02-25 12:34:16 +01:00
|
|
|
PluginListDelegate::PluginListDelegate(QListWidget* parent)
|
|
|
|
: QItemDelegate(parent)
|
|
|
|
, m_listWidget(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginListDelegate::drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
|
|
|
|
{
|
2012-03-05 23:34:00 +01:00
|
|
|
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) {
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option.state & QStyle::State_Selected) {
|
|
|
|
painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight));
|
|
|
|
painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
painter->setPen(option.palette.color(cg, QPalette::Text));
|
|
|
|
}
|
|
|
|
|
2012-02-25 12:34:16 +01:00
|
|
|
QTextDocument textDocument;
|
|
|
|
textDocument.setHtml(text);
|
|
|
|
|
|
|
|
QTextLayout textLayout(textDocument.begin());
|
|
|
|
textLayout.setFont(option.font);
|
|
|
|
|
|
|
|
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1;
|
|
|
|
QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
|
|
|
|
|
|
|
|
textLayout.beginLayout();
|
|
|
|
qreal height = 0;
|
2012-03-05 23:34:00 +01:00
|
|
|
QTextLine line = textLayout.createLine();
|
2012-02-25 12:34:16 +01:00
|
|
|
|
2012-03-05 23:34:00 +01:00
|
|
|
while (line.isValid()) {
|
2012-02-25 12:34:16 +01:00
|
|
|
line.setLineWidth(textRect.width());
|
|
|
|
height += 3;
|
|
|
|
line.setPosition(QPoint(0, height));
|
|
|
|
height += line.height();
|
2012-03-05 23:34:00 +01:00
|
|
|
|
|
|
|
line = textLayout.createLine();
|
2012-02-25 12:34:16 +01:00
|
|
|
}
|
|
|
|
|
2012-03-05 23:34:00 +01:00
|
|
|
textLayout.endLayout();
|
2012-02-25 12:34:16 +01:00
|
|
|
textLayout.draw(painter, QPointF(textRect.left(), textRect.top()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize PluginListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
2012-03-07 21:28:31 +01:00
|
|
|
Q_UNUSED(index)
|
|
|
|
|
2012-03-05 23:34:00 +01:00
|
|
|
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1;
|
|
|
|
|
2012-03-06 15:28:52 +01:00
|
|
|
QSize size;
|
2012-03-05 23:34:00 +01:00
|
|
|
size.setWidth(m_listWidget->width() - 10);
|
|
|
|
|
|
|
|
// ( height of font * 3 = 3 lines ) + ( text margins ) + ( 2 free lines = every line is 3px )
|
|
|
|
size.setHeight((option.fontMetrics.height() * 3) + (textMargin * 2) + (2 * 3));
|
2012-02-25 12:34:16 +01:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|