Skip to content
Permalink
Browse files

[style] improve creation of default style db

Instead of copyign a pre-existing .db shipped with QGIS, create
a new .db and import an .xml file containing our default  symbols.

On the UX front, the main benefit is being able to ship with
pre-defined tags and favorite flags for the default symbols.

On the developer side, it means we get rid of the requirement
to maintain both an .xml and a binary .db in the source tree.
We also say aurevoir to the symbol_xml2db.py script and the
need to update the (obsolete) script every time we add a new
feature to QgsStyle.
  • Loading branch information
nirvn committed Nov 22, 2016
1 parent efd2992 commit bc130be6cbd4941f45c4af3cdcab795e9086faa0
@@ -208,8 +208,32 @@ class QgsStyle : QObject
//! Changes ramp's name
bool renameColorRamp( const QString& oldName, const QString& newName );

//! Creates a temporary memory database
bool createMemoryDB();
/** Creates an on-disk database
*
* This function creates a new on-disk permanent style database.
* \return returns the success state of the database creation
* \note added in QGIS 3.0
* \see createMemoryDb()
*/
bool createDb( const QString& filename );

/** Creates a temporary memory database
*
* This function is used to create a temporary style database in case a permanent on-disk database is not needed.
* \return returns the success state of the temporary memory database creation
* \note added in QGIS 3.0
* \see createDb()
*/
bool createMemoryDb();

/** Creates tables structure for new database
*
* This function is used to create the tables structure in a newly-created database.
* \return returns the success state of the temporary memory database creation
* \note added in QGIS 3.0
* \see createDB(), createMemoryDB()
*/
void createTables();

//! Loads a file into the style
bool load( const QString& filename );
@@ -1008,6 +1008,11 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
QgsCustomization::instance()->updateMainWindow( mToolbarMenu );
endProfile();

mSplash->showMessage( tr( "Populate saved styles" ), Qt::AlignHCenter | Qt::AlignBottom );
startProfile( QStringLiteral( "Populate saved styles" ) );
QgsStyle::defaultStyle();
endProfile();

mSplash->showMessage( tr( "QGIS Ready!" ), Qt::AlignHCenter | Qt::AlignBottom );

QgsMessageLog::logMessage( QgsApplication::showSettings(), QString::null, QgsMessageLog::INFO );
@@ -860,7 +860,7 @@ QString QgsApplication::userThemesFolder()

QString QgsApplication::defaultStylePath()
{
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/symbology-ng-style.db" );
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/symbology-ng-style.xml" );
}

QString QgsApplication::defaultThemesFolder()
@@ -58,11 +58,18 @@ QgsStyle* QgsStyle::defaultStyle() // static
// copy default style if user style doesn't exist
if ( !QFile::exists( styleFilename ) )
{
QFile::copy( QgsApplication::defaultStylePath(), styleFilename );
mDefaultStyle = new QgsStyle;
mDefaultStyle->createDb( styleFilename );
if ( QFile::exists( QgsApplication::defaultStylePath() ) )
{
mDefaultStyle->importXml( QgsApplication::defaultStylePath() );
}
}
else
{
mDefaultStyle = new QgsStyle;
mDefaultStyle->load( styleFilename );
}

mDefaultStyle = new QgsStyle;
mDefaultStyle->load( styleFilename );
}
return mDefaultStyle;
}
@@ -289,7 +296,22 @@ bool QgsStyle::openDB( const QString& filename )
return true;
}

bool QgsStyle::createMemoryDB()
bool QgsStyle::createDb( const QString& filename )
{
mErrorString.clear();
if ( !openDB( filename ) )
{
mErrorString = QStringLiteral( "Unable to create database" );
QgsDebugMsg( mErrorString );
return false;
}

createTables();

return true;
}

bool QgsStyle::createMemoryDb()
{
mErrorString.clear();
if ( !openDB( QStringLiteral( ":memory:" ) ) )
@@ -298,6 +320,14 @@ bool QgsStyle::createMemoryDB()
QgsDebugMsg( mErrorString );
return false;
}

createTables();

return true;
}

void QgsStyle::createTables()
{
char *query = sqlite3_mprintf( "CREATE TABLE symbol("\
"id INTEGER PRIMARY KEY,"\
"name TEXT UNIQUE,"\
@@ -322,7 +352,6 @@ bool QgsStyle::createMemoryDB()
"name TEXT,"\
"xml TEXT);" );
runEmptyQuery( query );
return true;
}

bool QgsStyle::load( const QString& filename )
@@ -273,16 +273,36 @@ class CORE_EXPORT QgsStyle : public QObject
//! Changes ramp's name
bool renameColorRamp( const QString& oldName, const QString& newName );

/** Creates an on-disk database
*
* This function creates a new on-disk permanent style database.
* \return returns the success state of the database creation
* \note added in QGIS 3.0
* \see createMemoryDb()
*/
bool createDb( const QString& filename );

/** Creates a temporary memory database
*
* This function is used if you do not need to associate styles with a permanent on-disk database.
* This function is used to create a temporary style database in case a permanent on-disk database is not needed.
* \return returns the success state of the temporary memory database creation
* \note added in QGIS 3.0
* \see createDb()
*/
bool createMemoryDb();

/** Creates tables structure for new database
*
* This function is used to create the tables structure in a newly-created database.
* \return returns the success state of the temporary memory database creation
* \note added in QGIS 3.0
* \see createDB(), createMemoryDB()
*/
bool createMemoryDB();
void createTables();

/** Loads a file into the style
*
* This function will populate styles from an on-disk database.
* This function will load an on-disk database and populate styles.
* \param filename location of the database to load styles from
* \return returns the success state of the database being loaded
*/
@@ -55,7 +55,7 @@ QgsStyleExportImportDialog::QgsStyleExportImportDialog( QgsStyle* style, QWidget
this, SLOT( selectionChanged( const QItemSelection&, const QItemSelection& ) ) );

mTempStyle = new QgsStyle();
mTempStyle->createMemoryDB();
mTempStyle->createMemoryDb();

// TODO validate
mFileName = QLatin1String( "" );
@@ -95,7 +95,7 @@ void TestStyle::initTestCase()

//initize a temporary memory-based style for tests to avoid clashing with shipped symbols
mStyle = new QgsStyle();
mStyle->createMemoryDB();
mStyle->createMemoryDb();

// cpt-city ramp, small selection available in <testdir>/cpt-city
QgsCptCityArchive::initArchives();

0 comments on commit bc130be

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