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