QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgsfeaturelistviewdelegate.cpp"
17#include "qgsvectorlayer.h"
19#include "qgsfeaturelistmodel.h"
20#include "qgsapplication.h"
23
24#include <QHBoxLayout>
25#include <QPushButton>
26#include <QLabel>
27#include <QApplication>
28#include <QMouseEvent>
29#include <QObject>
30
32 : QItemDelegate( parent )
33 , mListModel( listModel )
34 , mCurrentFeatureEdited( false )
35{
36}
37
39{
40 if ( pos.x() > ICON_SIZE )
41 {
42 return EditElement;
43 }
44 else
45 {
46 return SelectionElement;
47 }
48}
49
51{
52 mFeatureSelectionModel = featureSelectionModel;
53}
54
56{
57 mCurrentFeatureEdited = state;
58}
59
60void QgsFeatureListViewDelegate::setEditSelectionModel( QItemSelectionModel *editSelectionModel )
61{
62 mEditSelectionModel = editSelectionModel;
63}
64
65QSize QgsFeatureListViewDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
66{
67 Q_UNUSED( index )
68 const int height = ICON_SIZE;
69 return QSize( option.rect.width(), std::max( height, option.fontMetrics.height() ) );
70}
71
72void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
73{
74 const bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
75
76 QStyleOptionViewItem textOption = option;
77 textOption.state |= QStyle::State_Enabled;
78 if ( isEditSelection )
79 {
80 textOption.state |= QStyle::State_Selected;
81 }
82 drawBackground( painter, textOption, index );
83
84
85 static QPixmap sSelectedIcon;
86 if ( sSelectedIcon.isNull() )
87 sSelectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconSelected.svg" ) );
88 static QPixmap sDeselectedIcon;
89 if ( sDeselectedIcon.isNull() )
90 sDeselectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconDeselected.svg" ) );
91
92 const QString text = index.model()->data( index, Qt::EditRole ).toString();
94
95 // Icon layout options
96 const QStyleOptionViewItem iconOption;
97
98 QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
99
100 QPixmap icon = mFeatureSelectionModel->isSelected( index ) ? sSelectedIcon : sDeselectedIcon;
101
102 // Scale up the icon if needed
103 if ( option.rect.height() > ICON_SIZE )
104 {
105 icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
106 }
107
108 drawDecoration( painter, iconOption, iconLayoutBounds, icon );
109
110 // if conditional style also has an icon, draw that too
111 const QVariant conditionalIcon = index.model()->data( index, Qt::DecorationRole );
112 if ( conditionalIcon.isValid() )
113 {
114 const QPixmap pixmap = conditionalIcon.value<QPixmap>();
115 iconLayoutBounds.moveLeft( iconLayoutBounds.x() + icon.width() + QFontMetrics( textOption.font ).horizontalAdvance( 'X' ) );
116 iconLayoutBounds.setTop( option.rect.y() + ( option.rect.height() - pixmap.height() ) / 2.0 );
117 iconLayoutBounds.setHeight( pixmap.height() );
118 drawDecoration( painter, iconOption, iconLayoutBounds, pixmap );
119 }
120
121 // Text layout options
122 const QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
123
124 // start with font and foreground color from model's FontRole
125 const QVariant font = index.model()->data( index, Qt::FontRole );
126 if ( font.isValid() )
127 {
128 textOption.font = font.value<QFont>();
129 }
130 const QVariant textColor = index.model()->data( index, Qt::ForegroundRole );
131 if ( textColor.isValid() )
132 {
133 textOption.palette.setColor( QPalette::Text, textColor.value<QColor>() );
134 }
135
136 if ( featInfo.isNew )
137 {
138 textOption.font.setStyle( QFont::StyleItalic );
139 textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
140 textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
141 }
142 else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
143 {
144 textOption.font.setStyle( QFont::StyleItalic );
145 textOption.palette.setColor( QPalette::Text, Qt::red );
146 textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
147 }
148
149 drawDisplay( painter, textOption, textLayoutBounds, text );
150}
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.
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
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)
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
bool isEdited
True if feature has been edited.
bool isNew
True if feature is a newly added feature.