[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [krecipes] src/backends: Fill ingredients models with database.
From: José_Manuel_Santamaría_Lema <panfaust () gmail ! com>
Date: 2016-04-07 20:25:06
Message-ID: E1aoGUE-0007O4-VZ () scm ! kde ! org
[Download RAW message or body]
Git commit 1644b39ba52e6fe54a99e53bb7dccced3b332793 by José Manuel Santamaría 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 <QByteArray>
#include <QImageWriter>
#include <QPixmap>
+#include <QStandardItemModel>
#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 = new KreAllIngredientsModels( this );
+ }
+
+ QStandardItemModel * sourceModel = m_allIngredientsModels->sourceModel();
+ KreSingleColumnProxyModel * ingredientNameModel =
+ 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 = 0;
+ QModelIndex index;
+ while( query.next() ) {
+ index = sourceModel->index( row, 0 );
+ sourceModel->setData( index, query.value(0) );
+ index = 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 <kapplication.h>
#include <kconfiggroup.h>
@@ -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<Weight> 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() = 0;
+ virtual KreAllIngredientsModels * allIngredientsModels() = 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 really need it.
@@ -388,6 +393,8 @@ protected:
CategoryTree *m_categoryCache;
+ KreAllIngredientsModels * m_allIngredientsModels;
+
private:
QIODevice * m_dumpFile;
bool haltOperation;
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic