• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.12
  • kdeedu
  • parley
  • src
  • vocabulary
vocabularymodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  Copyright 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
4 
5  ***************************************************************************/
6 
7 /***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
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 *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "vocabularymodel.h"
16 
17 #include "parleydocument.h"
18 #include "prefs.h"
19 #include "languagesettings.h"
20 
21 #include <keduvoclesson.h>
22 #include <keduvocwordtype.h>
23 
24 #include "vocabularymimedata.h"
25 
26 #include <keduvocexpression.h>
27 #include <KIcon>
28 #include <KLocalizedString>
29 #include <KDebug>
30 #include <KMessageBox>
31 #include <QPixmap>
32 
33 using namespace Editor;
34 
35 VocabularyModel::VocabularyModel(QObject *parent)
36  :QAbstractTableModel(parent),
37  m_container(0), m_document(0)
38 {
39  m_recursive = Prefs::showSublessonentries() ? KEduVocContainer::Recursive
40  : KEduVocContainer::NotRecursive;
41 
42  qRegisterMetaType<KEduVocTranslation*>("KEduVocTranslationPointer");
43 }
44 
45 VocabularyModel::~VocabularyModel()
46 {
47 }
48 
49 void VocabularyModel::setDocument(KEduVocDocument * doc)
50 {
51  m_document = doc;
52  m_container = 0;
53  m_lesson = 0;
54 
55  // to get the headers right
56  // (better get rid of the reset)
57  reset();
58 
59  if ( m_document ) {
60  showContainer(m_document->lesson());
61  } else {
62  showContainer(0);
63  }
64 }
65 
66 
67 void VocabularyModel::showContainer(KEduVocContainer * container)
68 {
69  // use remove and insert rows. using reset resets all table headers too.
70  if (rowCount(QModelIndex()) > 0) {
71  beginRemoveRows(QModelIndex(), 0, rowCount(QModelIndex())-1);
72  m_container = 0;
73  endRemoveRows();
74  }
75  if (container) {
76  if (container->entryCount(m_recursive) > 0) {
77  beginInsertRows(QModelIndex(), 0, container->entryCount(m_recursive)-1);
78  m_container = container;
79  endInsertRows();
80  } else {
81  m_container = container;
82  }
83  }
84 }
85 
86 void VocabularyModel::setLesson(KEduVocLesson * lessonContainer)
87 {
88  m_lesson = lessonContainer;
89 }
90 
91 KEduVocLesson * VocabularyModel::lesson()
92 {
93  return m_lesson;
94 }
95 
96 int VocabularyModel::rowCount(const QModelIndex &index) const
97 {
98  // no lesson set - zarro rows
99  if ( !m_container ) {
100  return 0;
101  }
102  // only the root index has children because we have no hierarchical model.
103  if (index == QModelIndex()) {
104  return m_container->entryCount(m_recursive);
105  }
106  return 0;
107 }
108 
109 int VocabularyModel::columnCount(const QModelIndex &) const
110 {
111  if ( !m_document ) {
112  return 0;
113  }
114  return m_document->identifierCount()*EntryColumnsMAX;
115 }
116 
117 QVariant VocabularyModel::data(const QModelIndex & index, int role) const
118 {
119  if ( !m_document || !m_container ) {
120  return QVariant();
121  }
122 
123  int translationId = translation(index.column());
124  int entryColumn = columnType(index.column());
125 
126  switch (role) {
127  case Qt::DisplayRole:
128  switch (entryColumn) {
129  case Translation:
130  return QVariant(m_container->entry(index.row(), m_recursive)->translation(translationId)->text());
131  case Pronunciation:
132  return QVariant(m_container->entry(index.row(), m_recursive)->translation(translationId)->pronunciation());
133  case WordType:
134  // if no word type is set, we get a null pointer
135  if(m_container->entry(index.row(), m_recursive)->translation(translationId)->wordType()) {
136  return QVariant(m_container->entry(index.row(), m_recursive)->translation(translationId)->wordType()->name());
137  }
138  return QVariant(QString());
139  case Synonym: {
140  QStringList displayElements;
141  foreach(KEduVocTranslation* synonym, m_container->entry(index.row(), m_recursive)->translation(translationId)->synonyms()) {
142  displayElements.append(synonym->text());
143  }
144  return QVariant(displayElements.join("; "));
145  }
146  case Antonym: {
147  QStringList displayElements;
148  foreach(KEduVocTranslation* antonym, m_container->entry(index.row(), m_recursive)->translation(translationId)->antonyms()) {
149  displayElements.append(antonym->text());
150  }
151  return QVariant(displayElements.join("; "));
152  }
153  case Example: {
154  QString example = m_container->entry(index.row(), m_recursive)->translation(translationId)->example();
155  /*QString word = m_container->entry(index.row(), m_recursive)->translation(translationId)->text();
156  int pos = 0;
157  QString start = "<font color=\"#FF0000\"><b>";
158  QString end = "</b></font>";
159  while ((pos = example.indexOf(word, pos)) >= 0) {
160  example.insert(pos, start);
161  example.insert(pos+word.length()+start.length(), end);
162  pos += word.length()+start.length()+end.length();
163  }*/
164  return QVariant(example);
165  }
166  case Comment:
167  return QVariant(m_container->entry(index.row(), m_recursive)->translation(translationId)->comment());
168  case Paraphrase:
169  return QVariant(m_container->entry(index.row(), m_recursive)->translation(translationId)->paraphrase());
170  default:
171  return QVariant();
172  }
173  break;
174  case Qt::FontRole:
175  if (entryColumn == Translation) {
176  QString locale = m_document->identifier(translationId).locale();
177  LanguageSettings ls(locale);
178  ls.readConfig();
179  return ls.editorFont();
180  }
181  return QVariant();
182  case Qt::TextColorRole:
183  if (Prefs::useGradeColors() && entryColumn == Translation) {
184  int grade = m_container->entry(index.row(), m_recursive)->translation(translationId)->grade();
185  return Prefs::gradeColor(grade);
186  } else {
187  return QVariant();
188  }
189  case LocaleRole:
190  return QVariant(m_document->identifier(translationId).locale());
191  case EntryRole: {
192  QVariant v;
193  v.setValue(m_container->entry(index.row(), m_recursive));
194  return v;
195  }
196  case Qt::ToolTipRole: {
198  switch (entryColumn) {
199  case WordType:
200  return i18n("You can drag and drop words onto their word type.");
201  case Synonym:
202  return i18n("Enable the synonym view to edit synonyms.");
203  case Antonym:
204  return i18n("Enable the antonym view to edit antonyms.");
205  }
206  }
207  }
208  return QVariant();
209 }
210 
211 
212 bool VocabularyModel::setData(const QModelIndex &index, const QVariant &value, int role)
213 {
214  if ((!index.isValid()) || (role != Qt::EditRole)) {
215  return false;
216  }
217 
218  int translationId = translation(index.column());
219  int column = columnType(index.column());
220 
221  switch (column) {
222  case Translation:
223  m_container->entry(index.row(), m_recursive)->translation(translationId)->setText(value.toString());
224  break;
225  case Pronunciation:
226  m_container->entry(index.row(), m_recursive)->translation(translationId)->setPronunciation(value.toString());
227  break;
228  case WordType:
229  break;
230  case Example:
231  m_container->entry(index.row(), m_recursive)->translation(translationId)->setExample(value.toString());
232  break;
233  case Comment:
234  m_container->entry(index.row(), m_recursive)->translation(translationId)->setComment(value.toString());
235  break;
236  case Paraphrase:
237  m_container->entry(index.row(), m_recursive)->translation(translationId)->setParaphrase(value.toString());
238  break;
239  default:
240  return false;
241  }
242 
243  emit(dataChanged(index, index));
244  m_document->setModified();
245  return true;
246 }
247 
248 Qt::ItemFlags VocabularyModel::flags(const QModelIndex & index) const
249 {
250  if (!index.isValid()) {
251  return Qt::ItemIsDropEnabled;
252  }
253 
254  switch (columnType(index.column())) {
255  case Translation:
256  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
257  case WordType:
258  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
259  case Synonym:
260  case Antonym:
261  return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
262  default:
263  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDropEnabled;
264  }
265 }
266 
267 QVariant VocabularyModel::headerData(int section, Qt::Orientation orientation, int role) const
268 {
269  if (!m_document) {
270  return QVariant();
271  }
272 
273  if ( orientation == Qt::Horizontal ) {
274  int translationId = section / EntryColumnsMAX;
275  int entryColumn = section % EntryColumnsMAX;
276 
277  switch (role) {
278  case Qt::DisplayRole:
279  return VocabularyModel::columnTitle(m_document, translationId, entryColumn);
280  break;
281  } // switch role
282  } // if horizontal
283  return QVariant();
284 }
285 
286 QString VocabularyModel::columnTitle(KEduVocDocument *document, int translation, int column)
287 {
288  switch (column){
289  case Translation:
290  if (document->identifierCount() - 1 < translation) {
291  return QString();
292  }
293  return document->identifier(translation).name(); //returns "English", "German", etc
294  case Pronunciation:
295  return i18n("Pronunciation");
296  case WordType:
297  return i18n("Word Type");
298  case Synonym:
299  return i18n("Synonym");
300  case Antonym:
301  return i18n("Antonym");
302  case Example:
303  return i18n("Example");
304  case Comment:
305  return i18n("Comment");
306  case Paraphrase:
307  return i18n("Paraphrase");
308  }
309 
310  return QString();
311 }
312 
313 int VocabularyModel::translation(int column)
314 {
315  return column / EntryColumnsMAX;
316 }
317 
318 int VocabularyModel::columnType(int column)
319 {
320  return column % EntryColumnsMAX;
321 }
322 
323 QModelIndex VocabularyModel::appendEntry(KEduVocExpression *expression)
324 {
325  if(m_document->identifierCount() == 0) {
326  KMessageBox::information(0, i18n("Please use Edit -> Languages to set up your document."), i18n("No Languages Defined"));
327  return QModelIndex();
328  }
329 
330  if (!m_lesson || !m_lesson->parent()) {
331  KMessageBox::information(0, i18n("Select a lesson before adding vocabulary."), i18n("No Lesson Selected"));
332  delete expression;
333  return QModelIndex();
334  }
335 
336  beginInsertRows(QModelIndex(), rowCount(QModelIndex()), rowCount(QModelIndex()));
337  if (!expression) {
338  expression = new KEduVocExpression;
339  }
340  m_lesson->appendEntry(expression);
341  endInsertRows();
342 
343  return index(rowCount(QModelIndex()) - 1, 0, QModelIndex());
344 }
345 
346 bool VocabularyModel::removeRows(int row, int count, const QModelIndex & parent)
347 {
348  Q_UNUSED(parent);
349  if (count < 1 || row < 0 || row + count > m_container->entryCount(m_recursive)) {
350  return false;
351  }
352 
353  int bottomRow = row + count - 1;
354  beginRemoveRows(QModelIndex(), row, bottomRow);
355 
356  for (int i = bottomRow; i >= row; i--) {
357  delete m_container->entry(i, m_recursive);
358  }
359 
360  endRemoveRows();
361  return true;
362 }
363 
364 QStringList VocabularyModel::mimeTypes() const
365 {
366  return QStringList() << "text/plain";
367 }
368 
369 QMimeData * VocabularyModel::mimeData(const QModelIndexList & indexes) const
370 {
371  VocabularyMimeData *mimeData = new VocabularyMimeData();
372  QModelIndexList sortedIndexes = indexes;
373  qSort(sortedIndexes);
374 
375  kDebug() << "mimeData for " << indexes.count() << "indexes";
376 
377  QList<KEduVocTranslation*> translations;
378  foreach (const QModelIndex &index, sortedIndexes) {
379  // only add if it's a translation. other cells like word type are being ignored for now.
380  if (columnType(index.column()) == Translation) {
381  translations.append(m_container->entry(index.row(), m_recursive)->translation(translation(index.column())));
382  }
383  }
384 
385  mimeData->setTranslations(translations);
386  return mimeData;
387 }
388 
389 void VocabularyModel::showEntriesOfSubcontainers(bool show)
390 {
391  Prefs::setShowSublessonentries(show);
392  if (show) {
393  m_recursive = KEduVocContainer::Recursive;
394  } else {
395  m_recursive = KEduVocContainer::NotRecursive;
396  }
397  reset();
398 }
399 
400 void VocabularyModel::resetLanguages()
401 {
402  // play it save - this happens seldom enough to warrant a reload
403  setDocument(m_document);
404 }
405 
406 void VocabularyModel::automaticTranslation(bool enabled)
407 {
408  kDebug() << "auto trans enabled: " << enabled;
409  Prefs::setAutomaticTranslation(true);
410 }
411 
412 #include "vocabularymodel.moc"
Editor::VocabularyModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: vocabularymodel.cpp:267
Editor::VocabularyModel::columnCount
int columnCount(const QModelIndex &) const
Definition: vocabularymodel.cpp:109
LanguageSettings::editorFont
QFont editorFont() const
Get The font used in the editor.
Definition: languagesettings.h:154
Editor::VocabularyModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: vocabularymodel.cpp:248
languagesettings.h
Prefs::useGradeColors
static bool useGradeColors()
Get Use your own colors.
Definition: prefs.h:958
Prefs::setAutomaticTranslation
static void setAutomaticTranslation(bool v)
Set Enable automatic translation of the lesson entries.
Definition: prefs.h:226
prefs.h
QObject
Editor::VocabularyModel::Example
Definition: vocabularymodel.h:40
Editor::VocabularyModel::VocabularyModel
VocabularyModel(QObject *parent=0)
Definition: vocabularymodel.cpp:35
vocabularymimedata.h
parleydocument.h
Editor::VocabularyModel::translation
static int translation(int column)
Returns which translation this column matches.
Definition: vocabularymodel.cpp:313
Editor::VocabularyModel::showContainer
void showContainer(KEduVocContainer *container)
Whatever the contents, the model will now display it.
Definition: vocabularymodel.cpp:67
Editor::VocabularyModel::Pronunciation
Definition: vocabularymodel.h:36
Editor::VocabularyModel::data
QVariant data(const QModelIndex &, int) const
Definition: vocabularymodel.cpp:117
Editor::VocabularyModel::columnType
static int columnType(int column)
Returns the type of the column specified.
Definition: vocabularymodel.cpp:318
Editor::VocabularyModel::EntryColumnsMAX
Definition: vocabularymodel.h:45
Editor::VocabularyModel::showEntriesOfSubcontainers
void showEntriesOfSubcontainers(bool show)
Show the entries of child lessons in selected lessons.
Definition: vocabularymodel.cpp:389
Editor::VocabularyMimeData
Definition: vocabularymimedata.h:27
Editor::VocabularyModel::EntryRole
Definition: vocabularymodel.h:51
QMimeData
Editor::VocabularyModel::Antonym
Definition: vocabularymodel.h:39
LanguageSettings
Definition: languagesettings.h:10
Editor::VocabularyModel::mimeTypes
QStringList mimeTypes() const
Definition: vocabularymodel.cpp:364
Editor::VocabularyModel::WordType
Definition: vocabularymodel.h:37
Editor::VocabularyModel::setDocument
void setDocument(KEduVocDocument *doc)
Definition: vocabularymodel.cpp:49
Editor::VocabularyModel::LocaleRole
Definition: vocabularymodel.h:52
Prefs::showSublessonentries
static bool showSublessonentries()
Get When enabled a lesson also shows entries from its sublessons.
Definition: prefs.h:787
Editor::VocabularyModel::mimeData
QMimeData * mimeData(const QModelIndexList &indexes) const
Definition: vocabularymodel.cpp:369
Prefs::gradeColor
static QColor gradeColor(int i)
Get Colors used to display different grades.
Definition: prefs.h:977
Editor::VocabularyModel::Translation
Definition: vocabularymodel.h:35
Editor::VocabularyModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Definition: vocabularymodel.cpp:346
Editor::VocabularyModel::lesson
KEduVocLesson * lesson()
Definition: vocabularymodel.cpp:91
Editor::VocabularyModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: vocabularymodel.cpp:212
Editor::VocabularyModel::Paraphrase
Definition: vocabularymodel.h:42
Editor::VocabularyModel::setLesson
void setLesson(KEduVocLesson *lessonContainer)
Definition: vocabularymodel.cpp:86
Editor::VocabularyModel::Synonym
Definition: vocabularymodel.h:38
Editor::VocabularyModel::automaticTranslation
void automaticTranslation(bool enabled)
Set automatic translation to enabled/disabled.
Definition: vocabularymodel.cpp:406
Editor::VocabularyModel::appendEntry
QModelIndex appendEntry(KEduVocExpression *expression=0)
Definition: vocabularymodel.cpp:323
Editor::VocabularyModel::columnTitle
static QString columnTitle(KEduVocDocument *document, int translation, int column)
Returns the name of the entryColumns column.
Definition: vocabularymodel.cpp:286
Editor::VocabularyModel::~VocabularyModel
~VocabularyModel()
Definition: vocabularymodel.cpp:45
Editor::VocabularyModel::rowCount
int rowCount(const QModelIndex &) const
Definition: vocabularymodel.cpp:96
Editor::VocabularyModel::Comment
Definition: vocabularymodel.h:41
QAbstractTableModel
vocabularymodel.h
Editor::VocabularyModel::resetLanguages
void resetLanguages()
Definition: vocabularymodel.cpp:400
Prefs::setShowSublessonentries
static void setShowSublessonentries(bool v)
Set When enabled a lesson also shows entries from its sublessons.
Definition: prefs.h:777
Editor::VocabularyMimeData::setTranslations
void setTranslations(QList< KEduVocTranslation * > translation)
Definition: vocabularymimedata.cpp:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal