QGIS API Documentation  3.25.0-Master (10b47c2603)
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 total = 0;
31  for ( int i = 0; i < ( vectorPattern.size() - 1 ); ++i )
32  {
33  const double dash = vectorPattern.at( i );
34  ++i;
35  const double space = vectorPattern.at( i );
36  total += dash + space;
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  mPatternLengthLabel->setText( QLocale().toString( total, 'f', 6 ) );
45 
46  connect( mAddButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mAddButton_clicked );
47  connect( mRemoveButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mRemoveButton_clicked );
48  connect( mDashSpaceTreeWidget, &QTreeWidget::itemChanged, this, [ this ] { emit widgetChanged(); } );
49 
50  connect( this, &QgsPanelWidget::widgetChanged, this, [ = ]
51  {
52  const QVector<qreal> pattern = dashDotVector();
53  double total = 0;
54  for ( qreal part : pattern )
55  {
56  total += part;
57  }
58  mPatternLengthLabel->setText( QLocale().toString( total, 'f', 6 ) );
59  } );
60 }
61 
62 void QgsDashSpaceWidget::mAddButton_clicked()
63 {
64  //add new (default) item
65  QTreeWidgetItem *entry = new QTreeWidgetItem();
66  entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
67  entry->setText( 0, QStringLiteral( "5" ) );
68  entry->setText( 1, QStringLiteral( "2" ) );
69  mDashSpaceTreeWidget->addTopLevelItem( entry );
70  emit widgetChanged();
71 }
72 
73 void QgsDashSpaceWidget::mRemoveButton_clicked()
74 {
75  //get active item
76  QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->currentItem();
77  if ( currentItem )
78  {
79  mDashSpaceTreeWidget->takeTopLevelItem( mDashSpaceTreeWidget->indexOfTopLevelItem( currentItem ) );
80  }
81  emit widgetChanged();
82 }
83 
84 QVector<qreal> QgsDashSpaceWidget::dashDotVector() const
85 {
86  QVector<qreal> dashVector;
87  const int nTopLevelItems = mDashSpaceTreeWidget->topLevelItemCount();
88  dashVector.reserve( nTopLevelItems * 2 );
89  for ( int i = 0; i < nTopLevelItems; ++i )
90  {
91  QTreeWidgetItem *currentItem = mDashSpaceTreeWidget->topLevelItem( i );
92  if ( currentItem )
93  {
94  dashVector << QgsDoubleValidator::toDouble( currentItem->text( 0 ) ) << QgsDoubleValidator::toDouble( currentItem->text( 1 ) );
95  }
96  }
97  return dashVector;
98 }
99 
101 {
102  QTreeWidgetItem *headerItem = mDashSpaceTreeWidget->headerItem();
103  headerItem->setText( 0, QStringLiteral( "%1 (%2)" ).arg( tr( "Dash" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
104  headerItem->setText( 1, QStringLiteral( "%1 (%2)" ).arg( tr( "Space" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
105 }
106 
107 QgsDashSpaceDialog::QgsDashSpaceDialog( const QVector<qreal> &v, QWidget *parent, Qt::WindowFlags f ) : QDialog( parent, f )
108 {
109  QVBoxLayout *vLayout = new QVBoxLayout();
110  mWidget = new QgsDashSpaceWidget( v );
111  vLayout->addWidget( mWidget );
112  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
113  connect( bbox, &QDialogButtonBox::accepted, this, &QgsDashSpaceDialog::accept );
114  connect( bbox, &QDialogButtonBox::rejected, this, &QgsDashSpaceDialog::reject );
115  vLayout->addWidget( bbox );
116  setLayout( vLayout );
117  setWindowTitle( tr( "Custom Dash Pattern" ) );
118 }
119 
120 QVector<qreal> QgsDashSpaceDialog::dashDotVector() const
121 {
122  return mWidget->dashDotVector();
123 }
124 
126 {
127  mWidget->setUnit( unit );
128 }
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