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