2012-04-19 22:42:35 +02: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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "locationcompleterdelegate.h"
|
2012-04-22 17:09:43 +02:00
|
|
|
#include "locationcompleterview.h"
|
2012-05-05 16:06:24 +02:00
|
|
|
#include "locationcompletermodel.h"
|
2012-04-22 20:51:28 +02:00
|
|
|
#include "iconprovider.h"
|
2012-04-19 22:42:35 +02:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
LocationCompleterDelegate::LocationCompleterDelegate(LocationCompleterView* parent)
|
2012-04-19 22:42:35 +02:00
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
, m_rowHeight(0)
|
|
|
|
, m_padding(0)
|
2012-04-22 17:09:43 +02:00
|
|
|
, m_view(parent)
|
2012-04-19 22:42:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2012-08-11 19:42:58 +02:00
|
|
|
|
2012-04-19 22:42:35 +02:00
|
|
|
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;
|
|
|
|
|
2012-04-22 20:51:28 +02:00
|
|
|
opt.state &= ~QStyle::State_MouseOver;
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
if (m_view->hoveredIndex() == index) {
|
|
|
|
opt.state |= QStyle::State_Selected;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
opt.state &= ~QStyle::State_Selected;
|
2012-04-19 22:42:35 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 22:48:52 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2012-07-12 12:55:22 +02:00
|
|
|
const QPalette::ColorRole colorRole = QPalette::Text;
|
|
|
|
const QPalette::ColorRole colorLinkRole = QPalette::Link;
|
|
|
|
#else
|
2012-04-19 22:42:35 +02:00
|
|
|
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;
|
2012-07-12 12:55:22 +02:00
|
|
|
#endif
|
2012-04-19 22:42:35 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2012-08-11 19:42:58 +02:00
|
|
|
// Draw star to bookmark items
|
|
|
|
int starPixmapWidth = 0;
|
|
|
|
if (index.data(LocationCompleterModel::BookmarkRole).toBool()) {
|
|
|
|
const QPixmap starPixmap = qIconProvider->bookmarkIcon();
|
|
|
|
QSize starSize = starPixmap.size();
|
|
|
|
//new
|
|
|
|
starPixmapWidth = starSize.width();
|
|
|
|
QPoint pos(rightPosition - starPixmapWidth, opt.rect.top() + m_padding);
|
|
|
|
QRect starRect(pos, starSize);
|
|
|
|
painter->drawPixmap(starRect, starPixmap);
|
|
|
|
}
|
|
|
|
|
2012-08-25 13:08:21 +02:00
|
|
|
const QString &searchText = index.data(LocationCompleterModel::SearchStringRole).toString();
|
|
|
|
|
2012-04-19 22:42:35 +02:00
|
|
|
// Draw title
|
|
|
|
const int leftTitleEdge = leftPosition + 2;
|
2012-08-25 13:08:21 +02:00
|
|
|
// RTL Support: remove conflicting of right-aligned text and starpixmap!
|
2012-08-11 19:42:58 +02:00
|
|
|
const int rightTitleEdge = rightPosition - m_padding - starPixmapWidth;
|
2012-04-19 22:42:35 +02:00
|
|
|
QRect titleRect(leftTitleEdge, opt.rect.top() + m_padding, rightTitleEdge - leftTitleEdge, titleMetrics.height());
|
2012-05-05 16:06:24 +02:00
|
|
|
QString title(titleMetrics.elidedText(index.data(LocationCompleterModel::TitleRole).toString(), Qt::ElideRight, titleRect.width()));
|
2012-04-19 22:42:35 +02:00
|
|
|
painter->setFont(titleFont);
|
2012-08-11 19:42:58 +02:00
|
|
|
|
2012-08-25 13:08:21 +02:00
|
|
|
drawHighlightedTextLine(titleRect, title, searchText, painter, style, opt, colorRole);
|
2012-04-19 22:42:35 +02:00
|
|
|
|
|
|
|
// Draw link
|
2012-09-01 17:23:17 +02:00
|
|
|
const int infoYPos = titleRect.bottom() + opt.fontMetrics.leading() + 2;
|
2012-04-19 22:42:35 +02:00
|
|
|
QRect linkRect(titleRect.x(), infoYPos, titleRect.width(), opt.fontMetrics.height());
|
2012-08-25 13:08:21 +02:00
|
|
|
QString link(opt.fontMetrics.elidedText(index.data(Qt::DisplayRole).toString(), Qt::ElideRight, linkRect.width()));
|
2012-04-19 22:42:35 +02:00
|
|
|
painter->setFont(opt.font);
|
2012-04-22 20:51:28 +02:00
|
|
|
|
2012-08-25 13:08:21 +02:00
|
|
|
drawHighlightedTextLine(linkRect, link, searchText, painter, style, opt, colorLinkRole);
|
2012-09-01 17:23:17 +02:00
|
|
|
|
2012-09-02 11:42:41 +02:00
|
|
|
// Draw line at the very bottom of item if the item is not highlighted
|
|
|
|
if (!(opt.state & QStyle::State_Selected)) {
|
|
|
|
QRect lineRect(opt.rect.left(), opt.rect.bottom(), opt.rect.width(), 1);
|
|
|
|
painter->fillRect(lineRect, opt.palette.color(QPalette::AlternateBase));
|
|
|
|
}
|
2012-08-25 13:08:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-01 16:58:55 +02:00
|
|
|
bool sizeBiggerThan(const QString &s1, const QString &s2)
|
|
|
|
{
|
|
|
|
return s1.size() > s2.size();
|
|
|
|
}
|
|
|
|
|
2012-08-25 13:08:21 +02:00
|
|
|
void LocationCompleterDelegate::drawHighlightedTextLine(const QRect &rect, QString text, const QString &searchText,
|
|
|
|
QPainter* painter, const QStyle* style, const QStyleOptionViewItemV4 &option,
|
|
|
|
const QPalette::ColorRole &role) const
|
|
|
|
{
|
|
|
|
QList<int> delimiters;
|
2012-09-04 12:42:45 +02:00
|
|
|
QStringList searchStrings = searchText.split(QLatin1Char(' '), QString::SkipEmptyParts);
|
2012-09-01 16:58:55 +02:00
|
|
|
|
|
|
|
// Look for longer parts first
|
|
|
|
qSort(searchStrings.begin(), searchStrings.end(), sizeBiggerThan);
|
2012-08-25 13:08:21 +02:00
|
|
|
|
|
|
|
foreach(const QString & string, searchStrings) {
|
|
|
|
int delimiter = text.indexOf(string, 0, Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
while (delimiter != -1) {
|
2012-09-01 16:58:55 +02:00
|
|
|
int start = delimiter;
|
|
|
|
int end = delimiter + string.length();
|
|
|
|
|
|
|
|
bool alreadyContains = false;
|
|
|
|
for (int i = 0; i < delimiters.count(); ++i) {
|
|
|
|
int dStart = delimiters.at(i);
|
|
|
|
int dEnd = delimiters.at(++i);
|
|
|
|
|
|
|
|
if (dStart <= start && dEnd >= end) {
|
|
|
|
alreadyContains = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alreadyContains) {
|
|
|
|
delimiters.append(start);
|
|
|
|
delimiters.append(end);
|
|
|
|
}
|
2012-08-25 13:08:21 +02:00
|
|
|
|
2012-09-01 16:58:55 +02:00
|
|
|
delimiter = text.indexOf(string, end, Qt::CaseInsensitive);
|
2012-08-25 13:08:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-01 15:05:16 +02:00
|
|
|
// We need to sort delimiters to properly paint all parts that user typed
|
|
|
|
qSort(delimiters);
|
|
|
|
|
2012-08-25 13:08:21 +02:00
|
|
|
// If we don't find any match, just paint it without any highlight
|
|
|
|
if (delimiters.isEmpty() || delimiters.count() % 2) {
|
|
|
|
drawTextLine(rect, text, painter, style, option, role);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont normalFont = painter->font();
|
|
|
|
QFont boldFont = normalFont;
|
|
|
|
boldFont.setBold(true);
|
|
|
|
|
|
|
|
QFontMetrics normalMetrics(normalFont);
|
|
|
|
QFontMetrics boldMetrics(boldFont);
|
|
|
|
|
|
|
|
int lastEndPos = 0;
|
|
|
|
int lastRectPos = rect.left();
|
|
|
|
|
|
|
|
while (!delimiters.isEmpty()) {
|
|
|
|
int start = delimiters.takeFirst();
|
|
|
|
int end = delimiters.takeFirst();
|
|
|
|
|
|
|
|
const QString &normalPart = text.mid(lastEndPos, start - lastEndPos);
|
|
|
|
const QString &boldPart = text.mid(start, end - start);
|
|
|
|
|
|
|
|
lastEndPos = end;
|
|
|
|
|
|
|
|
if (!normalPart.isEmpty()) {
|
|
|
|
int width = normalMetrics.width(normalPart);
|
2012-09-01 15:05:16 +02:00
|
|
|
const QRect &nRect = adjustRect(rect, QRect(lastRectPos, rect.top(), width, rect.height()));
|
2012-08-25 13:08:21 +02:00
|
|
|
|
2012-09-01 15:05:16 +02:00
|
|
|
if (nRect.width() > 0) {
|
|
|
|
painter->setFont(normalFont);
|
|
|
|
drawTextLine(nRect, normalPart, painter, style, option, role);
|
2012-08-25 13:08:21 +02:00
|
|
|
|
2012-09-01 15:05:16 +02:00
|
|
|
lastRectPos += nRect.width();
|
|
|
|
}
|
2012-08-25 13:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!boldPart.isEmpty()) {
|
|
|
|
int width = boldMetrics.width(boldPart);
|
2012-09-01 15:05:16 +02:00
|
|
|
const QRect &bRect = adjustRect(rect, QRect(lastRectPos, rect.top(), width, rect.height()));
|
2012-08-25 13:08:21 +02:00
|
|
|
|
2012-09-01 15:05:16 +02:00
|
|
|
if (bRect.width() > 0) {
|
|
|
|
painter->setFont(boldFont);
|
|
|
|
drawTextLine(bRect, boldPart, painter, style, option, role);
|
2012-08-25 13:08:21 +02:00
|
|
|
|
2012-09-01 15:05:16 +02:00
|
|
|
// Paint manually line under text instead of using QFont::underline
|
2012-09-01 16:58:55 +02:00
|
|
|
QRect underlineRect(bRect.left(), bRect.top() + boldMetrics.ascent() + 1,
|
|
|
|
bRect.width(), boldFont.pointSize() > 8 ? 2 : 1);
|
2012-09-01 15:05:16 +02:00
|
|
|
painter->fillRect(underlineRect, option.palette.color(role));
|
2012-09-01 16:58:55 +02:00
|
|
|
|
|
|
|
lastRectPos += bRect.width();
|
2012-09-01 15:05:16 +02:00
|
|
|
}
|
2012-08-25 13:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (delimiters.isEmpty() && lastEndPos != text.size()) {
|
|
|
|
const QString &lastText = text.mid(lastEndPos);
|
|
|
|
|
|
|
|
int width = normalMetrics.width(lastText);
|
|
|
|
QRect nRect(lastRectPos, rect.top(), width, rect.height());
|
|
|
|
|
|
|
|
painter->setFont(normalFont);
|
|
|
|
drawTextLine(adjustRect(rect, nRect), lastText, painter, style, option, role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RTL Support
|
|
|
|
#define LRE QChar(0x202A)
|
|
|
|
#define RLE QChar(0x202B)
|
|
|
|
#define PDF QChar(0x202C)
|
|
|
|
|
|
|
|
void LocationCompleterDelegate::drawTextLine(const QRect &rect, QString text, QPainter* painter,
|
|
|
|
const QStyle* style, const QStyleOptionViewItemV4 &option,
|
|
|
|
const QPalette::ColorRole &role) const
|
|
|
|
{
|
|
|
|
if (rect.width() > 0) {
|
|
|
|
const Qt::LayoutDirection direction = option.widget ? option.widget->layoutDirection() : QApplication::layoutDirection();
|
|
|
|
Qt::LayoutDirection textDirection = text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
|
|
|
|
Qt::Alignment alignment = textDirection == direction ? Qt::AlignLeft : Qt::AlignRight;
|
|
|
|
|
|
|
|
text.isRightToLeft() ? text.prepend(RLE) : text.prepend(LRE);
|
|
|
|
text.append(PDF);
|
|
|
|
|
|
|
|
style->drawItemText(painter, rect, Qt::TextSingleLine | alignment, option.palette, true, text, role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect LocationCompleterDelegate::adjustRect(const QRect &original, const QRect &created) const
|
|
|
|
{
|
2012-09-01 15:05:16 +02:00
|
|
|
if (created.left() + created.width() >= original.right()) {
|
2012-08-25 13:08:21 +02:00
|
|
|
QRect nRect = created;
|
2012-09-01 15:05:16 +02:00
|
|
|
nRect.setWidth(original.right() - created.left());
|
2012-08-25 13:08:21 +02:00
|
|
|
|
|
|
|
return nRect;
|
|
|
|
}
|
2012-08-11 19:42:58 +02:00
|
|
|
|
2012-08-25 13:08:21 +02:00
|
|
|
return created;
|
2012-04-19 22:42:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-09-01 17:23:17 +02:00
|
|
|
// 2 px bigger space between title and link because of underlining
|
|
|
|
m_rowHeight = 2 * m_padding + opt.fontMetrics.leading() + opt.fontMetrics.height() + titleMetrics.height() + 2;
|
2012-04-19 22:42:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return QSize(200, m_rowHeight);
|
|
|
|
}
|