Changeset View
Changeset View
Standalone View
Standalone View
src/statistics/statisticsmodel.cpp
Show All 9 Lines | |||||
10 | * it under the terms of the GNU General Public License as published by * | 10 | * it under the terms of the GNU General Public License as published by * | ||
11 | * the Free Software Foundation; either version 2 of the License, or * | 11 | * the Free Software Foundation; either version 2 of the License, or * | ||
12 | * (at your option) any later version. * | 12 | * (at your option) any later version. * | ||
13 | * * | 13 | * * | ||
14 | ***************************************************************************/ | 14 | ***************************************************************************/ | ||
15 | 15 | | |||
16 | #include "statisticsmodel.h" | 16 | #include "statisticsmodel.h" | ||
17 | #include "statisticslegendwidget.h" | 17 | #include "statisticslegendwidget.h" | ||
18 | #include "utils.h" | ||||
18 | 19 | | |||
20 | #include <KEduVocExpression> | ||||
21 | #include <KEduVocTranslation> | ||||
22 | #include <KEduVocWordtype> | ||||
19 | #include <KLocalizedString> | 23 | #include <KLocalizedString> | ||
20 | #include <QGradient> | 24 | #include <QGradient> | ||
21 | #include <QDebug> | 25 | #include <QDebug> | ||
22 | 26 | | |||
23 | StatisticsModel::StatisticsModel(QObject * parent) | 27 | StatisticsModel::StatisticsModel(QObject *parent) | ||
24 | : ContainerModel(KEduVocContainer::Lesson, parent) | 28 | : ContainerModel(KEduVocContainer::Lesson, parent) | ||
25 | { | 29 | { | ||
26 | } | 30 | } | ||
27 | 31 | | |||
28 | QVariant StatisticsModel::headerData(int section, Qt::Orientation orientation, int role) const | 32 | QVariant StatisticsModel::headerData(int section, Qt::Orientation orientation, int role) const | ||
29 | { | 33 | { | ||
30 | if (section >= FirstDataColumn) { | 34 | if (section >= FirstDataColumn) { | ||
31 | if (role == Qt::DisplayRole | 35 | if (role == Qt::DisplayRole | ||
32 | && m_doc->identifierCount() > (section - FirstDataColumn) ) { | 36 | && m_doc->identifierCount() > (section - FirstDataColumn) ) { | ||
33 | return i18nc("Confidence level in language, table header", "Confidence (%1)" | 37 | return i18nc("Confidence level in language, table header", "Confidence (%1)" | ||
34 | , m_doc->identifier(section - FirstDataColumn).name()); | 38 | , m_doc->identifier(section - FirstDataColumn).name()); | ||
35 | } | 39 | } | ||
36 | } | 40 | } | ||
37 | return ContainerModel::headerData(section, orientation, role); | 41 | return ContainerModel::headerData(section, orientation, role); | ||
38 | } | 42 | } | ||
39 | 43 | | |||
40 | QVariant StatisticsModel::data(const QModelIndex & index, int role) const | 44 | QVariant StatisticsModel::data(const QModelIndex &index, int role) const | ||
41 | { | 45 | { | ||
42 | if (index.column() >= FirstDataColumn) { | 46 | Q_ASSERT(!m_documentSettings.isEmpty()); | ||
47 | | ||||
43 | KEduVocContainer *container = static_cast<KEduVocContainer*>(index.internalPointer()); | 48 | KEduVocContainer *container = static_cast<KEduVocContainer*>(index.internalPointer()); | ||
44 | QVariant var; | 49 | | ||
50 | // Entrie count | ||||
51 | if (index.column() == TotalCountColumn) { | ||||
52 | if (role == Qt::DisplayRole) { | ||||
53 | switch (Prefs::practiceDirection()) { | ||||
54 | case Prefs::EnumPracticeDirection::KnownToLearning: | ||||
55 | return entryCountForPracticeMode(container, Prefs::learningLanguage()); | ||||
56 | case Prefs::EnumPracticeDirection::LearningToKnown: | ||||
57 | return entryCountForPracticeMode(container, Prefs::knownLanguage()); | ||||
58 | case Prefs::EnumPracticeDirection::MixedDirectionsWordsOnly: | ||||
59 | case Prefs::EnumPracticeDirection::MixedDirectionsWithSound: | ||||
60 | return entryCountForPracticeMode(container, Prefs::knownLanguage()) | ||||
61 | + entryCountForPracticeMode(container, Prefs::learningLanguage()); | ||||
62 | default: | ||||
63 | return entryCountForPracticeMode(container, Prefs::learningLanguage()); | ||||
64 | } | ||||
65 | } | ||||
66 | if (role == Qt::TextAlignmentRole) { | ||||
67 | return Qt::AlignRight; | ||||
68 | } | ||||
69 | } | ||||
70 | | ||||
71 | // Colorbars | ||||
72 | if (index.column() >= FirstDataColumn) { | ||||
73 | int translation = index.column() - FirstDataColumn; | ||||
45 | switch (role) { | 74 | switch (role) { | ||
46 | case Container: | 75 | case Container: | ||
76 | { | ||||
47 | // Return a pointer to the container we are working on. | 77 | // Return a pointer to the container we are working on. | ||
78 | QVariant var; | ||||
48 | var.setValue(container); | 79 | var.setValue(container); | ||
49 | return var; | 80 | return var; | ||
81 | } | ||||
50 | case TotalPercent: // Average grade | 82 | case TotalPercent: // Average grade | ||
51 | return container->averageGrade(index.column() - FirstDataColumn, KEduVocContainer::Recursive); | 83 | return averageGradeForPracticeMode(container, translation); | ||
52 | case TotalCount: | 84 | case TotalCount: | ||
53 | return container->entryCount(KEduVocContainer::Recursive); | 85 | return entryCountForPracticeMode(container, translation); | ||
86 | case ActiveConjugationTenses: | ||||
87 | return m_documentSettings.at(translation)->conjugationTenses(); | ||||
54 | default: | 88 | default: | ||
55 | if (role >= Qt::UserRole) { | 89 | if ((role >= Grade0) && (role <= Grade7)) { | ||
56 | return container->expressionsOfGrade( | 90 | return expressionsOfGradeForPracticeMode(container, translation, role - Grade0); | ||
57 | index.column() - FirstDataColumn, role - Grade0, KEduVocContainer::Recursive); | | |||
58 | } | 91 | } | ||
59 | } | 92 | } | ||
60 | } | 93 | } | ||
61 | 94 | | |||
62 | // checkboxes | 95 | // Checkboxes | ||
63 | if (index.column() == 0 && role == Qt::CheckStateRole) { | 96 | if (index.column() == 0 && role == Qt::CheckStateRole) { | ||
64 | KEduVocContainer *container = static_cast<KEduVocContainer*>(index.internalPointer()); | | |||
65 | if (container->inPractice()) { | 97 | if (container->inPractice()) { | ||
66 | return Qt::Checked; | 98 | return Qt::Checked; | ||
67 | } else { | 99 | } else { | ||
68 | return Qt::Unchecked; | 100 | return Qt::Unchecked; | ||
69 | } | 101 | } | ||
70 | } | 102 | } | ||
71 | 103 | | |||
72 | return ContainerModel::data(index, role); | 104 | return ContainerModel::data(index, role); | ||
73 | } | 105 | } | ||
74 | 106 | | |||
75 | 107 | int StatisticsModel::averageGradeForPracticeMode(KEduVocContainer *container, int translation) const | |||
108 | { | ||||
109 | WordCount wordCount; | ||||
110 | wordCount.fillFromContainerForPracticeMode( | ||||
111 | *container, | ||||
112 | translation, | ||||
113 | m_documentSettings.at(translation)->conjugationTenses() | ||||
114 | ); | ||||
115 | return wordCount.percentageCompleted(); | ||||
116 | } | ||||
117 | | ||||
118 | int StatisticsModel::entryCountForPracticeMode(KEduVocContainer *container, int translation) const | ||||
119 | { | ||||
120 | WordCount wordCount; | ||||
121 | wordCount.fillFromContainerForPracticeMode( | ||||
122 | *container, | ||||
123 | translation, | ||||
124 | m_documentSettings.at(translation)->conjugationTenses() | ||||
125 | ); | ||||
126 | return wordCount.totalWords - wordCount.invalid; | ||||
127 | } | ||||
128 | | ||||
129 | int StatisticsModel::expressionsOfGradeForPracticeMode(KEduVocContainer *container, | ||||
130 | int translation, grade_t grade) const | ||||
131 | { | ||||
132 | WordCount wordCount; | ||||
133 | wordCount.fillFromContainerForPracticeMode( | ||||
134 | *container, | ||||
135 | translation, | ||||
136 | m_documentSettings.at(translation)->conjugationTenses() | ||||
137 | ); | ||||
138 | return wordCount.grades[grade]; | ||||
139 | } | ||||
76 | 140 | | |||
77 | Qt::ItemFlags StatisticsModel::flags(const QModelIndex & index) const | 141 | Qt::ItemFlags StatisticsModel::flags(const QModelIndex &index) const | ||
78 | { | 142 | { | ||
79 | if (index.isValid()) { | 143 | if (index.isValid()) { | ||
80 | if (index.column() == 0) { | 144 | if (index.column() == 0) { | ||
81 | return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); | 145 | return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); | ||
82 | } | 146 | } | ||
83 | return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); | 147 | return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); | ||
84 | } | 148 | } | ||
85 | return 0; | 149 | return 0; | ||
86 | } | 150 | } | ||
87 | 151 | | |||
88 | int StatisticsModel::columnCount(const QModelIndex & parent) const | 152 | int StatisticsModel::columnCount(const QModelIndex &parent) const | ||
89 | { | 153 | { | ||
90 | Q_UNUSED(parent) | 154 | Q_UNUSED(parent) | ||
91 | return m_doc->identifierCount() + FirstDataColumn; | 155 | return m_doc->identifierCount() + FirstDataColumn; | ||
92 | } | 156 | } | ||
93 | 157 | | |||
94 | Qt::DropActions StatisticsModel::supportedDragActions() const | 158 | Qt::DropActions StatisticsModel::supportedDragActions() const | ||
95 | { | 159 | { | ||
96 | return 0; | 160 | return 0; | ||
97 | } | 161 | } | ||
98 | 162 | | |||
99 | KEduVocContainer * StatisticsModel::rootContainer() const | 163 | KEduVocContainer *StatisticsModel::rootContainer() const | ||
100 | { | 164 | { | ||
101 | if (!m_doc) { | 165 | if (!m_doc) { | ||
102 | return 0; | 166 | return 0; | ||
103 | } | 167 | } | ||
104 | return m_doc->lesson(); | 168 | return m_doc->lesson(); | ||
105 | } | 169 | } | ||
170 | | ||||
171 | void StatisticsModel::loadDocumentsSettings() | ||||
172 | { | ||||
173 | m_documentSettings.clear(); | ||||
174 | if (m_doc == nullptr) { | ||||
175 | return; | ||||
176 | } | ||||
177 | for (int i = 0 ; i < m_doc->identifierCount(); ++i) { | ||||
178 | m_documentSettings << QSharedPointer<DocumentSettings>( | ||||
179 | new DocumentSettings(m_doc->url().url() + QString::number(i)) | ||||
180 | ); | ||||
181 | m_documentSettings.last()->load(); | ||||
182 | | ||||
183 | } | ||||
184 | } | ||||
185 | | ||||
186 | void StatisticsModel::setDocument(KEduVocDocument *doc) | ||||
187 | { | ||||
188 | beginResetModel(); | ||||
189 | m_doc = doc; | ||||
190 | loadDocumentsSettings(); | ||||
191 | endResetModel(); | ||||
192 | } | ||||
193 | | ||||
194 | | ||||
195 | void StatisticsModel::updateDocumentSettings() | ||||
196 | { | ||||
197 | beginResetModel(); | ||||
198 | loadDocumentsSettings(); | ||||
199 | endResetModel(); | ||||
200 | } | ||||
201 | |