Skip to content

Commit 81d865a

Browse files
committed
Merge pull request #408 from olivierdalang/showBackgroundCheckboxForComposerItem
[feature] added draw background checkbox in composer items Fix #6389
2 parents 50d4bb6 + b642722 commit 81d865a

File tree

6 files changed

+170
-92
lines changed

6 files changed

+170
-92
lines changed

src/app/composer/qgscomposeritemwidget.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,28 @@ void QgsComposerItemWidget::on_mOutlineWidthSpinBox_valueChanged( double d )
140140
mItem->endCommand();
141141
}
142142

143-
void QgsComposerItemWidget::on_mFrameCheckBox_stateChanged( int state )
143+
void QgsComposerItemWidget::on_mFrameGroupBox_toggled( bool state )
144144
{
145145
if ( !mItem )
146146
{
147147
return;
148148
}
149149

150150
mItem->beginCommand( tr( "Item frame toggled" ) );
151-
if ( state == Qt::Checked )
152-
{
153-
mItem->setFrameEnabled( true );
154-
}
155-
else
151+
mItem->setFrameEnabled( state );
152+
mItem->update();
153+
mItem->endCommand();
154+
}
155+
156+
void QgsComposerItemWidget::on_mBackgroundGroupBox_toggled( bool state )
157+
{
158+
if ( !mItem )
156159
{
157-
mItem->setFrameEnabled( false );
160+
return;
158161
}
162+
163+
mItem->beginCommand( tr( "Item background toggled" ) );
164+
mItem->setBackgroundEnabled( state );
159165
mItem->update();
160166
mItem->endCommand();
161167
}
@@ -169,26 +175,22 @@ void QgsComposerItemWidget::setValuesForGuiElements()
169175

170176
mOpacitySlider->blockSignals( true );
171177
mOutlineWidthSpinBox->blockSignals( true );
172-
mFrameCheckBox->blockSignals( true );
178+
mFrameGroupBox->blockSignals( true );
179+
mBackgroundGroupBox->blockSignals( true );
173180
mItemIdLineEdit->blockSignals( true );
174181
mOpacitySpinBox->blockSignals( true );
175182

176183
mOpacitySpinBox->setValue( mItem->brush().color().alpha() );
177184
mOpacitySlider->setValue( mItem->brush().color().alpha() );
178185
mOutlineWidthSpinBox->setValue( mItem->pen().widthF() );
179186
mItemIdLineEdit->setText( mItem->id() );
180-
if ( mItem->hasFrame() )
181-
{
182-
mFrameCheckBox->setCheckState( Qt::Checked );
183-
}
184-
else
185-
{
186-
mFrameCheckBox->setCheckState( Qt::Unchecked );
187-
}
188-
187+
mFrameGroupBox->setChecked( mItem->hasFrame() );
188+
mBackgroundGroupBox->setChecked( mItem->hasBackground() );
189+
189190
mOpacitySlider->blockSignals( false );
190191
mOutlineWidthSpinBox->blockSignals( false );
191-
mFrameCheckBox->blockSignals( false );
192+
mFrameGroupBox->blockSignals( false );
193+
mBackgroundGroupBox->blockSignals( false );
192194
mItemIdLineEdit->blockSignals( false );
193195
mOpacitySpinBox->blockSignals( false );
194196
}

src/app/composer/qgscomposeritemwidget.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class QgsComposerItemWidget: public QWidget, private Ui::QgsComposerItemWidgetBa
3737
void on_mOpacitySlider_sliderReleased();
3838
void on_mOpacitySpinBox_valueChanged( int value );
3939
void on_mOutlineWidthSpinBox_valueChanged( double d );
40-
void on_mFrameCheckBox_stateChanged( int state );
40+
void on_mFrameGroupBox_toggled( bool state );
41+
void on_mBackgroundGroupBox_toggled( bool state );
4142
void on_mPositionButton_clicked();
4243
void on_mItemIdLineEdit_textChanged( const QString& text );
4344

src/core/composer/qgscomposeritem.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
4646
, mHAlignSnapItem( 0 )
4747
, mVAlignSnapItem( 0 )
4848
, mFrame( false )
49+
, mBackground( true )
4950
, mItemPositionLocked( false )
5051
, mLastValidViewScaleFactor( -1 )
5152
, mRotation( 0 )
@@ -61,6 +62,7 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
6162
, mHAlignSnapItem( 0 )
6263
, mVAlignSnapItem( 0 )
6364
, mFrame( false )
65+
, mBackground( true )
6466
, mItemPositionLocked( false )
6567
, mLastValidViewScaleFactor( -1 )
6668
, mRotation( 0 )
@@ -130,6 +132,16 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
130132
composerItemElem.setAttribute( "frame", "false" );
131133
}
132134

135+
//frame
136+
if ( mBackground )
137+
{
138+
composerItemElem.setAttribute( "background", "true" );
139+
}
140+
else
141+
{
142+
composerItemElem.setAttribute( "background", "false" );
143+
}
144+
133145
//scene rect
134146
composerItemElem.setAttribute( "x", QString::number( transform().dx() ) );
135147
composerItemElem.setAttribute( "y", QString::number( transform().dy() ) );
@@ -200,6 +212,17 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
200212
mFrame = false;
201213
}
202214

215+
//frame
216+
QString background = itemElem.attribute( "background" );
217+
if ( background.compare( "true", Qt::CaseInsensitive ) == 0 )
218+
{
219+
mBackground = true;
220+
}
221+
else
222+
{
223+
mBackground = false;
224+
}
225+
203226
//position lock for mouse moves/resizes
204227
QString positionLock = itemElem.attribute( "positionLock" );
205228
if ( positionLock.compare( "true", Qt::CaseInsensitive ) == 0 )
@@ -799,9 +822,9 @@ void QgsComposerItem::setSceneRect( const QRectF& rectangle )
799822

800823
void QgsComposerItem::drawBackground( QPainter* p )
801824
{
802-
if ( p )
825+
if ( mBackground && p )
803826
{
804-
p->setBrush( brush() );
827+
p->setBrush( brush() );//this causes a problem in atlas generation
805828
p->setPen( Qt::NoPen );
806829
p->setRenderHint( QPainter::Antialiasing, true );
807830
p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );

src/core/composer/qgscomposeritem.h

+18
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
175175
*/
176176
void setFrameEnabled( bool drawFrame ) {mFrame = drawFrame;}
177177

178+
179+
/** Whether this item has a Background or not.
180+
* @returns true if there is a Background around this item, otherwise false.
181+
* @note introduced since 2.0
182+
* @see hasBackground
183+
*/
184+
bool hasBackground() const {return mBackground;}
185+
186+
/** Set whether this item has a Background drawn around it or not.
187+
* @param drawBackground draw Background
188+
* @returns nothing
189+
* @note introduced in 2.0
190+
* @see hasBackground
191+
*/
192+
void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
193+
178194
/**Composite operations for item groups do nothing per default*/
179195
virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
180196
virtual void removeItems() {}
@@ -271,6 +287,8 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
271287

272288
/**True if item fram needs to be painted*/
273289
bool mFrame;
290+
/**True if item background needs to be painted*/
291+
bool mBackground;
274292

275293
/**True if item position and size cannot be changed with mouse move
276294
@note: this member was added in version 1.2*/

src/core/composer/qgscomposermap.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ void QgsComposerMap::cache( void )
227227
double forcedWidthScaleFactor = w / requestExtent.width() / mapUnitsToMM();
228228

229229
mCacheImage = QImage( w, h, QImage::Format_ARGB32 );
230-
mCacheImage.fill( brush().color().rgb() ); //consider the item background brush
230+
mCacheImage.fill( QColor(255,255,255,0).rgba() ); // the background is drawn by composerItem, but we still need to start with that empty fill to avoid artifacts
231+
231232
double mapUnitsPerPixel = mExtent.width() / w;
232233

233234
// WARNING: ymax in QgsMapToPixel is device height!!!

src/ui/qgscomposeritemwidgetbase.ui

+103-70
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,128 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>235</width>
10-
<height>277</height>
9+
<width>393</width>
10+
<height>391</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Form</string>
1515
</property>
16-
<layout class="QGridLayout" name="gridLayout">
17-
<item row="0" column="0" colspan="3">
18-
<widget class="QPushButton" name="mFrameColorButton">
16+
<layout class="QFormLayout" name="formLayout_2">
17+
<property name="fieldGrowthPolicy">
18+
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
19+
</property>
20+
<item row="0" column="0" colspan="2">
21+
<widget class="QPushButton" name="mPositionButton">
1922
<property name="text">
20-
<string>Frame color...</string>
23+
<string>Position and size...</string>
2124
</property>
2225
</widget>
2326
</item>
24-
<item row="1" column="0" colspan="3">
25-
<widget class="QPushButton" name="mBackgroundColorButton">
26-
<property name="text">
27-
<string>Background color...</string>
27+
<item row="1" column="0" colspan="2">
28+
<widget class="QgsCollapsibleGroupBox" name="mFrameGroupBox">
29+
<property name="title">
30+
<string>Show frame</string>
31+
</property>
32+
<property name="checkable">
33+
<bool>true</bool>
2834
</property>
35+
<layout class="QGridLayout" name="gridLayout_2">
36+
<item row="0" column="0" colspan="2">
37+
<widget class="QPushButton" name="mFrameColorButton">
38+
<property name="text">
39+
<string>Frame color...</string>
40+
</property>
41+
</widget>
42+
</item>
43+
<item row="1" column="0">
44+
<widget class="QLabel" name="mOutlineWidthLabel">
45+
<property name="text">
46+
<string>Thickness</string>
47+
</property>
48+
<property name="wordWrap">
49+
<bool>true</bool>
50+
</property>
51+
<property name="buddy">
52+
<cstring>mOutlineWidthSpinBox</cstring>
53+
</property>
54+
</widget>
55+
</item>
56+
<item row="1" column="1">
57+
<widget class="QDoubleSpinBox" name="mOutlineWidthSpinBox"/>
58+
</item>
59+
</layout>
2960
</widget>
3061
</item>
31-
<item row="2" column="0" colspan="3">
32-
<layout class="QHBoxLayout" name="horizontalLayout">
33-
<item>
34-
<widget class="QLabel" name="mOpacityLabel">
35-
<property name="text">
36-
<string>Opacity</string>
37-
</property>
38-
<property name="wordWrap">
39-
<bool>true</bool>
40-
</property>
41-
<property name="buddy">
42-
<cstring>mOpacitySlider</cstring>
43-
</property>
44-
</widget>
45-
</item>
46-
<item>
47-
<widget class="QSlider" name="mOpacitySlider">
48-
<property name="maximum">
49-
<number>255</number>
50-
</property>
51-
<property name="orientation">
52-
<enum>Qt::Horizontal</enum>
53-
</property>
54-
</widget>
55-
</item>
56-
<item>
57-
<widget class="QSpinBox" name="mOpacitySpinBox">
58-
<property name="maximum">
59-
<number>255</number>
60-
</property>
61-
</widget>
62-
</item>
63-
</layout>
64-
</item>
65-
<item row="3" column="0">
66-
<widget class="QLabel" name="mOutlineWidthLabel">
67-
<property name="text">
68-
<string>Outline width</string>
62+
<item row="2" column="0" colspan="2">
63+
<widget class="QgsCollapsibleGroupBox" name="mBackgroundGroupBox">
64+
<property name="title">
65+
<string>Show background</string>
6966
</property>
70-
<property name="wordWrap">
71-
<bool>true</bool>
67+
<property name="flat">
68+
<bool>false</bool>
7269
</property>
73-
<property name="buddy">
74-
<cstring>mOutlineWidthSpinBox</cstring>
70+
<property name="checkable">
71+
<bool>true</bool>
7572
</property>
76-
</widget>
77-
</item>
78-
<item row="3" column="2">
79-
<widget class="QDoubleSpinBox" name="mOutlineWidthSpinBox"/>
80-
</item>
81-
<item row="4" column="0" colspan="3">
82-
<widget class="QPushButton" name="mPositionButton">
83-
<property name="text">
84-
<string>Position and size...</string>
73+
<property name="checked">
74+
<bool>true</bool>
8575
</property>
76+
<layout class="QGridLayout" name="gridLayout">
77+
<item row="0" column="0" colspan="3">
78+
<widget class="QPushButton" name="mBackgroundColorButton">
79+
<property name="text">
80+
<string>Background color...</string>
81+
</property>
82+
</widget>
83+
</item>
84+
<item row="1" column="0">
85+
<widget class="QLabel" name="mOpacityLabel">
86+
<property name="text">
87+
<string>Opacity</string>
88+
</property>
89+
<property name="wordWrap">
90+
<bool>true</bool>
91+
</property>
92+
<property name="buddy">
93+
<cstring>mOpacitySlider</cstring>
94+
</property>
95+
</widget>
96+
</item>
97+
<item row="1" column="1">
98+
<widget class="QSlider" name="mOpacitySlider">
99+
<property name="maximum">
100+
<number>255</number>
101+
</property>
102+
<property name="orientation">
103+
<enum>Qt::Horizontal</enum>
104+
</property>
105+
</widget>
106+
</item>
107+
<item row="1" column="2">
108+
<widget class="QSpinBox" name="mOpacitySpinBox">
109+
<property name="maximum">
110+
<number>255</number>
111+
</property>
112+
</widget>
113+
</item>
114+
</layout>
86115
</widget>
87116
</item>
88-
<item row="5" column="0" colspan="2">
89-
<widget class="QCheckBox" name="mFrameCheckBox">
90-
<property name="text">
91-
<string>Show frame</string>
92-
</property>
93-
</widget>
117+
<item row="3" column="0" colspan="2">
118+
<layout class="QHBoxLayout" name="horizontalLayout"/>
94119
</item>
95-
<item row="6" column="0">
120+
<item row="5" column="0">
96121
<widget class="QLabel" name="mIdLabel">
97122
<property name="text">
98123
<string>Item ID</string>
99124
</property>
100125
</widget>
101126
</item>
102-
<item row="6" column="1" colspan="2">
127+
<item row="5" column="1">
103128
<widget class="QLineEdit" name="mItemIdLineEdit"/>
104129
</item>
105-
<item row="7" column="0" colspan="2">
130+
<item row="6" column="0" colspan="2">
106131
<spacer name="verticalSpacer">
107132
<property name="orientation">
108133
<enum>Qt::Vertical</enum>
@@ -117,6 +142,14 @@
117142
</item>
118143
</layout>
119144
</widget>
145+
<customwidgets>
146+
<customwidget>
147+
<class>QgsCollapsibleGroupBox</class>
148+
<extends>QGroupBox</extends>
149+
<header>qgscollapsiblegroupbox.h</header>
150+
<container>1</container>
151+
</customwidget>
152+
</customwidgets>
120153
<resources/>
121154
<connections>
122155
<connection>

0 commit comments

Comments
 (0)