Skip to content
Permalink
Browse files

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
  • Loading branch information
wonder-sk committed May 16, 2011
1 parent 247fa7b commit e0eb6d829a7ebc8b8b1f64b719519dd1abe2a30e
@@ -138,15 +138,61 @@ void QgsBrowser::itemClicked(const QModelIndex& index)
// this should probably go to the model and only emit signal when a layer is clicked


mActionSetProjection->setEnabled ( ptr->capabilities() & QgsDataItem::SetCrs );

mParamWidget = ptr->paramWidget();
if ( mParamWidget ) {
paramLayout->addWidget ( mParamWidget );
mParamWidget->show();
paramEnable = true;
}

if (ptr->mType == QgsDataItem::Layer)
{
QgsLayerItem* item = static_cast<QgsLayerItem*>(ptr);
bool res = layerClicked(item);

if (res)
{
metaEnable = true;
previewEnable = true;
if ( mLayer->type() == QgsMapLayer::VectorLayer ) {
attributesEnable = true;
}
}
}
else
{
mActionSetProjection->setEnabled ( false );
}

// force update of the current tab
updateCurrentTab();

int selected = -1;
if ( mLastTab.contains( typeid(*ptr).name() ) )
{
selected = mLastTab[ typeid(*ptr).name()];
}

// Enabling tabs call tabChanged !
tabWidget->setTabEnabled ( tabWidget->indexOf( paramTab ), paramEnable);
tabWidget->setTabEnabled ( tabWidget->indexOf( metaTab ), metaEnable );
tabWidget->setTabEnabled ( tabWidget->indexOf( previewTab ), previewEnable );
tabWidget->setTabEnabled ( tabWidget->indexOf( attributesTab ), attributesEnable );

// select tab according last selection for this data item
if ( selected >= 0 )
{
qDebug("set tab %s %d", typeid(*ptr).name(), selected );
tabWidget->setCurrentIndex ( selected );
}

qDebug("clicked: %d %d %s", index.row(), index.column(), ptr->mName.toAscii().data());
}

bool QgsBrowser::layerClicked(QgsLayerItem* ptr)
{
mActionSetProjection->setEnabled ( ptr->capabilities() & QgsLayerItem::SetCrs );

QgsMapLayer::LayerType type;
QString providerKey;
QString uri;
@@ -185,47 +231,20 @@ void QgsBrowser::itemClicked(const QModelIndex& index)
}
}

if ( mLayer && mLayer->isValid() )
{
QgsDebugMsg ( "Layer created");

QgsMapLayerRegistry::instance()->addMapLayer(mLayer);

metaEnable = true;
previewEnable = true;
if ( mLayer->type() == QgsMapLayer::VectorLayer ) {
attributesEnable = true;
}
// force update of the current tab
updateCurrentTab();
}
else
if ( !mLayer || !mLayer->isValid() )
{
qDebug("No layer" );
return false;
}

int selected = -1;
if ( mLastTab.contains( typeid(*ptr).name() ) )
{
selected = mLastTab[ typeid(*ptr).name()];
}
QgsDebugMsg ( "Layer created");

// Enabling tabs call tabChanged !
tabWidget->setTabEnabled ( tabWidget->indexOf( paramTab ), paramEnable);
tabWidget->setTabEnabled ( tabWidget->indexOf( metaTab ), metaEnable );
tabWidget->setTabEnabled ( tabWidget->indexOf( previewTab ), previewEnable );
tabWidget->setTabEnabled ( tabWidget->indexOf( attributesTab ), attributesEnable );

// select tab according last selection for this data item
if ( selected >= 0 )
{
qDebug("set tab %s %d", typeid(*ptr).name(), selected );
tabWidget->setCurrentIndex ( selected );
}

qDebug("clicked: %d %d %s", index.row(), index.column(), ptr->mName.toAscii().data());
QgsMapLayerRegistry::instance()->addMapLayer(mLayer);

return true;
}


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

QgsDataItem* ptr = (QgsDataItem*) mIndex.internalPointer();
if ( ! ptr->setCrs ( srs ) )
if ( ptr->mType == QgsDataItem::Layer )
{
QMessageBox::critical( this, tr( "CRS" ), tr( "Cannot set layer CRS" ));
QgsLayerItem* layerItem = static_cast<QgsLayerItem*>(ptr);
if ( ! layerItem->setCrs ( srs ) )
{
QMessageBox::critical( this, tr( "CRS" ), tr( "Cannot set layer CRS" ));
}
}
QgsDebugMsg( srs.authid() + " - " + srs.description() );
}
@@ -24,6 +24,7 @@
#include "ui_qgsbrowserbase.h"

class QgsBrowserModel;
class QgsLayerItem;
class QgsMapLayer;

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

bool layerClicked(QgsLayerItem* ptr);

enum Tab
{
Metadata,
@@ -38,15 +38,19 @@
#include "qgslogger.h"
#include "qgsproviderregistry.h"

// shared icons
QIcon QgsLayerItem::sIconPoint, QgsLayerItem::sIconLine, QgsLayerItem::sIconPolygon, QgsLayerItem::sIconTable, QgsLayerItem::sIconDefault;
QIcon QgsDataCollectionItem::sDirIcon;


QgsDataItem::QgsDataItem(QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path)
: QObject(parent), mType(type), mParent(parent), mPopulated(false), mName(name), mPath(path)
{
}

QIcon QgsDataItem::icon()
{
if ( !mIcon.isNull() ) return mIcon;
return mDefaultIcon;
return mIcon;
}

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

// ---------------------------------------------------------------------

QgsLayerItem::QgsLayerItem(QgsDataItem* parent, QString name, QString path)
: QgsDataItem(Layer, parent, name, path)
{
}

QgsLayerItem::QgsLayerItem(QgsDataItem* parent, QgsDataItem::Type type, QString name, QString path, QString uri)
: QgsDataItem(type, parent, name, path), mUri(uri)
{

}
QIcon QgsLayerItem::icon()
QgsLayerItem::QgsLayerItem(QgsDataItem* parent, QString name, QString path, QString uri, LayerType layerType)
: QgsDataItem(Layer, parent, name, path), mUri(uri), mLayerType(layerType)
{
if ( !mIcon.isNull() ) return mIcon;

QString name;
switch ( mType )
if (sIconPoint.isNull())
{
case QgsDataItem::Point:
name = "/mIconPointLayer.png";
break;
case QgsDataItem::Line:
name = "/mIconLineLayer.png";
break;
case QgsDataItem::Polygon:
name = "/mIconPolygonLayer.png";
break;
case QgsDataItem::TableLayer:
name = "/mIconTableLayer.png";
break;
default:
name = "/mIconLayer.png";
break;
// initialize shared icons
sIconPoint = QIcon( getThemePixmap( "/mIconPointLayer.png" ) );
sIconLine = QIcon( getThemePixmap( "/mIconLineLayer.png" ) );
sIconPolygon = QIcon( getThemePixmap( "/mIconPolygonLayer.png" ) );
sIconTable = QIcon( getThemePixmap( "/mIconTableLayer.png" ) );
sIconDefault = QIcon( getThemePixmap( "/mIconLayer.png" ) );
}
//QgsDebugMsg( QString ( "mType = %1 name = %2").arg ( mType).arg (name ) );
if ( !name.isEmpty() )

switch (layerType)
{
QPixmap pixmap = getThemePixmap ( name );
//QgsDebugMsg( QString ( "pixmap.isNull = %1").arg(pixmap.isNull() ) );
return QIcon ( pixmap );
case Point: mIcon = sIconPoint; break;
case Line: mIcon = sIconLine; break;
case Polygon: mIcon = sIconPolygon; break;
case TableLayer: mIcon = sIconTable; break;
default: mIcon = sIconDefault; break;
}
return mDefaultIcon;
}

bool QgsLayerItem::equal(const QgsDataItem *other)
@@ -257,10 +241,15 @@ bool QgsLayerItem::equal(const QgsDataItem *other)
QgsDataCollectionItem::QgsDataCollectionItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path)
: QgsDataItem( type, parent, name, path)
{
QStyle *style = QApplication::style();
mDefaultIcon = QIcon( style->standardPixmap( QStyle::SP_DirClosedIcon ) );
mDefaultIcon.addPixmap( style->standardPixmap( QStyle::SP_DirOpenIcon ),
QIcon::Normal, QIcon::On );

if (sDirIcon.isNull())
{
// initialize shared icons
QStyle *style = QApplication::style();
sDirIcon = QIcon( style->standardPixmap( QStyle::SP_DirClosedIcon ) );
sDirIcon.addPixmap( style->standardPixmap( QStyle::SP_DirOpenIcon ),
QIcon::Normal, QIcon::On );
}
}
QgsDataCollectionItem::~QgsDataCollectionItem()
{
@@ -279,6 +268,8 @@ typedef QgsDataItem * dataItem_t(QString);
QgsDirectoryItem::QgsDirectoryItem(QgsDataItem* parent, QString name, QString path)
: QgsDataCollectionItem(Directory, parent, name, path)
{
mIcon = sDirIcon;

if ( mLibraries.size() == 0 ) {
QStringList keys = QgsProviderRegistry::instance()->providerList();
QStringList::const_iterator i;

0 comments on commit e0eb6d8

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