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

parley

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