QGIS API Documentation 3.99.0-Master (d270888f95f)
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 ) {
71 return a.position() < b.position();
72 } );
73
74 return result;
75}
76
78{
79 QTreeWidgetItem *headerItem = mTabPositionTreeWidget->headerItem();
80 headerItem->setText( 0, u"%1 (%2)"_s.arg( tr( "Position" ), QgsUnitTypes::toAbbreviatedString( unit ) ) );
81}
82
83void QgsTabPositionWidget::mAddButton_clicked()
84{
85 const QList<QgsTextFormat::Tab> currentPositions = positions();
86 double newPosition = 6;
87 if ( !currentPositions.empty() )
88 {
89 newPosition = currentPositions.last().position() + 6;
90 }
91
92 QTreeWidgetItem *entry = new QTreeWidgetItem();
93 entry->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
94 entry->setText( 0, QLocale().toString( newPosition ) );
95 entry->setData( 0, Qt::EditRole, newPosition );
96 mTabPositionTreeWidget->addTopLevelItem( entry );
97 emitPositionsChanged();
98}
99
100void QgsTabPositionWidget::mRemoveButton_clicked()
101{
102 if ( QTreeWidgetItem *currentItem = mTabPositionTreeWidget->currentItem() )
103 {
104 mTabPositionTreeWidget->takeTopLevelItem( mTabPositionTreeWidget->indexOfTopLevelItem( currentItem ) );
105 }
106 emitPositionsChanged();
107}
108
109void QgsTabPositionWidget::emitPositionsChanged()
110{
111 emit positionsChanged( positions() );
112}
113
114
115QgsTabPositionDialog::QgsTabPositionDialog( QWidget *parent, Qt::WindowFlags f )
116 : QDialog( parent, f )
117{
118 QVBoxLayout *vLayout = new QVBoxLayout();
119 mWidget = new QgsTabPositionWidget();
120 vLayout->addWidget( mWidget );
121 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
122 connect( bbox, &QDialogButtonBox::accepted, this, &QgsTabPositionDialog::accept );
123 connect( bbox, &QDialogButtonBox::rejected, this, &QgsTabPositionDialog::reject );
124 vLayout->addWidget( bbox );
125 setLayout( vLayout );
126 setWindowTitle( tr( "Tab Positions" ) );
127}
128
129void QgsTabPositionDialog::setPositions( const QList<QgsTextFormat::Tab> &positions )
130{
131 mWidget->setPositions( positions );
132}
133
134QList<QgsTextFormat::Tab> QgsTabPositionDialog::positions() const
135{
136 return mWidget->positions();
137}
138
140{
141 mWidget->setUnit( unit );
142}
RenderUnit
Rendering size units.
Definition qgis.h:5255
@ Millimeters
Millimeters.
Definition qgis.h:5256
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.