QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
18#include "qgsapplication.h"
19#include "qgsguiutils.h"
20
22 : QComboBox( parent )
23{
24 populate();
25 setCurrentIndex( 0 );
26 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
27 {
28 if ( !mBlockChanged )
29 emit changed();
30 } );
31}
32
33void QgsAlignmentComboBox::setAvailableAlignments( Qt::Alignment alignments )
34{
35 mAlignments = alignments;
36 populate();
37}
38
40{
41 return static_cast< Qt::Alignment >( currentData().toInt() );
42}
43
44void QgsAlignmentComboBox::setCurrentAlignment( Qt::Alignment alignment )
45{
46 const int index = findData( QVariant( alignment ) );
47 if ( index >= 0 )
48 setCurrentIndex( index );
49}
50
51void QgsAlignmentComboBox::customizeAlignmentDisplay( Qt::Alignment alignment, const QString &text, const QIcon &icon )
52{
53 const int index = findData( QVariant( alignment ) );
54 if ( index >= 0 )
55 {
56 if ( !text.isEmpty() )
57 setItemText( index, text );
58 if ( !icon.isNull() )
59 setItemIcon( index, icon );
60 }
61}
62
63void QgsAlignmentComboBox::populate()
64{
65 const Qt::Alignment prevAlign = currentAlignment();
66
67 mBlockChanged = true;
68 clear();
69
70 if ( mAlignments & Qt::AlignLeft )
71 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignLeft.svg" ) ), tr( "Left" ), Qt::AlignLeft );
72 if ( mAlignments & Qt::AlignHCenter )
73 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignCenter.svg" ) ), tr( "Center" ), Qt::AlignHCenter );
74 if ( mAlignments & Qt::AlignRight )
75 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignRight.svg" ) ), tr( "Right" ), Qt::AlignRight );
76 if ( mAlignments & Qt::AlignJustify )
77 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignJustify.svg" ) ), tr( "Justify" ), Qt::AlignJustify );
78
79 if ( mAlignments & Qt::AlignTop )
80 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignTop.svg" ) ), tr( "Top" ), Qt::AlignTop );
81 if ( mAlignments & Qt::AlignVCenter )
82 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignVCenter.svg" ) ), tr( "Vertical Center" ), Qt::AlignVCenter );
83 if ( mAlignments & Qt::AlignBottom )
84 addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconAlignBottom.svg" ) ), tr( "Bottom" ), Qt::AlignBottom );
85
86 const int index = findData( QVariant( prevAlign ) );
87 if ( index >= 0 )
88 setCurrentIndex( index );
89
90 mBlockChanged = false;
91 if ( currentAlignment() != prevAlign )
92 emit changed();
93}
94
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.