QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
63{
64 return ( Qt::BrushStyle ) currentData().toInt();
65}
66
67void QgsBrushStyleComboBox::setBrushStyle( Qt::BrushStyle style )
68{
69 const int idx = findData( QVariant( static_cast<int>( style ) ) );
70 setCurrentIndex( idx == -1 ? 0 : idx );
71}
72
73QIcon QgsBrushStyleComboBox::iconForBrush( Qt::BrushStyle style )
74{
75 QPixmap pix( iconSize() );
76 QPainter p;
77 pix.fill( Qt::transparent );
78
79 p.begin( &pix );
80 const QBrush brush( view()->palette().color( QPalette::Text ), style );
81 p.setBrush( brush );
82 const QPen pen( Qt::NoPen );
83 p.setPen( pen );
84 p.drawRect( QRect( QPoint( 0, 0 ), iconSize() ) );
85 p.end();
86
87 return QIcon( pix );
88}
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,...