QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsLayoutTableBackgroundColorsDialog::QgsLayoutTableBackgroundColorsDialog
QgsLayoutTableBackgroundColorsDialog(QgsLayoutTable *table, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutTableBackgroundColorsDialog.
Definition: qgslayouttablebackgroundcolorsdialog.cpp:28
QgsLayoutTableStyle::cellBackgroundColor
QColor cellBackgroundColor
Cell background color.
Definition: qgslayouttable.h:87
QgsLayoutTable::backgroundColor
QColor backgroundColor() const
Returns the color used for the background of the table.
Definition: qgslayouttable.h:462
QgsLayoutMultiFrame::endCommand
void endCommand()
Completes the current item command and push it onto the layout's undo stack.
Definition: qgslayoutmultiframe.cpp:298
QgsLayoutMultiFrame::UndoTableCellStyle
@ UndoTableCellStyle
Table cell style.
Definition: qgslayoutmultiframe.h:116
QgsColorButton::setColor
void setColor(const QColor &color)
Sets the current color for the button.
Definition: qgscolorbutton.cpp:646
qgsgui.h
QgsLayoutTable::cellStyle
const QgsLayoutTableStyle * cellStyle(CellStyleGroup group) const
Returns the cell style for a cell group.
Definition: qgslayouttable.cpp:973
QgsLayoutTable::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets the color used for background of table.
Definition: qgslayouttable.cpp:916
QgsLayoutTable::OddColumns
@ OddColumns
Style odd numbered columns.
Definition: qgslayouttable.h:163
QgsLayoutTable::FirstRow
@ FirstRow
Style first row only.
Definition: qgslayouttable.h:170
QgsLayoutTable::FirstColumn
@ FirstColumn
Style first column only.
Definition: qgslayouttable.h:167
QgsGui::enableAutoGeometryRestore
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:139
QgsColorButton::setAllowOpacity
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
Definition: qgscolorbutton.cpp:793
QgsLayoutTable::HeaderRow
@ HeaderRow
Style header row.
Definition: qgslayouttable.h:169
QgsLayoutMultiFrame::update
void update()
Forces a redraw of all child frames.
Definition: qgslayoutmultiframe.cpp:445
qgslayouttablebackgroundcolorsdialog.h
qgslayouttable.h
QgsLayoutTable::OddRows
@ OddRows
Style odd numbered rows.
Definition: qgslayouttable.h:165
QgsLayoutTable::LastRow
@ LastRow
Style last row only.
Definition: qgslayouttable.h:171
QgsLayoutTable
A class to display a table in the print layout, and allow the table to span over multiple frames.
Definition: qgslayouttable.h:113
qgslayout.h
QgsLayoutTable::EvenRows
@ EvenRows
Style even numbered rows.
Definition: qgslayouttable.h:166
QgsLayoutMultiFrame::beginCommand
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
Definition: qgslayoutmultiframe.cpp:290
QgsLayoutTableStyle::enabled
bool enabled
Whether the styling option is enabled.
Definition: qgslayouttable.h:84
QgsLayoutTableStyle
Styling option for a layout table cell.
Definition: qgslayouttable.h:77
QgsLayoutTable::LastColumn
@ LastColumn
Style last column only.
Definition: qgslayouttable.h:168
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsLayoutTable::setCellStyle
void setCellStyle(CellStyleGroup group, const QgsLayoutTableStyle &style)
Sets the cell style for a cell group.
Definition: qgslayouttable.cpp:965
qgssettings.h
QgsColorButton
A cross platform button subclass for selecting colors.
Definition: qgscolorbutton.h:36
qgshelp.h
QgsLayoutTable::EvenColumns
@ EvenColumns
Style even numbered columns.
Definition: qgslayouttable.h:164
QgsColorButton::setColorDialogTitle
void setColorDialogTitle(const QString &title)
Set the title for the color chooser dialog window.
Definition: qgscolorbutton.cpp:798