QGIS API Documentation  3.6.0-Noosa (5873452)
qgspenstylecombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspenstylecombobox.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 "qgspenstylecombobox.h"
17 
18 #include "qgsapplication.h"
19 #include "qgsguiutils.h"
20 
21 #include <QList>
22 #include <QPair>
23 
24 #include <QPainter>
25 #include <QPen>
26 
28  : QComboBox( parent )
29 {
30  QList < QPair<Qt::PenStyle, QString> > styles;
31  styles << qMakePair( Qt::SolidLine, tr( "Solid Line" ) )
32  << qMakePair( Qt::NoPen, tr( "No Pen" ) )
33  << qMakePair( Qt::DashLine, tr( "Dash Line" ) )
34  << qMakePair( Qt::DotLine, tr( "Dot Line" ) )
35  << qMakePair( Qt::DashDotLine, tr( "Dash Dot Line" ) )
36  << qMakePair( Qt::DashDotDotLine, tr( "Dash Dot Dot Line" ) );
37 
38  int iconSize = QgsGuiUtils::scaleIconSize( 16 );
39  setIconSize( QSize( iconSize * 2, iconSize ) );
40 
41  for ( int i = 0; i < styles.count(); i++ )
42  {
43  Qt::PenStyle style = styles.at( i ).first;
44  QString name = styles.at( i ).second;
45  addItem( iconForPen( style ), name, QVariant( ( int ) style ) );
46  }
47 }
48 
49 Qt::PenStyle QgsPenStyleComboBox::penStyle() const
50 {
51  return ( Qt::PenStyle ) currentData().toInt();
52 }
53 
54 void QgsPenStyleComboBox::setPenStyle( Qt::PenStyle style )
55 {
56  int idx = findData( QVariant( ( int ) style ) );
57  setCurrentIndex( idx == -1 ? 0 : idx );
58 }
59 
60 QIcon QgsPenStyleComboBox::iconForPen( Qt::PenStyle style )
61 {
62  QPixmap pix( iconSize() );
63  QPainter p;
64  pix.fill( Qt::transparent );
65 
66  p.begin( &pix );
67  QPen pen( style );
68  pen.setWidth( 2 );
69  p.setPen( pen );
70  double mid = iconSize().height() / 2.0;
71  p.drawLine( 0, mid, iconSize().width(), mid );
72  p.end();
73 
74  return QIcon( pix );
75 }
76 
77 
79 // join
80 
82  : QComboBox( parent )
83 {
84  QString path = QgsApplication::defaultThemePath();
85  addItem( QIcon( path + "/join_bevel.svg" ), tr( "Bevel" ), QVariant( Qt::BevelJoin ) );
86  addItem( QIcon( path + "/join_miter.svg" ), tr( "Miter" ), QVariant( Qt::MiterJoin ) );
87  addItem( QIcon( path + "/join_round.svg" ), tr( "Round" ), QVariant( Qt::RoundJoin ) );
88 }
89 
90 Qt::PenJoinStyle QgsPenJoinStyleComboBox::penJoinStyle() const
91 {
92  return ( Qt::PenJoinStyle ) currentData().toInt();
93 }
94 
95 void QgsPenJoinStyleComboBox::setPenJoinStyle( Qt::PenJoinStyle style )
96 {
97  int idx = findData( QVariant( style ) );
98  setCurrentIndex( idx == -1 ? 0 : idx );
99 }
100 
101 
103 // cap
104 
106  : QComboBox( parent )
107 {
108  QString path = QgsApplication::defaultThemePath();
109  addItem( QIcon( path + "/cap_square.svg" ), tr( "Square" ), QVariant( Qt::SquareCap ) );
110  addItem( QIcon( path + "/cap_flat.svg" ), tr( "Flat" ), QVariant( Qt::FlatCap ) );
111  addItem( QIcon( path + "/cap_round.svg" ), tr( "Round" ), QVariant( Qt::RoundCap ) );
112 }
113 
114 Qt::PenCapStyle QgsPenCapStyleComboBox::penCapStyle() const
115 {
116  return ( Qt::PenCapStyle ) currentData().toInt();
117 }
118 
119 void QgsPenCapStyleComboBox::setPenCapStyle( Qt::PenCapStyle style )
120 {
121  int idx = findData( QVariant( style ) );
122  setCurrentIndex( idx == -1 ? 0 : idx );
123 }
void setPenStyle(Qt::PenStyle style)
static QString defaultThemePath()
Returns the path to the default theme directory.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
void setPenCapStyle(Qt::PenCapStyle style)
QgsPenJoinStyleComboBox(QWidget *parent=nullptr)
Qt::PenJoinStyle penJoinStyle() const
void setPenJoinStyle(Qt::PenJoinStyle style)
QIcon iconForPen(Qt::PenStyle style)
Qt::PenCapStyle penCapStyle() const
QgsPenCapStyleComboBox(QWidget *parent=nullptr)
Qt::PenStyle penStyle() const
QgsPenStyleComboBox(QWidget *parent=nullptr)