QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 "qgsguiutils.h"
18
19#include <QList>
20#include <QPair>
21
22#include <QBrush>
23#include <QPainter>
24#include <QPen>
25#include <QAbstractItemView>
26
28 : QComboBox( parent )
29{
30 QList < QPair<Qt::BrushStyle, QString> > styles;
31 styles << qMakePair( Qt::SolidPattern, tr( "Solid" ) )
32 << qMakePair( Qt::NoBrush, tr( "No Brush" ) )
33 << qMakePair( Qt::HorPattern, tr( "Horizontal" ) )
34 << qMakePair( Qt::VerPattern, tr( "Vertical" ) )
35 << qMakePair( Qt::CrossPattern, tr( "Cross" ) )
36 << qMakePair( Qt::BDiagPattern, tr( "BDiagonal" ) )
37 << qMakePair( Qt::FDiagPattern, tr( "FDiagonal" ) )
38 << qMakePair( Qt::DiagCrossPattern, tr( "Diagonal X" ) )
39 << qMakePair( Qt::Dense1Pattern, tr( "Dense 1" ) )
40 << qMakePair( Qt::Dense2Pattern, tr( "Dense 2" ) )
41 << qMakePair( Qt::Dense3Pattern, tr( "Dense 3" ) )
42 << qMakePair( Qt::Dense4Pattern, tr( "Dense 4" ) )
43 << qMakePair( Qt::Dense5Pattern, tr( "Dense 5" ) )
44 << qMakePair( Qt::Dense6Pattern, tr( "Dense 6" ) )
45 << qMakePair( Qt::Dense7Pattern, tr( "Dense 7" ) );
46
47 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
48 setIconSize( QSize( iconSize * 2, iconSize ) );
49
50 for ( int i = 0; i < styles.count(); i++ )
51 {
52 const Qt::BrushStyle style = styles.at( i ).first;
53 const QString name = styles.at( i ).second;
54 addItem( iconForBrush( style ), name, QVariant( static_cast<int>( style ) ) );
55 }
56
57 setCurrentIndex( 1 );
58
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,...