Skip to content

Commit e0eb6d8

Browse files
committed
QgsDataItem refactoring
- hasChildren, rowCount, icon methods made non-virtual - shared default icons - data item's mType should be used for differentiating classes - layer-specific virtual methods moved down to QgsLayerItem
1 parent 247fa7b commit e0eb6d8

9 files changed

+166
-141
lines changed

src/browser/qgsbrowser.cpp

+61-38
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,61 @@ void QgsBrowser::itemClicked(const QModelIndex& index)
138138
// this should probably go to the model and only emit signal when a layer is clicked
139139

140140

141-
mActionSetProjection->setEnabled ( ptr->capabilities() & QgsDataItem::SetCrs );
142-
143141
mParamWidget = ptr->paramWidget();
144142
if ( mParamWidget ) {
145143
paramLayout->addWidget ( mParamWidget );
146144
mParamWidget->show();
147145
paramEnable = true;
148146
}
149147

148+
if (ptr->mType == QgsDataItem::Layer)
149+
{
150+
QgsLayerItem* item = static_cast<QgsLayerItem*>(ptr);
151+
bool res = layerClicked(item);
152+
153+
if (res)
154+
{
155+
metaEnable = true;
156+
previewEnable = true;
157+
if ( mLayer->type() == QgsMapLayer::VectorLayer ) {
158+
attributesEnable = true;
159+
}
160+
}
161+
}
162+
else
163+
{
164+
mActionSetProjection->setEnabled ( false );
165+
}
166+
167+
// force update of the current tab
168+
updateCurrentTab();
169+
170+
int selected = -1;
171+
if ( mLastTab.contains( typeid(*ptr).name() ) )
172+
{
173+
selected = mLastTab[ typeid(*ptr).name()];
174+
}
175+
176+
// Enabling tabs call tabChanged !
177+
tabWidget->setTabEnabled ( tabWidget->indexOf( paramTab ), paramEnable);
178+
tabWidget->setTabEnabled ( tabWidget->indexOf( metaTab ), metaEnable );
179+
tabWidget->setTabEnabled ( tabWidget->indexOf( previewTab ), previewEnable );
180+
tabWidget->setTabEnabled ( tabWidget->indexOf( attributesTab ), attributesEnable );
181+
182+
// select tab according last selection for this data item
183+
if ( selected >= 0 )
184+
{
185+
qDebug("set tab %s %d", typeid(*ptr).name(), selected );
186+
tabWidget->setCurrentIndex ( selected );
187+
}
188+
189+
qDebug("clicked: %d %d %s", index.row(), index.column(), ptr->mName.toAscii().data());
190+
}
191+
192+
bool QgsBrowser::layerClicked(QgsLayerItem* ptr)
193+
{
194+
mActionSetProjection->setEnabled ( ptr->capabilities() & QgsLayerItem::SetCrs );
195+
150196
QgsMapLayer::LayerType type;
151197
QString providerKey;
152198
QString uri;
@@ -185,47 +231,20 @@ void QgsBrowser::itemClicked(const QModelIndex& index)
185231
}
186232
}
187233

188-
if ( mLayer && mLayer->isValid() )
189-
{
190-
QgsDebugMsg ( "Layer created");
191-
192-
QgsMapLayerRegistry::instance()->addMapLayer(mLayer);
193-
194-
metaEnable = true;
195-
previewEnable = true;
196-
if ( mLayer->type() == QgsMapLayer::VectorLayer ) {
197-
attributesEnable = true;
198-
}
199-
// force update of the current tab
200-
updateCurrentTab();
201-
}
202-
else
234+
if ( !mLayer || !mLayer->isValid() )
203235
{
204236
qDebug("No layer" );
237+
return false;
205238
}
206239

207-
int selected = -1;
208-
if ( mLastTab.contains( typeid(*ptr).name() ) )
209-
{
210-
selected = mLastTab[ typeid(*ptr).name()];
211-
}
240+
QgsDebugMsg ( "Layer created");
212241

213-
// Enabling tabs call tabChanged !
214-
tabWidget->setTabEnabled ( tabWidget->indexOf( paramTab ), paramEnable);
215-
tabWidget->setTabEnabled ( tabWidget->indexOf( metaTab ), metaEnable );
216-
tabWidget->setTabEnabled ( tabWidget->indexOf( previewTab ), previewEnable );
217-
tabWidget->setTabEnabled ( tabWidget->indexOf( attributesTab ), attributesEnable );
218-
219-
// select tab according last selection for this data item
220-
if ( selected >= 0 )
221-
{
222-
qDebug("set tab %s %d", typeid(*ptr).name(), selected );
223-
tabWidget->setCurrentIndex ( selected );
224-
}
225-
226-
qDebug("clicked: %d %d %s", index.row(), index.column(), ptr->mName.toAscii().data());
242+
QgsMapLayerRegistry::instance()->addMapLayer(mLayer);
243+
244+
return true;
227245
}
228246

247+
229248
void QgsBrowser::itemDoubleClicked(const QModelIndex& index)
230249
{
231250
QgsDataItem* ptr = (QgsDataItem*) index.internalPointer();
@@ -301,9 +320,13 @@ void QgsBrowser::on_mActionSetProjection_triggered()
301320
// selectedIndexes() is protected
302321

303322
QgsDataItem* ptr = (QgsDataItem*) mIndex.internalPointer();
304-
if ( ! ptr->setCrs ( srs ) )
323+
if ( ptr->mType == QgsDataItem::Layer )
305324
{
306-
QMessageBox::critical( this, tr( "CRS" ), tr( "Cannot set layer CRS" ));
325+
QgsLayerItem* layerItem = static_cast<QgsLayerItem*>(ptr);
326+
if ( ! layerItem->setCrs ( srs ) )
327+
{
328+
QMessageBox::critical( this, tr( "CRS" ), tr( "Cannot set layer CRS" ));
329+
}
307330
}
308331
QgsDebugMsg( srs.authid() + " - " + srs.description() );
309332
}

src/browser/qgsbrowser.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ui_qgsbrowserbase.h"
2525

2626
class QgsBrowserModel;
27+
class QgsLayerItem;
2728
class QgsMapLayer;
2829

2930
class QgsBrowser : public QMainWindow, private Ui::QgsBrowserBase
@@ -60,6 +61,8 @@ class QgsBrowser : public QMainWindow, private Ui::QgsBrowserBase
6061
protected:
6162
void keyPressEvent( QKeyEvent * e );
6263

64+
bool layerClicked(QgsLayerItem* ptr);
65+
6366
enum Tab
6467
{
6568
Metadata,

src/core/qgsdataitem.cpp

+33-42
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@
3838
#include "qgslogger.h"
3939
#include "qgsproviderregistry.h"
4040

41+
// shared icons
42+
QIcon QgsLayerItem::sIconPoint, QgsLayerItem::sIconLine, QgsLayerItem::sIconPolygon, QgsLayerItem::sIconTable, QgsLayerItem::sIconDefault;
43+
QIcon QgsDataCollectionItem::sDirIcon;
44+
45+
4146
QgsDataItem::QgsDataItem(QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path)
4247
: QObject(parent), mType(type), mParent(parent), mPopulated(false), mName(name), mPath(path)
4348
{
4449
}
4550

4651
QIcon QgsDataItem::icon()
4752
{
48-
if ( !mIcon.isNull() ) return mIcon;
49-
return mDefaultIcon;
53+
return mIcon;
5054
}
5155

5256
// TODO: This is copy from QgisApp, bad
@@ -197,47 +201,27 @@ void QgsDataItem::refresh()
197201

198202
// ---------------------------------------------------------------------
199203

200-
QgsLayerItem::QgsLayerItem(QgsDataItem* parent, QString name, QString path)
201-
: QgsDataItem(Layer, parent, name, path)
202-
{
203-
}
204-
205-
QgsLayerItem::QgsLayerItem(QgsDataItem* parent, QgsDataItem::Type type, QString name, QString path, QString uri)
206-
: QgsDataItem(type, parent, name, path), mUri(uri)
207-
{
208-
209-
}
210-
QIcon QgsLayerItem::icon()
204+
QgsLayerItem::QgsLayerItem(QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType)
205+
: QgsDataItem(Layer, parent, name, path), mUri(uri), mLayerType(layerType)
211206
{
212-
if ( !mIcon.isNull() ) return mIcon;
213-
214-
QString name;
215-
switch ( mType )
207+
if (sIconPoint.isNull())
216208
{
217-
case QgsDataItem::Point:
218-
name = "/mIconPointLayer.png";
219-
break;
220-
case QgsDataItem::Line:
221-
name = "/mIconLineLayer.png";
222-
break;
223-
case QgsDataItem::Polygon:
224-
name = "/mIconPolygonLayer.png";
225-
break;
226-
case QgsDataItem::TableLayer:
227-
name = "/mIconTableLayer.png";
228-
break;
229-
default:
230-
name = "/mIconLayer.png";
231-
break;
209+
// initialize shared icons
210+
sIconPoint = QIcon( getThemePixmap( "/mIconPointLayer.png" ) );
211+
sIconLine = QIcon( getThemePixmap( "/mIconLineLayer.png" ) );
212+
sIconPolygon = QIcon( getThemePixmap( "/mIconPolygonLayer.png" ) );
213+
sIconTable = QIcon( getThemePixmap( "/mIconTableLayer.png" ) );
214+
sIconDefault = QIcon( getThemePixmap( "/mIconLayer.png" ) );
232215
}
233-
//QgsDebugMsg( QString ( "mType = %1 name = %2").arg ( mType).arg (name ) );
234-
if ( !name.isEmpty() )
216+
217+
switch (layerType)
235218
{
236-
QPixmap pixmap = getThemePixmap ( name );
237-
//QgsDebugMsg( QString ( "pixmap.isNull = %1").arg(pixmap.isNull() ) );
238-
return QIcon ( pixmap );
219+
case Point: mIcon = sIconPoint; break;
220+
case Line: mIcon = sIconLine; break;
221+
case Polygon: mIcon = sIconPolygon; break;
222+
case TableLayer: mIcon = sIconTable; break;
223+
default: mIcon = sIconDefault; break;
239224
}
240-
return mDefaultIcon;
241225
}
242226

243227
bool QgsLayerItem::equal(const QgsDataItem *other)
@@ -257,10 +241,15 @@ bool QgsLayerItem::equal(const QgsDataItem *other)
257241
QgsDataCollectionItem::QgsDataCollectionItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path)
258242
: QgsDataItem( type, parent, name, path)
259243
{
260-
QStyle *style = QApplication::style();
261-
mDefaultIcon = QIcon( style->standardPixmap( QStyle::SP_DirClosedIcon ) );
262-
mDefaultIcon.addPixmap( style->standardPixmap( QStyle::SP_DirOpenIcon ),
263-
QIcon::Normal, QIcon::On );
244+
245+
if (sDirIcon.isNull())
246+
{
247+
// initialize shared icons
248+
QStyle *style = QApplication::style();
249+
sDirIcon = QIcon( style->standardPixmap( QStyle::SP_DirClosedIcon ) );
250+
sDirIcon.addPixmap( style->standardPixmap( QStyle::SP_DirOpenIcon ),
251+
QIcon::Normal, QIcon::On );
252+
}
264253
}
265254
QgsDataCollectionItem::~QgsDataCollectionItem()
266255
{
@@ -279,6 +268,8 @@ typedef QgsDataItem * dataItem_t(QString);
279268
QgsDirectoryItem::QgsDirectoryItem(QgsDataItem* parent, QString name, QString path)
280269
: QgsDataCollectionItem(Directory, parent, name, path)
281270
{
271+
mIcon = sDirIcon;
272+
282273
if ( mLibraries.size() == 0 ) {
283274
QStringList keys = QgsProviderRegistry::instance()->providerList();
284275
QStringList::const_iterator i;

0 commit comments

Comments
 (0)