QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsalignmentcombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalignmentcombobox.h
3 ---------------------
4 begin : June 2019
5 copyright : (C) 2019 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_qgsalignmentcombobox.cpp"
18
19#include "qgsapplication.h"
20#include "qgsguiutils.h"
21
23 : QComboBox( parent )
24{
25 populate();
26 setCurrentIndex( 0 );
27 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
28 {
29 if ( !mBlockChanged )
30 emit changed();
31 } );
32}
33
34void QgsAlignmentComboBox::setAvailableAlignments( Qt::Alignment alignments )
35{
36 mAlignments = alignments;
37 populate();
38}
39
41{
42 return static_cast< Qt::Alignment >( currentData().toInt() );
43}
44
45void QgsAlignmentComboBox::setCurrentAlignment( Qt::Alignment alignment )
46{
47 const int index = findData( QVariant( alignment ) );
48 if ( index >= 0 )
49 setCurrentIndex( index );
50}
51
52void QgsAlignmentComboBox::customizeAlignmentDisplay( Qt::Alignment alignment, const QString &text, const QIcon &icon )
53{
54 const int index = findData( QVariant( alignment ) );
55 if ( index >= 0 )
56 {
57 if ( !text.isEmpty() )
58 setItemText( index, text );
59 if ( !icon.isNull() )
60 setItemIcon( index, icon );
61 }
62}
63
64void QgsAlignmentComboBox::populate()
65{
66 const Qt::Alignment prevAlign = currentAlignment();
67
68 mBlockChanged = true;
69 clear();
70
71 if ( mAlignments & Qt::AlignLeft )
72 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignLeft.svg" ) ), tr( "Left" ), Qt::AlignLeft );
73 if ( mAlignments & Qt::AlignHCenter )
74 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignCenter.svg" ) ), tr( "Center" ), Qt::AlignHCenter );
75 if ( mAlignments & Qt::AlignRight )
76 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignRight.svg" ) ), tr( "Right" ), Qt::AlignRight );
77 if ( mAlignments & Qt::AlignJustify )
78 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignJustify.svg" ) ), tr( "Justify" ), Qt::AlignJustify );
79
80 if ( mAlignments & Qt::AlignTop )
81 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignTop.svg" ) ), tr( "Top" ), Qt::AlignTop );
82 if ( mAlignments & Qt::AlignVCenter )
83 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignVCenter.svg" ) ), tr( "Vertical Center" ), Qt::AlignVCenter );
84 if ( mAlignments & Qt::AlignBottom )
85 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignBottom.svg" ) ), tr( "Bottom" ), Qt::AlignBottom );
86
87 const int index = findData( QVariant( prevAlign ) );
88 if ( index >= 0 )
89 setCurrentIndex( index );
90
91 mBlockChanged = false;
92 if ( currentAlignment() != prevAlign )
93 emit changed();
94}
95
QgsAlignmentComboBox(QWidget *parent=nullptr)
Constructor for QgsAlignmentComboBox, with the specified parent widget.
void changed()
Emitted when the alignment is changed.
void setCurrentAlignment(Qt::Alignment alignment)
Sets the current alignment choice.
void customizeAlignmentDisplay(Qt::Alignment alignment, const QString &text=QString(), const QIcon &icon=QIcon())
Sets the text and icon to use for a particular alignment option, replacing the default text or icon.
Qt::Alignment currentAlignment() const
Returns the current alignment choice.
void setAvailableAlignments(Qt::Alignment alignments)
Sets the available alignment choices shown in the combo box.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.