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