QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
16 #include "qgsbrushstylecombobox.h"
17 
18 #include <QList>
19 #include <QPair>
20 
21 #include <QBrush>
22 #include <QPainter>
23 #include <QPen>
24 
26  : QComboBox( parent )
27 {
29  styles << qMakePair( Qt::SolidPattern, tr( "Solid" ) )
30  << qMakePair( Qt::NoBrush, tr( "No Brush" ) )
31  << qMakePair( Qt::HorPattern, tr( "Horizontal" ) )
32  << qMakePair( Qt::VerPattern, tr( "Vertical" ) )
33  << qMakePair( Qt::CrossPattern, tr( "Cross" ) )
34  << qMakePair( Qt::BDiagPattern, tr( "BDiagonal" ) )
35  << qMakePair( Qt::FDiagPattern, tr( "FDiagonal" ) )
36  << qMakePair( Qt::DiagCrossPattern, tr( "Diagonal X" ) )
37  << qMakePair( Qt::Dense1Pattern, tr( "Dense 1" ) )
38  << qMakePair( Qt::Dense2Pattern, tr( "Dense 2" ) )
39  << qMakePair( Qt::Dense3Pattern, tr( "Dense 3" ) )
40  << qMakePair( Qt::Dense4Pattern, tr( "Dense 4" ) )
41  << qMakePair( Qt::Dense5Pattern, tr( "Dense 5" ) )
42  << qMakePair( Qt::Dense6Pattern, tr( "Dense 6" ) )
43  << qMakePair( Qt::Dense7Pattern, tr( "Dense 7" ) );
44 
45  setIconSize( QSize( 32, 16 ) );
46 
47  for ( int i = 0; i < styles.count(); i++ )
48  {
49  Qt::BrushStyle style = styles.at( i ).first;
50  QString name = styles.at( i ).second;
51  addItem( iconForBrush( style ), name, QVariant(( int )style ) );
52  }
53 
54  setCurrentIndex( 1 );
55 
56 }
57 
58 
59 Qt::BrushStyle QgsBrushStyleComboBox::brushStyle() const
60 {
61  return ( Qt::BrushStyle ) itemData( currentIndex() ).toInt();
62 }
63 
65 {
66  int idx = findData( QVariant(( int )style ) );
67  setCurrentIndex( idx == -1 ? 0 : idx );
68 }
69 
71 {
72  QPixmap pix( iconSize() );
73  QPainter p;
74  pix.fill( Qt::transparent );
75 
76  p.begin( &pix );
77  QBrush brush( QColor( 100, 100, 100 ), style );
78  p.setBrush( brush );
79  QPen pen( Qt::NoPen );
80  p.setPen( pen );
81  p.drawRect( QRect( QPoint( 0, 0 ), iconSize() ) );
82  p.end();
83 
84  return QIcon( pix );
85 }
bool end()
void fill(const QColor &color)
QStyle * style() const
const T & at(int i) const
void setIconSize(const QSize &size)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setBrushStyle(Qt::BrushStyle style)
void addItem(const QString &text, const QVariant &userData)
void drawRect(const QRectF &rectangle)
const char * name() const
int count(const T &value) const
int toInt(bool *ok) const
void setPen(const QColor &color)
QIcon iconForBrush(Qt::BrushStyle style)
void setBrush(const QBrush &brush)
Qt::BrushStyle brushStyle() const
QVariant itemData(int index, int role) const
QgsBrushStyleComboBox(QWidget *parent=nullptr)
int findData(const QVariant &data, int role, QFlags< Qt::MatchFlag > flags) const
void setCurrentIndex(int index)
bool begin(QPaintDevice *device)