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