-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgscomposermultiframe.cpp
315 lines (284 loc) · 9.71 KB
/
qgscomposermultiframe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/***************************************************************************
qgscomposermultiframe.cpp
------------------------------------------------------------
begin : July 2012
copyright : (C) 2012 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscomposermultiframe.h"
#include "qgscomposerframe.h"
#include "qgscomposition.h"
#include <QtCore>
QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c, bool createUndoCommands ):
mComposition( c ),
mResizeMode( UseExistingFrames ),
mCreateUndoCommands( createUndoCommands ),
mIsRecalculatingSize( false )
{
mComposition->addMultiFrame( this );
connect( mComposition, SIGNAL( nPagesChanged() ), this, SLOT( handlePageChange() ) );
}
QgsComposerMultiFrame::QgsComposerMultiFrame():
mComposition( 0 ),
mResizeMode( UseExistingFrames ),
mIsRecalculatingSize( false )
{
}
QgsComposerMultiFrame::~QgsComposerMultiFrame()
{
deleteFrames();
}
void QgsComposerMultiFrame::setResizeMode( ResizeMode mode )
{
if ( mode != mResizeMode )
{
mResizeMode = mode;
recalculateFrameSizes();
emit changed();
}
}
void QgsComposerMultiFrame::recalculateFrameSizes()
{
if ( mFrameItems.size() < 1 )
{
return;
}
QSizeF size = totalSize();
double totalHeight = size.height();
if ( totalHeight < 1 )
{
return;
}
double currentY = 0;
double currentHeight = 0;
QgsComposerFrame* currentItem = 0;
for ( int i = 0; i < mFrameItems.size(); ++i )
{
if ( mResizeMode != RepeatOnEveryPage && currentY >= totalHeight )
{
if ( mResizeMode == RepeatUntilFinished || mResizeMode == ExtendToNextPage ) //remove unneeded frames in extent mode
{
for ( int j = mFrameItems.size(); j > i; --j )
{
removeFrame( j - 1 );
}
}
return;
}
currentItem = mFrameItems.value( i );
currentHeight = currentItem->rect().height();
if ( mResizeMode == RepeatOnEveryPage )
{
currentItem->setContentSection( QRectF( 0, 0, currentItem->rect().width(), currentHeight ) );
}
else
{
currentHeight = findNearbyPageBreak( currentY + currentHeight ) - currentY;
currentItem->setContentSection( QRectF( 0, currentY, currentItem->rect().width(), currentHeight ) );
}
currentItem->update();
currentY += currentHeight;
}
//at end of frames but there is still content left. Add other pages if ResizeMode ==
if ( mResizeMode != UseExistingFrames )
{
while (( mResizeMode == RepeatOnEveryPage ) || currentY < totalHeight )
{
//find out on which page the lower left point of the last frame is
int page = qFloor(( currentItem->pos().y() + currentItem->rect().height() ) / ( mComposition->paperHeight() + mComposition->spaceBetweenPages() ) ) + 1;
if ( mResizeMode == RepeatOnEveryPage )
{
if ( page >= mComposition->numPages() )
{
break;
}
}
else
{
//add an extra page if required
if ( mComposition->numPages() < ( page + 1 ) )
{
mComposition->setNumPages( page + 1 );
}
}
double frameHeight = 0;
if ( mResizeMode == RepeatUntilFinished || mResizeMode == RepeatOnEveryPage )
{
frameHeight = currentItem->rect().height();
}
else //mResizeMode == ExtendToNextPage
{
frameHeight = ( currentY + mComposition->paperHeight() ) > totalHeight ? totalHeight - currentY : mComposition->paperHeight();
}
double newFrameY = page * ( mComposition->paperHeight() + mComposition->spaceBetweenPages() );
if ( mResizeMode == RepeatUntilFinished || mResizeMode == RepeatOnEveryPage )
{
newFrameY += currentItem->pos().y() - ( page - 1 ) * ( mComposition->paperHeight() + mComposition->spaceBetweenPages() );
}
QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, currentItem->pos().x(),
newFrameY,
currentItem->rect().width(), frameHeight );
if ( mResizeMode == RepeatOnEveryPage )
{
newFrame->setContentSection( QRectF( 0, 0, newFrame->rect().width(), newFrame->rect().height() ) );
}
else
{
newFrame->setContentSection( QRectF( 0, currentY, newFrame->rect().width(), newFrame->rect().height() ) );
}
//copy some settings from the parent frame
newFrame->setBackgroundColor( currentItem->backgroundColor() );
newFrame->setBackgroundEnabled( currentItem->hasBackground() );
newFrame->setBlendMode( currentItem->blendMode() );
newFrame->setFrameEnabled( currentItem->hasFrame() );
newFrame->setFrameJoinStyle( currentItem->frameJoinStyle() );
newFrame->setFrameOutlineWidth( currentItem->frameOutlineWidth() );
newFrame->setOpacity( currentItem->opacity() );
currentY += frameHeight;
currentItem = newFrame;
addFrame( newFrame, false );
}
}
}
void QgsComposerMultiFrame::handleFrameRemoval( QgsComposerItem* item )
{
QgsComposerFrame* frame = dynamic_cast<QgsComposerFrame*>( item );
if ( !frame )
{
return;
}
int index = mFrameItems.indexOf( frame );
if ( index == -1 )
{
return;
}
mFrameItems.removeAt( index );
if ( mFrameItems.size() > 0 )
{
if ( resizeMode() != QgsComposerMultiFrame::RepeatOnEveryPage && !mIsRecalculatingSize )
{
//removing a frame forces the multi frame to UseExistingFrames resize mode
//otherwise the frame may not actually be removed, leading to confusing ui behaviour
mResizeMode = QgsComposerMultiFrame::UseExistingFrames;
emit changed();
}
recalculateFrameSizes();
}
}
void QgsComposerMultiFrame::handlePageChange()
{
if ( mComposition->numPages() < 1 )
{
return;
}
if ( mResizeMode != RepeatOnEveryPage )
{
return;
}
//remove items beginning on non-existing pages
for ( int i = mFrameItems.size() - 1; i >= 0; --i )
{
QgsComposerFrame* frame = mFrameItems[i];
int page = frame->pos().y() / ( mComposition->paperHeight() + mComposition->spaceBetweenPages() );
if ( page > ( mComposition->numPages() - 1 ) )
{
removeFrame( i );
}
}
//page number of the last item
QgsComposerFrame* lastFrame = mFrameItems.last();
int lastItemPage = lastFrame->pos().y() / ( mComposition->paperHeight() + mComposition->spaceBetweenPages() );
for ( int i = lastItemPage + 1; i < mComposition->numPages(); ++i )
{
//copy last frame to current page
QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, lastFrame->pos().x(),
lastFrame->pos().y() + mComposition->paperHeight() + mComposition->spaceBetweenPages(),
lastFrame->rect().width(), lastFrame->rect().height() );
addFrame( newFrame, false );
lastFrame = newFrame;
}
recalculateFrameSizes();
update();
}
void QgsComposerMultiFrame::removeFrame( int i )
{
if ( i >= mFrameItems.count() )
{
return;
}
QgsComposerFrame* frameItem = mFrameItems[i];
if ( mComposition )
{
mIsRecalculatingSize = true;
mComposition->removeComposerItem( frameItem );
mIsRecalculatingSize = false;
}
mFrameItems.removeAt( i );
}
void QgsComposerMultiFrame::update()
{
QList<QgsComposerFrame*>::iterator frameIt = mFrameItems.begin();
for ( ; frameIt != mFrameItems.end(); ++frameIt )
{
( *frameIt )->update();
}
}
void QgsComposerMultiFrame::deleteFrames()
{
ResizeMode bkResizeMode = mResizeMode;
mResizeMode = UseExistingFrames;
QObject::disconnect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( handleFrameRemoval( QgsComposerItem* ) ) );
QList<QgsComposerFrame*>::iterator frameIt = mFrameItems.begin();
for ( ; frameIt != mFrameItems.end(); ++frameIt )
{
mComposition->removeComposerItem( *frameIt, false );
delete *frameIt;
}
QObject::connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( handleFrameRemoval( QgsComposerItem* ) ) );
mFrameItems.clear();
mResizeMode = bkResizeMode;
}
QgsComposerFrame* QgsComposerMultiFrame::frame( int i ) const
{
if ( i >= mFrameItems.size() )
{
return 0;
}
return mFrameItems.at( i );
}
bool QgsComposerMultiFrame::_writeXML( QDomElement& elem, QDomDocument& doc, bool ignoreFrames ) const
{
elem.setAttribute( "resizeMode", mResizeMode );
if ( !ignoreFrames )
{
QList<QgsComposerFrame*>::const_iterator frameIt = mFrameItems.constBegin();
for ( ; frameIt != mFrameItems.constEnd(); ++frameIt )
{
( *frameIt )->writeXML( elem, doc );
}
}
return true;
}
bool QgsComposerMultiFrame::_readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
{
mResizeMode = ( ResizeMode )itemElem.attribute( "resizeMode", "0" ).toInt();
if ( !ignoreFrames )
{
QDomNodeList frameList = itemElem.elementsByTagName( "ComposerFrame" );
for ( int i = 0; i < frameList.size(); ++i )
{
QDomElement frameElem = frameList.at( i ).toElement();
QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, 0, 0, 0, 0 );
newFrame->readXML( frameElem, doc );
addFrame( newFrame );
}
}
return true;
}