QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgstabpositionwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstabpositionwidget.h
3 ---------------------
4 begin : October 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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
17
18#include "qgsapplication.h"
19#include "qgsdoublevalidator.h"
20#include "qgsunittypes.h"
21
22#include <QDialogButtonBox>
23#include <QString>
24
25#include "moc_qgstabpositionwidget.cpp"
26
27using namespace Qt::StringLiterals;
28
30 : QgsPanelWidget( parent )
31{
32 setupUi( this );
33
34 mAddButton->setIcon( QgsApplication::getThemeIcon( "symbologyAdd.svg" ) );
35 mRemoveButton->setIcon( QgsApplication::getThemeIcon( "symbologyRemove.svg" ) );
36
38
39 connect( mAddButton, &QPushButton::clicked, this, &QgsTabPositionWidget::mAddButton_clicked );
40 connect( mRemoveButton, &QPushButton::clicked, this, &QgsTabPositionWidget::mRemoveButton_clicked );
41 connect( mTabPositionTreeWidget, &QTreeWidget::itemChanged, this, &QgsTabPositionWidget::emitPositionsChanged );
42}
43
44void QgsTabPositionWidget::setPositions( const QList<QgsTextFormat::Tab> &positions )
45{
46 mTabPositionTreeWidget->clear();
47 for ( const QgsTextFormat::Tab &tab : positions )
48 {
49 QTreeWidgetItem *entry = new QTreeWidgetItem();
50 entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
51 entry->setText( 0, QLocale().toString( tab.position() ) );
52 entry->setData( 0, Qt::EditRole, tab.position() );
53 mTabPositionTreeWidget->addTopLevelItem( entry );
54 }
55}
56
57QList<QgsTextFormat::Tab> QgsTabPositionWidget::positions() const
58{
59 QList<QgsTextFormat::Tab> result;
60 const int nTopLevelItems = mTabPositionTreeWidget->topLevelItemCount();
61 result.reserve( nTopLevelItems );
62 for ( int i = 0; i < nTopLevelItems; ++i )
63 {
64 if ( QTreeWidgetItem *currentItem = mTabPositionTreeWidget->topLevelItem( i ) )
65 {
66 result << QgsTextFormat::Tab( QgsDoubleValidator::toDouble( currentItem->text( 0 ) ) );
67 }
68 }
69
70 std::sort( result.begin(), result.end(), []( const QgsTextFormat::Tab &a, const QgsTextFormat::Tab &b ) { return a.position() < b.position(); } );
71
72 return result;
73}
74
76{
77 QTreeWidgetItem *headerItem = mTabPositionTreeWidget->headerItem();
78 headerItem->setText( 0, u"%1 (%2)"_s.arg( tr( "Position" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
79}
80
81void QgsTabPositionWidget::mAddButton_clicked()
82{
83 const QList<QgsTextFormat::Tab> currentPositions = positions();
84 double newPosition = 6;
85 if ( !currentPositions.empty() )
86 {
87 newPosition = currentPositions.last().position() + 6;
88 }
89
90 QTreeWidgetItem *entry = new QTreeWidgetItem();
91 entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
92 entry->setText( 0, QLocale().toString( newPosition ) );
93 entry->setData( 0, Qt::EditRole, newPosition );
94 mTabPositionTreeWidget->addTopLevelItem( entry );
95 emitPositionsChanged();
96}
97
98void QgsTabPositionWidget::mRemoveButton_clicked()
99{
100 if ( QTreeWidgetItem *currentItem = mTabPositionTreeWidget->currentItem() )
101 {
102 mTabPositionTreeWidget->takeTopLevelItem( mTabPositionTreeWidget->indexOfTopLevelItem( currentItem ) );
103 }
104 emitPositionsChanged();
105}
106
107void QgsTabPositionWidget::emitPositionsChanged()
108{
109 emit positionsChanged( positions() );
110}
111
112
113QgsTabPositionDialog::QgsTabPositionDialog( QWidget *parent, Qt::WindowFlags f )
114 : QDialog( parent, f )
115{
116 QVBoxLayout *vLayout = new QVBoxLayout();
117 mWidget = new QgsTabPositionWidget();
118 vLayout->addWidget( mWidget );
119 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
120 connect( bbox, &QDialogButtonBox::accepted, this, &QgsTabPositionDialog::accept );
121 connect( bbox, &QDialogButtonBox::rejected, this, &QgsTabPositionDialog::reject );
122 vLayout->addWidget( bbox );
123 setLayout( vLayout );
124 setWindowTitle( tr( "Tab Positions" ) );
125}
126
127void QgsTabPositionDialog::setPositions( const QList<QgsTextFormat::Tab> &positions )
128{
129 mWidget->setPositions( positions );
130}
131
132QList<QgsTextFormat::Tab> QgsTabPositionDialog::positions() const
133{
134 return mWidget->positions();
135}
136
138{
139 mWidget->setUnit( unit );
140}
RenderUnit
Rendering size units.
Definition qgis.h:5340
@ Millimeters
Millimeters.
Definition qgis.h:5341
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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.
QList< QgsTextFormat::Tab > positions() const
Returns the tab positions defined in the dialog.
QgsTabPositionDialog(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Constructor for QgsTabPositionDialog.
void setPositions(const QList< QgsTextFormat::Tab > &positions)
Sets the tab positions to show in the dialog.
void setUnit(Qgis::RenderUnit unit)
Sets the unit type used for the tab positions (used to update interface labels).
A widget for configuring QgsTextFormat tab positions.
void setUnit(Qgis::RenderUnit unit)
Sets the unit type used for the tab positions (used to update interface labels).
void setPositions(const QList< QgsTextFormat::Tab > &positions)
Sets the tab positions to show in the widget.
QgsTabPositionWidget(QWidget *parent=nullptr)
Constructor for QgsTabPositionWidget, with the specified parent widget.
void positionsChanged(const QList< QgsTextFormat::Tab > &positions)
Emitted when positions are changed in the widget.
QList< QgsTextFormat::Tab > positions() const
Returns the tab positions defined in the widget.
Defines a tab position for a text format.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.