Skip to content

Commit

Permalink
Loading from db now works. Still missing a lot of controls and info a…
Browse files Browse the repository at this point in the history
…bout errors
  • Loading branch information
Emilio Loi committed Apr 14, 2013
1 parent 002eb0d commit fcb27d2
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 52 deletions.
84 changes: 84 additions & 0 deletions src/app/qgsloadstylefromdbdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,93 @@
#include "qgslogger.h"

#include <QMessageBox>
#include <QVector>

QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
: QDialog( parent )
{
setupUi( this );
setWindowTitle( "Load style from database" );
mSelectedStyleId = tr( "" );

mLoadButton->setDisabled( true );
mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
mRelatedTable->horizontalHeader()->setStretchLastSection( true );
mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );

mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
mOthersTable->horizontalHeader()->setStretchLastSection( true );
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );

connect(mRelatedTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
connect(mOthersTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
connect(mCancelButton, SIGNAL( clicked() ), this, SLOT( cancelButtonClicked() ) );
connect(mLoadButton, SIGNAL( clicked() ), this, SLOT( loadButtonClicked() ) );

setTabOrder( mRelatedTable, mOthersTable );
setTabOrder( mOthersTable, mCancelButton );
setTabOrder( mCancelButton, mLoadButton );
}

void QgsLoadStyleFromDBDialog::initializeLists( QVector<QString> ids, QVector<QString> names,
QVector<QString> descriptions, int sectionLimit )
{
mIds = ids;
mNames = names;
mDescriptions = descriptions;
mSectionLimit = sectionLimit;
int relatedTableNOfCols = ( sectionLimit > 0 ) ? 2 : 1;
int othersTableNOfCols = ( ids.count() - sectionLimit > 0 ) ? 2 : 1;
QString twoColsHeader( "Name;Description" );
QString oneColsHeader( "No style found in the database" );
QString relatedTableHeader = ( relatedTableNOfCols == 1 ) ? oneColsHeader : twoColsHeader;
QString othersTableHeader = ( othersTableNOfCols == 1 ) ? oneColsHeader : twoColsHeader;

mRelatedTable->setColumnCount( relatedTableNOfCols );
mOthersTable->setColumnCount( othersTableNOfCols );
mRelatedTable->setHorizontalHeaderLabels( relatedTableHeader.split( ";" ) );
mOthersTable->setHorizontalHeaderLabels( othersTableHeader.split( ";" ) );
mRelatedTable->setRowCount( sectionLimit );
mOthersTable->setRowCount( ids.count() - sectionLimit );
mRelatedTable->setDisabled( ( relatedTableNOfCols == 1 ) );
mOthersTable->setDisabled( ( othersTableNOfCols == 1 ) );

for( int i=0; i<sectionLimit; i++ )
{
mRelatedTable->setItem(i, 0, new QTableWidgetItem( names.value( i, "" ) ) );
mRelatedTable->setItem(i, 1, new QTableWidgetItem( descriptions.value( i, "" ) ) );
}
for( int i=sectionLimit; i<ids.count(); i++ )
{
mOthersTable->setItem(i, 0, new QTableWidgetItem( names.value( i, "" ) ) );
mOthersTable->setItem(i, 1, new QTableWidgetItem( descriptions.value( i, "" ) ) );
}
}

QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
{
return mSelectedStyleId;
}

void QgsLoadStyleFromDBDialog::cellSelectedRelatedTable( int r )
{
mLoadButton->setEnabled( true );
mSelectedStyleId = mIds.value( r );
}

void QgsLoadStyleFromDBDialog::cellSelectedOthersTable( int r )
{
mLoadButton->setEnabled( true );
mSelectedStyleId = mIds.value( r + mSectionLimit );
}

void QgsLoadStyleFromDBDialog::cancelButtonClicked()
{
mSelectedStyleId = tr( "" ) ;
this->close();
}

void QgsLoadStyleFromDBDialog::loadButtonClicked()
{
this->close();
}
34 changes: 17 additions & 17 deletions src/app/qgsloadstylefromdbdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@
* *
***************************************************************************/

#ifndef QGSADDATTRDIALOG_H
#define QGSADDATTRDIALOG_H
#ifndef QGSLOADFILEFROMDBDIALOG_H
#define QGSLOADFILEFROMDBDIALOG_H

#include <QVector>

#include "ui_qgsloadstylefromdbdialog.h"
#include "qgisgui.h"
#include "qgsfield.h"

//class QgsVectorLayer;

class QgsLoadStyleFromDBDialog: public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
{
QString mSelectedStyleId;
int mSectionLimit;
QVector<QString> mIds, mNames, mDescriptions;
QString qmlStyle;
Q_OBJECT
public:
explicit QgsLoadStyleFromDBDialog( QWidget *parent = 0 );
// QgsAddAttrDialog( QgsVectorLayer *vlayer,
// QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
// QgsAddAttrDialog( const std::list<QString>& typelist,
// QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );

// QgsField field() const;
void initializeLists( QVector<QString> ids, QVector<QString> names, QVector<QString> descriptions, int sectionLimit);
QString getSelectedStyleId();

public slots:
// void on_mTypeBox_currentIndexChanged( int idx );
// void on_mLength_editingFinished();
// void accept();
public slots:
void cellSelectedRelatedTable( int r );
void cellSelectedOthersTable( int r );
void cancelButtonClicked();
void loadButtonClicked();

private:
// QString mLayerType;
private:

// void setPrecisionMinMax();
};

#endif
#endif //QGSLOADFILEFROMDBDIALOG_H
4 changes: 2 additions & 2 deletions src/app/qgssavestyletodbdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* *
***************************************************************************/

#ifndef QGSADDATTRDIALOG_H
#define QGSADDATTRDIALOG_H
#ifndef QGSSAVESTYLETODBDIALOG_H
#define QGSSAVESTYLETODBDIALOG_H

#include "ui_qgssavetodbdialog.h"
#include "qgisgui.h"
Expand Down
31 changes: 29 additions & 2 deletions src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
#include "qgslabelinggui.h"
#include "qgslabel.h"
#include "qgslegenditem.h"
//#include "qgsloadstylefromdbdialog.h"

#include "qgsgenericprojectionselector.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
#include "qgspluginmetadata.h"
#include "qgspluginregistry.h"
#include "qgsproject.h"
#include "qgssavestyletodbdialog.h"
#include "qgsloadstylefromdbdialog.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerproperties.h"
#include "qgsconfig.h"
Expand Down Expand Up @@ -748,7 +749,33 @@ void QgsVectorLayerProperties::loadStyleMenuTriggered( QAction *action )

void QgsVectorLayerProperties::showListOfStylesFromDatabase()
{
// QgsLoadStyleFromDBDialog dialog;
QString errorMsg;
QVector<QString> ids, names, descriptions;



int sectionLimit = layer->listStylesInDatabase(ids, names, descriptions, errorMsg);

if( !errorMsg.isNull() )
{
QMessageBox::warning(this, tr( "Error!" ), tr( "Retrieving" ) );
return;
}
QgsLoadStyleFromDBDialog dialog;
dialog.initializeLists(ids, names, descriptions, sectionLimit);
dialog.exec();

QString selectedStyleId = dialog.getSelectedStyleId();
if( selectedStyleId.compare( tr( "" ) ) )
{
QString qmlStyle = layer->getStyleFromDatabase( selectedStyleId, errorMsg );
if( !qmlStyle.isNull() ){
layer->applyNamedStyle( qmlStyle, errorMsg);
reset();
}

}

}

QList<QgsVectorOverlayPlugin*> QgsVectorLayerProperties::overlayPlugins() const
Expand Down
118 changes: 91 additions & 27 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QSettings>
#include <QString>
#include <QDomNode>
#include <QVector>

#include "qgsvectorlayer.h"

Expand Down Expand Up @@ -92,6 +93,19 @@ typedef QString loadStyle_t(
QString& errCause
);

typedef int listStyles_t(
const QString& uri,
QVector<QString> &ids,
QVector<QString> &names,
QVector<QString> &descriptions,
QString& errCause
);

typedef QString getStyleById_t(
const QString& uri,
QString styleID,
QString& errCause
);


QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
Expand Down Expand Up @@ -3718,6 +3732,48 @@ QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
return elem;
}

int QgsVectorLayer::listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names, QVector<QString> &descriptions, QString &msgError )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return -1;
}
listStyles_t* listStylesExternalMethod = ( listStyles_t * ) cast_to_fptr(myLib->resolve("listStyles"));

if ( !listStylesExternalMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no listStyles method" ).arg( mProviderKey );
return -1;
}

return listStylesExternalMethod(mDataSource, ids, names, descriptions, msgError);
}

QString QgsVectorLayer::getStyleFromDatabase(QString styleId, QString &msgError)
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return QObject::tr( "" );
}
getStyleById_t* getStyleByIdMethod = ( getStyleById_t * ) cast_to_fptr(myLib->resolve("getStyleById"));

if ( !getStyleByIdMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no getStyleById method" ).arg( mProviderKey );
return QObject::tr( "" );
}

return getStyleByIdMethod( mDataSource, styleId, msgError );
}

void QgsVectorLayer::saveStyleToDatabase(QString name, QString description,
bool useAsDefault, QString uiFileContent, QString &msgError){

Expand Down Expand Up @@ -3757,6 +3813,9 @@ void QgsVectorLayer::saveStyleToDatabase(QString name, QString description,
description, uiFileContent, useAsDefault, msgError);
}




QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
{
//TODO find a better way to check if uri is pointing to db
Expand All @@ -3773,33 +3832,7 @@ QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFla
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if( qml.compare( tr( "" ) ) )
{
QDomDocument myDocument( "qgis" );
myDocument.setContent( qml );

QDomElement myRoot = myDocument.firstChildElement( "qgis" );

if( myRoot.isNull() )
{
theResultFlag = false;
return tr( "Error: qgis element could not be found in %1" ).arg( theURI );;
}
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );

#if 0
//read transparency level
QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
if ( ! transparencyNode.isNull() )
{
// set transparency level only if it's in project
// (otherwise it sets the layer transparent)
QDomElement myElement = transparencyNode.toElement();
setTransparency( myElement.text().toInt() );
}
#endif

theResultFlag = readSymbology( myRoot, errorMsg );
theResultFlag = this->applyNamedStyle( qml, errorMsg);
}
}
}
Expand All @@ -3810,3 +3843,34 @@ QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFla
}
return tr( "" );
}

bool QgsVectorLayer::applyNamedStyle(QString namedStyle, QString errorMsg )
{
QDomDocument myDocument( "qgis" );
myDocument.setContent( namedStyle );

QDomElement myRoot = myDocument.firstChildElement( "qgis" );

if( myRoot.isNull() )
{
errorMsg = tr( "Error: qgis element could not be found" );
return false;
}
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );

#if 0
//read transparency level
QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
if ( ! transparencyNode.isNull() )
{
// set transparency level only if it's in project
// (otherwise it sets the layer transparent)
QDomElement myElement = transparencyNode.toElement();
setTransparency( myElement.text().toInt() );
}
#endif

return readSymbology( myRoot, errorMsg );
}
7 changes: 7 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QSet>
#include <QList>
#include <QStringList>
#include <QVector>

#include "qgis.h"
#include "qgsmaplayer.h"
Expand Down Expand Up @@ -385,7 +386,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
bool useAsDefault, QString uiFileContent,
QString &msgError );

virtual int listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names,
QVector<QString> &descriptions, QString &msgError );

virtual QString getStyleFromDatabase( QString styleId, QString &msgError );

virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag );
virtual bool applyNamedStyle(QString namedStyle , QString errorMsg);

/** convert a saved attribute editor element into a AttributeEditor structure as it's used internally.
* @param elem the DOM element
Expand Down
Loading

0 comments on commit fcb27d2

Please sign in to comment.