QGIS API Documentation  3.0.2-Girona (307d082)
qgsdashspacedialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdashspacedialog.cpp
3  ----------------------
4  begin : January 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco at hugis dot net
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 "qgsdashspacedialog.h"
17 #include "qgsapplication.h"
18 #include <QFile>
19 
20 QString iconPath( const QString &iconFile )
21 {
22  // try active theme
23  QString path = QgsApplication::activeThemePath();
24  if ( QFile::exists( path + iconFile ) )
25  return path + iconFile;
26 
27  // use default theme
28  return QgsApplication::defaultThemePath() + iconFile;
29 }
30 
31 QgsDashSpaceDialog::QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent, Qt::WindowFlags f ): QDialog( parent, f )
32 {
33  setupUi( this );
34  connect( mAddButton, &QPushButton::clicked, this, &QgsDashSpaceDialog::mAddButton_clicked );
35  connect( mRemoveButton, &QPushButton::clicked, this, &QgsDashSpaceDialog::mRemoveButton_clicked );
36 
37  mAddButton->setIcon( QIcon( iconPath( "symbologyAdd.svg" ) ) );
38  mRemoveButton->setIcon( QIcon( iconPath( "symbologyRemove.svg" ) ) );
39 
40  double dash = 0;
41  double space = 0;
42  for ( int i = 0; i < ( v.size() - 1 ); ++i )
43  {
44  dash = v.at( i );
45  ++i;
46  space = v.at( i );
47  QTreeWidgetItem *entry = new QTreeWidgetItem();
48  entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
49  entry->setText( 0, QString::number( dash ) );
50  entry->setText( 1, QString::number( space ) );
51  mDashSpaceTreeWidget->addTopLevelItem( entry );
52  }
53 }
54 
55 void QgsDashSpaceDialog::mAddButton_clicked()
56 {
57  //add new (default) item
58  QTreeWidgetItem *entry = new QTreeWidgetItem();
59  entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
60  entry->setText( 0, QStringLiteral( "5" ) );
61  entry->setText( 1, QStringLiteral( "2" ) );
62  mDashSpaceTreeWidget->addTopLevelItem( entry );
63 }
64 
65 void QgsDashSpaceDialog::mRemoveButton_clicked()
66 {
67  //get active item
68  QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->currentItem();
69  if ( currentItem )
70  {
71  mDashSpaceTreeWidget->takeTopLevelItem( mDashSpaceTreeWidget->indexOfTopLevelItem( currentItem ) );
72  }
73 }
74 
75 QVector<qreal> QgsDashSpaceDialog::dashDotVector() const
76 {
77  QVector<qreal> dashVector;
78  int nTopLevelItems = mDashSpaceTreeWidget->topLevelItemCount();
79  for ( int i = 0; i < nTopLevelItems; ++i )
80  {
81  QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->topLevelItem( i );
82  if ( currentItem )
83  {
84  dashVector << currentItem->text( 0 ).toDouble() << currentItem->text( 1 ).toDouble();
85  }
86  }
87  return dashVector;
88 }
89 
static QString defaultThemePath()
Returns the path to the default theme directory.
QString iconPath(const QString &iconFile)
QgsDashSpaceDialog(const QVector< qreal > &v, QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
Constructor for QgsDashSpaceDialog.
QVector< qreal > dashDotVector() const
static QString activeThemePath()
Returns the path to the currently active theme directory.