Git commit 1644b39ba52e6fe54a99e53bb7dccced3b332793 by Jos=C3=A9 Manuel San= tamar=C3=ADa Lema. Committed on 07/04/2016 at 20:15. Pushed by joselema into branch 'master'. Fill ingredients models with database. M +44 -0 src/backends/qsqlrecipedb.cpp M +3 -0 src/backends/qsqlrecipedb.h M +3 -2 src/backends/recipedb.cpp M +7 -0 src/backends/recipedb.h http://commits.kde.org/krecipes/1644b39ba52e6fe54a99e53bb7dccced3b332793 diff --git a/src/backends/qsqlrecipedb.cpp b/src/backends/qsqlrecipedb.cpp index a3ccdfb..f2f3fe7 100644 --- a/src/backends/qsqlrecipedb.cpp +++ b/src/backends/qsqlrecipedb.cpp @@ -17,9 +17,12 @@ #include #include #include +#include #include "datablocks/categorytree.h" #include "datablocks/rating.h" #include "datablocks/weight.h" +#include "models/kreallingredientsmodels.h" +#include "models/kresinglecolumnproxymodel.h" = #include "propertycalculator.h" = @@ -177,6 +180,47 @@ RecipeDB::Error QSqlRecipeDB::connect( bool create_db,= bool create_tables ) return NoError; } = +void QSqlRecipeDB::loadAllIngredientsModels() +{ + if ( !m_allIngredientsModels ) { + m_allIngredientsModels =3D new KreAllIngredientsModels( this ); + } + + QStandardItemModel * sourceModel =3D m_allIngredientsModels->sourceModel(= ); + KreSingleColumnProxyModel * ingredientNameModel =3D + m_allIngredientsModels->ingredientNameModel(); + + QSqlQuery query( "SELECT id,name FROM ingredients", *database); + query.exec(); + + CHECK_QUERY(query,return;) + + //Pre-allocate memory for model + sourceModel->setRowCount( getCount("ingredients") ); + sourceModel->setColumnCount( 2 ); + + int row =3D 0; + QModelIndex index; + while( query.next() ) { + index =3D sourceModel->index( row, 0 ); + sourceModel->setData( index, query.value(0) ); + index =3D sourceModel->index( row, 1 ); + sourceModel->setData( index, query.value(1) ); + ++row; + } + + ingredientNameModel->sort(0); + ingredientNameModel->setDynamicSortFilter( true ); +} + +KreAllIngredientsModels * QSqlRecipeDB::allIngredientsModels() +{ + if ( !m_allIngredientsModels ) { + loadAllIngredientsModels(); + } + return m_allIngredientsModels; +} + void QSqlRecipeDB::transaction() { if ( m_transactionsEnabled ) { diff --git a/src/backends/qsqlrecipedb.h b/src/backends/qsqlrecipedb.h index ba3f7f8..2319bb2 100644 --- a/src/backends/qsqlrecipedb.h +++ b/src/backends/qsqlrecipedb.h @@ -94,6 +94,9 @@ public: = RecipeDB::Error connect( bool create_db, bool create_tables ); = + void loadAllIngredientsModels(); + KreAllIngredientsModels * allIngredientsModels(); + virtual void transaction(); virtual void commit(); virtual void enableTransactions(); diff --git a/src/backends/recipedb.cpp b/src/backends/recipedb.cpp index 2c61efd..affbffc 100644 --- a/src/backends/recipedb.cpp +++ b/src/backends/recipedb.cpp @@ -10,7 +10,7 @@ * (at your option) any later version. * **************************************************************************= */ = -#include "backends/recipedb.h" +#include "recipedb.h" = #include #include @@ -66,7 +66,8 @@ struct ingredient_nutrient_data }; = RecipeDB::RecipeDB() : - QObject(), m_categoryCache(0), haltOperation(false), process(NULL) + QObject(), m_categoryCache(0), haltOperation(false), process(NULL), + m_allIngredientsModels(0) { new KrecipesdbAdaptor( this ); QDBusConnection::sessionBus().registerObject("/KrecipesDB", this); diff --git a/src/backends/recipedb.h b/src/backends/recipedb.h index dc5f77d..ffc8262 100644 --- a/src/backends/recipedb.h +++ b/src/backends/recipedb.h @@ -43,6 +43,8 @@ class RecipeSearchParameters; class Weight; typedef QList WeightList; = +class KreAllIngredientsModels; + = class RecipeDB: public QObject { @@ -160,6 +162,9 @@ public: /** Convenience method. Calls the above with arguments from KConfig. */ static RecipeDB* createDatabase(); = + virtual void loadAllIngredientsModels() =3D 0; + virtual KreAllIngredientsModels * allIngredientsModels() =3D 0; + //FIXME: Actually this functions shouldn't be exposed here since RecipeDB= is suposed //to represent a generic database which may be not be based on SQL. Not a= big problem //for now. In any case, please try to not use this functions unless you r= eally need it. @@ -388,6 +393,8 @@ protected: = CategoryTree *m_categoryCache; = + KreAllIngredientsModels * m_allIngredientsModels; + private: QIODevice * m_dumpFile; bool haltOperation;