Skip to content
Permalink
Browse files

Followup 9cad526, allow item rename through browser model

  • Loading branch information
nyalldawson committed Oct 11, 2018
1 parent 66f5f54 commit 5beb4e2b8d45a75f9ee23d3f4ed8ed0489517379
@@ -53,6 +53,8 @@ Constructor for QgsBrowserModel, with the specified ``parent`` object.

virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;

virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );

virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;

virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
@@ -199,7 +199,8 @@ Items that return valid URI will be returned in mime data when dragging a select
SetCrs,
Fertile,
Fast,
Collapse
Collapse,
Rename,
};
typedef QFlags<QgsDataItem::Capability> Capabilities;

@@ -210,11 +211,30 @@ Writes the selected crs into data source. The original data source will be modif
method.
%End

virtual bool rename( const QString &name );
%Docstring
Sets a new ``name`` for the item, and returns true if the item was successfully renamed.

Items which implement this method should return the QgsDataItem.Rename capability.

The default implementation does nothing.

.. versionadded:: 3.4
%End


virtual Capabilities capabilities2() const;
%Docstring
Returns the capabilities for the data item.

.. seealso:: :py:func:`setCapabilities`
%End

virtual void setCapabilities( Capabilities capabilities );
%Docstring
Sets the capabilities for the data item.

.. seealso:: :py:func:`capabilities`
%End


@@ -136,9 +136,6 @@ Connections changed in the browser
Show event override
%End

virtual void keyPressEvent( QKeyEvent *event );


};


@@ -200,6 +200,10 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const

if ( ptr->acceptDrop() )
flags |= Qt::ItemIsDropEnabled;

if ( ptr->capabilities2() & QgsDataItem::Rename )
flags |= Qt::ItemIsEditable;

return flags;
}

@@ -213,7 +217,7 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
{
return QVariant();
}
else if ( role == Qt::DisplayRole )
else if ( role == Qt::DisplayRole || role == Qt::EditRole )
{
return item->name();
}
@@ -249,6 +253,31 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
}
}

bool QgsBrowserModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if ( !index.isValid() )
return false;


QgsDataItem *item = dataItem( index );
if ( !item )
{
return false;
}

if ( !( item->capabilities2() & QgsDataItem::Rename ) )
return false;

switch ( role )
{
case Qt::EditRole:
{
return item->rename( value.toString() );
}
}
return false;
}

QVariant QgsBrowserModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
Q_UNUSED( section );
@@ -89,6 +89,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel

Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
@@ -551,6 +551,11 @@ bool QgsDataItem::handleDoubleClick()
return false;
}

bool QgsDataItem::rename( const QString & )
{
return false;
}

QgsDataItem::State QgsDataItem::state() const
{
return mState;
@@ -1559,12 +1564,13 @@ QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name,
: QgsDirectoryItem( parent, name, dirPath, path )
, mFavorites( parent )
{

mCapabilities |= Rename;
}

void QgsFavoriteItem::rename( const QString &name )
bool QgsFavoriteItem::rename( const QString &name )
{
mFavorites->renameFavorite( dirPath(), name );
return true;
}


@@ -210,7 +210,8 @@ class CORE_EXPORT QgsDataItem : public QObject
SetCrs = 1 << 0, //!< Can set CRS on layer or group of layers
Fertile = 1 << 1, //!< Can create children. Even items without this capability may have children, but cannot create them, it means that children are created by item ancestors.
Fast = 1 << 2, //!< CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,wfs,wcs,postgres...) are considered fast because they are reading data only from QgsSettings
Collapse = 1 << 3 //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
Collapse = 1 << 3, //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
Rename = 1 << 4, //!< Item can be renamed
};
Q_DECLARE_FLAGS( Capabilities, Capability )

@@ -220,11 +221,30 @@ class CORE_EXPORT QgsDataItem : public QObject
*/
virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) { Q_UNUSED( crs ); return false; }

// ### QGIS 3 - rename to capabilities()
/**
* Sets a new \a name for the item, and returns true if the item was successfully renamed.
*
* Items which implement this method should return the QgsDataItem::Rename capability.
*
* The default implementation does nothing.
*
* \since QGIS 3.4
*/
virtual bool rename( const QString &name );

// ### QGIS 4 - rename to capabilities()

/**
* Returns the capabilities for the data item.
*
* \see setCapabilities()
*/
virtual Capabilities capabilities2() const { return mCapabilities; }

/**
* Sets the capabilities for the data item.
*
* \see capabilities()
*/
virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }

@@ -782,10 +802,7 @@ class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem

QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );

/**
* Sets a new \a name for the favorite, storing the new name permanently for the favorite.
*/
void rename( const QString &name );
bool rename( const QString &name ) override;

private:

@@ -157,15 +157,6 @@ void QgsBrowserDockWidget::showEvent( QShowEvent *e )
QgsDockWidget::showEvent( e );
}

void QgsBrowserDockWidget::keyPressEvent( QKeyEvent *event )
{
if ( event->key() == Qt::Key_F2 )
{
renameFavorite();
event->accept();
}
}

void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index )
{
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );
@@ -110,8 +110,6 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
//! Show event override
void showEvent( QShowEvent *event ) override;

void keyPressEvent( QKeyEvent *event ) override;

private slots:
void itemDoubleClicked( const QModelIndex &index );
void renameFavorite();

0 comments on commit 5beb4e2

Please sign in to comment.
You can’t perform that action at this time.