QGIS API Documentation  3.13.0-Master (740be229cb)
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 
16 #include "qgsalignmentcombobox.h"
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 
33 void QgsAlignmentComboBox::setAvailableAlignments( Qt::Alignment alignments )
34 {
35  mAlignments = alignments;
36  populate();
37 }
38 
40 {
41  return static_cast< Qt::Alignment >( currentData().toInt() );
42 }
43 
44 void QgsAlignmentComboBox::setCurrentAlignment( Qt::Alignment alignment )
45 {
46  const int index = findData( QVariant( alignment ) );
47  if ( index >= 0 )
48  setCurrentIndex( index );
49 }
50 
51 void 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 
63 void QgsAlignmentComboBox::populate()
64 {
65  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  const int index = findData( QVariant( prevAlign ) );
80  if ( index >= 0 )
81  setCurrentIndex( index );
82 
83  mBlockChanged = false;
84  if ( currentAlignment() != prevAlign )
85  emit changed();
86 }
87 
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...
QgsAlignmentComboBox(QWidget *parent=nullptr)
Constructor for QgsAlignmentComboBox, with the specified parent widget.
void setAvailableAlignments(Qt::Alignment alignments)
Sets the available alignment choices shown in the combo box.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void setCurrentAlignment(Qt::Alignment alignment)
Sets the current alignment choice.
void changed()
Emitted when the alignment is changed.
Qt::Alignment currentAlignment() const
Returns the current alignment choice.