Nicer table view for guide editing

This commit is contained in:
Nyall Dawson 2017-07-25 23:01:10 +10:00
parent 886a1208ef
commit 048a5b79b6
3 changed files with 58 additions and 17 deletions

View File

@ -127,7 +127,7 @@ class QgsLayoutGuide : QObject
};
class QgsLayoutGuideCollection : QAbstractListModel
class QgsLayoutGuideCollection : QAbstractTableModel
{
%Docstring
Stores and manages the snap guides used by a layout.
@ -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 );

View File

@ -149,7 +149,7 @@ QgsLayoutGuide::Orientation QgsLayoutGuide::orientation() const
//
QgsLayoutGuideCollection::QgsLayoutGuideCollection( QgsLayout *layout )
: QAbstractListModel( layout )
: QAbstractTableModel( layout )
, mLayout( layout )
{
@ -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() )
@ -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();
@ -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;
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;
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;
}

View File

@ -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
@ -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;