summaryrefslogtreecommitdiff
path: root/src/main-view/AppModel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main-view/AppModel.cpp')
-rw-r--r--src/main-view/AppModel.cpp196
1 files changed, 196 insertions, 0 deletions
diff --git a/src/main-view/AppModel.cpp b/src/main-view/AppModel.cpp
new file mode 100644
index 0000000..45e1956
--- /dev/null
+++ b/src/main-view/AppModel.cpp
@@ -0,0 +1,196 @@
+#include "AppModel.h"
+
+#include <stdlib.h>
+
+#include "../utils/RandomGenerator.h"
+#include "../dictionary/Dictionary.h"
+#include "../main-view/DictTableModel.h"
+#include "../study/WordDrillModel.h"
+#include "../study/SpacedRepetitionModel.h"
+#include "../dictionary/CardPack.h"
+
+AppModel::AppModel():
+ curDictIndex(-1), curCardPackIndex(0)
+{
+}
+
+AppModel::~AppModel()
+{
+ while(!dictionaries.isEmpty())
+ {
+ QPair<Dictionary*, DictTableModel*> pair = dictionaries.takeLast();
+ delete pair.second;
+ delete pair.first;
+ }
+}
+
+bool AppModel::openDictionary(const QString& filePath)
+{
+ Dictionary* dict = new Dictionary( "", false, this );
+ bool ok = dict->load(filePath);
+ if(ok)
+ addDictionary(dict);
+ else
+ {
+ errorMessage = dict->getErrorMessage();
+ delete dict;
+ }
+ return ok;
+}
+
+void AppModel::addDictionary( Dictionary* aDict )
+{
+ DictTableModel* dictModel = new DictTableModel( aDict );
+ dictionaries << qMakePair( aDict, dictModel );
+ curDictIndex = dictionaries.size() - 1;
+}
+
+Dictionary* AppModel::newDictionary()
+{
+ Dictionary* dict = new Dictionary( "", false, this );
+ dict->setDefaultFields();
+ addDictionary( dict );
+ return dict;
+}
+
+void AppModel::fixupCurDictIx()
+{
+ if( curDictIndex < 0 || curDictIndex >= dictionaries.size() )
+ curDictIndex = dictionaries.size()-1;
+}
+
+Dictionary* AppModel::curDictionary()
+ {
+ if( dictionaries.isEmpty() )
+ return NULL;
+ fixupCurDictIx();
+ return dictionaries[curDictIndex].first;
+ }
+
+DictTableModel* AppModel::curDictModel()
+ {
+ if( dictionaries.isEmpty() )
+ return NULL;
+ fixupCurDictIx();
+ return dictionaries[curDictIndex].second;
+ }
+
+Dictionary* AppModel::dictionary(int aIndex) const
+ {
+ if( aIndex < 0 || aIndex >= dictionaries.size() )
+ return NULL;
+ return dictionaries[aIndex].first;
+ }
+
+int AppModel::indexOfDictionary( Dictionary* aDic ) const
+ {
+ for( int i = 0; i < dictionaries.size(); i++ )
+ {
+ QPair<Dictionary*, DictTableModel*> pair = dictionaries.at( i );
+ if( pair.first == aDic )
+ return i;
+ }
+ return -1; // Not found
+ }
+
+int AppModel::indexOfDictionary( const QString& aFilePath ) const
+ {
+ for( int i = 0; i < dictionaries.size(); i++ )
+ {
+ QPair<Dictionary*, DictTableModel*> pair = dictionaries.at( i );
+ Q_ASSERT( pair.first );
+ if( pair.first->getFilePath() == aFilePath )
+ return i;
+ }
+ return -1; // Not found
+ }
+
+CardPack* AppModel::curCardPack()
+ {
+ Dictionary* curDic = curDictionary();
+ if( curDic )
+ return curDic->cardPack( curCardPackIndex );
+ else
+ return NULL;
+ }
+
+bool AppModel::setCurDictionary(int index)
+ {
+ if( dictionaries.isEmpty() )
+ {
+ curDictIndex = -1;
+ return false;
+ }
+ if( index < 0 || index >= dictionaries.size())
+ {
+ curDictIndex = dictionaries.size()-1;
+ return false;
+ }
+ curDictIndex = index;
+ return true;
+ }
+
+bool AppModel::removeDictionary( int aIndex )
+ {
+ if( aIndex < 0 || aIndex >= dictionaries.size() )
+ return false;
+ QPair<Dictionary*, DictTableModel*> pair = dictionaries.takeAt( aIndex );
+ delete pair.second;
+ delete pair.first;
+ return true;
+ }
+
+/** Destroys dic model and the dictionary itself.
+ */
+void AppModel::removeDictModel( QAbstractItemModel* aDictModel )
+ {
+ int i = 0;
+ for( ;i < dictionaries.size(); i++ )
+ {
+ QPair<Dictionary*, DictTableModel*> pair = dictionaries.at( i );
+ if( pair.second == aDictModel )
+ {
+ dictionaries.removeAt( i ); // Update the size of m_dictionaries before destroying
+ delete pair.first;
+ delete pair.second;
+ break;
+ }
+ }
+ }
+
+IStudyModel* AppModel::createStudyModel(int studyType, int cardPackIndex)
+ {
+ Dictionary* curDic = curDictionary();
+ if(!curDic)
+ {
+ errorMessage = tr("No dictionary opened.");
+ return NULL;
+ }
+ if(curDic->entriesNum() == 0)
+ {
+ errorMessage = tr("The current dictionary is empty.");
+ return NULL;
+ }
+
+ curCardPackIndex = cardPackIndex;
+ CardPack* cardPack = curDic->cardPack(curCardPackIndex);
+ if(!cardPack || cardPack->cardsNum() == 0)
+ {
+ errorMessage = tr("The current dictionary is empty.");
+ return NULL;
+ }
+
+ IStudyModel* studyModel = NULL;
+ switch(studyType)
+ {
+ case WordDrill:
+ studyModel = new WordDrillModel(cardPack);
+ break;
+ case SpacedRepetition:
+ studyModel = new SpacedRepetitionModel(cardPack, new RandomGenerator);
+ break;
+ }
+ if(studyModel)
+ studyModel->setDictModel( curDictModel() );
+ return studyModel;
+ }