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 ).width( QStringLiteral(
"XX" ) );
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 );
336 m.
setUnits( static_cast< QgsUnitTypes::LayoutUnit >( units ) );
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 )
389 if ( !mBlockUndoCommands )
391 beginInsertRows( QModelIndex(), mGuides.count(), mGuides.count() );
392 mGuides.append( guide );
394 if ( !mBlockUndoCommands )
397 QModelIndex index = createIndex( mGuides.length() - 1, 0 );
400 emit dataChanged( index, index );
406 int row = mGuides.indexOf( guide );
415 int row = mGuides.indexOf( guide );
426 qDeleteAll( mGuides );
435 mBlockUndoCommands =
true;
438 const auto constMGuides = mGuides;
441 if ( guide->page() != page )
448 for (
int p = 0; p < mPageCollection->
pageCount(); ++p )
450 if ( p == sourcePage )
453 std::unique_ptr< QgsLayoutGuide> newGuide(
new QgsLayoutGuide( guide->orientation(), guide->position(), mPageCollection->
page( p ) ) );
454 newGuide->setLayout( mLayout );
455 if ( newGuide->item()->isVisible() )
463 mBlockUndoCommands =
false;
468 const auto constMGuides = mGuides;
482 QList<QgsLayoutGuide *> res;
483 const auto constMGuides = mGuides;
486 if ( guide->orientation() == orientation && guide->item()->isVisible() &&
487 ( page < 0 || mPageCollection->
page( page ) == guide->
page() ) )
495 QList<QgsLayoutGuide *> res;
496 const auto constMGuides = mGuides;
499 if ( mPageCollection->
page( page ) == guide->
page() )
507 return mGuidesVisible;
518 void QgsLayoutGuideCollection::pageAboutToBeRemoved(
int pageNumber )
520 mBlockUndoCommands =
true;
521 const auto constGuidesOnPage =
guidesOnPage( pageNumber );
526 mBlockUndoCommands =
false;
531 QDomElement element = document.createElement( QStringLiteral(
"GuideCollection" ) );
532 element.setAttribute( QStringLiteral(
"visible" ), mGuidesVisible );
533 const auto constMGuides = mGuides;
536 QDomElement guideElement = document.createElement( QStringLiteral(
"Guide" ) );
537 guideElement.setAttribute( QStringLiteral(
"orientation" ), guide->orientation() );
538 guideElement.setAttribute( QStringLiteral(
"page" ), mPageCollection->
pageNumber( guide->page() ) );
539 guideElement.setAttribute( QStringLiteral(
"position" ), guide->position().length() );
541 element.appendChild( guideElement );
544 parentElement.appendChild( element );
550 QDomElement element = e;
551 if ( element.nodeName() != QStringLiteral(
"GuideCollection" ) )
553 element = element.firstChildElement( QStringLiteral(
"GuideCollection" ) );
556 if ( element.nodeName() != QStringLiteral(
"GuideCollection" ) )
561 mBlockUndoCommands =
true;
563 qDeleteAll( mGuides );
566 mGuidesVisible = element.attribute( QStringLiteral(
"visible" ), QStringLiteral(
"0" ) ) != QLatin1String(
"0" );
567 QDomNodeList guideNodeList = element.elementsByTagName( QStringLiteral(
"Guide" ) );
568 for (
int i = 0; i < guideNodeList.size(); ++i )
570 QDomElement element = guideNodeList.at( i ).toElement();
571 Qt::Orientation orientation =
static_cast< Qt::Orientation
>( element.attribute( QStringLiteral(
"orientation" ), QStringLiteral(
"1" ) ).toInt() );
572 double pos = element.attribute( QStringLiteral(
"position" ), QStringLiteral(
"0" ) ).toDouble();
574 int page = element.attribute( QStringLiteral(
"page" ), QStringLiteral(
"0" ) ).toInt();
581 mBlockUndoCommands =
false;
590 : QSortFilterProxyModel( parent )
591 , mOrientation( orientation )
594 setDynamicSortFilter(
true );
606 QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
608 if ( orientation != mOrientation )
612 return page == mPage;
619 return leftPos < rightPos;
void update()
Updates the position of the guide's line item.
The class is used as a container of context for various read/write operations on other objects...
QgsUnitTypes::LayoutUnit units() const
Returns the units for the measurement.
~QgsLayoutGuide() override
void pageAboutToBeRemoved(int pageNumber)
Emitted just before a page is removed 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...
QgsLayoutMeasurement position() const
Returns the guide's position within the page.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
Contains the configuration for a single snap guide used by a layout.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
void setPage(int page)
Sets the current page for filtering matching guides.
bool visible() const
Returns true if the guide lines should be drawn.
void setLayoutPosition(double position)
Sets the guide's position in absolute layout units.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
void setLayout(QgsLayout *layout)
Sets the layout the guide belongs to.
double layoutPosition() const
Returns the guide's position in absolute layout units.
Qt::Orientation orientation() const
Returns the guide's orientation.
void clear()
Removes all guides from the collection.
void applyGuidesToAllOtherPages(int sourcePage)
Resets all other pages' guides to match the guides from the specified sourcePage. ...
void positionChanged()
Emitted when the guide's position is changed.
QVariant data(const QModelIndex &index, int role) const override
QGraphicsLineItem * item()
Returns the guide's line item.
void addGuide(QgsLayoutGuide *guide)
Adds a guide to the collection.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the collection's state in a DOM element.
void endCommand()
Saves final state of an object and pushes the active command to the undo history. ...
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
void setLength(const double length)
Sets the length of the measurement.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
Guide position in layout coordinates.
void setGuideLayoutPosition(QgsLayoutGuide *guide, double position)
Sets the absolute position (in layout coordinates) for guide within the layout.
QgsLayout * layout() const
Returns the layout the guide belongs to.
~QgsLayoutGuideCollection() override
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
int page() const
Returns the page the item is currently on, with the first page returning 0.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
void setPage(QgsLayoutItemPage *page)
Sets the page the guide is contained within.
A manager for a collection of pages in a layout.
Guide position units role.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
int pageCount() const
Returns the number of pages in the collection.
int columnCount(const QModelIndex &) const override
void setUnits(const QgsUnitTypes::LayoutUnit units)
Sets the units for the measurement.
bool readXml(const QDomElement &collectionElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection's state from a DOM element.
QgsLayoutGuideCollection(QgsLayout *layout, QgsLayoutPageCollection *pageCollection)
Constructor for QgsLayoutGuideCollection belonging to the specified layout, and linked to the specifi...
QList< QgsLayoutGuide *> guides()
Returns a list of all guides contained in the collection.
QgsLayoutGuide(Qt::Orientation orientation, QgsLayoutMeasurement position, QgsLayoutItemPage *page)
Constructor for a new guide with the specified orientation and initial position.
void setPosition(QgsLayoutMeasurement position)
Sets the guide's position within the page.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
double length() const
Returns the length of the measurement.
QgsLayoutGuideProxyModel(QObject *parent, Qt::Orientation orientation, int page)
Constructor for QgsLayoutGuideProxyModel, filtered to guides of the specified orientation and page on...
QList< QgsLayoutGuide *> guidesOnPage(int page)
Returns the list of guides contained on a matching page.
void update()
Updates the position (and visibility) of all guide line items.
QgsLayout * layout() override
Returns the layout the object belongs to.
QgsLayoutItemPage * page()
Returns the page the guide is contained within.
LayoutUnit
Layout measurement units.
int rowCount(const QModelIndex &) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
void setVisible(bool visible)
Sets whether the guide lines should be visible.
void removeGuide(QgsLayoutGuide *guide)
Removes the specified guide, and deletes it.
Item representing the paper in a layout.