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