Skip to content

Commit 048a5b7

Browse files
committedAug 7, 2017
Nicer table view for guide editing
1 parent 886a120 commit 048a5b7

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed
 

‎python/core/layout/qgslayoutguidecollection.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class QgsLayoutGuide : QObject
127127

128128
};
129129

130-
class QgsLayoutGuideCollection : QAbstractListModel
130+
class QgsLayoutGuideCollection : QAbstractTableModel
131131
{
132132
%Docstring
133133
Stores and manages the snap guides used by a layout.
@@ -156,6 +156,8 @@ class QgsLayoutGuideCollection : QAbstractListModel
156156

157157
virtual int rowCount( const QModelIndex & ) const;
158158

159+
virtual int columnCount( const QModelIndex & ) const;
160+
159161
virtual QVariant data( const QModelIndex &index, int role ) const;
160162

161163
virtual bool setData( const QModelIndex &index, const QVariant &value, int role );

‎src/core/layout/qgslayoutguidecollection.cpp

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ QgsLayoutGuide::Orientation QgsLayoutGuide::orientation() const
149149
//
150150

151151
QgsLayoutGuideCollection::QgsLayoutGuideCollection( QgsLayout *layout )
152-
: QAbstractListModel( layout )
152+
: QAbstractTableModel( layout )
153153
, mLayout( layout )
154154
{
155155

@@ -165,6 +165,14 @@ int QgsLayoutGuideCollection::rowCount( const QModelIndex & ) const
165165
return mGuides.count();
166166
}
167167

168+
int QgsLayoutGuideCollection::columnCount( const QModelIndex &parent ) const
169+
{
170+
if ( parent.isValid() )
171+
return 0;
172+
173+
return 2;
174+
}
175+
168176
QVariant QgsLayoutGuideCollection::data( const QModelIndex &index, int role ) const
169177
{
170178
if ( !index.isValid() )
@@ -179,12 +187,12 @@ QVariant QgsLayoutGuideCollection::data( const QModelIndex &index, int role ) co
179187
case Qt::DisplayRole:
180188
case Qt::EditRole:
181189
{
182-
return guide->position().length();
190+
if ( index.column() == 0 )
191+
return guide->position().length();
192+
else
193+
return QgsUnitTypes::toAbbreviatedString( guide->position().units() );
183194
}
184195

185-
case Qt::TextAlignmentRole:
186-
return QVariant( Qt::AlignRight | Qt::AlignVCenter );
187-
188196
case OrientationRole:
189197
return guide->orientation();
190198

@@ -215,19 +223,49 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant
215223

216224
QgsLayoutGuide *guide = mGuides.at( index.row() );
217225

218-
if ( role == Qt::EditRole )
226+
switch ( role )
219227
{
220-
bool ok = false;
221-
double newPos = value.toDouble( &ok );
222-
if ( !ok )
223-
return false;
224-
225-
QgsLayoutMeasurement m = guide->position();
226-
m.setLength( newPos );
227-
guide->setPosition( m );
228-
guide->update();
229-
return true;
228+
case Qt::EditRole:
229+
{
230+
bool ok = false;
231+
double newPos = value.toDouble( &ok );
232+
if ( !ok )
233+
return false;
234+
235+
QgsLayoutMeasurement m = guide->position();
236+
m.setLength( newPos );
237+
guide->setPosition( m );
238+
guide->update();
239+
return true;
240+
}
241+
case PositionRole:
242+
{
243+
bool ok = false;
244+
double newPos = value.toDouble( &ok );
245+
if ( !ok )
246+
return false;
247+
248+
QgsLayoutMeasurement m = guide->position();
249+
m.setLength( newPos );
250+
guide->setPosition( m );
251+
guide->update();
252+
return true;
253+
}
254+
case UnitsRole:
255+
{
256+
bool ok = false;
257+
int units = value.toInt( &ok );
258+
if ( !ok )
259+
return false;
260+
261+
QgsLayoutMeasurement m = guide->position();
262+
m.setUnits( static_cast< QgsUnitTypes::LayoutUnit >( units ) );
263+
guide->setPosition( m );
264+
guide->update();
265+
return true;
266+
}
230267
}
268+
231269
return false;
232270
}
233271

‎src/core/layout/qgslayoutguidecollection.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class CORE_EXPORT QgsLayoutGuide : public QObject
161161
* \brief Stores and manages the snap guides used by a layout.
162162
* \since QGIS 3.0
163163
*/
164-
class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractListModel
164+
class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
165165
{
166166

167167
Q_OBJECT
@@ -185,6 +185,7 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractListModel
185185
~QgsLayoutGuideCollection();
186186

187187
int rowCount( const QModelIndex & ) const override;
188+
int columnCount( const QModelIndex & ) const override;
188189
QVariant data( const QModelIndex &index, int role ) const override;
189190
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
190191
Qt::ItemFlags flags( const QModelIndex &index ) const override;

0 commit comments

Comments
 (0)
Please sign in to comment.