QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgslayouttablebackgroundcolorsdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayouttablebackgroundcolorsdialog.cpp
3  ----------------------------------------
4  begin : November 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgslayouttable.h"
20 #include "qgslayout.h"
21 #include "qgssettings.h"
22 #include "qgshelp.h"
23 #include "qgsgui.h"
24 
25 #include <QCheckBox>
26 #include <QPushButton>
27 
29  : QDialog( parent, flags )
30  , mTable( table )
31 {
32  setupUi( this );
34 
35  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsLayoutTableBackgroundColorsDialog::buttonBox_accepted );
36  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsLayoutTableBackgroundColorsDialog::buttonBox_rejected );
37  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutTableBackgroundColorsDialog::showHelp );
38 
39  mCheckBoxMap.insert( QgsLayoutTable::OddColumns, mOddColumnsCheckBox );
40  mCheckBoxMap.insert( QgsLayoutTable::EvenColumns, mEvenColumnsCheckBox );
41  mCheckBoxMap.insert( QgsLayoutTable::OddRows, mOddRowsCheckBox );
42  mCheckBoxMap.insert( QgsLayoutTable::EvenRows, mEvenRowsCheckBox );
43  mCheckBoxMap.insert( QgsLayoutTable::FirstColumn, mFirstColumnCheckBox );
44  mCheckBoxMap.insert( QgsLayoutTable::LastColumn, mLastColumnCheckBox );
45  mCheckBoxMap.insert( QgsLayoutTable::HeaderRow, mHeaderRowCheckBox );
46  mCheckBoxMap.insert( QgsLayoutTable::FirstRow, mFirstRowCheckBox );
47  mCheckBoxMap.insert( QgsLayoutTable::LastRow, mLastRowCheckBox );
48 
49  mColorButtonMap.insert( QgsLayoutTable::OddColumns, mOddColumnsColorButton );
50  mColorButtonMap.insert( QgsLayoutTable::EvenColumns, mEvenColumnsColorButton );
51  mColorButtonMap.insert( QgsLayoutTable::OddRows, mOddRowsColorButton );
52  mColorButtonMap.insert( QgsLayoutTable::EvenRows, mEvenRowsColorButton );
53  mColorButtonMap.insert( QgsLayoutTable::FirstColumn, mFirstColumnColorButton );
54  mColorButtonMap.insert( QgsLayoutTable::LastColumn, mLastColumnColorButton );
55  mColorButtonMap.insert( QgsLayoutTable::HeaderRow, mHeaderRowColorButton );
56  mColorButtonMap.insert( QgsLayoutTable::FirstRow, mFirstRowColorButton );
57  mColorButtonMap.insert( QgsLayoutTable::LastRow, mLastRowColorButton );
58 
59  connect( buttonBox->button( QDialogButtonBox::Apply ), &QAbstractButton::clicked, this, &QgsLayoutTableBackgroundColorsDialog::apply );
60 
61  setGuiElementValues();
62 }
63 
64 void QgsLayoutTableBackgroundColorsDialog::apply()
65 {
66  if ( !mTable )
67  return;
68 
69  mTable->beginCommand( tr( "Change Table Background" ), QgsLayoutMultiFrame::UndoTableCellStyle );
70  for ( auto checkBoxIt = mCheckBoxMap.constBegin(); checkBoxIt != mCheckBoxMap.constEnd(); ++checkBoxIt )
71  {
72  QgsLayoutTableStyle style;
73  style.enabled = checkBoxIt.value()->isChecked();
74  if ( QgsColorButton *button = mColorButtonMap.value( checkBoxIt.key() ) )
75  style.cellBackgroundColor = button->color();
76 
77  mTable->setCellStyle( checkBoxIt.key(), style );
78  }
79 
80  mTable->setBackgroundColor( mDefaultColorButton->color() );
81  mTable->endCommand();
82  mTable->update();
83 }
84 
85 void QgsLayoutTableBackgroundColorsDialog::buttonBox_accepted()
86 {
87  apply();
88  accept();
89 }
90 
91 void QgsLayoutTableBackgroundColorsDialog::buttonBox_rejected()
92 {
93  reject();
94 }
95 
96 void QgsLayoutTableBackgroundColorsDialog::showHelp()
97 {
98  QgsHelp::openHelp( QStringLiteral( "print_composer/composer_items/composer_attribute_table.html#appearance" ) );
99 }
100 
101 void QgsLayoutTableBackgroundColorsDialog::setGuiElementValues()
102 {
103  if ( !mTable )
104  return;
105 
106  for ( auto it = mCheckBoxMap.constBegin(); it != mCheckBoxMap.constEnd(); ++it )
107  {
108  it.value()->setChecked( mTable->cellStyle( it.key() )->enabled );
109  QgsColorButton *button = mColorButtonMap.value( it.key() );
110  if ( !button )
111  continue;
112  button->setEnabled( mTable->cellStyle( it.key() )->enabled );
113  button->setColor( mTable->cellStyle( it.key() )->cellBackgroundColor );
114  button->setAllowOpacity( true );
115  button->setColorDialogTitle( tr( "Select Background Color" ) );
116  }
117 
118  mDefaultColorButton->setColor( mTable->backgroundColor() );
119  mDefaultColorButton->setAllowOpacity( true );
120  mDefaultColorButton->setColorDialogTitle( tr( "Select Background Color" ) );
121  mDefaultColorButton->setShowNoColor( true );
122  mDefaultColorButton->setNoColorString( tr( "No Background" ) );
123 }
A cross platform button subclass for selecting colors.
void setColorDialogTitle(const QString &title)
Set the title for the color chooser dialog window.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
void setColor(const QColor &color)
Sets the current color for the button.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:156
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
void endCommand()
Completes the current item command and push it onto the layout's undo stack.
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
void update()
Forces a redraw of all child frames.
@ UndoTableCellStyle
Table cell style.
QgsLayoutTableBackgroundColorsDialog(QgsLayoutTable *table, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutTableBackgroundColorsDialog.
Styling option for a layout table cell.
QColor cellBackgroundColor
Cell background color.
bool enabled
Whether the styling option is enabled.
A class to display a table in the print layout, and allow the table to span over multiple frames.
QColor backgroundColor() const
Returns the color used for the background of the table.
void setBackgroundColor(const QColor &color)
Sets the color used for background of table.
@ FirstRow
Style first row only.
@ EvenColumns
Style even numbered columns.
@ EvenRows
Style even numbered rows.
@ HeaderRow
Style header row.
@ OddColumns
Style odd numbered columns.
@ FirstColumn
Style first column only.
@ LastColumn
Style last column only.
@ LastRow
Style last row only.
@ OddRows
Style odd numbered rows.
void setCellStyle(CellStyleGroup group, const QgsLayoutTableStyle &style)
Sets the cell style for a cell group.
const QgsLayoutTableStyle * cellStyle(CellStyleGroup group) const
Returns the cell style for a cell group.