QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsbrushstylecombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsbrushstylecombobox.cpp
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
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 ***************************************************************************/
15
17#include "moc_qgsbrushstylecombobox.cpp"
18#include "qgsguiutils.h"
19
20#include <QList>
21#include <QPair>
22
23#include <QBrush>
24#include <QPainter>
25#include <QPen>
26#include <QAbstractItemView>
27
29 : QComboBox( parent )
30{
31 QList < QPair<Qt::BrushStyle, QString> > styles;
32 styles << qMakePair( Qt::SolidPattern, tr( "Solid" ) )
33 << qMakePair( Qt::NoBrush, tr( "No Brush" ) )
34 << qMakePair( Qt::HorPattern, tr( "Horizontal" ) )
35 << qMakePair( Qt::VerPattern, tr( "Vertical" ) )
36 << qMakePair( Qt::CrossPattern, tr( "Cross" ) )
37 << qMakePair( Qt::BDiagPattern, tr( "BDiagonal" ) )
38 << qMakePair( Qt::FDiagPattern, tr( "FDiagonal" ) )
39 << qMakePair( Qt::DiagCrossPattern, tr( "Diagonal X" ) )
40 << qMakePair( Qt::Dense1Pattern, tr( "Dense 1" ) )
41 << qMakePair( Qt::Dense2Pattern, tr( "Dense 2" ) )
42 << qMakePair( Qt::Dense3Pattern, tr( "Dense 3" ) )
43 << qMakePair( Qt::Dense4Pattern, tr( "Dense 4" ) )
44 << qMakePair( Qt::Dense5Pattern, tr( "Dense 5" ) )
45 << qMakePair( Qt::Dense6Pattern, tr( "Dense 6" ) )
46 << qMakePair( Qt::Dense7Pattern, tr( "Dense 7" ) );
47
48 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
49 setIconSize( QSize( iconSize * 2, iconSize ) );
50
51 for ( int i = 0; i < styles.count(); i++ )
52 {
53 const Qt::BrushStyle style = styles.at( i ).first;
54 const QString name = styles.at( i ).second;
55 addItem( iconForBrush( style ), name, QVariant( static_cast<int>( style ) ) );
56 }
57
58 setCurrentIndex( 1 );
59
60}
61
62
64{
65 return ( Qt::BrushStyle ) currentData().toInt();
66}
67
68void QgsBrushStyleComboBox::setBrushStyle( Qt::BrushStyle style )
69{
70 const int idx = findData( QVariant( static_cast<int>( style ) ) );
71 setCurrentIndex( idx == -1 ? 0 : idx );
72}
73
74QIcon QgsBrushStyleComboBox::iconForBrush( Qt::BrushStyle style )
75{
76 QPixmap pix( iconSize() );
77 QPainter p;
78 pix.fill( Qt::transparent );
79
80 p.begin( &pix );
81 const QBrush brush( view()->palette().color( QPalette::Text ), style );
82 p.setBrush( brush );
83 const QPen pen( Qt::NoPen );
84 p.setPen( pen );
85 p.drawRect( QRect( QPoint( 0, 0 ), iconSize() ) );
86 p.end();
87
88 return QIcon( pix );
89}
Qt::BrushStyle brushStyle() const
QIcon iconForBrush(Qt::BrushStyle style)
QgsBrushStyleComboBox(QWidget *parent=nullptr)
void setBrushStyle(Qt::BrushStyle style)
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...