From f27137f8ae6ad2a36bca7fa7b0e111d87dcd058b Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 28 Dec 2012 01:59:39 +0100 Subject: [PATCH] Fixed rendering issues in ListWidget in SiteInfo It should now be rendered well with all possible font sizes. Closes #663 --- src/lib/lib.pro | 6 +- src/lib/preferences/pluginlistdelegate.h | 2 +- src/lib/tools/listitemdelegate.cpp | 88 ++++++++++++++++++++++++ src/lib/tools/listitemdelegate.h | 41 +++++++++++ src/lib/webview/siteinfo.cpp | 6 ++ src/lib/webview/siteinfo.ui | 16 +---- 6 files changed, 142 insertions(+), 17 deletions(-) create mode 100644 src/lib/tools/listitemdelegate.cpp create mode 100644 src/lib/tools/listitemdelegate.h diff --git a/src/lib/lib.pro b/src/lib/lib.pro index 8586d6605..03e42dbf5 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -194,7 +194,8 @@ SOURCES += \ network/schemehandlers/qupzillaschemehandler.cpp \ network/schemehandlers/adblockschemehandler.cpp \ network/schemehandlers/fileschemehandler.cpp \ - other/registerqappassociation.cpp + other/registerqappassociation.cpp \ + tools/listitemdelegate.cpp HEADERS += \ webview/tabpreview.h \ @@ -351,7 +352,8 @@ HEADERS += \ network/schemehandlers/qupzillaschemehandler.h \ network/schemehandlers/adblockschemehandler.h \ network/schemehandlers/fileschemehandler.h \ - other/registerqappassociation.h + other/registerqappassociation.h \ + tools/listitemdelegate.h FORMS += \ preferences/autofillmanager.ui \ diff --git a/src/lib/preferences/pluginlistdelegate.h b/src/lib/preferences/pluginlistdelegate.h index 7d4ef7b22..c4296fd89 100644 --- a/src/lib/preferences/pluginlistdelegate.h +++ b/src/lib/preferences/pluginlistdelegate.h @@ -27,7 +27,7 @@ class QListWidget; class QT_QUPZILLA_EXPORT PluginListDelegate : public QStyledItemDelegate { public: - PluginListDelegate(QListWidget* parent); + explicit PluginListDelegate(QListWidget* parent); void paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; diff --git a/src/lib/tools/listitemdelegate.cpp b/src/lib/tools/listitemdelegate.cpp new file mode 100644 index 000000000..80c069532 --- /dev/null +++ b/src/lib/tools/listitemdelegate.cpp @@ -0,0 +1,88 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2010-2012 David Rosca +* +* 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 . +* ============================================================ */ +#include "listitemdelegate.h" + +#include +#include + +ListItemDelegate::ListItemDelegate(int iconSize, QWidget* parent) + : QStyledItemDelegate(parent) + , m_iconSize(iconSize) + , m_itemHeight(0) + , m_padding(0) +{ +} + +int ListItemDelegate::itemHeight() const +{ + return m_itemHeight; +} + +void ListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QStyleOptionViewItemV4 opt = option; + initStyleOption(&opt, index); + + const QWidget* w = opt.widget; + const QStyle* style = w ? w->style() : QApplication::style(); + const Qt::LayoutDirection direction = w ? w->layoutDirection() : QApplication::layoutDirection(); + +#ifdef Q_OS_WIN + const QPalette::ColorRole colorRole = QPalette::Text; +#else + const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text; +#endif + + int topPosition = opt.rect.top() + m_padding; + + // Draw background + style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w); + + // Draw icon + QRect iconRect(opt.rect.left() + (opt.rect.width() - m_iconSize) / 2, topPosition, m_iconSize, m_iconSize); + QRect visualIconRect = style->visualRect(direction, opt.rect, iconRect); + QPixmap pixmap = index.data(Qt::DecorationRole).value().pixmap(m_iconSize); + painter->drawPixmap(visualIconRect, pixmap); + topPosition += m_iconSize + m_padding; + + // Draw title + const QString &title = index.data(Qt::DisplayRole).toString(); + const int leftTitleEdge = opt.rect.left() + m_padding; + QRect titleRect(leftTitleEdge, topPosition, opt.rect.width() - 2 * m_padding, opt.fontMetrics.height()); + QRect visualTitleRect = style->visualRect(direction, opt.rect, titleRect); + style->drawItemText(painter, visualTitleRect, Qt::AlignCenter, opt.palette, true, title, colorRole); +} + +QSize ListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + if (!m_itemHeight) { + QStyleOptionViewItemV4 opt(option); + initStyleOption(&opt, index); + + const QWidget* w = opt.widget; + const QStyle* style = w ? w->style() : QApplication::style(); + const int padding = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; + + m_padding = padding > 5 ? padding : 5; + + m_itemHeight = 3 * m_padding + opt.fontMetrics.height() + m_iconSize; + } + + int width = 2 * m_padding + option.fontMetrics.width(index.data(Qt::DisplayRole).toString()); + return QSize(width > (m_iconSize + 2 * m_padding) ? width : m_iconSize + 2 * m_padding, m_itemHeight); +} diff --git a/src/lib/tools/listitemdelegate.h b/src/lib/tools/listitemdelegate.h new file mode 100644 index 000000000..7add3305f --- /dev/null +++ b/src/lib/tools/listitemdelegate.h @@ -0,0 +1,41 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2010-2012 David Rosca +* +* 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 . +* ============================================================ */ +#ifndef LISTITEMDELEGATE_H +#define LISTITEMDELEGATE_H + +#include + +#include "qz_namespace.h" + +class QT_QUPZILLA_EXPORT ListItemDelegate : public QStyledItemDelegate +{ +public: + explicit ListItemDelegate(int iconSize, QWidget* parent); + + int itemHeight() const; + + void paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; + +private: + int m_iconSize; + mutable int m_itemHeight; + mutable int m_padding; +}; + +#endif // LISTITEMDELEGATE_H diff --git a/src/lib/webview/siteinfo.cpp b/src/lib/webview/siteinfo.cpp index 3ea712091..d4dfa6f42 100644 --- a/src/lib/webview/siteinfo.cpp +++ b/src/lib/webview/siteinfo.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "siteinfo.h" #include "ui_siteinfo.h" +#include "listitemdelegate.h" #include "webview.h" #include "webpage.h" #include "mainapplication.h" @@ -53,12 +54,17 @@ SiteInfo::SiteInfo(WebView* view, QWidget* parent) setAttribute(Qt::WA_DeleteOnClose); ui->setupUi(this); + ListItemDelegate* delegate = new ListItemDelegate(24, ui->listWidget); + ui->listWidget->setItemDelegate(delegate); + ui->listWidget->item(0)->setIcon(QIcon::fromTheme("document-properties", QIcon(":/icons/preferences/document-properties.png"))); ui->listWidget->item(1)->setIcon(QIcon::fromTheme("applications-graphics", QIcon(":/icons/preferences/applications-graphics.png"))); ui->listWidget->item(2)->setIcon(QIcon::fromTheme("text-x-sql", QIcon(":/icons/preferences/text-x-sql.png"))); ui->listWidget->item(3)->setIcon(QIcon::fromTheme("dialog-password", QIcon(":/icons/preferences/dialog-password.png"))); ui->listWidget->item(0)->setSelected(true); + ui->listWidget->setFixedHeight(delegate->itemHeight()); + WebPage* webPage = view->page(); QWebFrame* frame = view->page()->mainFrame(); QString title = view->title(); diff --git a/src/lib/webview/siteinfo.ui b/src/lib/webview/siteinfo.ui index a7e0c5065..4f55dcc25 100644 --- a/src/lib/webview/siteinfo.ui +++ b/src/lib/webview/siteinfo.ui @@ -16,18 +16,6 @@ - - - 0 - 0 - - - - - 16777215 - 53 - - Qt::NoFocus @@ -40,8 +28,8 @@ QListView::Static - - QListView::LeftToRight + + QListView::Adjust QListView::IconMode