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