QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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 )
25 : QgsPanelWidget( parent )
26{
27 setupUi( this );
28
29 mAddButton->setIcon( QgsApplication::getThemeIcon( "symbologyAdd.svg" ) );
30 mRemoveButton->setIcon( QgsApplication::getThemeIcon( "symbologyRemove.svg" ) );
31
32 double total = 0;
33 for ( int i = 0; i < ( vectorPattern.size() - 1 ); ++i )
34 {
35 const double dash = vectorPattern.at( i );
36 ++i;
37 const double space = vectorPattern.at( i );
38 total += dash + space;
39 QTreeWidgetItem *entry = new QTreeWidgetItem();
40 entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
41 entry->setText( 0, QLocale().toString( dash ) );
42 entry->setText( 1, QLocale().toString( space ) );
43 mDashSpaceTreeWidget->addTopLevelItem( entry );
44 }
45
46 mPatternLengthLabel->setText( QLocale().toString( total, 'f', 6 ) );
47
48 connect( mAddButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mAddButton_clicked );
49 connect( mRemoveButton, &QPushButton::clicked, this, &QgsDashSpaceWidget::mRemoveButton_clicked );
50 connect( mDashSpaceTreeWidget, &QTreeWidget::itemChanged, this, [this] { emit widgetChanged(); } );
51
52 connect( this, &QgsPanelWidget::widgetChanged, this, [=] {
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 )
109 : QDialog( parent, f )
110{
111 QVBoxLayout *vLayout = new QVBoxLayout();
112 mWidget = new QgsDashSpaceWidget( v );
113 vLayout->addWidget( mWidget );
114 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
115 connect( bbox, &QDialogButtonBox::accepted, this, &QgsDashSpaceDialog::accept );
116 connect( bbox, &QDialogButtonBox::rejected, this, &QgsDashSpaceDialog::reject );
117 vLayout->addWidget( bbox );
118 setLayout( vLayout );
119 setWindowTitle( tr( "Custom Dash Pattern" ) );
120}
121
123{
124 return mWidget->dashDotVector();
125}
126
128{
129 mWidget->setUnit( unit );
130}
RenderUnit
Rendering size units.
Definition qgis.h:4892
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.