Skip to content

Commit

Permalink
Nicer table view for guide editing
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 886a120 commit 048a5b7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
4 changes: 3 additions & 1 deletion python/core/layout/qgslayoutguidecollection.sip
Expand Up @@ -127,7 +127,7 @@ class QgsLayoutGuide : QObject

};

class QgsLayoutGuideCollection : QAbstractListModel
class QgsLayoutGuideCollection : QAbstractTableModel
{
%Docstring
Stores and manages the snap guides used by a layout.
Expand Down Expand Up @@ -156,6 +156,8 @@ class QgsLayoutGuideCollection : QAbstractListModel

virtual int rowCount( const QModelIndex & ) const;

virtual int columnCount( const QModelIndex & ) const;

virtual QVariant data( const QModelIndex &index, int role ) const;

virtual bool setData( const QModelIndex &index, const QVariant &value, int role );
Expand Down
70 changes: 54 additions & 16 deletions src/core/layout/qgslayoutguidecollection.cpp
Expand Up @@ -149,7 +149,7 @@ QgsLayoutGuide::Orientation QgsLayoutGuide::orientation() const
//

QgsLayoutGuideCollection::QgsLayoutGuideCollection( QgsLayout *layout )
: QAbstractListModel( layout )
: QAbstractTableModel( layout )
, mLayout( layout )
{

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

int QgsLayoutGuideCollection::columnCount( const QModelIndex &parent ) const
{
if ( parent.isValid() )
return 0;

return 2;
}

QVariant QgsLayoutGuideCollection::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() )
Expand All @@ -179,12 +187,12 @@ QVariant QgsLayoutGuideCollection::data( const QModelIndex &index, int role ) co
case Qt::DisplayRole:
case Qt::EditRole:
{
return guide->position().length();
if ( index.column() == 0 )
return guide->position().length();
else
return QgsUnitTypes::toAbbreviatedString( guide->position().units() );
}

case Qt::TextAlignmentRole:
return QVariant( Qt::AlignRight | Qt::AlignVCenter );

case OrientationRole:
return guide->orientation();

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

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

if ( role == Qt::EditRole )
switch ( role )
{
bool ok = false;
double newPos = value.toDouble( &ok );
if ( !ok )
return false;

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
guide->setPosition( m );
guide->update();
return true;
case Qt::EditRole:
{
bool ok = false;
double newPos = value.toDouble( &ok );
if ( !ok )
return false;

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
guide->setPosition( m );
guide->update();
return true;
}
case PositionRole:
{
bool ok = false;
double newPos = value.toDouble( &ok );
if ( !ok )
return false;

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
guide->setPosition( m );
guide->update();
return true;
}
case UnitsRole:
{
bool ok = false;
int units = value.toInt( &ok );
if ( !ok )
return false;

QgsLayoutMeasurement m = guide->position();
m.setUnits( static_cast< QgsUnitTypes::LayoutUnit >( units ) );
guide->setPosition( m );
guide->update();
return true;
}
}

return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/layout/qgslayoutguidecollection.h
Expand Up @@ -161,7 +161,7 @@ class CORE_EXPORT QgsLayoutGuide : public QObject
* \brief Stores and manages the snap guides used by a layout.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractListModel
class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
{

Q_OBJECT
Expand All @@ -185,6 +185,7 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractListModel
~QgsLayoutGuideCollection();

int rowCount( const QModelIndex & ) const override;
int columnCount( const QModelIndex & ) const override;
QVariant data( const QModelIndex &index, int role ) const override;
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;
Expand Down

0 comments on commit 048a5b7

Please sign in to comment.