Skip to content

Commit

Permalink
Now you can add ui files
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilio Loi committed Apr 13, 2013
1 parent 4e52a8a commit 8694c19
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 25 deletions.
47 changes: 47 additions & 0 deletions src/app/qgssavestyletodbdialog.cpp
Expand Up @@ -9,6 +9,11 @@

#include "qgssavestyletodbdialog.h"

#include <QFileDialog>
#include <QSettings>
#include <QDomDocument>
#include <QMessageBox>

QgsSaveStyleToDbDialog::QgsSaveStyleToDbDialog( QWidget *parent ) :
QDialog( parent )
{
Expand All @@ -18,6 +23,7 @@ QgsSaveStyleToDbDialog::QgsSaveStyleToDbDialog( QWidget *parent ) :
setTabOrder( mNameEdit, mDescriptionEdit );
setTabOrder( mDescriptionEdit, mUseAsDefault );
setTabOrder( mUseAsDefault, buttonBox );

}
QString QgsSaveStyleToDbDialog::getName()
{
Expand All @@ -31,3 +37,44 @@ bool QgsSaveStyleToDbDialog::isDefault()
{
return mUseAsDefault->isChecked();
}
QString QgsSaveStyleToDbDialog::getUIFileContent()
{
return mUIFileContent;
}

void QgsSaveStyleToDbDialog::on_mFilePickButton_clicked()
{
QSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();

QString myFileName = QFileDialog::getOpenFileName( this, tr( "Attach Qt Creator UI file" ), myLastUsedDir, tr( "Qt Creator UI file .ui" ) + " (*.ui)" );
if ( myFileName.isNull() )
{
return;
}


QFileInfo myFI( myFileName );

QFile uiFile( myFI.filePath() );

QString myPath = myFI.path();
myQSettings.setValue( "style/lastStyleDir", myPath );

if(uiFile.open( QIODevice::ReadOnly ) )
{
QString content( uiFile.readAll() );
QDomDocument doc;

if( !doc.setContent(content) || doc.documentElement().tagName().compare( "ui" ) )
{
QMessageBox::warning(this, tr( "Wrong file" ),
tr( "The selected file does not appear to be a valid Qt Creator UI file."));
return;
}
mUIFileContent = content;
mFileNameLabel->setText( myFI.fileName() );
}

}

4 changes: 4 additions & 0 deletions src/app/qgssavestyletodbdialog.h
Expand Up @@ -16,15 +16,19 @@

class QgsSaveStyleToDbDialog : public QDialog, private Ui::QgsSaveToDBDialog
{
QString mUIFileContent;
Q_OBJECT
public:
explicit QgsSaveStyleToDbDialog(QWidget *parent = 0);

signals:

public slots:
QString getUIFileContent();
QString getName();
QString getDescription();
bool isDefault();
void on_mFilePickButton_clicked();
};

#endif // QGSSAVESTYLETODBDIALOG_H
17 changes: 13 additions & 4 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -645,11 +645,12 @@ void QgsVectorLayerProperties::saveStyleAs( StyleType styleType )
{
QString styleName = askToUser.getName();
QString styleDesc = askToUser.getDescription();
QString uiFileContent = askToUser.getUIFileContent();
bool isDefault = askToUser.isDefault();

apply();

layer->saveStyleToDatabase( styleName, styleDesc, isDefault, msgError );
layer->saveStyleToDatabase( styleName, styleDesc, isDefault, uiFileContent, msgError );
if( !msgError.isNull() )
{
QMessageBox::warning( this, infoWindowTitle, msgError );
Expand Down Expand Up @@ -733,14 +734,22 @@ void QgsVectorLayerProperties::loadStyleMenuTriggered( QAction *action )

int index = mLoadStyleMenu->actions().indexOf( action );

//Load from filesystem
if ( index == 0 )
if ( index == 0 ) //Load from filesystem
{
this->on_pbnLoadStyle_clicked();
this->on_pbnLoadStyle_clicked();
}
else if( index == 1 ) //Load from database
{
this->showListOfStylesFromDatabase();
}

}

void QgsVectorLayerProperties::showListOfStylesFromDatabase()
{
//TODO el1073 ui&logic
}

QList<QgsVectorOverlayPlugin*> QgsVectorLayerProperties::overlayPlugins() const
{
QList<QgsVectorOverlayPlugin*> pluginList;
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -136,6 +136,8 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope

/** called when is possible to choice if load the style from filesystem or from db */
void loadStyleMenuTriggered( QAction * );
/** when provider supports, it will list all the styles relative the layer */
void showListOfStylesFromDatabase();

protected:

Expand Down
7 changes: 5 additions & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -82,6 +82,7 @@ typedef bool saveStyle_t(
const QString& sldStyle,
const QString& styleName,
const QString& styleDescription,
const QString& uiFileContent,
bool useAsDefault,
QString& errCause
);
Expand Down Expand Up @@ -3717,7 +3718,8 @@ QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
return elem;
}

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

QString sldStyle, qmlStyle;
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
Expand Down Expand Up @@ -3751,7 +3753,8 @@ void QgsVectorLayer::saveStyleToDatabase(QString name, QString description, bool
}
sldStyle = sldDocument.toString();

saveStyleExternalMethod(mDataSource, qmlStyle, sldStyle, name, description, useAsDefault, msgError);
saveStyleExternalMethod(mDataSource, qmlStyle, sldStyle, name,
description, uiFileContent, useAsDefault, msgError);
}

QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -382,7 +382,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
virtual bool writeXml( QDomNode & layer_node, QDomDocument & doc );

virtual void saveStyleToDatabase( QString name, QString description,
bool useAsDefault, QString &msgError );
bool useAsDefault, QString uiFileContent,
QString &msgError );

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

Expand Down
22 changes: 16 additions & 6 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3226,7 +3226,7 @@ QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause )

QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QString& sldStyle,
const QString& styleName, const QString& styleDescription,
bool useAsDefault, QString& errCause )
const QString& uiFileContent, bool useAsDefault, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column;
Expand All @@ -3251,7 +3251,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
res = conn->PQexec( createTabeQuery );
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
errCause = QObject::tr( "Unable to save layer style. It's not possible to create the destination table on the database. Maybe you need to contact " );
errCause = QObject::tr( "Unable to save layer style. It's not possible to create the destination table on the database. Maybe this is due to table permissions (user=%1). Please contact your database admin" ).arg( dsUri.username() );
conn->disconnect();
return false;
}
Expand All @@ -3263,13 +3263,20 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
f_geometry_column = dsUri.geometryColumn();
QString owner = dsUri.username();
QString isdef = (useAsDefault) ? QObject::tr( "true" ) : QObject::tr( "false" );
QString uiFileColumn( "" );
QString uiFileValue( "" );
if( !uiFileContent.isEmpty() )
{
uiFileColumn.append( QObject::tr( ", ui" ) );
uiFileValue.append( QObject::tr( ",XMLPARSE(DOCUMENT %1)" ).arg( QgsPostgresConn::quotedValue( uiFileContent ) ) );
}

QString sql = QObject::tr( "INSERT INTO %1 ( f_table_catalog, "
"f_table_schema, f_table_name, f_geometry_column, "
"styleName, styleQML, styleSLD, useAsDefault, "
"description, owner) "
"description, owner %12) "
"VALUES(%2,%3,%4,%5,%6,XMLPARSE(DOCUMENT %7),"
"XMLPARSE(DOCUMENT %8),%9,%10,%11);" )
"XMLPARSE(DOCUMENT %8),%9,%10,%11 %13);" )
.arg( styleTableName )
.arg( QgsPostgresConn::quotedValue( f_table_catalog ) )
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
Expand All @@ -3280,7 +3287,10 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
.arg( QgsPostgresConn::quotedValue( sldStyle ) )
.arg( isdef )
.arg( QgsPostgresConn::quotedValue( styleDescription ) )
.arg( QgsPostgresConn::quotedValue( owner ) );
.arg( QgsPostgresConn::quotedValue( owner ) )
.arg( uiFileColumn )
.arg( uiFileValue );


if( useAsDefault )
{
Expand All @@ -3298,7 +3308,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
conn->disconnect();
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{
errCause = QObject::tr( "Unable to save layer style. It's not possible to insert a new record in style table. " );
errCause = QObject::tr( "Unable to save layer style. It's not possible to insert a new record in style table. Maybe this is due to table permissions (user=%1). Please contact your database admin" );
return false;
}
return true;
Expand Down
58 changes: 46 additions & 12 deletions src/ui/qgssavetodbdialog.ui
Expand Up @@ -17,8 +17,12 @@
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QPlainTextEdit" name="mDescriptionEdit"/>
<item row="5" column="1">
<widget class="QCheckBox" name="mUseAsDefault">
<property name="text">
<string>Use as default style for this layer</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="descriptionLabel">
Expand All @@ -27,6 +31,19 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPlainTextEdit" name="mDescriptionEdit"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="mUILabel">
<property name="text">
<string>UI</string>
</property>
<property name="buddy">
<cstring>mNameEdit</cstring>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="nameLabel">
<property name="text">
Expand All @@ -37,7 +54,10 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="0" column="1">
<widget class="QLineEdit" name="mNameEdit"/>
</item>
<item row="6" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
Expand All @@ -47,15 +67,29 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="mUseAsDefault">
<property name="text">
<string>Use as default style for this layer</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="mNameEdit"/>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="mFileNameLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="mFilePickButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Open...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down

0 comments on commit 8694c19

Please sign in to comment.