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 <QListView>
16#include <QModelIndex>
17#include <QTextDocument>
18#include <QPainter>
19#include <QApplication>
20#include <QAbstractTextDocumentLayout>
21#include <QSettings>
22
23
24namespace Marble {
25MapViewItemDelegate::MapViewItemDelegate( QListView *view ) :
26 m_view(view),
27 m_bookmarkIcon(QStringLiteral(":/icons/bookmarks.png"))
28{
29 // nothing to do
30}
31
32
33void MapViewItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
34{
35 QStyleOptionViewItem styleOption = option;
36 initStyleOption( &styleOption, index );
37 styleOption.text = QString();
38 styleOption.icon = QIcon();
39
40 bool const selected = styleOption.state & QStyle::State_Selected;
41 bool const active = styleOption.state & QStyle::State_Active;
42 bool const hover = styleOption.state & QStyle::State_MouseOver;
43 QPalette::ColorGroup const colorGroup = active ? QPalette::Active : QPalette::Inactive;
44 if ( selected || hover ) {
45 styleOption.features &= ~QStyleOptionViewItem::Alternate;
47 painter->fillRect( styleOption.rect, styleOption.palette.color( colorGroup, colorRole ) );
48 }
49 QStyle* style = styleOption.widget ? styleOption.widget->style() : QApplication::style();
50 style->drawControl( QStyle::CE_ItemViewItem, &styleOption, painter, styleOption.widget );
51
52 QRect const rect = styleOption.rect;
53 QSize const iconSize = styleOption.decorationSize;
54 QRect const iconRect( rect.topLeft(), iconSize );
55 QIcon const icon = index.data( Qt::DecorationRole ).value<QIcon>();
56 painter->drawPixmap( iconRect, icon.pixmap( iconSize ) );
57
58 int const padding = 5;
59 QString const name = index.data().toString();
60 const bool isFavorite = QSettings().contains(QLatin1String("Favorites/") + name);
61 QSize const bookmarkSize( 16, 16 );
62 QRect bookmarkRect( iconRect.bottomRight(), bookmarkSize );
63 bookmarkRect.translate( QPoint( -bookmarkSize.width() - padding, -bookmarkSize.height() - padding ) );
64 QIcon::Mode mode = isFavorite ? QIcon::Normal : QIcon::Disabled;
65 painter->drawPixmap( bookmarkRect, m_bookmarkIcon.pixmap( bookmarkSize, mode ) );
66
67 QTextDocument document;
68 document.setTextWidth( rect.width() - iconSize.width() - padding );
69 document.setDefaultFont( styleOption.font );
70 document.setHtml( text( index ) );
71
72 QRect textRect = QRect( iconRect.topRight(), QSize( document.textWidth() - padding, rect.height() - padding ) );
73 painter->save();
74 painter->translate( textRect.topLeft() );
75 painter->setClipRect( textRect.translated( -textRect.topLeft() ) );
77 paintContext.palette = styleOption.palette;
78 QPalette::ColorRole const role = selected && active ? QPalette::HighlightedText : QPalette::Text;
79 paintContext.palette.setColor( QPalette::Text, styleOption.palette.color( colorGroup, role ) );
80 document.documentLayout()->draw( painter, paintContext );
81 painter->restore();
82}
83
84QSize MapViewItemDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
85{
86 if ( index.column() == 0 ) {
87 QSize const iconSize = option.decorationSize;
88 QTextDocument doc;
89 doc.setDefaultFont( option.font );
90 doc.setTextWidth( m_view->width() - iconSize.width() - 10 );
91 doc.setHtml( text( index ) );
92 return QSize( iconSize.width() + doc.size().width(), iconSize.height() );
93 }
94
95 return QSize();
96}
97
98QString MapViewItemDelegate::text( const QModelIndex &index )
99{
100 QString const title = index.data().toString();
101 QString const description = index.data( Qt::UserRole+2 ).toString();
102 return QString("<p><b>%1</b></p>%2").arg( title, description );
103}
104
105}
106
107#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()
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
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 Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.