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