QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgsapplication.h"
19#include "qgsguiutils.h"
20
21#include <QString>
22
23#include "moc_qgsalignmentcombobox.cpp"
24
25using namespace Qt::StringLiterals;
26
28 : QComboBox( parent )
29{
30 populate();
31 setCurrentIndex( 0 );
32 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [this] {
33 if ( !mBlockChanged )
34 emit changed();
35 } );
36}
37
38void QgsAlignmentComboBox::setAvailableAlignments( Qt::Alignment alignments )
39{
40 mAlignments = alignments;
41 populate();
42}
43
45{
46 return static_cast<Qt::Alignment>( currentData().toInt() );
47}
48
49void QgsAlignmentComboBox::setCurrentAlignment( Qt::Alignment alignment )
50{
51 const int index = findData( QVariant( alignment ) );
52 if ( index >= 0 )
53 setCurrentIndex( index );
54}
55
57{
58 switch ( currentAlignment() )
59 {
60 case Qt::AlignLeft:
62 case Qt::AlignRight:
64
65 case Qt::AlignHCenter:
67 case Qt::AlignJustify:
69 case Qt::AlignAbsolute:
71
72 default:
73 break;
74 }
75
77}
78
80{
81 switch ( currentAlignment() )
82 {
83 case Qt::AlignTop:
85 case Qt::AlignBottom:
87 case Qt::AlignVCenter:
89 case Qt::AlignBaseline:
90 return Qgis::TextVerticalAlignment::Bottom; // not yet supported
91
92 default:
93 break;
94 }
95
97}
98
100{
101 switch ( alignment )
102 {
104 setCurrentAlignment( Qt::AlignmentFlag::AlignLeft );
105 break;
107 setCurrentAlignment( Qt::AlignmentFlag::AlignHCenter );
108 break;
110 setCurrentAlignment( Qt::AlignmentFlag::AlignRight );
111 break;
113 setCurrentAlignment( Qt::AlignmentFlag::AlignJustify );
114 break;
115 }
116}
117
119{
120 switch ( alignment )
121 {
123 setCurrentAlignment( Qt::AlignmentFlag::AlignTop );
124 break;
126 setCurrentAlignment( Qt::AlignmentFlag::AlignVCenter );
127 break;
129 setCurrentAlignment( Qt::AlignmentFlag::AlignBottom );
130 break;
131 }
132}
133
134void QgsAlignmentComboBox::customizeAlignmentDisplay( Qt::Alignment alignment, const QString &text, const QIcon &icon )
135{
136 const int index = findData( QVariant( alignment ) );
137 if ( index >= 0 )
138 {
139 if ( !text.isEmpty() )
140 setItemText( index, text );
141 if ( !icon.isNull() )
142 setItemIcon( index, icon );
143 }
144}
145
146void QgsAlignmentComboBox::populate()
147{
148 const Qt::Alignment prevAlign = currentAlignment();
149
150 mBlockChanged = true;
151 clear();
152
153 if ( mAlignments & Qt::AlignLeft )
154 addItem( QgsApplication::getThemeIcon( u"/mIconAlignLeft.svg"_s ), tr( "Left" ), Qt::AlignLeft );
155 if ( mAlignments & Qt::AlignHCenter )
156 addItem( QgsApplication::getThemeIcon( u"/mIconAlignCenter.svg"_s ), tr( "Center" ), Qt::AlignHCenter );
157 if ( mAlignments & Qt::AlignRight )
158 addItem( QgsApplication::getThemeIcon( u"/mIconAlignRight.svg"_s ), tr( "Right" ), Qt::AlignRight );
159 if ( mAlignments & Qt::AlignJustify )
160 addItem( QgsApplication::getThemeIcon( u"/mIconAlignJustify.svg"_s ), tr( "Justify" ), Qt::AlignJustify );
161
162 if ( mAlignments & Qt::AlignTop )
163 addItem( QgsApplication::getThemeIcon( u"/mIconAlignTop.svg"_s ), tr( "Top" ), Qt::AlignTop );
164 if ( mAlignments & Qt::AlignVCenter )
165 addItem( QgsApplication::getThemeIcon( u"/mIconAlignVCenter.svg"_s ), tr( "Vertical Center" ), Qt::AlignVCenter );
166 if ( mAlignments & Qt::AlignBottom )
167 addItem( QgsApplication::getThemeIcon( u"/mIconAlignBottom.svg"_s ), tr( "Bottom" ), Qt::AlignBottom );
168
169 const int index = findData( QVariant( prevAlign ) );
170 if ( index >= 0 )
171 setCurrentIndex( index );
172
173 mBlockChanged = false;
174 if ( currentAlignment() != prevAlign )
175 emit changed();
176}
TextVerticalAlignment
Text vertical alignment.
Definition qgis.h:3019
@ Bottom
Align to bottom.
Definition qgis.h:3022
@ Top
Align to top.
Definition qgis.h:3020
@ VerticalCenter
Center align.
Definition qgis.h:3021
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:3000
@ Justify
Justify align.
Definition qgis.h:3004
@ Center
Center align.
Definition qgis.h:3002
QgsAlignmentComboBox(QWidget *parent=nullptr)
Constructor for QgsAlignmentComboBox, with the specified parent widget.
void changed()
Emitted when the alignment is changed.
Qgis::TextHorizontalAlignment horizontalAlignment() const
Returns the current alignment choice as a QGIS horizontal text alignment enum.
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.
Qgis::TextVerticalAlignment verticalAlignment() const
Returns the current alignment choice as a QGIS horizontal text alignment enum.
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.