QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsfeaturelistviewdelegate.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeaturelistviewdelegate.cpp
3 ---------------------
4 begin : February 2013
5 copyright : (C) 2013 by Matthias Kuhn
6 email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
17#include "qgsapplication.h"
19#include "qgsfeaturelistmodel.h"
21#include "qgsvectorlayer.h"
23
24#include <QApplication>
25#include <QHBoxLayout>
26#include <QLabel>
27#include <QMouseEvent>
28#include <QObject>
29#include <QPushButton>
30#include <QString>
31
32#include "moc_qgsfeaturelistviewdelegate.cpp"
33
34using namespace Qt::StringLiterals;
35
37 : QItemDelegate( parent )
38 , mListModel( listModel )
39{
40}
41
43{
44 if ( pos.x() > ICON_SIZE )
45 {
46 return EditElement;
47 }
48 else
49 {
50 return SelectionElement;
51 }
52}
53
55{
56 mFeatureSelectionModel = featureSelectionModel;
57}
58
60{
61 mCurrentFeatureEdited = state;
62}
63
64void QgsFeatureListViewDelegate::setEditSelectionModel( QItemSelectionModel *editSelectionModel )
65{
66 mEditSelectionModel = editSelectionModel;
67}
68
69QSize QgsFeatureListViewDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
70{
71 Q_UNUSED( index )
72 const int height = ICON_SIZE;
73 return QSize( option.rect.width(), std::max( height, option.fontMetrics.height() ) );
74}
75
76void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
77{
78 const bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
79
80 QStyleOptionViewItem textOption = option;
81 textOption.state |= QStyle::State_Enabled;
82 if ( isEditSelection )
83 {
84 textOption.state |= QStyle::State_Selected;
85 }
86 drawBackground( painter, textOption, index );
87
88
89 static QPixmap sSelectedIcon;
90 if ( sSelectedIcon.isNull() )
91 sSelectedIcon = QgsApplication::getThemePixmap( u"/mIconSelected.svg"_s );
92 static QPixmap sDeselectedIcon;
93 if ( sDeselectedIcon.isNull() )
94 sDeselectedIcon = QgsApplication::getThemePixmap( u"/mIconDeselected.svg"_s );
95
96 const QString text = index.model()->data( index, Qt::EditRole ).toString();
98
99 // Icon layout options
100 const QStyleOptionViewItem iconOption;
101
102 QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
103
104 QPixmap icon = mFeatureSelectionModel->isSelected( index ) ? sSelectedIcon : sDeselectedIcon;
105
106 // Scale up the icon if needed
107 if ( option.rect.height() > ICON_SIZE )
108 {
109 icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
110 }
111
112 drawDecoration( painter, iconOption, iconLayoutBounds, icon );
113
114 // if conditional style also has an icon, draw that too
115 const QVariant conditionalIcon = index.model()->data( index, Qt::DecorationRole );
116 if ( conditionalIcon.isValid() )
117 {
118 const QPixmap pixmap = conditionalIcon.value<QPixmap>();
119 iconLayoutBounds.moveLeft( iconLayoutBounds.x() + icon.width() + QFontMetrics( textOption.font ).horizontalAdvance( 'X' ) );
120 iconLayoutBounds.setTop( option.rect.y() + ( option.rect.height() - pixmap.height() ) / 2.0 );
121 iconLayoutBounds.setHeight( pixmap.height() );
122 drawDecoration( painter, iconOption, iconLayoutBounds, pixmap );
123 }
124
125 // Text layout options
126 const QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
127
128 // start with font and foreground color from model's FontRole
129 const QVariant font = index.model()->data( index, Qt::FontRole );
130 if ( font.isValid() )
131 {
132 textOption.font = font.value<QFont>();
133 }
134 const QVariant textColor = index.model()->data( index, Qt::ForegroundRole );
135 if ( textColor.isValid() )
136 {
137 textOption.palette.setColor( QPalette::Text, textColor.value<QColor>() );
138 }
139
140 if ( featInfo.isNew )
141 {
142 textOption.font.setStyle( QFont::StyleItalic );
143 textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
144 textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
145 }
146 else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
147 {
148 textOption.font.setStyle( QFont::StyleItalic );
149 textOption.palette.setColor( QPalette::Text, Qt::red );
150 textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
151 }
152
153 drawDisplay( painter, textOption, textLayoutBounds, text );
154}
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
A proxy model for feature lists.
QgsFeatureListViewDelegate(QgsFeatureListModel *listModel, QObject *parent=nullptr)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
Item selection model for selecting features.
bool isEdited
True if feature has been edited.
bool isNew
True if feature is a newly added feature.