Skip to content

Commit fcb27d2

Browse files
author
Emilio Loi
committedApr 14, 2013
Loading from db now works. Still missing a lot of controls and info about errors
1 parent 002eb0d commit fcb27d2

8 files changed

+314
-52
lines changed
 

‎src/app/qgsloadstylefromdbdialog.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,93 @@
1111
#include "qgslogger.h"
1212

1313
#include <QMessageBox>
14+
#include <QVector>
1415

1516
QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
1617
: QDialog( parent )
1718
{
1819
setupUi( this );
20+
setWindowTitle( "Load style from database" );
21+
mSelectedStyleId = tr( "" );
22+
23+
mLoadButton->setDisabled( true );
24+
mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
25+
mRelatedTable->horizontalHeader()->setStretchLastSection( true );
26+
mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
27+
28+
mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
29+
mOthersTable->horizontalHeader()->setStretchLastSection( true );
30+
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
31+
32+
connect(mRelatedTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
33+
connect(mOthersTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
34+
connect(mCancelButton, SIGNAL( clicked() ), this, SLOT( cancelButtonClicked() ) );
35+
connect(mLoadButton, SIGNAL( clicked() ), this, SLOT( loadButtonClicked() ) );
36+
37+
setTabOrder( mRelatedTable, mOthersTable );
38+
setTabOrder( mOthersTable, mCancelButton );
39+
setTabOrder( mCancelButton, mLoadButton );
40+
}
41+
42+
void QgsLoadStyleFromDBDialog::initializeLists( QVector<QString> ids, QVector<QString> names,
43+
QVector<QString> descriptions, int sectionLimit )
44+
{
45+
mIds = ids;
46+
mNames = names;
47+
mDescriptions = descriptions;
48+
mSectionLimit = sectionLimit;
49+
int relatedTableNOfCols = ( sectionLimit > 0 ) ? 2 : 1;
50+
int othersTableNOfCols = ( ids.count() - sectionLimit > 0 ) ? 2 : 1;
51+
QString twoColsHeader( "Name;Description" );
52+
QString oneColsHeader( "No style found in the database" );
53+
QString relatedTableHeader = ( relatedTableNOfCols == 1 ) ? oneColsHeader : twoColsHeader;
54+
QString othersTableHeader = ( othersTableNOfCols == 1 ) ? oneColsHeader : twoColsHeader;
55+
56+
mRelatedTable->setColumnCount( relatedTableNOfCols );
57+
mOthersTable->setColumnCount( othersTableNOfCols );
58+
mRelatedTable->setHorizontalHeaderLabels( relatedTableHeader.split( ";" ) );
59+
mOthersTable->setHorizontalHeaderLabels( othersTableHeader.split( ";" ) );
60+
mRelatedTable->setRowCount( sectionLimit );
61+
mOthersTable->setRowCount( ids.count() - sectionLimit );
62+
mRelatedTable->setDisabled( ( relatedTableNOfCols == 1 ) );
63+
mOthersTable->setDisabled( ( othersTableNOfCols == 1 ) );
64+
65+
for( int i=0; i<sectionLimit; i++ )
66+
{
67+
mRelatedTable->setItem(i, 0, new QTableWidgetItem( names.value( i, "" ) ) );
68+
mRelatedTable->setItem(i, 1, new QTableWidgetItem( descriptions.value( i, "" ) ) );
69+
}
70+
for( int i=sectionLimit; i<ids.count(); i++ )
71+
{
72+
mOthersTable->setItem(i, 0, new QTableWidgetItem( names.value( i, "" ) ) );
73+
mOthersTable->setItem(i, 1, new QTableWidgetItem( descriptions.value( i, "" ) ) );
74+
}
75+
}
76+
77+
QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
78+
{
79+
return mSelectedStyleId;
80+
}
81+
82+
void QgsLoadStyleFromDBDialog::cellSelectedRelatedTable( int r )
83+
{
84+
mLoadButton->setEnabled( true );
85+
mSelectedStyleId = mIds.value( r );
86+
}
87+
88+
void QgsLoadStyleFromDBDialog::cellSelectedOthersTable( int r )
89+
{
90+
mLoadButton->setEnabled( true );
91+
mSelectedStyleId = mIds.value( r + mSectionLimit );
92+
}
93+
94+
void QgsLoadStyleFromDBDialog::cancelButtonClicked()
95+
{
96+
mSelectedStyleId = tr( "" ) ;
97+
this->close();
98+
}
99+
100+
void QgsLoadStyleFromDBDialog::loadButtonClicked()
101+
{
102+
this->close();
19103
}

‎src/app/qgsloadstylefromdbdialog.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@
77
* *
88
***************************************************************************/
99

10-
#ifndef QGSADDATTRDIALOG_H
11-
#define QGSADDATTRDIALOG_H
10+
#ifndef QGSLOADFILEFROMDBDIALOG_H
11+
#define QGSLOADFILEFROMDBDIALOG_H
12+
13+
#include <QVector>
1214

1315
#include "ui_qgsloadstylefromdbdialog.h"
1416
#include "qgisgui.h"
1517
#include "qgsfield.h"
1618

17-
//class QgsVectorLayer;
18-
1919
class QgsLoadStyleFromDBDialog: public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
2020
{
21+
QString mSelectedStyleId;
22+
int mSectionLimit;
23+
QVector<QString> mIds, mNames, mDescriptions;
24+
QString qmlStyle;
2125
Q_OBJECT
2226
public:
2327
explicit QgsLoadStyleFromDBDialog( QWidget *parent = 0 );
24-
// QgsAddAttrDialog( QgsVectorLayer *vlayer,
25-
// QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
26-
// QgsAddAttrDialog( const std::list<QString>& typelist,
27-
// QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
2828

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

31-
public slots:
32-
// void on_mTypeBox_currentIndexChanged( int idx );
33-
// void on_mLength_editingFinished();
34-
// void accept();
32+
public slots:
33+
void cellSelectedRelatedTable( int r );
34+
void cellSelectedOthersTable( int r );
35+
void cancelButtonClicked();
36+
void loadButtonClicked();
3537

36-
private:
37-
// QString mLayerType;
38+
private:
3839

39-
// void setPrecisionMinMax();
4040
};
4141

42-
#endif
42+
#endif //QGSLOADFILEFROMDBDIALOG_H

‎src/app/qgssavestyletodbdialog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* *
88
***************************************************************************/
99

10-
#ifndef QGSADDATTRDIALOG_H
11-
#define QGSADDATTRDIALOG_H
10+
#ifndef QGSSAVESTYLETODBDIALOG_H
11+
#define QGSSAVESTYLETODBDIALOG_H
1212

1313
#include "ui_qgssavetodbdialog.h"
1414
#include "qgisgui.h"

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434
#include "qgslabelinggui.h"
3535
#include "qgslabel.h"
3636
#include "qgslegenditem.h"
37-
//#include "qgsloadstylefromdbdialog.h"
37+
3838
#include "qgsgenericprojectionselector.h"
3939
#include "qgslogger.h"
4040
#include "qgsmaplayerregistry.h"
4141
#include "qgspluginmetadata.h"
4242
#include "qgspluginregistry.h"
4343
#include "qgsproject.h"
4444
#include "qgssavestyletodbdialog.h"
45+
#include "qgsloadstylefromdbdialog.h"
4546
#include "qgsvectorlayer.h"
4647
#include "qgsvectorlayerproperties.h"
4748
#include "qgsconfig.h"
@@ -748,7 +749,33 @@ void QgsVectorLayerProperties::loadStyleMenuTriggered( QAction *action )
748749

749750
void QgsVectorLayerProperties::showListOfStylesFromDatabase()
750751
{
751-
// QgsLoadStyleFromDBDialog dialog;
752+
QString errorMsg;
753+
QVector<QString> ids, names, descriptions;
754+
755+
756+
757+
int sectionLimit = layer->listStylesInDatabase(ids, names, descriptions, errorMsg);
758+
759+
if( !errorMsg.isNull() )
760+
{
761+
QMessageBox::warning(this, tr( "Error!" ), tr( "Retrieving" ) );
762+
return;
763+
}
764+
QgsLoadStyleFromDBDialog dialog;
765+
dialog.initializeLists(ids, names, descriptions, sectionLimit);
766+
dialog.exec();
767+
768+
QString selectedStyleId = dialog.getSelectedStyleId();
769+
if( selectedStyleId.compare( tr( "" ) ) )
770+
{
771+
QString qmlStyle = layer->getStyleFromDatabase( selectedStyleId, errorMsg );
772+
if( !qmlStyle.isNull() ){
773+
layer->applyNamedStyle( qmlStyle, errorMsg);
774+
reset();
775+
}
776+
777+
}
778+
752779
}
753780

754781
QList<QgsVectorOverlayPlugin*> QgsVectorLayerProperties::overlayPlugins() const

‎src/core/qgsvectorlayer.cpp

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <QSettings>
3232
#include <QString>
3333
#include <QDomNode>
34+
#include <QVector>
3435

3536
#include "qgsvectorlayer.h"
3637

@@ -92,6 +93,19 @@ typedef QString loadStyle_t(
9293
QString& errCause
9394
);
9495

96+
typedef int listStyles_t(
97+
const QString& uri,
98+
QVector<QString> &ids,
99+
QVector<QString> &names,
100+
QVector<QString> &descriptions,
101+
QString& errCause
102+
);
103+
104+
typedef QString getStyleById_t(
105+
const QString& uri,
106+
QString styleID,
107+
QString& errCause
108+
);
95109

96110

97111
QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
@@ -3718,6 +3732,48 @@ QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
37183732
return elem;
37193733
}
37203734

3735+
int QgsVectorLayer::listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names, QVector<QString> &descriptions, QString &msgError )
3736+
{
3737+
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
3738+
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
3739+
if ( !myLib )
3740+
{
3741+
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
3742+
return -1;
3743+
}
3744+
listStyles_t* listStylesExternalMethod = ( listStyles_t * ) cast_to_fptr(myLib->resolve("listStyles"));
3745+
3746+
if ( !listStylesExternalMethod )
3747+
{
3748+
delete myLib;
3749+
msgError = QObject::tr( "Provider %1 has no listStyles method" ).arg( mProviderKey );
3750+
return -1;
3751+
}
3752+
3753+
return listStylesExternalMethod(mDataSource, ids, names, descriptions, msgError);
3754+
}
3755+
3756+
QString QgsVectorLayer::getStyleFromDatabase(QString styleId, QString &msgError)
3757+
{
3758+
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
3759+
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
3760+
if ( !myLib )
3761+
{
3762+
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
3763+
return QObject::tr( "" );
3764+
}
3765+
getStyleById_t* getStyleByIdMethod = ( getStyleById_t * ) cast_to_fptr(myLib->resolve("getStyleById"));
3766+
3767+
if ( !getStyleByIdMethod )
3768+
{
3769+
delete myLib;
3770+
msgError = QObject::tr( "Provider %1 has no getStyleById method" ).arg( mProviderKey );
3771+
return QObject::tr( "" );
3772+
}
3773+
3774+
return getStyleByIdMethod( mDataSource, styleId, msgError );
3775+
}
3776+
37213777
void QgsVectorLayer::saveStyleToDatabase(QString name, QString description,
37223778
bool useAsDefault, QString uiFileContent, QString &msgError){
37233779

@@ -3757,6 +3813,9 @@ void QgsVectorLayer::saveStyleToDatabase(QString name, QString description,
37573813
description, uiFileContent, useAsDefault, msgError);
37583814
}
37593815

3816+
3817+
3818+
37603819
QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
37613820
{
37623821
//TODO find a better way to check if uri is pointing to db
@@ -3773,33 +3832,7 @@ QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFla
37733832
qml = loadStyleExternalMethod( mDataSource, errorMsg );
37743833
if( qml.compare( tr( "" ) ) )
37753834
{
3776-
QDomDocument myDocument( "qgis" );
3777-
myDocument.setContent( qml );
3778-
3779-
QDomElement myRoot = myDocument.firstChildElement( "qgis" );
3780-
3781-
if( myRoot.isNull() )
3782-
{
3783-
theResultFlag = false;
3784-
return tr( "Error: qgis element could not be found in %1" ).arg( theURI );;
3785-
}
3786-
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
3787-
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
3788-
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );
3789-
3790-
#if 0
3791-
//read transparency level
3792-
QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
3793-
if ( ! transparencyNode.isNull() )
3794-
{
3795-
// set transparency level only if it's in project
3796-
// (otherwise it sets the layer transparent)
3797-
QDomElement myElement = transparencyNode.toElement();
3798-
setTransparency( myElement.text().toInt() );
3799-
}
3800-
#endif
3801-
3802-
theResultFlag = readSymbology( myRoot, errorMsg );
3835+
theResultFlag = this->applyNamedStyle( qml, errorMsg);
38033836
}
38043837
}
38053838
}
@@ -3810,3 +3843,34 @@ QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFla
38103843
}
38113844
return tr( "" );
38123845
}
3846+
3847+
bool QgsVectorLayer::applyNamedStyle(QString namedStyle, QString errorMsg )
3848+
{
3849+
QDomDocument myDocument( "qgis" );
3850+
myDocument.setContent( namedStyle );
3851+
3852+
QDomElement myRoot = myDocument.firstChildElement( "qgis" );
3853+
3854+
if( myRoot.isNull() )
3855+
{
3856+
errorMsg = tr( "Error: qgis element could not be found" );
3857+
return false;
3858+
}
3859+
toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
3860+
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
3861+
setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );
3862+
3863+
#if 0
3864+
//read transparency level
3865+
QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
3866+
if ( ! transparencyNode.isNull() )
3867+
{
3868+
// set transparency level only if it's in project
3869+
// (otherwise it sets the layer transparent)
3870+
QDomElement myElement = transparencyNode.toElement();
3871+
setTransparency( myElement.text().toInt() );
3872+
}
3873+
#endif
3874+
3875+
return readSymbology( myRoot, errorMsg );
3876+
}

‎src/core/qgsvectorlayer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QSet>
2323
#include <QList>
2424
#include <QStringList>
25+
#include <QVector>
2526

2627
#include "qgis.h"
2728
#include "qgsmaplayer.h"
@@ -385,7 +386,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
385386
bool useAsDefault, QString uiFileContent,
386387
QString &msgError );
387388

389+
virtual int listStylesInDatabase( QVector<QString> &ids, QVector<QString> &names,
390+
QVector<QString> &descriptions, QString &msgError );
391+
392+
virtual QString getStyleFromDatabase( QString styleId, QString &msgError );
393+
388394
virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag );
395+
virtual bool applyNamedStyle(QString namedStyle , QString errorMsg);
389396

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

0 commit comments

Comments
 (0)
Please sign in to comment.