Skip to content

Commit 4565d8d

Browse files
committed
Add missing files
1 parent df3bcdd commit 4565d8d

File tree

3 files changed

+493
-0
lines changed

3 files changed

+493
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/***************************************************************************
2+
qgslayoutguidewidget.h
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+
#ifndef QGSLAYOUTGUIDEWIDGET_H
18+
#define QGSLAYOUTGUIDEWIDGET_H
19+
20+
#include "ui_qgslayoutguidewidgetbase.h"
21+
#include "qgspanelwidget.h"
22+
#include <QStyledItemDelegate>
23+
24+
25+
class QgsLayoutView;
26+
class QgsLayout;
27+
class QgsLayoutGuideProxyModel;
28+
29+
class QgsLayoutGuideWidget: public QgsPanelWidget, private Ui::QgsLayoutGuideWidgetBase
30+
{
31+
Q_OBJECT
32+
public:
33+
QgsLayoutGuideWidget( QWidget *parent, QgsLayout *layout, QgsLayoutView *layoutView );
34+
35+
private slots:
36+
37+
void addHorizontalGuide();
38+
void addVerticalGuide();
39+
40+
void deleteHorizontalGuide();
41+
void deleteVerticalGuide();
42+
43+
void pageChanged( int page );
44+
45+
private:
46+
47+
QgsLayout *mLayout = nullptr;
48+
QgsLayoutGuideProxyModel *mHozProxyModel = nullptr;
49+
QgsLayoutGuideProxyModel *mVertProxyModel = nullptr;
50+
int mPage = 0;
51+
52+
};
53+
54+
55+
class QgsLayoutGuidePositionDelegate : public QStyledItemDelegate
56+
{
57+
Q_OBJECT
58+
59+
public:
60+
61+
QgsLayoutGuidePositionDelegate( QgsLayout *layout, QAbstractItemModel *model );
62+
63+
protected:
64+
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
65+
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
66+
67+
void setModelData( const QModelIndex &index, const QVariant &value, int role ) const;
68+
69+
private:
70+
71+
QgsLayout *mLayout = nullptr;
72+
QAbstractItemModel *mModel = nullptr;
73+
};
74+
75+
class QgsLayoutGuideUnitDelegate : public QStyledItemDelegate
76+
{
77+
Q_OBJECT
78+
79+
public:
80+
81+
QgsLayoutGuideUnitDelegate( QgsLayout *layout, QAbstractItemModel *model );
82+
83+
protected:
84+
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
85+
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
86+
87+
void setModelData( const QModelIndex &index, const QVariant &value, int role ) const;
88+
89+
private:
90+
91+
QgsLayout *mLayout = nullptr;
92+
93+
QAbstractItemModel *mModel = nullptr;
94+
};
95+
96+
#endif // QGSLAYOUTGUIDEWIDGET_H

0 commit comments

Comments
 (0)