18#include "moc_qgslayoutguidecollection.cpp"
25#include <QGraphicsLineItem>
34 , mOrientation( orientation )
35 , mPosition( position )
41 if ( mLayout && mLineItem )
43 mLayout->removeItem( mLineItem );
73 if ( !mLayout || !mLineItem )
83 double layoutPos = mLayout->convertToLayoutUnits( mPosition );
84 bool showGuide = mLayout->guides().visible();
85 switch ( mOrientation )
88 if ( layoutPos > mPage->rect().height() )
94 mLineItem->setLine( 0, layoutPos + mPage->y(), mPage->rect().width(), layoutPos + mPage->y() );
95 mLineItem->setVisible( showGuide );
101 if ( layoutPos > mPage->rect().width() )
107 mLineItem->setLine( layoutPos, mPage->y(), layoutPos, mPage->y() + mPage->rect().height() );
108 mLineItem->setVisible( showGuide );
125 switch ( mOrientation )
128 return mLineItem->mapToScene( mLineItem->line().p1() ).y();
131 return mLineItem->mapToScene( mLineItem->line().p1() ).x();
142 switch ( mOrientation )
145 p = mPage->mapFromScene( QPointF( 0,
position ) ).y();
149 p = mPage->mapFromScene( QPointF(
position, 0 ) ).x();
152 mPosition = mLayout->convertFromLayoutUnits( p, mPosition.
units() );
168 mLineItem =
new QGraphicsLineItem();
171 QPen linePen( Qt::DotLine );
172 linePen.setColor( Qt::red );
175 linePen.setWidthF( 0 );
176 mLineItem->setPen( linePen );
179 mLayout->addItem( mLineItem );
195 : QAbstractTableModel( layout )
197 , mPageCollection( pageCollection )
200 mHeaderSize = QFontMetrics( f ).boundingRect( QStringLiteral(
"XX" ) ).width();
207 qDeleteAll( mGuides );
217 return mGuides.count();
222 if ( parent.isValid() )
230 if ( !index.isValid() )
233 if ( index.row() >= mGuides.count() || index.row() < 0 )
239 case Qt::DisplayRole:
242 if ( index.column() == 0 )
249 return QVariant::fromValue( guide->
orientation() );
270 if ( !index.isValid() )
273 if ( index.row() >= mGuides.count() || index.row() < 0 )
283 double newPos = value.toDouble( &ok );
293 emit dataChanged( index, index, QVector<int>() << role );
299 double newPos = value.toDouble( &ok );
312 emit dataChanged( index, index, QVector<int>() << role );
319 double newPos = value.toDouble( &ok );
326 emit dataChanged( index, index, QVector<int>() << role );
333 int units = value.toInt( &ok );
343 emit dataChanged( index, index, QVector<int>() << role );
356 if ( !index.isValid() )
357 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
358 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
363 if ( role == Qt::DisplayRole )
365 else if ( role == Qt::SizeHintRole )
367 return QSize( mHeaderSize, mHeaderSize );
369 return QAbstractTableModel::headerData( section, orientation, role );
374 if ( parent.isValid() )
377 if ( !mBlockUndoCommands )
379 beginRemoveRows( parent, row, row + count - 1 );
380 for (
int i = 0; i < count; ++ i )
382 delete mGuides.takeAt( row );
385 if ( !mBlockUndoCommands )
392 if ( guide->
layout() != mLayout )
395 if ( !mBlockUndoCommands )
397 beginInsertRows( QModelIndex(), mGuides.count(), mGuides.count() );
398 mGuides.append( guide );
400 if ( !mBlockUndoCommands )
403 QModelIndex index = createIndex( mGuides.length() - 1, 0 );
406 emit dataChanged( index, index );
412 int row = mGuides.indexOf( guide );
421 int row = mGuides.indexOf( guide );
432 qDeleteAll( mGuides );
441 mBlockUndoCommands =
true;
444 const auto constMGuides = mGuides;
447 if ( guide->page() != page )
452 const auto constMGuidesNew = mGuides;
455 for (
int p = 0; p < mPageCollection->
pageCount(); ++p )
457 if ( p == sourcePage )
460 std::unique_ptr< QgsLayoutGuide> newGuide(
new QgsLayoutGuide( guide->orientation(), guide->position(), mPageCollection->
page( p ) ) );
461 newGuide->setLayout( mLayout );
462 if ( newGuide->item()->isVisible() )
470 mBlockUndoCommands =
false;
475 const auto constMGuides = mGuides;
489 QList<QgsLayoutGuide *> res;
490 const auto constMGuides = mGuides;
493 if ( guide->orientation() == orientation && guide->item()->isVisible() &&
494 ( page < 0 || mPageCollection->
page( page ) == guide->
page() ) )
502 QList<QgsLayoutGuide *> res;
503 const auto constMGuides = mGuides;
506 if ( mPageCollection->
page( page ) == guide->
page() )
514 return mGuidesVisible;
525void QgsLayoutGuideCollection::pageAboutToBeRemoved(
int pageNumber )
527 mBlockUndoCommands =
true;
528 const auto constGuidesOnPage =
guidesOnPage( pageNumber );
533 mBlockUndoCommands =
false;
538 QDomElement element = document.createElement( QStringLiteral(
"GuideCollection" ) );
539 element.setAttribute( QStringLiteral(
"visible" ), mGuidesVisible );
540 const auto constMGuides = mGuides;
543 QDomElement guideElement = document.createElement( QStringLiteral(
"Guide" ) );
544 guideElement.setAttribute( QStringLiteral(
"orientation" ), guide->orientation() );
545 guideElement.setAttribute( QStringLiteral(
"page" ), mPageCollection->
pageNumber( guide->page() ) );
546 guideElement.setAttribute( QStringLiteral(
"position" ), guide->position().length() );
548 element.appendChild( guideElement );
551 parentElement.appendChild( element );
557 QDomElement element = e;
558 if ( element.nodeName() != QLatin1String(
"GuideCollection" ) )
560 element = element.firstChildElement( QStringLiteral(
"GuideCollection" ) );
563 if ( element.nodeName() != QLatin1String(
"GuideCollection" ) )
568 mBlockUndoCommands =
true;
570 qDeleteAll( mGuides );
573 mGuidesVisible = element.attribute( QStringLiteral(
"visible" ), QStringLiteral(
"0" ) ) != QLatin1String(
"0" );
574 QDomNodeList guideNodeList = element.elementsByTagName( QStringLiteral(
"Guide" ) );
575 for (
int i = 0; i < guideNodeList.size(); ++i )
577 QDomElement element = guideNodeList.at( i ).toElement();
578 Qt::Orientation orientation =
static_cast< Qt::Orientation
>( element.attribute( QStringLiteral(
"orientation" ), QStringLiteral(
"1" ) ).toInt() );
579 double pos = element.attribute( QStringLiteral(
"position" ), QStringLiteral(
"0" ) ).toDouble();
581 int page = element.attribute( QStringLiteral(
"page" ), QStringLiteral(
"0" ) ).toInt();
588 mBlockUndoCommands =
false;
597 : QSortFilterProxyModel( parent )
598 , mOrientation( orientation )
601 setDynamicSortFilter(
true );
613 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
615 if ( orientation != mOrientation )
619 return page == mPage;
626 return leftPos < rightPos;
LayoutUnit
Layout measurement units.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the collection's state in a DOM element.
int columnCount(const QModelIndex &) const override
void addGuide(QgsLayoutGuide *guide)
Adds a guide to the collection.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QgsLayout * layout() override
Returns the layout the object belongs to.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QgsLayoutGuideCollection(QgsLayout *layout, QgsLayoutPageCollection *pageCollection)
Constructor for QgsLayoutGuideCollection belonging to the specified layout, and linked to the specifi...
bool readXml(const QDomElement &collectionElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection's state from a DOM element.
@ Position
Guide position role.
@ LayoutPosition
Guide position in layout coordinates.
@ Orientation
Guide orientation role.
@ Units
Guide position units role.
QVariant data(const QModelIndex &index, int role) const override
void applyGuidesToAllOtherPages(int sourcePage)
Resets all other pages' guides to match the guides from the specified sourcePage.
void removeGuide(QgsLayoutGuide *guide)
Removes the specified guide, and deletes it.
QList< QgsLayoutGuide * > guidesOnPage(int page)
Returns the list of guides contained on a matching page.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
~QgsLayoutGuideCollection() override
void setGuideLayoutPosition(QgsLayoutGuide *guide, double position)
Sets the absolute position (in layout coordinates) for guide within the layout.
QList< QgsLayoutGuide * > guides()
Returns a list of all guides contained in the collection.
int rowCount(const QModelIndex &) const override
void clear()
Removes all guides from the collection.
Qt::ItemFlags flags(const QModelIndex &index) const override
bool visible() const
Returns true if the guide lines should be drawn.
void update()
Updates the position (and visibility) of all guide line items.
void setVisible(bool visible)
Sets whether the guide lines should be visible.
void setPage(int page)
Sets the current page for filtering matching guides.
QgsLayoutGuideProxyModel(QObject *parent, Qt::Orientation orientation, int page)
Constructor for QgsLayoutGuideProxyModel, filtered to guides of the specified orientation and page on...
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Contains the configuration for a single snap guide used by a layout.
QgsLayoutMeasurement position() const
Returns the guide's position within the page.
QgsLayoutItemPage * page()
Returns the page the guide is contained within.
Qt::Orientation orientation() const
Returns the guide's orientation.
QgsLayout * layout() const
Returns the layout the guide belongs to.
void setLayout(QgsLayout *layout)
Sets the layout the guide belongs to.
~QgsLayoutGuide() override
void setLayoutPosition(double position)
Sets the guide's position in absolute layout units.
void setPage(QgsLayoutItemPage *page)
Sets the page the guide is contained within.
void setPosition(QgsLayoutMeasurement position)
Sets the guide's position within the page.
QgsLayoutGuide(Qt::Orientation orientation, QgsLayoutMeasurement position, QgsLayoutItemPage *page)
Constructor for a new guide with the specified orientation and initial position.
double layoutPosition() const
Returns the guide's position in absolute layout units.
void positionChanged()
Emitted when the guide's position is changed.
void update()
Updates the position of the guide's line item.
QGraphicsLineItem * item()
Returns the guide's line item.
Item representing the paper in a layout.
int page() const
Returns the page the item is currently on, with the first page returning 0.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
void setLength(const double length)
Sets the length of the measurement.
Qgis::LayoutUnit units() const
Returns the units for the measurement.
void setUnits(const Qgis::LayoutUnit units)
Sets the units for the measurement.
double length() const
Returns the length of the measurement.
A manager for a collection of pages in a layout.
void pageAboutToBeRemoved(int pageNumber)
Emitted just before a page is removed from the collection.
int pageCount() const
Returns the number of pages in the collection.
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
int pageNumber(QgsLayoutItemPage *page) const
Returns the page number for the specified page, or -1 if the page is not contained in the collection.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
@ ZGuide
Z-value for page guides.
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
The class is used as a container of context for various read/write operations on other objects.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static Q_INVOKABLE Qgis::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.