Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Followup 9cad526, allow item rename through browser model
  • Loading branch information
nyalldawson committed Oct 11, 2018
1 parent 66f5f54 commit 5beb4e2
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 24 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgsbrowsermodel.sip.in
Expand Up @@ -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;
Expand Down
22 changes: 21 additions & 1 deletion python/core/auto_generated/qgsdataitem.sip.in
Expand Up @@ -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;

Expand All @@ -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


Expand Down
3 changes: 0 additions & 3 deletions python/gui/auto_generated/qgsbrowserdockwidget.sip.in
Expand Up @@ -136,9 +136,6 @@ Connections changed in the browser
Show event override
%End

virtual void keyPressEvent( QKeyEvent *event );


};


Expand Down
31 changes: 30 additions & 1 deletion src/core/qgsbrowsermodel.cpp
Expand Up @@ -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;
}

Expand All @@ -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();
}
Expand Down Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/core/qgsdataitem.cpp
Expand Up @@ -551,6 +551,11 @@ bool QgsDataItem::handleDoubleClick()
return false;
}

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

QgsDataItem::State QgsDataItem::state() const
{
return mState;
Expand Down Expand Up @@ -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;
}


Expand Down
29 changes: 23 additions & 6 deletions src/core/qgsdataitem.h
Expand Up @@ -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 )

Expand All @@ -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; }

Expand Down Expand Up @@ -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:

Expand Down
9 changes: 0 additions & 9 deletions src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -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 ) );
Expand Down
2 changes: 0 additions & 2 deletions src/gui/qgsbrowserdockwidget.h
Expand Up @@ -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();
Expand Down

0 comments on commit 5beb4e2

Please sign in to comment.