QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgstabpositionwidget.cpp"
18#include "qgsapplication.h"
19#include "qgsdoublevalidator.h"
20#include "qgsunittypes.h"
21
22#include <QDialogButtonBox>
23
25 : QgsPanelWidget( parent )
26{
27 setupUi( this );
28
29 mAddButton->setIcon( QgsApplication::getThemeIcon( "symbologyAdd.svg" ) );
30 mRemoveButton->setIcon( QgsApplication::getThemeIcon( "symbologyRemove.svg" ) );
31
33
34 connect( mAddButton, &QPushButton::clicked, this, &QgsTabPositionWidget::mAddButton_clicked );
35 connect( mRemoveButton, &QPushButton::clicked, this, &QgsTabPositionWidget::mRemoveButton_clicked );
36 connect( mTabPositionTreeWidget, &QTreeWidget::itemChanged, this, &QgsTabPositionWidget::emitPositionsChanged );
37}
38
39void QgsTabPositionWidget::setPositions( const QList<QgsTextFormat::Tab> &positions )
40{
41 mTabPositionTreeWidget->clear();
42 for ( const QgsTextFormat::Tab &tab : positions )
43 {
44 QTreeWidgetItem *entry = new QTreeWidgetItem();
45 entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
46 entry->setText( 0, QLocale().toString( tab.position() ) );
47 entry->setData( 0, Qt::EditRole, tab.position() );
48 mTabPositionTreeWidget->addTopLevelItem( entry );
49 }
50}
51
52QList<QgsTextFormat::Tab> QgsTabPositionWidget::positions() const
53{
54 QList<QgsTextFormat::Tab> result;
55 const int nTopLevelItems = mTabPositionTreeWidget->topLevelItemCount();
56 result.reserve( nTopLevelItems );
57 for ( int i = 0; i < nTopLevelItems; ++i )
58 {
59 if ( QTreeWidgetItem *currentItem = mTabPositionTreeWidget->topLevelItem( i ) )
60 {
61 result << QgsTextFormat::Tab( QgsDoubleValidator::toDouble( currentItem->text( 0 ) ) );
62 }
63 }
64
65 std::sort( result.begin(), result.end(), []( const QgsTextFormat::Tab & a, const QgsTextFormat::Tab & b )
66 {
67 return a.position() < b.position();
68 } );
69
70 return result;
71}
72
74{
75 QTreeWidgetItem *headerItem = mTabPositionTreeWidget->headerItem();
76 headerItem->setText( 0, QStringLiteral( "%1 (%2)" ).arg( tr( "Position" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
77}
78
79void QgsTabPositionWidget::mAddButton_clicked()
80{
81 const QList< QgsTextFormat::Tab > currentPositions = positions();
82 double newPosition = 6;
83 if ( !currentPositions.empty() )
84 {
85 newPosition = currentPositions.last().position() + 6;
86 }
87
88 QTreeWidgetItem *entry = new QTreeWidgetItem();
89 entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
90 entry->setText( 0, QLocale().toString( newPosition ) );
91 entry->setData( 0, Qt::EditRole, newPosition );
92 mTabPositionTreeWidget->addTopLevelItem( entry );
93 emitPositionsChanged();
94}
95
96void QgsTabPositionWidget::mRemoveButton_clicked()
97{
98 if ( QTreeWidgetItem *currentItem = mTabPositionTreeWidget->currentItem() )
99 {
100 mTabPositionTreeWidget->takeTopLevelItem( mTabPositionTreeWidget->indexOfTopLevelItem( currentItem ) );
101 }
102 emitPositionsChanged();
103}
104
105void QgsTabPositionWidget::emitPositionsChanged()
106{
107 emit positionsChanged( positions() );
108}
109
110
111QgsTabPositionDialog::QgsTabPositionDialog( QWidget *parent, Qt::WindowFlags f )
112 : QDialog( parent, f )
113{
114 QVBoxLayout *vLayout = new QVBoxLayout();
115 mWidget = new QgsTabPositionWidget();
116 vLayout->addWidget( mWidget );
117 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
118 connect( bbox, &QDialogButtonBox::accepted, this, &QgsTabPositionDialog::accept );
119 connect( bbox, &QDialogButtonBox::rejected, this, &QgsTabPositionDialog::reject );
120 vLayout->addWidget( bbox );
121 setLayout( vLayout );
122 setWindowTitle( tr( "Tab Positions" ) );
123}
124
125void QgsTabPositionDialog::setPositions( const QList<QgsTextFormat::Tab> &positions )
126{
127 mWidget->setPositions( positions );
128}
129
130QList<QgsTextFormat::Tab> QgsTabPositionDialog::positions() const
131{
132 return mWidget->positions();
133}
134
136{
137 mWidget->setUnit( unit );
138}
RenderUnit
Rendering size units.
Definition qgis.h:4847
@ Millimeters
Millimeters.
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.
Base class for any widget that can be shown as a 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.