16
16
***************************************************************************/
17
17
#include < QWidget>
18
18
#include < QDomNode>
19
+ #include < QFile>
19
20
#include < QGraphicsScene>
20
21
#include < QGraphicsSceneMouseEvent>
22
+ #include < QGraphicsView>
21
23
#include < QPainter>
22
24
23
25
#include " qgscomposition.h"
24
26
#include " qgscomposeritem.h"
25
27
28
+
26
29
#include < limits>
30
+ #include " qgsapplication.h"
27
31
#include " qgsrectangle.h" // just for debugging
28
32
#include " qgslogger.h"
29
33
30
34
#define FONT_WORKAROUND_SCALE 10 // scale factor for upscaling fontsize and downscaling painter
31
35
32
- QgsComposerItem::QgsComposerItem ( QgsComposition* composition, bool manageZValue ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
36
+ QgsComposerItem::QgsComposerItem ( QgsComposition* composition, bool manageZValue ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), \
37
+ mFrame( true ), mItemPositionLocked(false )
33
38
{
34
39
setFlag ( QGraphicsItem::ItemIsSelectable, true );
35
40
setAcceptsHoverEvents ( true );
@@ -119,6 +124,17 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
119
124
composerItemElem.setAttribute ( " zValue" , QString::number ( zValue () ) );
120
125
composerItemElem.setAttribute ( " outlineWidth" , QString::number ( pen ().widthF () ) );
121
126
127
+ // position lock for mouse moves/resizes
128
+ if (mItemPositionLocked )
129
+ {
130
+ composerItemElem.setAttribute ( " positionLock" , " true" );
131
+ }
132
+ else
133
+ {
134
+ composerItemElem.setAttribute ( " positionLock" , " false" );
135
+ }
136
+
137
+
122
138
// frame color
123
139
QDomElement frameColorElem = doc.createElement ( " FrameColor" );
124
140
QColor frameColor = pen ().color ();
@@ -160,6 +176,17 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
160
176
mFrame = false ;
161
177
}
162
178
179
+ // position lock for mouse moves/resizes
180
+ QString positionLock = itemElem.attribute (" positionLock" );
181
+ if (positionLock.compare (" true" , Qt::CaseInsensitive) == 0 )
182
+ {
183
+ mItemPositionLocked = true ;
184
+ }
185
+ else
186
+ {
187
+ mItemPositionLocked = false ;
188
+ }
189
+
163
190
// position
164
191
double x, y, width, height;
165
192
bool xOk, yOk, widthOk, heightOk;
@@ -219,7 +246,11 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
219
246
220
247
void QgsComposerItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
221
248
{
222
- qWarning ( " QgsComposerItem::mouseMoveEvent" );
249
+ if (mItemPositionLocked )
250
+ {
251
+ return ;
252
+ }
253
+
223
254
if ( mBoundingResizeRectangle )
224
255
{
225
256
double diffX = event->lastScenePos ().x () - mLastMouseEventPos .x ();
@@ -232,6 +263,11 @@ void QgsComposerItem::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
232
263
233
264
void QgsComposerItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
234
265
{
266
+ if (mItemPositionLocked )
267
+ {
268
+ return ;
269
+ }
270
+
235
271
// set current position and type of mouse move action
236
272
mMouseMoveStartPos = event->lastScenePos ();
237
273
mLastMouseEventPos = event->lastScenePos ();
@@ -260,6 +296,12 @@ void QgsComposerItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
260
296
261
297
void QgsComposerItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
262
298
{
299
+
300
+ if (mItemPositionLocked )
301
+ {
302
+ return ;
303
+ }
304
+
263
305
// delete frame rectangle
264
306
if ( mBoundingResizeRectangle )
265
307
{
@@ -289,6 +331,10 @@ Qt::CursorShape QgsComposerItem::cursorForPosition( const QPointF& itemCoordPos
289
331
{
290
332
QgsComposerItem::MouseMoveAction mouseAction = mouseMoveActionForPosition ( itemCoordPos );
291
333
334
+ if ( mouseAction == QgsComposerItem::NoAction)
335
+ {
336
+ return Qt::ForbiddenCursor;
337
+ }
292
338
if ( mouseAction == QgsComposerItem::MoveItem )
293
339
{
294
340
return Qt::ClosedHandCursor;
@@ -314,8 +360,11 @@ Qt::CursorShape QgsComposerItem::cursorForPosition( const QPointF& itemCoordPos
314
360
QgsComposerItem::MouseMoveAction QgsComposerItem::mouseMoveActionForPosition ( const QPointF& itemCoordPos )
315
361
{
316
362
317
- // move content tool
318
-
363
+ // no action at all if item position is locked for mouse
364
+ if (mItemPositionLocked )
365
+ {
366
+ return QgsComposerItem::NoAction;
367
+ }
319
368
320
369
bool nearLeftBorder = false ;
321
370
bool nearRightBorder = false ;
@@ -472,15 +521,53 @@ void QgsComposerItem::drawSelectionBoxes( QPainter* p )
472
521
473
522
if ( mComposition ->plotStyle () == QgsComposition::Preview )
474
523
{
475
- p->setPen ( QPen ( QColor ( 0 , 0 , 255 ) ) );
476
- p->setBrush ( QBrush ( QColor ( 0 , 0 , 255 ) ) );
524
+ // size of symbol boxes depends on zoom level in composer view
525
+ double viewScaleFactor = horizontalViewScaleFactor ();
526
+ double rectHandlerSize = 10.0 / viewScaleFactor;
527
+ double lockSymbolSize = 20.0 / viewScaleFactor;
477
528
478
- double s = 5 ;
529
+ // make sure the boxes don't get too large
530
+ if (rectHandlerSize > (rect ().width () / 3 ))
531
+ {
532
+ rectHandlerSize = rect ().width () / 3 ;
533
+ }
534
+ if (rectHandlerSize > (rect ().height () / 3 ))
535
+ {
536
+ rectHandlerSize = rect ().height () / 3 ;
537
+ }
538
+ if (lockSymbolSize > (rect ().width () / 3 ))
539
+ {
540
+ lockSymbolSize = rect ().width () / 3 ;
541
+ }
542
+ if (lockSymbolSize > (rect ().height () / 3 ))
543
+ {
544
+ lockSymbolSize = rect ().height () / 3 ;
545
+ }
479
546
480
- p->drawRect ( QRectF ( 0 , 0 , s, s ) );
481
- p->drawRect ( QRectF ( rect ().width () - s, 0 , s, s ) );
482
- p->drawRect ( QRectF ( rect ().width () - s, rect ().height () - s, s, s ) );
483
- p->drawRect ( QRectF ( 0 , rect ().height () - s, s, s ) );
547
+ if (mItemPositionLocked )
548
+ {
549
+ // draw lock symbol at upper left edge. Use QImage to be independant of the graphic system
550
+ QString lockIconPath = QgsApplication::activeThemePath () + " /mIconLock.png" ;
551
+ if (!QFile::exists (lockIconPath))
552
+ {
553
+ lockIconPath = QgsApplication::defaultThemePath () + " /mIconLock.png" ;
554
+ }
555
+
556
+ QImage lockImage (lockIconPath);
557
+ if (!lockImage.isNull ())
558
+ {
559
+ p->drawImage (QRectF (0 , 0 , lockSymbolSize, lockSymbolSize), lockImage, QRectF (0 , 0 , lockImage.width (), lockImage.height ()));
560
+ }
561
+ }
562
+ else // draw blue squares
563
+ {
564
+ p->setPen ( QPen ( QColor ( 0 , 0 , 255 ) ) );
565
+ p->setBrush ( QBrush ( QColor ( 0 , 0 , 255 ) ) );
566
+ p->drawRect ( QRectF ( 0 , 0 , rectHandlerSize, rectHandlerSize ) );
567
+ p->drawRect ( QRectF ( rect ().width () - rectHandlerSize, 0 , rectHandlerSize, rectHandlerSize ) );
568
+ p->drawRect ( QRectF ( rect ().width () - rectHandlerSize, rect ().height () - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
569
+ p->drawRect ( QRectF ( 0 , rect ().height () - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
570
+ }
484
571
}
485
572
}
486
573
@@ -637,3 +724,26 @@ QFont QgsComposerItem::scaledFontPixelSize( const QFont& font ) const
637
724
scaledFont.setPixelSize ( pixelSize );
638
725
return scaledFont;
639
726
}
727
+
728
+ double QgsComposerItem::horizontalViewScaleFactor () const
729
+ {
730
+ double result = 1 ;
731
+ if ( scene () )
732
+ {
733
+ QList<QGraphicsView*> viewList = scene ()->views ();
734
+ if ( viewList.size () > 0 )
735
+ {
736
+ result = viewList.at ( 0 )->transform ().m11 ();
737
+ }
738
+ else
739
+ {
740
+ return 1 ; // probably called from non-gui code
741
+ }
742
+ }
743
+ return result;
744
+ }
745
+
746
+ void QgsComposerItem::updateCursor (const QPointF& itemPos)
747
+ {
748
+ setCursor ( cursorForPosition (itemPos) );
749
+ }
0 commit comments