QGIS API Documentation  master-6164ace
src/gui/symbology-ng/qgspenstylecombobox.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgspenstylecombobox.cpp
00003     ---------------------
00004     begin                : November 2009
00005     copyright            : (C) 2009 by Martin Dobias
00006     email                : wonder dot sk at gmail dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgspenstylecombobox.h"
00017 
00018 #include "qgsapplication.h"
00019 
00020 #include <QList>
00021 #include <QPair>
00022 
00023 #include <QPainter>
00024 #include <QPen>
00025 
00026 QgsPenStyleComboBox::QgsPenStyleComboBox( QWidget* parent )
00027     : QComboBox( parent )
00028 {
00029   QList < QPair<Qt::PenStyle, QString> > styles;
00030   styles << qMakePair( Qt::SolidLine, tr( "Solid Line" ) )
00031   << qMakePair( Qt::NoPen, tr( "No Pen" ) )
00032   << qMakePair( Qt::DashLine, tr( "Dash Line" ) )
00033   << qMakePair( Qt::DotLine, tr( "Dot Line" ) )
00034   << qMakePair( Qt::DashDotLine, tr( "Dash Dot Line" ) )
00035   << qMakePair( Qt::DashDotDotLine, tr( "Dash Dot Dot Line" ) );
00036 
00037   setIconSize( QSize( 32, 12 ) );
00038 
00039   for ( int i = 0; i < styles.count(); i++ )
00040   {
00041     Qt::PenStyle style = styles.at( i ).first;
00042     QString name = styles.at( i ).second;
00043     addItem( iconForPen( style ), name, QVariant( style ) );
00044   }
00045 }
00046 
00047 Qt::PenStyle QgsPenStyleComboBox::penStyle() const
00048 {
00049   return ( Qt::PenStyle ) itemData( currentIndex() ).toInt();
00050 }
00051 
00052 void QgsPenStyleComboBox::setPenStyle( Qt::PenStyle style )
00053 {
00054   int idx = findData( QVariant( style ) );
00055   setCurrentIndex( idx == -1 ? 0 : idx );
00056 }
00057 
00058 QIcon QgsPenStyleComboBox::iconForPen( Qt::PenStyle style )
00059 {
00060   QPixmap pix( iconSize() );
00061   QPainter p;
00062   pix.fill( Qt::transparent );
00063 
00064   p.begin( &pix );
00065   QPen pen( style );
00066   pen.setWidth( 2 );
00067   p.setPen( pen );
00068   double mid = iconSize().height() / 2.0;
00069   p.drawLine( 0, mid, iconSize().width(), mid );
00070   p.end();
00071 
00072   return QIcon( pix );
00073 }
00074 
00075 
00077 // join
00078 
00079 QgsPenJoinStyleComboBox::QgsPenJoinStyleComboBox( QWidget* parent )
00080     : QComboBox( parent )
00081 {
00082   QString path = QgsApplication::defaultThemePath();
00083   addItem( QIcon( path + "/join_bevel.png" ), tr( "Bevel" ), QVariant( Qt::BevelJoin ) );
00084   addItem( QIcon( path + "/join_miter.png" ), tr( "Miter" ), QVariant( Qt::MiterJoin ) );
00085   addItem( QIcon( path + "/join_round.png" ), tr( "Round" ), QVariant( Qt::RoundJoin ) );
00086 }
00087 
00088 Qt::PenJoinStyle QgsPenJoinStyleComboBox::penJoinStyle() const
00089 {
00090   return ( Qt::PenJoinStyle ) itemData( currentIndex() ).toInt();
00091 }
00092 
00093 void QgsPenJoinStyleComboBox::setPenJoinStyle( Qt::PenJoinStyle style )
00094 {
00095   int idx = findData( QVariant( style ) );
00096   setCurrentIndex( idx == -1 ? 0 : idx );
00097 }
00098 
00099 
00101 // cap
00102 
00103 QgsPenCapStyleComboBox::QgsPenCapStyleComboBox( QWidget* parent )
00104     : QComboBox( parent )
00105 {
00106   QString path = QgsApplication::defaultThemePath();
00107   addItem( QIcon( path + "/cap_square.png" ), tr( "Square" ), QVariant( Qt::SquareCap ) );
00108   addItem( QIcon( path + "/cap_flat.png" ), tr( "Flat" ), QVariant( Qt::FlatCap ) );
00109   addItem( QIcon( path + "/cap_round.png" ), tr( "Round" ), QVariant( Qt::RoundCap ) );
00110 }
00111 
00112 Qt::PenCapStyle QgsPenCapStyleComboBox::penCapStyle() const
00113 {
00114   return ( Qt::PenCapStyle ) itemData( currentIndex() ).toInt();
00115 }
00116 
00117 void QgsPenCapStyleComboBox::setPenCapStyle( Qt::PenCapStyle style )
00118 {
00119   int idx = findData( QVariant( style ) );
00120   setCurrentIndex( idx == -1 ? 0 : idx );
00121 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines