• 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
  • editor
vocabularydelegate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  Copyright 2006, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
4  Copyright 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
5 
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "vocabularydelegate.h"
18 #include "vocabularymodel.h"
19 #include "vocabularyfilter.h"
20 
21 #include "prefs.h"
22 #include "languagesettings.h"
23 #include "readonlycontainermodel.h"
24 
25 #include <keduvocexpression.h>
26 #include <keduvocwordtype.h>
27 #include <KPassivePopup>
28 #include <KComboBox>
29 #include <KDebug>
30 #include <KLineEdit>
31 #include <KLocale>
32 #include <QTreeView>
33 #include <QHeaderView>
34 #include <QDBusInterface>
35 #include <QKeyEvent>
36 #include <QtGui>
37 
38 using namespace Editor;
39 
40 VocabularyDelegate::VocabularyDelegate(QObject *parent)
41  : QItemDelegate(parent), m_doc(0), m_translator(0)
42 {
43 }
44 
45 QSet<QString> VocabularyDelegate::getTranslations(const QModelIndex & index) const
46 {
47  if (Prefs::automaticTranslation() == false) return QSet<QString>();
48 
49  QSet<QString> translations; //translations of this column from all the other languages
50 
51  int language = index.column() / VocabularyModel::EntryColumnsMAX;
52  QString toLanguage = m_doc->identifier(language).locale();
53 
54  //iterate through all the Translation columns
55  for (int i = 0; i < index.model()->columnCount(index.parent()); i ++) {
56  if (VocabularyModel::columnType(i) == VocabularyModel::Translation) { //translation column
57  QString fromLanguage = m_doc->identifier(VocabularyModel::translation(i)).locale();
58  QString word = index.model()->index(index.row(), i, QModelIndex()).data().toString();
59 
60  if (fromLanguage != toLanguage) {
61 // kDebug() << fromLanguage << toLanguage << word;
62  //get the word translations and add them to the translations set
63  QSet<QString> * tr = m_translator->getTranslation(word, fromLanguage, toLanguage);
64  if (tr)
65  translations.unite(* (tr));
66  }
67  }
68  }
69 
70  return translations;
71 }
72 
73 QWidget * VocabularyDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
74 {
75  Q_UNUSED(option);
76 
77  if (!index.isValid()) {
78  return 0;
79  }
80 
81  switch (VocabularyModel::columnType(index.column())) {
82  case VocabularyModel::WordClass: {
83  if (!m_doc) return 0;
84  KComboBox *wordTypeCombo = new KComboBox(parent);
85 
86  WordTypeBasicModel *basicWordTypeModel = new WordTypeBasicModel(parent);
87  wordTypeCombo->setModel(basicWordTypeModel);
88  QTreeView *view = new QTreeView(parent);
89 
90  view->setModel(basicWordTypeModel);
91  wordTypeCombo->setView(view);
92 
93  view->header()->setVisible(false);
94  view->setRootIsDecorated(true);
95 
96  basicWordTypeModel->setDocument(m_doc);
97  view->expandAll();
98 
99  kDebug() << "index data" << index.data().toString();
100  //view->setCurrentItem();
101 
102  return wordTypeCombo;
103  }
104 
105  case VocabularyModel::Translation: {
106  if (!m_doc || !m_translator) return 0;
107 
108  if (VocabularyModel::columnType(index.column()) == VocabularyModel::Translation) {
109  //get the translations of this word (fetch only with the help of scripts, if enabled)
110  QSet<QString> translations = getTranslations(index);
111 
112  //create combo box
113  //if there is only one word and that is the suggestion word (in translations) then don't create the combobox
114  if (!translations.isEmpty() && !(translations.size() == 1 && (*translations.begin()) == index.model()->data(index, Qt::DisplayRole).toString())) {
115  KComboBox *translationCombo = new KComboBox(parent);
116  translationCombo->setFrame(false);
117  translationCombo->addItems(translations.toList());
118  translationCombo->setEditable(true);
119  translationCombo->setFont(index.model()->data(index, Qt::FontRole).value<QFont>());
120  translationCombo->setEditText(index.model()->data(index, Qt::DisplayRole).toString());
121  translationCombo->completionObject()->setItems(translations.toList());
122  return translationCombo;
123  }
124  }
125  // no break - we fall back to a line edit if there are not multiple translations fetched online
126  }
127  default: {
128  KLineEdit *editor = new KLineEdit(parent);
129  editor->setFrame(false);
130  editor->setFont(index.model()->data(index, Qt::FontRole).value<QFont>());
131  editor->setText(index.model()->data(index, Qt::DisplayRole).toString());
132 
133  QString locale = index.model()->data(index, VocabularyModel::LocaleRole).toString();
134  if (!locale.isEmpty()) {
135  LanguageSettings settings(locale);
136  settings.readConfig();
137  QString layout = settings.keyboardLayout();
138  if (!layout.isEmpty()) {
139  QDBusInterface kxkb("org.kde.keyboard", "/Layouts", "org.kde.KeyboardLayouts");
140  if (kxkb.isValid()) {
141  kxkb.call("setLayout", layout);
142  }
143  }
144  }
145  return editor;
146  }
147  }
148 }
149 
150 bool VocabularyDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view,
151  const QStyleOptionViewItem &option, const QModelIndex &index)
152 {
153  Q_UNUSED(view);
154 
155  if (event->type() == QEvent::ToolTip) {
156  QPainterPath audioPainterPath;
157  QPainterPath imagePainterPath;
158  audioPainterPath.addPolygon(audioPolygon(option));
159  imagePainterPath.addPolygon(imagePolygon(option));
160 
161  int column = columnType(index.column());
162 
163  if (audioPainterPath.contains(event->pos()) && hasAudio(index) && (column == Translation || column == Pronunciation)) {
164  QToolTip::showText(event->globalPos(), i18n("Sound file selected: %1", audioUrl(index)));
165  }
166  else if (imagePainterPath.contains(event->pos()) && hasImage(index) && (column == Translation || column == Pronunciation)) {
167  QToolTip::showText(event->globalPos(), i18n("Image file selected: %1", imageUrl(index)));
168  }
169  else {
170  QToolTip::hideText();
171  event->ignore();
172  }
173  return true;
174  }
175  return false;
176 }
177 
178 QPolygon VocabularyDelegate::audioPolygon(const QStyleOptionViewItem &option) const
179 {
180  QRect rect = option.rect;
181  QPolygon polygon;
182  polygon << QPoint(rect.x() + rect.width() - 10, rect.y());
183  polygon << QPoint(rect.x() + rect.width(), rect.y());
184  polygon << QPoint(rect.x() + rect.width(), rect.y() + 10);
185  return polygon;
186 }
187 
188 QPolygon VocabularyDelegate::imagePolygon(const QStyleOptionViewItem &option) const
189 {
190  QRect rect = option.rect;
191  QPolygon polygon;
192  polygon << QPoint(rect.x() + rect.width() - 10, rect.y() + rect.height());
193  polygon << QPoint(rect.x() + rect.width(), rect.y() + rect.height());
194  polygon << QPoint(rect.x() + rect.width(), rect.y() + rect.height() - 10);
195  return polygon;
196 }
197 
198 bool VocabularyDelegate::hasAudio(const QModelIndex &index) const
199 {
200  return !audioUrl(index).isEmpty();
201 }
202 
203 bool VocabularyDelegate::hasImage(const QModelIndex &index) const
204 {
205  return !imageUrl(index).isEmpty();
206 }
207 
208 QString VocabularyDelegate::audioUrl(const QModelIndex &index) const
209 {
210  QVariant audioVar = index.data(VocabularyModel::AudioRole);
211  QString audioUrl = audioVar.toString();
212  return audioUrl;
213 }
214 
215 QString VocabularyDelegate::imageUrl(const QModelIndex &index) const
216 {
217  QVariant imageVar = index.data(VocabularyModel::ImageRole);
218  QString imageUrl = imageVar.toString();
219  return imageUrl;
220 }
221 
222 int VocabularyDelegate::columnType(int column)
223 {
224  return column % EntryColumnsMAX;
225 }
226 
227 void VocabularyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
228 {
229  QItemDelegate::paint(painter, option, index);
230  painter->save();
231 
232  int column = columnType(index.column());
233 
234  if (hasAudio(index) == true && (column == Translation || column == Pronunciation)) {
235  painter->setPen(QPen(Qt::red));
236  painter->setBrush(QBrush(Qt::red));
237  painter->drawPolygon(audioPolygon(option));
238  }
239  if (hasImage(index) == true && (column == Translation || column == Pronunciation)) {
240  painter->setPen(QPen(Qt::blue));
241  painter->setBrush(QBrush(Qt::blue));
242  painter->drawPolygon(imagePolygon(option));
243  }
244  painter->restore();
245 }
246 
247 void VocabularyDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
248 {
249  if (!index.isValid()) {
250  return;
251  }
252 
253  switch (VocabularyModel::columnType(index.column())) {
254  case (VocabularyModel::Translation) : {
255  QString value = index.model()->data(index, Qt::DisplayRole).toString();
256  KComboBox * translationCombo = qobject_cast<KComboBox*> (editor);
257  if (translationCombo) {
258  translationCombo->setEditText(value);
259  if (value.isEmpty()) {
260  // show the translations that were fetched as popup
261  translationCombo->showPopup();
262  }
263  break;
264  }
265  }
266  default: {
267  QString value = index.model()->data(index, Qt::DisplayRole).toString();
268 
269  KLineEdit *lineEdit = qobject_cast<KLineEdit*> (editor);
270  if (lineEdit) {
271  lineEdit->setText(value);
272  }
273  }
274  }
275 }
276 
277 void VocabularyDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
278 {
279  if (!index.isValid()) {
280  return;
281  }
282 
283  switch (VocabularyModel::columnType(index.column())) {
284  case (VocabularyModel::WordClass) : {
285  kDebug() << "word type editor";
286  KComboBox *combo = qobject_cast<KComboBox*> (editor);
287  if (!combo) {
288  return;
289  }
290  kDebug() << "combo" << combo->currentText();
291  QModelIndex comboIndex = combo->view()->currentIndex();
292  KEduVocWordType* wordType = static_cast<KEduVocWordType*>(comboIndex.internalPointer());
293 
294  // the root is the same as no word type
295  if (wordType && wordType->parent() == 0) {
296  wordType = 0;
297  }
298 
299  VocabularyFilter *filter = qobject_cast<VocabularyFilter*> (model);
300  VocabularyModel *vocModel = qobject_cast<VocabularyModel*> ((filter)->sourceModel());
301  Q_ASSERT(vocModel);
302  QVariant data = vocModel->data(filter->mapToSource(index), VocabularyModel::EntryRole);
303 
304  KEduVocExpression *expression = data.value<KEduVocExpression*>();
305  Q_ASSERT(expression);
306  int translationId = VocabularyModel::translation(index.column());
307 
308  expression->translation(translationId)->setWordType(wordType);
309 
310  }
311  case (VocabularyModel::Translation) : {
312  KComboBox * translationCombo = qobject_cast<KComboBox*> (editor);
313  if (translationCombo) {
314  model->setData(index, translationCombo->currentText());
315  break;
316  }
317  }
318  default: {
319  KLineEdit *lineEdit = qobject_cast<KLineEdit*> (editor);
320  if (lineEdit) {
321  model->setData(index, lineEdit->text());
322  }
323  }
324  }
325 }
326 
327 void VocabularyDelegate::setDocument(KEduVocDocument * doc)
328 {
329  m_doc = doc;
330 }
331 
332 /*
333 QPair< QString, QString > VocabularyDelegate::guessWordType(const QString & entry, int language) const
334 {
335  kDebug() << "guessing word type for: " << entry;
336 
337  QString article = entry.section(" ", 0, 0);
338  if ( article.length() < entry.length() ) {
339  if ( article == ->identifier(language).articles().article(KEduVocWordFlag::Singular| KEduVocWordFlag::Definite| KEduVocWordFlag::Masculine) ) {
340  kDebug() << "Noun masculine";
341  return qMakePair(m_doc->wordTypes().specialTypeNoun(), m_doc->wordTypes().specialTypeNounMale());
342  }
343 
344  }
345  return qMakePair(QString(), QString());
346 }
347 */
348 
349 
350 VocabularyDelegate::WordTypeBasicModel::WordTypeBasicModel(QObject * parent)
351  : ReadonlyContainerModel(KEduVocContainer::WordType, parent)
352 {
353 }
354 
355 KEduVocContainer * VocabularyDelegate::WordTypeBasicModel::rootContainer() const
356 {
357  if (!m_doc) {
358  return 0;
359  }
360  return m_doc->wordTypeContainer();
361 }
362 
367 void VocabularyDelegate::setTranslator(Translator* translator)
368 {
369  m_translator = translator;
370 }
371 
372 #include "vocabularydelegate.moc"
ReadonlyContainerModel
Model for the tree of containers (lessons, word types).
Definition: readonlycontainermodel.h:33
QModelIndex
QWidget
QDBusAbstractInterface::isValid
bool isValid() const
QEvent::type
Type type() const
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
Editor::VocabularyDelegate::setEditorData
void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:247
QAbstractItemView
Editor::VocabularyDelegate::Pronunciation
Definition: vocabularydelegate.h:37
languagesettings.h
Editor::VocabularyDelegate::EntryColumnsMAX
Definition: vocabularydelegate.h:46
VocabularyFilter
Definition: vocabularyfilter.h:26
QSet::size
int size() const
Editor::VocabularyDelegate::hasImage
bool hasImage(const QModelIndex &index) const
Definition: vocabularydelegate.cpp:203
QFont
VocabularyModel::WordClass
Definition: vocabularymodel.h:36
QPainterPath::contains
bool contains(const QPointF &point) const
Editor::VocabularyDelegate::setDocument
void setDocument(KEduVocDocument *doc)
Definition: vocabularydelegate.cpp:327
Editor::VocabularyDelegate::WordTypeBasicModel::rootContainer
KEduVocContainer * rootContainer() const
Definition: vocabularydelegate.cpp:355
Editor::VocabularyDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:227
VocabularyModel::Translation
Definition: vocabularymodel.h:34
QWidget::setVisible
virtual void setVisible(bool visible)
Editor::VocabularyDelegate::setModelData
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:277
QPainter::save
void save()
QHelpEvent::pos
const QPoint & pos() const
QVariant::value
T value() const
QPainter::drawPolygon
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
QRect::height
int height() const
QBrush
QRect::x
int x() const
QRect::y
int y() const
QItemDelegate::paint
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QPoint
prefs.h
QToolTip::showText
void showText(const QPoint &pos, const QString &text, QWidget *w)
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QDBusAbstractInterface::call
QDBusMessage call(const QString &method, const QVariant &arg1, const QVariant &arg2, const QVariant &arg3, const QVariant &arg4, const QVariant &arg5, const QVariant &arg6, const QVariant &arg7, const QVariant &arg8)
Editor::VocabularyDelegate::imageUrl
QString imageUrl(const QModelIndex &index) const
Definition: vocabularydelegate.cpp:215
QPolygon
QRect
QModelIndex::isValid
bool isValid() const
QPainterPath::addPolygon
void addPolygon(const QPolygonF &polygon)
VocabularyModel::columnType
static int columnType(int column)
Returns the type of the column specified.
Definition: vocabularymodel.cpp:315
Editor::VocabularyDelegate::audioUrl
QString audioUrl(const QModelIndex &index) const
Definition: vocabularydelegate.cpp:208
QStyleOptionViewItem
QObject
QPainter::setPen
void setPen(const QColor &color)
QHelpEvent::globalPos
const QPoint & globalPos() const
Editor::VocabularyDelegate::WordTypeBasicModel::WordTypeBasicModel
WordTypeBasicModel(QObject *parent=0)
Definition: vocabularydelegate.cpp:350
QPainter
QString::isEmpty
bool isEmpty() const
QModelIndex::row
int row() const
QItemDelegate
QPainter::setBrush
void setBrush(const QBrush &brush)
QModelIndex::internalPointer
void * internalPointer() const
vocabularydelegate.h
Editor::VocabularyDelegate::Translation
Definition: vocabularydelegate.h:36
QSet
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
VocabularyModel::EntryRole
Definition: vocabularymodel.h:50
QString
QModelIndex::parent
QModelIndex parent() const
VocabularyModel::LocaleRole
Definition: vocabularymodel.h:51
readonlycontainermodel.h
QDBusInterface
LanguageSettings
Definition: languagesettings.h:10
VocabularyModel
Definition: vocabularymodel.h:29
QSet::begin
iterator begin()
QToolTip::hideText
void hideText()
QTreeView::expandAll
void expandAll()
Translator
Keeps the translated words.
Definition: translator.h:74
QPainter::restore
void restore()
Editor::VocabularyDelegate::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:73
QSortFilterProxyModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
QPainterPath
QRect::width
int width() const
QModelIndex::model
const QAbstractItemModel * model() const
QSet::unite
QSet< T > & unite(const QSet< T > &other)
QModelIndex::data
QVariant data(int role) const
QTreeView
Editor::VocabularyDelegate::hasAudio
bool hasAudio(const QModelIndex &index) const
Definition: vocabularydelegate.cpp:198
Prefs::automaticTranslation
static bool automaticTranslation()
Get Enable automatic translation of the unit entries.
Definition: prefs.h:241
QTreeView::setModel
virtual void setModel(QAbstractItemModel *model)
QAbstractItemModel::columnCount
virtual int columnCount(const QModelIndex &parent) const =0
VocabularyModel::translation
static int translation(int column)
Returns which translation this column matches.
Definition: vocabularymodel.cpp:310
ReadonlyContainerModel::setDocument
void setDocument(KEduVocDocument *doc)
Set the new source kvtml file.
Definition: readonlycontainermodel.cpp:43
QModelIndex::column
int column() const
VocabularyModel::ImageRole
Definition: vocabularymodel.h:53
VocabularyModel::AudioRole
Definition: vocabularymodel.h:52
QSet::isEmpty
bool isEmpty() const
QAbstractItemModel
QAbstractItemModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
QPen
QSet::toList
QList< T > toList() const
QTreeView::header
QHeaderView * header() const
Editor::VocabularyDelegate::setTranslator
void setTranslator(Translator *translator)
Sets the member variable m_translator to a Translator object.
Definition: vocabularydelegate.cpp:367
VocabularyModel::EntryColumnsMAX
Definition: vocabularymodel.h:44
Editor::VocabularyDelegate::VocabularyDelegate
VocabularyDelegate(QObject *parent=0)
Definition: vocabularydelegate.cpp:40
QTreeView::setRootIsDecorated
void setRootIsDecorated(bool show)
Translator::getTranslation
QSet< QString > * getTranslation(QString word, QString fromLanguage, QString toLanguage)
Returns a QStringList with all the translations of word from fromLanguage to toLanguage.
Definition: translator.cpp:45
vocabularyfilter.h
QHelpEvent
Editor::VocabularyDelegate::imagePolygon
QPolygon imagePolygon(const QStyleOptionViewItem &option) const
Definition: vocabularydelegate.cpp:188
QVariant::toString
QString toString() const
Editor::VocabularyDelegate::audioPolygon
QPolygon audioPolygon(const QStyleOptionViewItem &option) const
Definition: vocabularydelegate.cpp:178
Editor::VocabularyDelegate::helpEvent
bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
Definition: vocabularydelegate.cpp:150
LanguageSettings::keyboardLayout
QString keyboardLayout() const
Get Keyboard layout for this locale.
Definition: languagesettings.h:29
vocabularymodel.h
Editor::VocabularyDelegate::columnType
static int columnType(int column)
Definition: vocabularydelegate.cpp:222
Editor::VocabularyDelegate::WordTypeBasicModel
Definition: vocabularydelegate.h:82
QVariant
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