Skip to content

Commit

Permalink
[style] improve creation of default style db
Browse files Browse the repository at this point in the history
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 bc130be
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 14 deletions.
28 changes: 26 additions & 2 deletions python/core/symbology-ng/qgsstyle.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
41 changes: 35 additions & 6 deletions src/core/symbology-ng/qgsstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:" ) ) )
Expand All @@ -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,"\
Expand All @@ -322,7 +352,6 @@ bool QgsStyle::createMemoryDB()
"name TEXT,"\
"xml TEXT);" );
runEmptyQuery( query );
return true;
}

bool QgsStyle::load( const QString& filename )
Expand Down
26 changes: 23 additions & 3 deletions src/core/symbology-ng/qgsstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgsstyleexportimportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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( "" );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit bc130be

Please sign in to comment.