Skip to content

Commit b0956c9

Browse files
committedAug 7, 2017
[needs-docs] Partial WIP of dragging rulers to create guide lines
Unlike in 2.x, Layouts in 3.0 adopt the standard UX of dragging out rulers to create guide lines (instead of clicking on a ruler position to create a guide)
1 parent ef67275 commit b0956c9

File tree

4 files changed

+118
-13
lines changed

4 files changed

+118
-13
lines changed
 

‎python/gui/layout/qgslayoutruler.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class QgsLayoutRuler: QWidget
7272

7373
virtual void mouseMoveEvent( QMouseEvent *event );
7474

75+
virtual void mousePressEvent( QMouseEvent *event );
76+
77+
virtual void mouseReleaseEvent( QMouseEvent *event );
78+
7579

7680
signals:
7781
void cursorPosChanged( QPointF );

‎src/core/layout/qgslayoutguidecollection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void QgsLayoutGuide::update()
8080
switch ( mOrientation )
8181
{
8282
case Horizontal:
83-
if ( layoutPos > page->rect().width() )
83+
if ( layoutPos > page->rect().height() )
8484
{
8585
mLineItem->hide();
8686
}
@@ -93,7 +93,7 @@ void QgsLayoutGuide::update()
9393
break;
9494

9595
case Vertical:
96-
if ( layoutPos > page->rect().height() )
96+
if ( layoutPos > page->rect().width() )
9797
{
9898
mLineItem->hide();
9999
}

‎src/gui/layout/qgslayoutruler.cpp

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,118 @@ void QgsLayoutRuler::mouseMoveEvent( QMouseEvent *event )
428428
mMarkerPos = event->posF();
429429
update();
430430

431-
//update cursor position in status bar
432-
QPointF displayPos = mTransform.inverted().map( event->posF() );
433-
switch ( mOrientation )
431+
QPointF displayPos;
432+
if ( mCreatingGuide )
434433
{
435-
case Qt::Horizontal:
434+
// event -> layout coordinates
435+
QgsLayout *layout = mView->currentLayout();
436+
QPoint viewPoint = mView->mapFromGlobal( mapToGlobal( event->pos() ) );
437+
displayPos = mView->mapToScene( viewPoint );
438+
int pageNo = layout->pageCollection()->pageNumberForPoint( displayPos );
439+
QgsLayoutItemPage *page = layout->pageCollection()->page( pageNo );
440+
441+
switch ( mOrientation )
436442
{
437-
//mouse is over a horizontal ruler, so don't show a y coordinate
438-
displayPos.setY( 0 );
439-
break;
443+
case Qt::Horizontal:
444+
{
445+
//mouse is creating a horizontal ruler, so don't show x coordinate
446+
mGuideItem->setLine( 0, displayPos.y(), page->rect().width(), displayPos.y() );
447+
displayPos.setX( 0 );
448+
break;
449+
}
450+
case Qt::Vertical:
451+
{
452+
//mouse is creating a vertical ruler, so don't show a y coordinate
453+
mGuideItem->setLine( displayPos.x(), 0, displayPos.x(), page->rect().height() );
454+
displayPos.setY( 0 );
455+
break;
456+
}
440457
}
441-
case Qt::Vertical:
458+
}
459+
else
460+
{
461+
//update cursor position in status bar
462+
displayPos = mTransform.inverted().map( event->posF() );
463+
switch ( mOrientation )
442464
{
443-
//mouse is over a vertical ruler, so don't show an x coordinate
444-
displayPos.setX( 0 );
445-
break;
465+
case Qt::Horizontal:
466+
{
467+
//mouse is over a horizontal ruler, so don't show a y coordinate
468+
displayPos.setY( 0 );
469+
break;
470+
}
471+
case Qt::Vertical:
472+
{
473+
//mouse is over a vertical ruler, so don't show an x coordinate
474+
displayPos.setX( 0 );
475+
break;
476+
}
446477
}
447478
}
448479
emit cursorPosChanged( displayPos );
449480
}
481+
482+
void QgsLayoutRuler::mousePressEvent( QMouseEvent *event )
483+
{
484+
if ( event->button() == Qt::LeftButton )
485+
{
486+
mCreatingGuide = true;
487+
mGuideItem.reset( new QGraphicsLineItem() );
488+
489+
mGuideItem->setZValue( QgsLayout::ZGuide );
490+
QPen linePen( Qt::DashLine );
491+
linePen.setColor( Qt::red );
492+
linePen.setWidthF( 0 );
493+
mGuideItem->setPen( linePen );
494+
495+
mView->currentLayout()->addItem( mGuideItem.get() );
496+
497+
switch ( mOrientation )
498+
{
499+
case Qt::Horizontal:
500+
{
501+
QApplication::setOverrideCursor( Qt::SplitVCursor );
502+
break;
503+
}
504+
case Qt::Vertical:
505+
QApplication::setOverrideCursor( Qt::SplitHCursor );
506+
break;
507+
}
508+
}
509+
}
510+
511+
void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
512+
{
513+
if ( event->button() == Qt::LeftButton )
514+
{
515+
mCreatingGuide = false;
516+
QApplication::restoreOverrideCursor();
517+
mGuideItem.reset();
518+
519+
QgsLayout *layout = mView->currentLayout();
520+
521+
// create guide
522+
QPoint viewPoint = mView->mapFromGlobal( mapToGlobal( event->pos() ) );
523+
QPointF scenePos = mView->mapToScene( viewPoint );
524+
int page = layout->pageCollection()->pageNumberForPoint( scenePos );
525+
std::unique_ptr< QgsLayoutGuide > guide;
526+
switch ( mOrientation )
527+
{
528+
case Qt::Horizontal:
529+
{
530+
//mouse is creating a horizontal guide
531+
double posOnPage = layout->pageCollection()->positionOnPage( scenePos ).y();
532+
guide.reset( new QgsLayoutGuide( QgsLayoutGuide::Horizontal, QgsLayoutMeasurement( posOnPage, layout->units() ) ) );
533+
break;
534+
}
535+
case Qt::Vertical:
536+
{
537+
//mouse is creating a vertical guide
538+
guide.reset( new QgsLayoutGuide( QgsLayoutGuide::Vertical, QgsLayoutMeasurement( scenePos.x(), layout->units() ) ) );
539+
break;
540+
}
541+
}
542+
guide->setPage( page );
543+
mView->currentLayout()->guides().addGuide( guide.release() );
544+
}
545+
}

‎src/gui/layout/qgslayoutruler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget
8181
protected:
8282
void paintEvent( QPaintEvent *event ) override;
8383
void mouseMoveEvent( QMouseEvent *event ) override;
84+
void mousePressEvent( QMouseEvent *event ) override;
85+
void mouseReleaseEvent( QMouseEvent *event ) override;
8486

8587
private:
8688
static const int VALID_SCALE_MULTIPLES[];
@@ -102,6 +104,9 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget
102104
int mTextBaseline;
103105
int mMinSpacingVerticalLabels;
104106

107+
bool mCreatingGuide = false;
108+
std::unique_ptr< QGraphicsLineItem > mGuideItem;
109+
105110
//! Calculates the optimum labeled units for ruler so that labels are a good distance apart
106111
int optimumScale( double minPixelDiff, int &magnitude, int &multiple );
107112

0 commit comments

Comments
 (0)
Please sign in to comment.