1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

Completely changed look of address bar popup.

- using QStyledItemDelegate for rendering
This commit is contained in:
nowrep 2012-04-19 22:42:35 +02:00
parent 30f97be3b1
commit a40062fde6
6 changed files with 277 additions and 26 deletions

View File

@ -170,7 +170,8 @@ SOURCES += \
network/schemehandler.cpp \
tools/plaineditwithlines.cpp \
webview/websettings.cpp \
tools/focusselectlineedit.cpp
tools/focusselectlineedit.cpp \
navigation/locationcompleterdelegate.cpp
HEADERS += \
webview/tabpreview.h \
@ -313,7 +314,8 @@ HEADERS += \
tools/plaineditwithlines.h \
sidebar/sidebarinterface.h \
webview/websettings.h \
tools/focusselectlineedit.h
tools/focusselectlineedit.h \
navigation/locationcompleterdelegate.h
FORMS += \
preferences/autofillmanager.ui \

View File

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "locationcompleter.h"
#include "locationcompleterdelegate.h"
#include "locationbar.h"
#include "iconprovider.h"
#include "mainapplication.h"
@ -28,23 +29,25 @@
LocationCompleter::LocationCompleter(QObject* parent)
: QCompleter(parent)
{
setMaxVisibleItems(6);
QStandardItemModel* completeModel = new QStandardItemModel();
setModel(completeModel);
QTreeView* treeView = new QTreeView;
m_treeView = new CompleterTreeView();
setPopup(treeView);
treeView->setRootIsDecorated(false);
treeView->header()->hide();
treeView->header()->setStretchLastSection(false);
treeView->header()->setResizeMode(0, QHeaderView::Stretch);
treeView->header()->resizeSection(1, 0);
setPopup(m_treeView);
m_treeView->setRootIsDecorated(false);
m_treeView->header()->hide();
m_treeView->header()->setStretchLastSection(false);
m_treeView->header()->setResizeMode(0, QHeaderView::Stretch);
m_treeView->header()->resizeSection(1, 0);
m_treeView->setItemDelegateForColumn(0, new LocationCompleterDelegate(m_treeView));
setCompletionMode(QCompleter::PopupCompletion);
setCaseSensitivity(Qt::CaseInsensitive);
setWrapAround(true);
setCompletionColumn(1);
setMaxVisibleItems(6);
}
QStringList LocationCompleter::splitPath(const QString &path) const
@ -87,7 +90,6 @@ void LocationCompleter::showMostVisited()
query.exec("SELECT title, url FROM history ORDER BY count DESC LIMIT 15");
int i = 0;
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
QTreeView* treeView = qobject_cast<QTreeView*>(popup());
cModel->clear();
while (query.next()) {
@ -96,7 +98,8 @@ void LocationCompleter::showMostVisited()
QString url = query.value(1).toUrl().toEncoded();
iconText->setIcon(_iconForUrl(query.value(1).toUrl()).pixmap(16, 16));
iconText->setText(query.value(0).toString().replace("\n", "").append("\n" + url));
iconText->setText(query.value(0).toString());
iconText->setData(query.value(1), Qt::UserRole);
findUrl->setText(url);
QList<QStandardItem*> items;
@ -106,8 +109,8 @@ void LocationCompleter::showMostVisited()
i++;
}
treeView->header()->setResizeMode(0, QHeaderView::Stretch);
treeView->header()->resizeSection(1, 0);
m_treeView->header()->setResizeMode(0, QHeaderView::Stretch);
m_treeView->header()->resizeSection(1, 0);
popup()->setMinimumHeight(190);
@ -121,7 +124,6 @@ void LocationCompleter::refreshCompleter(const QString &string)
QString searchString = QString("%%1%").arg(string);
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
QTreeView* treeView = qobject_cast<QTreeView*>(popup());
cModel->clear();
QSqlQuery query;
@ -137,7 +139,8 @@ void LocationCompleter::refreshCompleter(const QString &string)
QString url = query.value(1).toUrl().toEncoded();
iconText->setIcon(IconProvider::iconFromImage(QImage::fromData(query.value(2).toByteArray())));
iconText->setText(query.value(0).toString().replace("\n", "").append("\n" + url));
iconText->setText(query.value(0).toString());
iconText->setData(query.value(1), Qt::UserRole);
findUrl->setText(url);
QList<QStandardItem*> items;
@ -159,7 +162,8 @@ void LocationCompleter::refreshCompleter(const QString &string)
QString url = query.value(1).toUrl().toEncoded();
iconText->setIcon(_iconForUrl(query.value(1).toUrl()).pixmap(16, 16));
iconText->setText(query.value(0).toString().replace("\n", "").append("\n" + url));
iconText->setText(query.value(0).toString());
iconText->setData(query.value(1), Qt::UserRole);
findUrl->setText(url);
QList<QStandardItem*> items;
@ -169,15 +173,15 @@ void LocationCompleter::refreshCompleter(const QString &string)
i++;
}
treeView->header()->setResizeMode(0, QHeaderView::Stretch);
treeView->header()->resizeSection(1, 0);
m_treeView->header()->setResizeMode(0, QHeaderView::Stretch);
m_treeView->header()->resizeSection(1, 0);
if (i > 6) {
popup()->setMinimumHeight(190);
m_treeView->setMinimumHeight(6 * m_treeView->rowHeight());
}
else {
popup()->setMinimumHeight(0);
m_treeView->setMinimumHeight(0);
}
popup()->setUpdatesEnabled(true);
m_treeView->setUpdatesEnabled(true);
}

View File

@ -22,6 +22,8 @@
#include "qz_namespace.h"
class CompleterTreeView;
class QT_QUPZILLA_EXPORT LocationCompleter : public QCompleter
{
Q_OBJECT
@ -35,6 +37,9 @@ signals:
public slots:
void refreshCompleter(const QString &string);
void showMostVisited();
private:
CompleterTreeView* m_treeView;
};
#endif // LOCATIONCOMPLETER_H

View File

@ -0,0 +1,179 @@
/* ============================================================
* 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/>.
* ============================================================ */
#include "locationcompleterdelegate.h"
#include <QPainter>
#include <QTreeView>
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
CompleterTreeView::CompleterTreeView(QWidget* parent)
: QTreeView(parent)
, m_selectedItemByMousePosition(false)
, m_rowHeight(0)
{
setMouseTracking(true);
}
bool CompleterTreeView::ignoreSelectedFlag() const
{
return m_selectedItemByMousePosition;
}
int CompleterTreeView::rowHeight() const
{
return m_rowHeight;
}
void CompleterTreeView::setRowHeight(int height)
{
m_rowHeight = height;
}
void CompleterTreeView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
m_selectedItemByMousePosition = false;
m_lastMouseIndex = current;
QTreeView::currentChanged(current, previous);
viewport()->repaint();
}
void CompleterTreeView::mouseMoveEvent(QMouseEvent* event)
{
QModelIndex last = m_lastMouseIndex;
QModelIndex atCursor = indexAt(mapFromGlobal(QCursor::pos()));
if (atCursor.isValid()) {
m_lastMouseIndex = atCursor;
m_selectedItemByMousePosition = true;
}
if (last != atCursor) {
viewport()->repaint();
}
QTreeView::mouseMoveEvent(event);
}
void CompleterTreeView::keyPressEvent(QKeyEvent* event)
{
if (currentIndex() != m_lastMouseIndex) {
setCurrentIndex(m_lastMouseIndex);
}
QTreeView::keyPressEvent(event);
}
LocationCompleterDelegate::LocationCompleterDelegate(CompleterTreeView* parent)
: QStyledItemDelegate(parent)
, m_rowHeight(0)
, m_padding(0)
, m_treeView(parent)
{
}
void LocationCompleterDelegate::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 int height = opt.rect.height();
const int center = height / 2 + opt.rect.top();
// Prepare title font
QFont titleFont = opt.font;
titleFont.setPointSize(titleFont.pointSize() + 1);
const QFontMetrics titleMetrics(titleFont);
int leftPosition = m_padding * 2;
int rightPosition = opt.rect.right() - m_padding;
if (m_treeView->ignoreSelectedFlag()) {
if (opt.state.testFlag(QStyle::State_MouseOver)) {
opt.state |= QStyle::State_Selected;
}
else {
opt.state &= ~QStyle::State_Selected;
}
}
const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
const QPalette::ColorRole colorLinkRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Link;
// Draw background
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
// Draw icon
const int iconSize = 16;
const int iconYPos = center - (iconSize / 2);
QRect iconRect(leftPosition, iconYPos, iconSize, iconSize);
QPixmap pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(iconSize);
painter->drawPixmap(iconRect, pixmap);
leftPosition = iconRect.right() + m_padding * 2;
// Draw title
const int leftTitleEdge = leftPosition + 2;
const int rightTitleEdge = rightPosition - m_padding;
QRect titleRect(leftTitleEdge, opt.rect.top() + m_padding, rightTitleEdge - leftTitleEdge, titleMetrics.height());
QString title(titleMetrics.elidedText(index.data(Qt::DisplayRole).toString(), Qt::ElideRight, titleRect.width()));
painter->setFont(titleFont);
style->drawItemText(painter, titleRect, Qt::AlignLeft | Qt::TextSingleLine, opt.palette, true, title, colorRole);
// Draw link
const int infoYPos = titleRect.bottom() + opt.fontMetrics.leading();
QRect linkRect(titleRect.x(), infoYPos, titleRect.width(), opt.fontMetrics.height());
const QString &link = opt.fontMetrics.elidedText(index.data(Qt::UserRole).toString(), Qt::ElideRight, linkRect.width());
painter->setFont(opt.font);
style->drawItemText(painter, linkRect, Qt::TextSingleLine | Qt::AlignLeft, opt.palette, true, link, colorLinkRole);
}
QSize LocationCompleterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index)
if (!m_rowHeight) {
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;
QFont titleFont = opt.font;
titleFont.setPointSize(titleFont.pointSize() + 1);
m_padding = padding > 3 ? padding : 3;
const QFontMetrics titleMetrics(titleFont);
m_rowHeight = 2 * m_padding + opt.fontMetrics.leading() + opt.fontMetrics.height() + titleMetrics.height();
m_treeView->setRowHeight(m_rowHeight);
m_treeView->setMaximumHeight(6 * m_rowHeight);
}
return QSize(200, m_rowHeight);
}

View File

@ -0,0 +1,65 @@
/* ============================================================
* 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/>.
* ============================================================ */
#ifndef LOCATIONCOMPLETERDELEGATE_H
#define LOCATIONCOMPLETERDELEGATE_H
#include <QStyledItemDelegate>
#include <QTreeView>
#include "qz_namespace.h"
class QT_QUPZILLA_EXPORT CompleterTreeView : public QTreeView
{
Q_OBJECT
public:
explicit CompleterTreeView(QWidget* parent = 0);
bool ignoreSelectedFlag() const;
int rowHeight() const;
void setRowHeight(int height);
private slots:
void currentChanged(const QModelIndex &current, const QModelIndex &previous);
protected:
void mouseMoveEvent(QMouseEvent* event);
void keyPressEvent(QKeyEvent* event);
private:
bool m_selectedItemByMousePosition;
int m_rowHeight;
QModelIndex m_lastMouseIndex;
};
class QT_QUPZILLA_EXPORT LocationCompleterDelegate : public QStyledItemDelegate
{
public:
explicit LocationCompleterDelegate(CompleterTreeView* parent = 0);
void paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
mutable int m_rowHeight;
mutable int m_padding;
CompleterTreeView* m_treeView;
};
#endif // LOCATIONCOMPLETERDELEGATE_H

View File

@ -19,11 +19,7 @@
#include <QPainter>
#include <QListWidget>
#include <QTextLayout>
#include <QTextDocument>
#include <QTextBlock>
#include <QApplication>
#include <QScrollBar>
PluginListDelegate::PluginListDelegate(QListWidget* parent)
: QStyledItemDelegate(parent)