Skip to content

Commit

Permalink
Improvements to embed dialogs and stability
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 3, 2011
1 parent 819084d commit 6f37598
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 65 deletions.
126 changes: 100 additions & 26 deletions src/app/legend/qgslegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )
// record which items were selected and hide them
foreach( QTreeWidgetItem * item, selectedItems() )
{
//prevent to drag out content under groups that are embedded from other
//project files.
if( parentGroupEmbedded( item ) )
{
continue;
}
item->setHidden( true );
mItemsBeingMoved << item;
}
Expand Down Expand Up @@ -324,6 +330,7 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )
if ( mItemsBeingMoved.isEmpty() )
{
QgsDebugMsg( "nothing to move" );
setCursor( QCursor( Qt::ArrowCursor ) );
return;
}

Expand Down Expand Up @@ -364,36 +371,47 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )

mDropTarget = layer;

if ( e->y() < ( y0 + y1 ) / 2 )
//prevent inserting content into embedded groups
if( !parentGroupEmbedded( litem ) )
{
QgsDebugMsg( "insert before layer" );
mDropAction = BEFORE;
line_y = y0;
}
else
{
QgsDebugMsg( "insert after layer" );
mDropAction = AFTER;
line_y = y1;
if ( e->y() < ( y0 + y1 ) / 2 )
{
QgsDebugMsg( "insert before layer" );
mDropAction = BEFORE;
line_y = y0;
}
else
{
QgsDebugMsg( "insert after layer" );
mDropAction = AFTER;
line_y = y1;
}
}
}
else if ( group )
{
else if ( group )
{
if ( yCoordAboveCenter( litem, e->y() ) ) //over center of item
{
QgsDebugMsg( "insert before group" );

line_y = visualItemRect( item ).top() + 1;
mDropTarget = item;
mDropAction = BEFORE;
//prevent inserting content into embedded groups
if( !parentGroupEmbedded( item ) )
{
line_y = visualItemRect( item ).top() + 1;
mDropTarget = item;
mDropAction = BEFORE;
}
}
else // below center of item
{
QgsDebugMsg( "insert into group" );
if( !groupEmbedded( item ) )
{
QgsDebugMsg( "insert into group" );

line_y = visualItemRect( item ).bottom() - 2;
mDropTarget = item;
mDropAction = INSERT;
line_y = visualItemRect( item ).bottom() - 2;
mDropTarget = item;
mDropAction = INSERT;
}
}
}
else
Expand Down Expand Up @@ -566,7 +584,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
{
qobject_cast<QgsLegendLayer*>( li )->addToPopupMenu( theMenu );

if ( li->parent() )
if ( li->parent() && !parentGroupEmbedded( li ) )
{
theMenu.addAction( tr( "&Make to toplevel item" ), this, SLOT( makeToTopLevelItem() ) );
}
Expand All @@ -583,7 +601,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
tr( "&Set group CRS" ), this, SLOT( legendGroupSetCRS() ) );
}

if ( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP )
if ( ( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP ) && !groupEmbedded( li ) && !parentGroupEmbedded( li ) )
{
theMenu.addAction( tr( "Re&name" ), this, SLOT( openEditor() ) );
}
Expand Down Expand Up @@ -644,6 +662,13 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
QDomElement legendElem = legendGroupList.at(i).toElement();
if( legendElem.attribute("name") == groupName )
{
//embedded groups cannot be embedded again
if( legendElem.attribute("embedded") == "1" )
{
mEmbeddedGroups.remove( groupName );
return 0;
}

QgsLegendGroup* group = 0;
if( parent )
{
Expand Down Expand Up @@ -678,6 +703,7 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
addEmbeddedGroup( childElem.attribute("name"), projectFilePath, group );
}
}
checkLayerOrderUpdate();
return group;
}
}
Expand Down Expand Up @@ -1092,6 +1118,12 @@ bool QgsLegend::writeXML( QList<QTreeWidgetItem *> items, QDomNode &node, QDomDo
legendlayerfilenode.setAttribute( "layerid", layer->id() );
layerfilegroupnode.appendChild( legendlayerfilenode );

//embedded layer?
if( !QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
{
legendlayerfilenode.setAttribute( "embedded", "1" );
}

// visible flag
legendlayerfilenode.setAttribute( "visible", ll->isVisible() );

Expand Down Expand Up @@ -1814,7 +1846,10 @@ void QgsLegend::openEditor()
QTreeWidgetItem* theItem = currentItem();
if ( theItem )
{
editItem( theItem, 0 );
if( !groupEmbedded( theItem ) && !parentGroupEmbedded( theItem ) )
{
editItem( theItem, 0 );
}
}
}

Expand All @@ -1823,10 +1858,13 @@ void QgsLegend::makeToTopLevelItem()
QgsLegendItem* theItem = dynamic_cast<QgsLegendItem *>( currentItem() );
if ( theItem )
{
theItem->storeAppearanceSettings();
removeItem( theItem );
addTopLevelItem( theItem );
theItem->restoreAppearanceSettings();
if( !parentGroupEmbedded( theItem ) )
{
theItem->storeAppearanceSettings();
removeItem( theItem );
addTopLevelItem( theItem );
theItem->restoreAppearanceSettings();
}
}
}

Expand Down Expand Up @@ -2130,3 +2168,39 @@ void QgsLegend::setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs
if ( renderFlagState )
mMapCanvas->setRenderFlag( true );
}

bool QgsLegend::parentGroupEmbedded( QTreeWidgetItem* item ) const
{
if( !item )
{
return false;
}

QgsLegendItem* lItem = dynamic_cast<QgsLegendItem*>(item);
if( lItem && lItem->parent() )
{
QgsLegendGroup* parentGroup = dynamic_cast<QgsLegendGroup*>( lItem->parent() );
if( parentGroup && parentGroup->type() == QgsLegendItem::LEGEND_GROUP
&& mEmbeddedGroups.contains( parentGroup->text( 0 ) ) )
{
return true;
}
}
return false;
}

bool QgsLegend::groupEmbedded( QTreeWidgetItem* item ) const
{
if( !item )
{
return false;
}

QgsLegendGroup* gItem = dynamic_cast<QgsLegendGroup*>(item);
if( !gItem )
{
return false;
}

return mEmbeddedGroups.contains( gItem->text( 0 ) );
}
6 changes: 6 additions & 0 deletions src/app/legend/qgslegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,12 @@ class QgsLegend : public QTreeWidget
*/
int getItemPos( QTreeWidgetItem* item );

/**Returns true if the item is a group embedde from another project*/
bool groupEmbedded( QTreeWidgetItem* item ) const;

/**Returns true if the parent group is embedded from another project*/
bool parentGroupEmbedded( QTreeWidgetItem* item ) const;

/**Pointer to the main canvas. Used for requiring repaints in case of legend changes*/
QgsMapCanvas* mMapCanvas;

Expand Down
38 changes: 34 additions & 4 deletions src/app/qgsembedlayerdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "qgsembedlayerdialog.h"
#include "qgisapp.h"
#include <QDomDocument>
#include <QFileDialog>
#include <QFileInfo>
#include <QSettings>

QgsEmbedLayerDialog::QgsEmbedLayerDialog( QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f )
{
setupUi( this );
QObject::connect( mButtonBox, SIGNAL(rejected() ), this, SLOT(reject() ) );
}

QgsEmbedLayerDialog::~QgsEmbedLayerDialog()
Expand All @@ -21,7 +25,7 @@ QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedGroups() const
{
if( (*itemIt)->data(0, Qt::UserRole).toString() == "group" )
{
result.push_back( qMakePair( (*itemIt)->text( 0 ), mProjectFileLineEdit->text() ) );
result.push_back( qMakePair( (*itemIt)->text( 0 ), mProjectPath ) );
}
}

Expand All @@ -38,15 +42,16 @@ QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedLayers() const
{
if( (*itemIt)->data(0, Qt::UserRole).toString() == "layer" )
{
result.push_back( qMakePair( (*itemIt)->data(0, Qt::UserRole + 1).toString(), mProjectFileLineEdit->text() ) );
result.push_back( qMakePair( (*itemIt)->data(0, Qt::UserRole + 1).toString(), mProjectPath ) );
}
}
return result;
}

void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
{
QString projectFile = QFileDialog::getOpenFileName( 0, tr("Select project file"), "",tr("QGIS project files (*.qgs)") );
QSettings s;
QString projectFile = QFileDialog::getOpenFileName( 0, tr("Select project file"), s.value("/qgis/last_embedded_project_path").toString() ,tr("QGIS project files (*.qgs)") );
if( !projectFile.isEmpty() )
{
mProjectFileLineEdit->setText( projectFile );
Expand All @@ -61,13 +66,14 @@ void QgsEmbedLayerDialog::on_mProjectFileLineEdit_editingFinished()

void QgsEmbedLayerDialog::changeProjectFile()
{
mTreeWidget->clear();
QFile projectFile( mProjectFileLineEdit->text() );
if( !projectFile.exists() )
{
return;
}

mTreeWidget->clear();

//parse project file and fill tree
if( !projectFile.open( QIODevice::ReadOnly ) )
{
Expand Down Expand Up @@ -101,13 +107,20 @@ void QgsEmbedLayerDialog::changeProjectFile()
addLegendGroupToTreeWidget( currentChildElem );
}
}

mProjectPath = mProjectFileLineEdit->text();
}

void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupElem, QTreeWidgetItem* parent )
{
QDomNodeList groupChildren = groupElem.childNodes();
QDomElement currentChildElem;

if(groupElem.attribute("embedded") == "1" )
{
return;
}

QTreeWidgetItem* groupItem = 0;
if( !parent )
{
Expand All @@ -117,6 +130,7 @@ void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupEl
{
groupItem = new QTreeWidgetItem( parent );
}
groupItem->setIcon( 0, QgisApp::getThemeIcon("mActionFolder.png") );
groupItem->setText( 0, groupElem.attribute("name") );
groupItem->setData( 0, Qt::UserRole, "group" );

Expand All @@ -136,6 +150,11 @@ void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupEl

void QgsEmbedLayerDialog::addLegendLayerToTreeWidget( const QDomElement& layerElem, QTreeWidgetItem* parent )
{
if(layerElem.attribute("embedded") == "1" )
{
return;
}

QTreeWidgetItem* item = 0;
if( parent )
{
Expand Down Expand Up @@ -179,4 +198,15 @@ void QgsEmbedLayerDialog::unselectChildren( QTreeWidgetItem* item )
}
}

void QgsEmbedLayerDialog::on_mButtonBox_accepted()
{
QSettings s;
QFileInfo fi( mProjectPath );
if( fi.exists() )
{
s.setValue("/qgis/last_embedded_project_path", fi.absolutePath() );
}
accept();
}


2 changes: 2 additions & 0 deletions src/app/qgsembedlayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class QgsEmbedLayerDialog: public QDialog, private Ui::QgsEmbedLayerDialogBase
void on_mBrowseFileToolButton_clicked();
void on_mProjectFileLineEdit_editingFinished();
void on_mTreeWidget_itemSelectionChanged();
void on_mButtonBox_accepted();

private:
void changeProjectFile();
void addLegendGroupToTreeWidget( const QDomElement& groupElem, QTreeWidgetItem* parent = 0 );
void addLegendLayerToTreeWidget( const QDomElement& layerElem, QTreeWidgetItem* parent = 0 );
void unselectChildren( QTreeWidgetItem* item );
QString mProjectPath;
};

#endif // QGSEMBEDLAYERSDIALOG_H
6 changes: 6 additions & 0 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,12 @@ bool QgsProject::createEmbeddedLayer( const QString& layerId, const QString& pro
QString id = mapLayerElem.firstChildElement("id").text();
if( id == layerId )
{
//layer can be embedded only once
if( mapLayerElem.attribute("embedded") == "1" )
{
return false;
}

mEmbeddedLayers.insert( layerId, qMakePair( projectFilePath, saveFlag ) );
if( addLayer( mapLayerElem, brokenNodes, vectorLayerList ) )
{
Expand Down
Loading

0 comments on commit 6f37598

Please sign in to comment.