|
| 1 | +/*************************************************************************** |
| 2 | + qgslayoutguidewidget.cpp |
| 3 | + ------------------------ |
| 4 | + begin : July 2017 |
| 5 | + copyright : (C) 2017 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | +/*************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +#include "qgslayoutguidewidget.h" |
| 18 | +#include "qgslayout.h" |
| 19 | +#include "qgslayoutview.h" |
| 20 | +#include "qgsdoublespinbox.h" |
| 21 | +#include "qgslayoutunitscombobox.h" |
| 22 | + |
| 23 | +QgsLayoutGuideWidget::QgsLayoutGuideWidget( QWidget *parent, QgsLayout *layout, QgsLayoutView *layoutView ) |
| 24 | + : QgsPanelWidget( parent ) |
| 25 | + , mLayout( layout ) |
| 26 | +{ |
| 27 | + setupUi( this ); |
| 28 | + setPanelTitle( tr( "Guides" ) ); |
| 29 | + |
| 30 | + mHozProxyModel = new QgsLayoutGuideProxyModel( mHozGuidesTableView, QgsLayoutGuide::Horizontal, 0 ); |
| 31 | + mHozProxyModel->setSourceModel( &mLayout->guides() ); |
| 32 | + mVertProxyModel = new QgsLayoutGuideProxyModel( mVertGuidesTableView, QgsLayoutGuide::Vertical, 0 ); |
| 33 | + mVertProxyModel->setSourceModel( &mLayout->guides() ); |
| 34 | + |
| 35 | + mHozGuidesTableView->setModel( mHozProxyModel ); |
| 36 | + mVertGuidesTableView->setModel( mVertProxyModel ); |
| 37 | + |
| 38 | + mHozGuidesTableView->setEditTriggers( QAbstractItemView::AllEditTriggers ); |
| 39 | + mVertGuidesTableView->setEditTriggers( QAbstractItemView::AllEditTriggers ); |
| 40 | + |
| 41 | + |
| 42 | + mHozGuidesTableView->setItemDelegateForColumn( 0, new QgsLayoutGuidePositionDelegate( mLayout, mHozProxyModel ) ); |
| 43 | + mHozGuidesTableView->setItemDelegateForColumn( 1, new QgsLayoutGuideUnitDelegate( mLayout, mHozProxyModel ) ); |
| 44 | + |
| 45 | + mVertGuidesTableView->setItemDelegateForColumn( 0, new QgsLayoutGuidePositionDelegate( mLayout, mVertProxyModel ) ); |
| 46 | + mVertGuidesTableView->setItemDelegateForColumn( 1, new QgsLayoutGuideUnitDelegate( mLayout, mVertProxyModel ) ); |
| 47 | + |
| 48 | + connect( mAddHozGuideButton, &QPushButton::clicked, this, &QgsLayoutGuideWidget::addHorizontalGuide ); |
| 49 | + connect( mAddVertGuideButton, &QPushButton::clicked, this, &QgsLayoutGuideWidget::addVerticalGuide ); |
| 50 | + |
| 51 | + connect( mDeleteHozGuideButton, &QPushButton::clicked, this, &QgsLayoutGuideWidget::deleteHorizontalGuide ); |
| 52 | + connect( mDeleteVertGuideButton, &QPushButton::clicked, this, &QgsLayoutGuideWidget::deleteVerticalGuide ); |
| 53 | + |
| 54 | + connect( layoutView, &QgsLayoutView::pageChanged, this, &QgsLayoutGuideWidget::pageChanged ); |
| 55 | + pageChanged( 0 ); |
| 56 | +} |
| 57 | + |
| 58 | +void QgsLayoutGuideWidget::addHorizontalGuide() |
| 59 | +{ |
| 60 | + std::unique_ptr< QgsLayoutGuide > newGuide( new QgsLayoutGuide( QgsLayoutGuide::Horizontal, QgsLayoutMeasurement( 0 ) ) ); |
| 61 | + newGuide->setPage( mPage ); |
| 62 | + mLayout->guides().addGuide( newGuide.release() ); |
| 63 | +} |
| 64 | + |
| 65 | +void QgsLayoutGuideWidget::addVerticalGuide() |
| 66 | +{ |
| 67 | + std::unique_ptr< QgsLayoutGuide > newGuide( new QgsLayoutGuide( QgsLayoutGuide::Vertical, QgsLayoutMeasurement( 0 ) ) ); |
| 68 | + newGuide->setPage( mPage ); |
| 69 | + mLayout->guides().addGuide( newGuide.release() ); |
| 70 | +} |
| 71 | + |
| 72 | +void QgsLayoutGuideWidget::deleteHorizontalGuide() |
| 73 | +{ |
| 74 | + Q_FOREACH ( const QModelIndex &index, mHozGuidesTableView->selectionModel()->selectedIndexes() ) |
| 75 | + { |
| 76 | + mHozGuidesTableView->closePersistentEditor( index ); |
| 77 | + if ( index.column() == 0 ) |
| 78 | + mHozProxyModel->removeRow( index.row() ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void QgsLayoutGuideWidget::deleteVerticalGuide() |
| 83 | +{ |
| 84 | + Q_FOREACH ( const QModelIndex &index, mVertGuidesTableView->selectionModel()->selectedIndexes() ) |
| 85 | + { |
| 86 | + mVertGuidesTableView->closePersistentEditor( index ); |
| 87 | + if ( index.column() == 0 ) |
| 88 | + mVertProxyModel->removeRow( index.row() ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void QgsLayoutGuideWidget::pageChanged( int page ) |
| 93 | +{ |
| 94 | + mPage = page; |
| 95 | + |
| 96 | + // have to close any open editors - or we'll get a crash |
| 97 | + |
| 98 | + // qt - y u no do this for me? |
| 99 | + Q_FOREACH ( const QModelIndex &index, mHozGuidesTableView->selectionModel()->selectedIndexes() ) |
| 100 | + { |
| 101 | + mHozGuidesTableView->closePersistentEditor( index ); |
| 102 | + } |
| 103 | + Q_FOREACH ( const QModelIndex &index, mVertGuidesTableView->selectionModel()->selectedIndexes() ) |
| 104 | + { |
| 105 | + mVertGuidesTableView->closePersistentEditor( index ); |
| 106 | + } |
| 107 | + |
| 108 | + mHozProxyModel->setPage( page ); |
| 109 | + mVertProxyModel->setPage( page ); |
| 110 | + mPageLabel->setText( tr( "Guides for page %1" ).arg( page + 1 ) ); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +QgsLayoutGuidePositionDelegate::QgsLayoutGuidePositionDelegate( QgsLayout *layout, QAbstractItemModel *model ) |
| 115 | + : mLayout( layout ) |
| 116 | + , mModel( model ) |
| 117 | +{ |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +QWidget *QgsLayoutGuidePositionDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index ) const |
| 122 | +{ |
| 123 | + QgsDoubleSpinBox *spin = new QgsDoubleSpinBox( parent ); |
| 124 | + spin->setMinimum( 0 ); |
| 125 | + spin->setMaximum( 1000000 ); |
| 126 | + spin->setDecimals( 2 ); |
| 127 | + spin->setShowClearButton( false ); |
| 128 | + connect( spin, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double v ) |
| 129 | + { |
| 130 | + // we want to update on every spin change, not just the final |
| 131 | + setModelData( index, v, QgsLayoutGuideCollection::PositionRole ); |
| 132 | + } ); |
| 133 | + return spin; |
| 134 | +} |
| 135 | + |
| 136 | +void QgsLayoutGuidePositionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const |
| 137 | +{ |
| 138 | + QgsDoubleSpinBox *spin = qobject_cast< QgsDoubleSpinBox * >( editor ); |
| 139 | + model->setData( index, spin->value(), QgsLayoutGuideCollection::PositionRole ); |
| 140 | +} |
| 141 | + |
| 142 | +void QgsLayoutGuidePositionDelegate::setModelData( const QModelIndex &index, const QVariant &value, int role ) const |
| 143 | +{ |
| 144 | + mModel->setData( index, value, role ); |
| 145 | +} |
| 146 | + |
| 147 | +QgsLayoutGuideUnitDelegate::QgsLayoutGuideUnitDelegate( QgsLayout *layout, QAbstractItemModel *model ) |
| 148 | + : mLayout( layout ) |
| 149 | + , mModel( model ) |
| 150 | +{ |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | +QWidget *QgsLayoutGuideUnitDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index ) const |
| 155 | +{ |
| 156 | + QgsLayoutUnitsComboBox *unitsCb = new QgsLayoutUnitsComboBox( parent ); |
| 157 | + connect( unitsCb, &QgsLayoutUnitsComboBox::changed, this, [ = ]( int unit ) |
| 158 | + { |
| 159 | + // we want to update on every unit change, not just the final |
| 160 | + setModelData( index, unit, QgsLayoutGuideCollection::UnitsRole ); |
| 161 | + } ); |
| 162 | + return unitsCb; |
| 163 | +} |
| 164 | + |
| 165 | +void QgsLayoutGuideUnitDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const |
| 166 | +{ |
| 167 | + QgsLayoutUnitsComboBox *cb = qobject_cast< QgsLayoutUnitsComboBox *>( editor ); |
| 168 | + model->setData( index, cb->unit(), QgsLayoutGuideCollection::UnitsRole ); |
| 169 | +} |
| 170 | + |
| 171 | +void QgsLayoutGuideUnitDelegate::setModelData( const QModelIndex &index, const QVariant &value, int role ) const |
| 172 | +{ |
| 173 | + mModel->setData( index, value, role ); |
| 174 | +} |
0 commit comments