QGIS API Documentation 3.39.0-Master (67e056379ed)
Loading...
Searching...
No Matches
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
23QgsDashSpaceWidget::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
62void 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
73void 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
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
107QgsDashSpaceDialog::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
121{
122 return mWidget->dashDotVector();
123}
124
126{
127 mWidget->setUnit( unit );
128}
RenderUnit
Rendering size units.
Definition qgis.h:4494
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setUnit(Qgis::RenderUnit unit)
Sets the unit type used for the dash space pattern (used to update interface labels)
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.
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(Qgis::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(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.