QGIS API Documentation  3.23.0-Master (dd0cd13a00)
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 "qgsdoublevalidator.h"
18 #include "qgsapplication.h"
19 
20 #include <QDialogButtonBox>
21 #include <QFile>
22 
23 QgsDashSpaceWidget::QgsDashSpaceWidget( const QVector<qreal> &vectorPattern, QWidget *parent ) : QgsPanelWidget( parent )
24 {
25  setupUi( this );
26 
27  mAddButton->setIcon( QgsApplication::getThemeIcon( "symbologyAdd.svg" ) );
28  mRemoveButton->setIcon( QgsApplication::getThemeIcon( "symbologyRemove.svg" ) );
29 
30  double dash = 0;
31  double space = 0;
32  for ( int i = 0; i < ( vectorPattern.size() - 1 ); ++i )
33  {
34  dash = vectorPattern.at( i );
35  ++i;
36  space = vectorPattern.at( i );
37  QTreeWidgetItem *entry = new QTreeWidgetItem();
38  entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
39  entry->setText( 0, QLocale().toString( dash ) );
40  entry->setText( 1, QLocale().toString( space ) );
41  mDashSpaceTreeWidget->addTopLevelItem( entry );
42  }
43 
44  connect( mAddButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mAddButton_clicked );
45  connect( mRemoveButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mRemoveButton_clicked );
46  connect( mDashSpaceTreeWidget, &QTreeWidget::itemChanged, this, [ this ] { emit widgetChanged(); } );
47 }
48 
49 void QgsDashSpaceWidget::mAddButton_clicked()
50 {
51  //add new (default) item
52  QTreeWidgetItem *entry = new QTreeWidgetItem();
53  entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
54  entry->setText( 0, QStringLiteral( "5" ) );
55  entry->setText( 1, QStringLiteral( "2" ) );
56  mDashSpaceTreeWidget->addTopLevelItem( entry );
57  emit widgetChanged();
58 }
59 
60 void QgsDashSpaceWidget::mRemoveButton_clicked()
61 {
62  //get active item
63  QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->currentItem();
64  if ( currentItem )
65  {
66  mDashSpaceTreeWidget->takeTopLevelItem( mDashSpaceTreeWidget->indexOfTopLevelItem( currentItem ) );
67  }
68  emit widgetChanged();
69 }
70 
71 QVector<qreal> QgsDashSpaceWidget::dashDotVector() const
72 {
73  QVector<qreal> dashVector;
74  const int nTopLevelItems = mDashSpaceTreeWidget->topLevelItemCount();
75  for ( int i = 0; i < nTopLevelItems; ++i )
76  {
77  QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->topLevelItem( i );
78  if ( currentItem )
79  {
80  dashVector << QgsDoubleValidator::toDouble( currentItem->text( 0 ) ) << QgsDoubleValidator::toDouble( currentItem->text( 1 ) );
81  }
82  }
83  return dashVector;
84 }
85 
87 {
88  QTreeWidgetItem *headerItem = mDashSpaceTreeWidget->headerItem();
89  headerItem->setText( 0, QStringLiteral( "%1 (%2)" ).arg( tr( "Dash" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
90  headerItem->setText( 1, QStringLiteral( "%1 (%2)" ).arg( tr( "Space" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
91 }
92 
93 QgsDashSpaceDialog::QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent, Qt::WindowFlags f ) : QDialog( parent, f )
94 {
95  QVBoxLayout *vLayout = new QVBoxLayout();
96  mWidget = new QgsDashSpaceWidget( v );
97  vLayout->addWidget( mWidget );
98  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
99  connect( bbox, &QDialogButtonBox::accepted, this, &QgsDashSpaceDialog::accept );
100  connect( bbox, &QDialogButtonBox::rejected, this, &QgsDashSpaceDialog::reject );
101  vLayout->addWidget( bbox );
102  setLayout( vLayout );
103  setWindowTitle( tr( "Custom Dash Pattern" ) );
104 }
105 
106 QVector<qreal> QgsDashSpaceDialog::dashDotVector() const
107 {
108  return mWidget->dashDotVector();
109 }
110 
112 {
113  mWidget->setUnit( unit );
114 }
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsDashSpaceDialog(const QVector< qreal > &v, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Constructor for QgsDashSpaceDialog.
QVector< qreal > dashDotVector() const
Returns the dash pattern as a list of numbers.
void setUnit(QgsUnitTypes::RenderUnit unit)
Sets the unit type used for the dash space pattern (used to update interface labels)
A widget to enter a custom dash space pattern for lines.
QVector< qreal > dashDotVector() const
Returns the dash pattern as a list of numbers.
QgsDashSpaceWidget(const QVector< qreal > &vectorPattern, QWidget *parent=nullptr)
Constructor for QgsDashSpaceWidget.
void setUnit(QgsUnitTypes::RenderUnit unit)
Sets the unit type used for the dash space pattern (used to update interface labels)
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
Base class for any widget that can be shown as a inline panel.
void widgetChanged()
Emitted when the widget state changes.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168