QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
28  QList < QPair<Qt::BrushStyle, QString> > styles;
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 
64 void QgsBrushStyleComboBox::setBrushStyle( Qt::BrushStyle style )
65 {
66  int idx = findData( QVariant(( int )style ) );
67  setCurrentIndex( idx == -1 ? 0 : idx );
68 }
69 
70 QIcon QgsBrushStyleComboBox::iconForBrush( Qt::BrushStyle style )
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 }