• 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
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 "basiccontainermodel.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 
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  {
57  if ( VocabularyModel::columnType ( i ) == VocabularyModel::Translation ) //translation column
58  {
59  QString fromLanguage = m_doc->identifier ( VocabularyModel::translation ( i ) ).locale();
60  QString word = index.model()->index ( index.row(),i,QModelIndex() ).data().toString();
61 
62  if ( fromLanguage != toLanguage )
63  {
64 // kDebug() << fromLanguage << toLanguage << word;
65  //get the word translations and add them to the translations set
66  QSet<QString> * tr = m_translator->getTranslation ( word,fromLanguage,toLanguage );
67  if ( tr )
68  translations.unite ( * ( tr ) );
69  }
70  }
71  }
72 
73  return translations;
74 }
75 
76 QWidget * VocabularyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
77 {
78  Q_UNUSED ( option );
79 
80  if ( !index.isValid() )
81  {
82  return 0;
83  }
84 
85  switch ( VocabularyModel::columnType ( index.column() ) )
86  {
87  case VocabularyModel::WordType:
88  {
89  if ( !m_doc ) return 0;
90  KComboBox *wordTypeCombo = new KComboBox ( parent );
91 
92  WordTypeBasicModel *basicWordTypeModel = new WordTypeBasicModel ( parent );
93  wordTypeCombo->setModel ( basicWordTypeModel );
94  QTreeView *view = new QTreeView ( parent );
95 
96  view->setModel ( basicWordTypeModel );
97  wordTypeCombo->setView ( view );
98 
99  view->header()->setVisible ( false );
100  view->setRootIsDecorated ( true );
101 
102  basicWordTypeModel->setDocument ( m_doc );
103  view->expandAll();
104 
105  kDebug() << "index data" << index.data().toString();
106  //view->setCurrentItem();
107 
108  return wordTypeCombo;
109  }
110 
111  case VocabularyModel::Translation:
112  {
113  if ( !m_doc || !m_translator ) return 0;
114 
115  if ( VocabularyModel::columnType ( index.column() ) == VocabularyModel::Translation )
116  {
117  //get the translations of this word (fetch only with the help of scripts, if enabled)
118  QSet<QString> translations = getTranslations ( index );
119 
120  //create combo box
121  //if there is only one word and that is the suggestion word (in translations) then don't create the combobox
122  if ( !translations.isEmpty() && ! ( translations.size() == 1 && ( *translations.begin() ) == index.model()->data ( index, Qt::DisplayRole ).toString() ) )
123  {
124  KComboBox *translationCombo = new KComboBox ( parent );
125  translationCombo->setFrame ( false );
126  translationCombo->addItems ( translations.toList() );
127  translationCombo->setEditable ( true );
128  translationCombo->setFont ( index.model()->data ( index, Qt::FontRole ).value<QFont>() );
129  translationCombo->setEditText ( index.model()->data ( index, Qt::DisplayRole ).toString() );
130  translationCombo->completionObject()->setItems ( translations.toList() );
131  return translationCombo;
132  }
133  }
134  // no break - we fall back to a line edit if there are not multiple translations fetched online
135  }
136  default:
137  {
138  KLineEdit *editor = new KLineEdit ( parent );
139  editor->setFrame ( false );
140  editor->setFont ( index.model()->data ( index, Qt::FontRole ).value<QFont>() );
141  editor->setText ( index.model()->data ( index, Qt::DisplayRole ).toString() );
142 
143  QString locale = index.model()->data ( index, VocabularyModel::LocaleRole ).toString();
144  if ( !locale.isEmpty() )
145  {
146  LanguageSettings settings ( locale );
147  settings.readConfig();
148  QString layout = settings.keyboardLayout();
149  if ( !layout.isEmpty() )
150  {
151  QDBusInterface kxkb ( "org.kde.keyboard", "/Layouts", "org.kde.KeyboardLayouts" );
152  if ( kxkb.isValid() )
153  {
154  kxkb.call ( "setLayout", layout );
155  }
156  }
157  }
158  return editor;
159  }
160  }
161 }
162 
163 void VocabularyDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
164 {
165  if ( !index.isValid() ) {
166  return;
167  }
168 
169  switch ( VocabularyModel::columnType ( index.column() ) )
170  {
171  case ( VocabularyModel::Translation ) :
172  {
173  QString value = index.model()->data ( index, Qt::DisplayRole ).toString();
174  KComboBox * translationCombo = qobject_cast<KComboBox*> ( editor );
175  if ( translationCombo )
176  {
177  translationCombo->setEditText ( value );
178  if (value.isEmpty()) {
179  // show the translations that were fetched as popup
180  translationCombo->showPopup();
181  }
182  break;
183  }
184  }
185  default:
186  {
187  QString value = index.model()->data ( index, Qt::DisplayRole ).toString();
188 
189  KLineEdit *lineEdit = qobject_cast<KLineEdit*> ( editor );
190  if ( lineEdit ) {
191  lineEdit->setText ( value );
192  }
193  }
194  }
195 }
196 
197 void VocabularyDelegate::setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
198 {
199  if ( !index.isValid() ) {
200  return;
201  }
202 
203  switch ( VocabularyModel::columnType ( index.column() ) )
204  {
205  case ( VocabularyModel::WordType ) :
206  {
207  kDebug() << "word type editor";
208  KComboBox *combo = qobject_cast<KComboBox*> ( editor );
209  if ( !combo ) {
210  return;
211  }
212  kDebug() << "combo" << combo->currentText();
213  QModelIndex comboIndex = combo->view()->currentIndex();
214  KEduVocWordType* wordType = static_cast<KEduVocWordType*> ( comboIndex.internalPointer() );
215 
216  // the root is the same as no word type
217  if ( wordType && wordType->parent() == 0 )
218  {
219  wordType = 0;
220  }
221 
222  VocabularyFilter *filter = qobject_cast<VocabularyFilter*> ( model );
223  VocabularyModel *vocModel = qobject_cast<VocabularyModel*> ( ( filter )->sourceModel() );
224  Q_ASSERT ( vocModel );
225  QVariant data = vocModel->data ( filter->mapToSource ( index ), VocabularyModel::EntryRole );
226 
227  KEduVocExpression *expression = data.value<KEduVocExpression*>();
228  Q_ASSERT ( expression );
229  int translationId = VocabularyModel::translation ( index.column() );
230 
231  expression->translation ( translationId )->setWordType ( wordType );
232 
233  }
234  case ( VocabularyModel::Translation ) :
235  {
236  KComboBox * translationCombo = qobject_cast<KComboBox*> ( editor );
237  if ( translationCombo )
238  {
239  model->setData ( index,translationCombo->currentText() );
240  break;
241  }
242  }
243  default:
244  {
245  KLineEdit *lineEdit = qobject_cast<KLineEdit*> ( editor );
246  if ( lineEdit )
247  {
248  model->setData ( index, lineEdit->text() );
249  }
250  }
251  }
252 }
253 
254 void VocabularyDelegate::setDocument(KEduVocDocument * doc)
255 {
256  m_doc = doc;
257 }
258 
259 /*
260 QPair< QString, QString > VocabularyDelegate::guessWordType(const QString & entry, int language) const
261 {
262  kDebug() << "guessing word type for: " << entry;
263 
264  QString article = entry.section(" ", 0, 0);
265  if ( article.length() < entry.length() ) {
266  if ( article == ->identifier(language).articles().article(KEduVocWordFlag::Singular| KEduVocWordFlag::Definite| KEduVocWordFlag::Masculine) ) {
267  kDebug() << "Noun masculine";
268  return qMakePair(m_doc->wordTypes().specialTypeNoun(), m_doc->wordTypes().specialTypeNounMale());
269  }
270 
271  }
272  return qMakePair(QString(), QString());
273 }
274 */
275 
276 
277 VocabularyDelegate::WordTypeBasicModel::WordTypeBasicModel ( QObject * parent )
278  :BasicContainerModel ( KEduVocContainer::WordType, parent )
279 {
280 }
281 
282 KEduVocContainer * VocabularyDelegate::WordTypeBasicModel::rootContainer() const
283 {
284  if ( !m_doc ) {
285  return 0;
286  }
287  return m_doc->wordTypeContainer();
288 }
289 
294 void VocabularyDelegate::setTranslator ( Translator* translator )
295 {
296  m_translator = translator;
297 }
298 
299 #include "vocabularydelegate.moc"
Editor::BasicContainerModel
Definition: basiccontainermodel.h:34
Editor::VocabularyDelegate::setEditorData
void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:163
languagesettings.h
Editor::VocabularyModel
Definition: vocabularymodel.h:30
Editor::VocabularyDelegate::setDocument
void setDocument(KEduVocDocument *doc)
Definition: vocabularydelegate.cpp:254
QWidget
Editor::VocabularyDelegate::WordTypeBasicModel::rootContainer
KEduVocContainer * rootContainer() const
Definition: vocabularydelegate.cpp:282
Editor::VocabularyDelegate::setModelData
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:197
prefs.h
QObject
Editor::VocabularyFilter
Definition: vocabularyfilter.h:27
Editor::VocabularyModel::translation
static int translation(int column)
Returns which translation this column matches.
Definition: vocabularymodel.cpp:313
QTreeView
Editor::VocabularyDelegate::WordTypeBasicModel::WordTypeBasicModel
WordTypeBasicModel(QObject *parent=0)
Definition: vocabularydelegate.cpp:277
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
vocabularydelegate.h
Editor::VocabularyModel::EntryRole
Definition: vocabularymodel.h:51
LanguageSettings
Definition: languagesettings.h:10
QAbstractItemModel
Editor::VocabularyModel::WordType
Definition: vocabularymodel.h:37
Translator
Keeps the translated words.
Definition: translator.h:74
Editor::VocabularyModel::LocaleRole
Definition: vocabularymodel.h:52
Editor::VocabularyDelegate::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: vocabularydelegate.cpp:76
QItemDelegate
Prefs::automaticTranslation
static bool automaticTranslation()
Get Enable automatic translation of the lesson entries.
Definition: prefs.h:236
Editor::VocabularyModel::Translation
Definition: vocabularymodel.h:35
Editor::VocabularyDelegate::setTranslator
void setTranslator(Translator *translator)
Sets the member variable m_translator to a Translator object.
Definition: vocabularydelegate.cpp:294
Editor::VocabularyDelegate::VocabularyDelegate
VocabularyDelegate(QObject *parent=0)
Definition: vocabularydelegate.cpp:40
Editor::BasicContainerModel::setDocument
void setDocument(KEduVocDocument *doc)
Set the new source kvtml file.
Definition: basiccontainermodel.cpp:44
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
basiccontainermodel.h
LanguageSettings::keyboardLayout
QString keyboardLayout() const
Get Keyboard layout for this locale.
Definition: languagesettings.h:29
vocabularymodel.h
Editor::VocabularyDelegate::WordTypeBasicModel
Definition: vocabularydelegate.h:56
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