23 #include <QGraphicsLineItem>
32 , mOrientation( orientation )
33 , mPosition( position )
39 if ( mLayout && mLineItem )
41 mLayout->removeItem( mLineItem );
71 if ( !mLayout || !mLineItem )
81 double layoutPos = mLayout->convertToLayoutUnits( mPosition );
82 bool showGuide = mLayout->guides().visible();
83 switch ( mOrientation )
86 if ( layoutPos > mPage->rect().height() )
92 mLineItem->setLine( 0, layoutPos + mPage->y(), mPage->rect().width(), layoutPos + mPage->y() );
93 mLineItem->setVisible( showGuide );
99 if ( layoutPos > mPage->rect().width() )
105 mLineItem->setLine( layoutPos, mPage->y(), layoutPos, mPage->y() + mPage->rect().height() );
106 mLineItem->setVisible( showGuide );
123 switch ( mOrientation )
126 return mLineItem->mapToScene( mLineItem->line().p1() ).y();
129 return mLineItem->mapToScene( mLineItem->line().p1() ).x();
140 switch ( mOrientation )
143 p = mPage->mapFromScene( QPointF( 0,
position ) ).y();
147 p = mPage->mapFromScene( QPointF(
position, 0 ) ).x();
150 mPosition = mLayout->convertFromLayoutUnits( p, mPosition.
units() );
166 mLineItem =
new QGraphicsLineItem();
169 QPen linePen( Qt::DotLine );
170 linePen.setColor( Qt::red );
173 linePen.setWidthF( 0 );
174 mLineItem->setPen( linePen );
177 mLayout->addItem( mLineItem );
193 : QAbstractTableModel( layout )
195 , mPageCollection( pageCollection )
198 mHeaderSize = QFontMetrics( f ).boundingRect( QStringLiteral(
"XX" ) ).width();
205 qDeleteAll( mGuides );
215 return mGuides.count();
220 if ( parent.isValid() )
228 if ( !index.isValid() )
231 if ( index.row() >= mGuides.count() || index.row() < 0 )
237 case Qt::DisplayRole:
240 if ( index.column() == 0 )
268 if ( !index.isValid() )
271 if ( index.row() >= mGuides.count() || index.row() < 0 )
281 double newPos = value.toDouble( &ok );
291 emit dataChanged( index, index, QVector<int>() << role );
297 double newPos = value.toDouble( &ok );
310 emit dataChanged( index, index, QVector<int>() << role );
317 double newPos = value.toDouble( &ok );
324 emit dataChanged( index, index, QVector<int>() << role );
331 int units = value.toInt( &ok );
341 emit dataChanged( index, index, QVector<int>() << role );
351 if ( !index.isValid() )
352 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
353 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
358 if ( role == Qt::DisplayRole )
360 else if ( role == Qt::SizeHintRole )
362 return QSize( mHeaderSize, mHeaderSize );
364 return QAbstractTableModel::headerData( section, orientation, role );
369 if ( parent.isValid() )
372 if ( !mBlockUndoCommands )
374 beginRemoveRows( parent, row, row + count - 1 );
375 for (
int i = 0; i < count; ++ i )
377 delete mGuides.takeAt( row );
380 if ( !mBlockUndoCommands )
387 if ( guide->
layout() != mLayout )
390 if ( !mBlockUndoCommands )
392 beginInsertRows( QModelIndex(), mGuides.count(), mGuides.count() );
393 mGuides.append( guide );
395 if ( !mBlockUndoCommands )
398 QModelIndex index = createIndex( mGuides.length() - 1, 0 );
401 emit dataChanged( index, index );
407 int row = mGuides.indexOf( guide );
416 int row = mGuides.indexOf( guide );
427 qDeleteAll( mGuides );
436 mBlockUndoCommands =
true;
439 const auto constMGuides = mGuides;
442 if ( guide->page() != page )
449 for (
int p = 0; p < mPageCollection->
pageCount(); ++p )
451 if ( p == sourcePage )
454 std::unique_ptr< QgsLayoutGuide> newGuide(
new QgsLayoutGuide( guide->orientation(), guide->position(), mPageCollection->
page( p ) ) );
455 newGuide->setLayout( mLayout );
456 if ( newGuide->item()->isVisible() )
464 mBlockUndoCommands =
false;
469 const auto constMGuides = mGuides;
483 QList<QgsLayoutGuide *> res;
484 const auto constMGuides = mGuides;
487 if ( guide->orientation() == orientation && guide->item()->isVisible() &&
488 ( page < 0 || mPageCollection->page( page ) == guide->page() ) )
496 QList<QgsLayoutGuide *> res;
497 const auto constMGuides = mGuides;
500 if ( mPageCollection->
page( page ) == guide->
page() )
508 return mGuidesVisible;
519 void QgsLayoutGuideCollection::pageAboutToBeRemoved(
int pageNumber )
521 mBlockUndoCommands =
true;
522 const auto constGuidesOnPage =
guidesOnPage( pageNumber );
527 mBlockUndoCommands =
false;
532 QDomElement element = document.createElement( QStringLiteral(
"GuideCollection" ) );
533 element.setAttribute( QStringLiteral(
"visible" ), mGuidesVisible );
534 const auto constMGuides = mGuides;
537 QDomElement guideElement = document.createElement( QStringLiteral(
"Guide" ) );
538 guideElement.setAttribute( QStringLiteral(
"orientation" ), guide->orientation() );
539 guideElement.setAttribute( QStringLiteral(
"page" ), mPageCollection->
pageNumber( guide->page() ) );
540 guideElement.setAttribute( QStringLiteral(
"position" ), guide->position().length() );
542 element.appendChild( guideElement );
545 parentElement.appendChild( element );
551 QDomElement element = e;
552 if ( element.nodeName() != QLatin1String(
"GuideCollection" ) )
554 element = element.firstChildElement( QStringLiteral(
"GuideCollection" ) );
557 if ( element.nodeName() != QLatin1String(
"GuideCollection" ) )
562 mBlockUndoCommands =
true;
564 qDeleteAll( mGuides );
567 mGuidesVisible = element.attribute( QStringLiteral(
"visible" ), QStringLiteral(
"0" ) ) != QLatin1String(
"0" );
568 QDomNodeList guideNodeList = element.elementsByTagName( QStringLiteral(
"Guide" ) );
569 for (
int i = 0; i < guideNodeList.size(); ++i )
571 QDomElement element = guideNodeList.at( i ).toElement();
572 Qt::Orientation orientation =
static_cast< Qt::Orientation
>( element.attribute( QStringLiteral(
"orientation" ), QStringLiteral(
"1" ) ).toInt() );
573 double pos = element.attribute( QStringLiteral(
"position" ), QStringLiteral(
"0" ) ).toDouble();
575 int page = element.attribute( QStringLiteral(
"page" ), QStringLiteral(
"0" ) ).toInt();
582 mBlockUndoCommands =
false;
591 : QSortFilterProxyModel( parent )
592 , mOrientation( orientation )
595 setDynamicSortFilter(
true );
607 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
609 if ( orientation != mOrientation )
613 return page == mPage;
620 return leftPos < rightPos;