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