Skip to content

Commit 5beb4e2

Browse files
committed
Followup 9cad526, allow item rename through browser model
1 parent 66f5f54 commit 5beb4e2

File tree

9 files changed

+85
-24
lines changed

9 files changed

+85
-24
lines changed

python/core/auto_generated/qgsbrowsermodel.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Constructor for QgsBrowserModel, with the specified ``parent`` object.
5353

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

56+
virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
57+
5658
virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
5759

5860
virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;

python/core/auto_generated/qgsdataitem.sip.in

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ Items that return valid URI will be returned in mime data when dragging a select
199199
SetCrs,
200200
Fertile,
201201
Fast,
202-
Collapse
202+
Collapse,
203+
Rename,
203204
};
204205
typedef QFlags<QgsDataItem::Capability> Capabilities;
205206

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

214+
virtual bool rename( const QString &name );
215+
%Docstring
216+
Sets a new ``name`` for the item, and returns true if the item was successfully renamed.
217+
218+
Items which implement this method should return the QgsDataItem.Rename capability.
219+
220+
The default implementation does nothing.
221+
222+
.. versionadded:: 3.4
223+
%End
224+
225+
213226
virtual Capabilities capabilities2() const;
227+
%Docstring
228+
Returns the capabilities for the data item.
229+
230+
.. seealso:: :py:func:`setCapabilities`
231+
%End
214232

215233
virtual void setCapabilities( Capabilities capabilities );
216234
%Docstring
217235
Sets the capabilities for the data item.
236+
237+
.. seealso:: :py:func:`capabilities`
218238
%End
219239

220240

python/gui/auto_generated/qgsbrowserdockwidget.sip.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ Connections changed in the browser
136136
Show event override
137137
%End
138138

139-
virtual void keyPressEvent( QKeyEvent *event );
140-
141-
142139
};
143140

144141

src/core/qgsbrowsermodel.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const
200200

201201
if ( ptr->acceptDrop() )
202202
flags |= Qt::ItemIsDropEnabled;
203+
204+
if ( ptr->capabilities2() & QgsDataItem::Rename )
205+
flags |= Qt::ItemIsEditable;
206+
203207
return flags;
204208
}
205209

@@ -213,7 +217,7 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
213217
{
214218
return QVariant();
215219
}
216-
else if ( role == Qt::DisplayRole )
220+
else if ( role == Qt::DisplayRole || role == Qt::EditRole )
217221
{
218222
return item->name();
219223
}
@@ -249,6 +253,31 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
249253
}
250254
}
251255

256+
bool QgsBrowserModel::setData( const QModelIndex &index, const QVariant &value, int role )
257+
{
258+
if ( !index.isValid() )
259+
return false;
260+
261+
262+
QgsDataItem *item = dataItem( index );
263+
if ( !item )
264+
{
265+
return false;
266+
}
267+
268+
if ( !( item->capabilities2() & QgsDataItem::Rename ) )
269+
return false;
270+
271+
switch ( role )
272+
{
273+
case Qt::EditRole:
274+
{
275+
return item->rename( value.toString() );
276+
}
277+
}
278+
return false;
279+
}
280+
252281
QVariant QgsBrowserModel::headerData( int section, Qt::Orientation orientation, int role ) const
253282
{
254283
Q_UNUSED( section );

src/core/qgsbrowsermodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
8989

9090
Qt::ItemFlags flags( const QModelIndex &index ) const override;
9191
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
92+
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
9293
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
9394
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
9495
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;

src/core/qgsdataitem.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ bool QgsDataItem::handleDoubleClick()
551551
return false;
552552
}
553553

554+
bool QgsDataItem::rename( const QString & )
555+
{
556+
return false;
557+
}
558+
554559
QgsDataItem::State QgsDataItem::state() const
555560
{
556561
return mState;
@@ -1559,12 +1564,13 @@ QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name,
15591564
: QgsDirectoryItem( parent, name, dirPath, path )
15601565
, mFavorites( parent )
15611566
{
1562-
1567+
mCapabilities |= Rename;
15631568
}
15641569

1565-
void QgsFavoriteItem::rename( const QString &name )
1570+
bool QgsFavoriteItem::rename( const QString &name )
15661571
{
15671572
mFavorites->renameFavorite( dirPath(), name );
1573+
return true;
15681574
}
15691575

15701576

src/core/qgsdataitem.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ class CORE_EXPORT QgsDataItem : public QObject
210210
SetCrs = 1 << 0, //!< Can set CRS on layer or group of layers
211211
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.
212212
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
213-
Collapse = 1 << 3 //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
213+
Collapse = 1 << 3, //!< The collapse/expand status for this items children should be ignored in order to avoid undesired network connections (wms etc.)
214+
Rename = 1 << 4, //!< Item can be renamed
214215
};
215216
Q_DECLARE_FLAGS( Capabilities, Capability )
216217

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

223-
// ### QGIS 3 - rename to capabilities()
224+
/**
225+
* Sets a new \a name for the item, and returns true if the item was successfully renamed.
226+
*
227+
* Items which implement this method should return the QgsDataItem::Rename capability.
228+
*
229+
* The default implementation does nothing.
230+
*
231+
* \since QGIS 3.4
232+
*/
233+
virtual bool rename( const QString &name );
234+
235+
// ### QGIS 4 - rename to capabilities()
236+
237+
/**
238+
* Returns the capabilities for the data item.
239+
*
240+
* \see setCapabilities()
241+
*/
224242
virtual Capabilities capabilities2() const { return mCapabilities; }
225243

226244
/**
227245
* Sets the capabilities for the data item.
246+
*
247+
* \see capabilities()
228248
*/
229249
virtual void setCapabilities( Capabilities capabilities ) { mCapabilities = capabilities; }
230250

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

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

785-
/**
786-
* Sets a new \a name for the favorite, storing the new name permanently for the favorite.
787-
*/
788-
void rename( const QString &name );
805+
bool rename( const QString &name ) override;
789806

790807
private:
791808

src/gui/qgsbrowserdockwidget.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,6 @@ void QgsBrowserDockWidget::showEvent( QShowEvent *e )
157157
QgsDockWidget::showEvent( e );
158158
}
159159

160-
void QgsBrowserDockWidget::keyPressEvent( QKeyEvent *event )
161-
{
162-
if ( event->key() == Qt::Key_F2 )
163-
{
164-
renameFavorite();
165-
event->accept();
166-
}
167-
}
168-
169160
void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index )
170161
{
171162
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );

src/gui/qgsbrowserdockwidget.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
110110
//! Show event override
111111
void showEvent( QShowEvent *event ) override;
112112

113-
void keyPressEvent( QKeyEvent *event ) override;
114-
115113
private slots:
116114
void itemDoubleClicked( const QModelIndex &index );
117115
void renameFavorite();

0 commit comments

Comments
 (0)