Marble

MapViewItemDelegate.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2004-2007 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
5// SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
6// SPDX-FileCopyrightText: 2010 Bastian Holst <bastianholst@gmx.de>
7// SPDX-FileCopyrightText: 2011-2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
8// SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
9//
10
11// Self
12#include "MapViewItemDelegate.h"
13
14// Qt
15#include <QAbstractTextDocumentLayout>
16#include <QApplication>
17#include <QListView>
18#include <QModelIndex>
19#include <QPainter>
20#include <QSettings>
21#include <QTextDocument>
22
23namespace Marble
24{
25MapViewItemDelegate::MapViewItemDelegate(QListView *view)
26 : m_view(view)
27 , m_bookmarkIcon(QStringLiteral(":/icons/bookmarks.png"))
28{
29 // nothing to do
30}
31
32void MapViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
33{
34 QStyleOptionViewItem styleOption = option;
35 initStyleOption(&styleOption, index);
36 styleOption.text = QString();
37 styleOption.icon = QIcon();
38
39 bool const selected = styleOption.state & QStyle::State_Selected;
40 bool const active = styleOption.state & QStyle::State_Active;
41 bool const hover = styleOption.state & QStyle::State_MouseOver;
42 QPalette::ColorGroup const colorGroup = active ? QPalette::Active : QPalette::Inactive;
43 if (selected || hover) {
44 styleOption.features &= ~QStyleOptionViewItem::Alternate;
46 painter->fillRect(styleOption.rect, styleOption.palette.color(colorGroup, colorRole));
47 }
48 QStyle *style = styleOption.widget ? styleOption.widget->style() : QApplication::style();
49 style->drawControl(QStyle::CE_ItemViewItem, &styleOption, painter, styleOption.widget);
50
51 QRect const rect = styleOption.rect;
52 QSize const iconSize = styleOption.decorationSize;
53 QRect const iconRect(rect.topLeft(), iconSize);
54 auto const icon = index.data(Qt::DecorationRole).value<QIcon>();
55 painter->drawPixmap(iconRect, icon.pixmap(iconSize));
56
57 int const padding = 5;
58 QString const name = index.data().toString();
59 const bool isFavorite = QSettings().contains(QLatin1StringView("Favorites/") + name);
60 QSize const bookmarkSize(16, 16);
61 QRect bookmarkRect(iconRect.bottomRight(), bookmarkSize);
62 bookmarkRect.translate(QPoint(-bookmarkSize.width() - padding, -bookmarkSize.height() - padding));
63 QIcon::Mode mode = isFavorite ? QIcon::Normal : QIcon::Disabled;
64 painter->drawPixmap(bookmarkRect, m_bookmarkIcon.pixmap(bookmarkSize, mode));
65
66 QTextDocument document;
67 document.setTextWidth(rect.width() - iconSize.width() - padding);
68 document.setDefaultFont(styleOption.font);
69 document.setHtml(text(index));
70
71 QRect textRect = QRect(iconRect.topRight(), QSize(document.textWidth() - padding, rect.height() - padding));
72 painter->save();
73 painter->translate(textRect.topLeft());
74 painter->setClipRect(textRect.translated(-textRect.topLeft()));
76 paintContext.palette = styleOption.palette;
77 QPalette::ColorRole const role = selected && active ? QPalette::HighlightedText : QPalette::Text;
78 paintContext.palette.setColor(QPalette::Text, styleOption.palette.color(colorGroup, role));
79 document.documentLayout()->draw(painter, paintContext);
80 painter->restore();
81}
82
83QSize MapViewItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
84{
85 if (index.column() == 0) {
86 QSize const iconSize = option.decorationSize;
87 QTextDocument doc;
88 doc.setDefaultFont(option.font);
89 doc.setTextWidth(m_view->width() - iconSize.width() - 10);
90 doc.setHtml(text(index));
91 return QSize(iconSize.width() + doc.size().width(), iconSize.height());
92 }
93
94 return {};
95}
96
97QString MapViewItemDelegate::text(const QModelIndex &index)
98{
99 QString const title = index.data().toString();
100 QString const description = index.data(Qt::UserRole + 2).toString();
101 return QStringLiteral("<p><b>%1</b></p>%2").arg(title, description);
102}
103
104}
105
106#include "moc_MapViewItemDelegate.cpp"
QString name(StandardAction id)
Binds a QML item to a specific geodetic location in screen coordinates.
virtual void draw(QPainter *painter, const PaintContext &context)=0
QStyle * style()
int column() const const
QVariant data(int role) const const
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void restore()
void save()
void setClipRect(const QRect &rectangle, Qt::ClipOperation operation)
void translate(const QPoint &offset)
int height() const const
QPoint topLeft() const const
QRect translated(const QPoint &offset) const const
int width() const const
bool contains(QAnyStringView key) const const
int height() const const
int width() const const
QString arg(Args &&... args) const const
virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
DecorationRole
QAbstractTextDocumentLayout * documentLayout() const const
void setDefaultFont(const QFont &font)
void setHtml(const QString &html)
void setTextWidth(qreal width)
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.