[Parley-devel] KDE/kdeedu/parley/src

Frederik Gladhorn frederik.gladhorn at gmx.de
Sun May 18 14:59:14 CEST 2008


SVN commit 809160 by gladhorn:

Split containermodel into a base class that is read only and the editable version.
Use the read only one column model to create a combobox in the delegate of the vocab table.
Now word type selection via combo box is possible again.
Add a few ugly casts.
CCMAIL: parley-devel at kde.org


 M  +1 -0      CMakeLists.txt  
 A             vocabulary/basiccontainermodel.cpp   [License: GPL (v2+)]
 A             vocabulary/basiccontainermodel.h   vocabulary/containermodel.h#808764 [License: GPL (v2+)]
 M  +4 -125    vocabulary/containermodel.cpp  
 M  +3 -22     vocabulary/containermodel.h  
 M  +89 -33    vocabulary/vocabularydelegate.cpp  
 M  +6 -7      vocabulary/vocabularydelegate.h  
 M  +8 -1      vocabulary/vocabularymodel.cpp  
 M  +1 -0      vocabulary/vocabularyview.cpp  


--- trunk/KDE/kdeedu/parley/src/CMakeLists.txt #809159:809160
@@ -42,6 +42,7 @@
     vocabulary/vocabularyfilter.cpp
     vocabulary/vocabularymimedata.cpp
     vocabulary/containermodel.cpp
+    vocabulary/basiccontainermodel.cpp
     vocabulary/lessonmodel.cpp
     vocabulary/wordtypemodel.cpp
     vocabulary/containerview.cpp
--- trunk/KDE/kdeedu/parley/src/vocabulary/containermodel.cpp #809159:809160
@@ -23,18 +23,15 @@
 #include <keduvocwordtype.h>
 #include <keduvocexpression.h>
 
-#include <KRandom>
-#include <KIcon>
-#include <kdebug.h>
-#include <klocale.h>
-#include <QItemSelection>
+#include <KDebug>
+#include <KLocalizedString>
 
 /** @file
   * Implementation of ContainerModel.
   * Functions to create the model from the lessons of the vocabulary document.
   */
 
-ContainerModel::ContainerModel(KEduVocLesson::EnumContainerType type, QObject * parent) : QAbstractItemModel(parent)
+ContainerModel::ContainerModel(KEduVocContainer::EnumContainerType type, QObject * parent) : BasicContainerModel(type, parent)
 {
     m_type = type;
     m_doc = 0;
@@ -42,102 +39,6 @@
     setSupportedDragActions(Qt::CopyAction | Qt::MoveAction);
 }
 
-void ContainerModel::setDocument(KEduVocDocument * doc)
-{
-    // cleanup old document
-    if (rowCount(QModelIndex()) > 0) {
-        beginRemoveRows(QModelIndex(), 0, 0);
-        m_doc = 0;
-        endRemoveRows();
-    }
-
-    if (doc) {
-        beginInsertRows(QModelIndex(), 0, 0);
-        m_doc = doc;
-        endInsertRows();
-    }
-}
-
-
-QModelIndex ContainerModel::index(int row, int column, const QModelIndex &parent) const
-{
-    if (!m_doc || !hasIndex(row, column, parent)) {
-        return QModelIndex();
-    }
-
-    KEduVocContainer *parentLesson;
-
-    if (!parent.isValid()) {
-        parentLesson = 0;
-    } else {
-        parentLesson = static_cast<KEduVocContainer*>(parent.internalPointer());
-    }
-
-    KEduVocContainer *childLesson;
-    if (!parentLesson) {
-        childLesson = rootContainer();
-    } else {
-        childLesson = parentLesson->childContainer(row);
-    }
-    return createIndex(row, column, childLesson);
-}
-
-
-QModelIndex ContainerModel::index(KEduVocContainer * container) const
-{
-    if(!container) {
-        return QModelIndex();
-    }
-
-    QModelIndex currentIndex = index(container->row(), 0, index(container->parent()));
-    Q_ASSERT(container == currentIndex.internalPointer());
-
-    return currentIndex;
-}
-
-
-QModelIndex ContainerModel::parent(const QModelIndex &index) const
-{
-    if (!index.isValid()) {
-        return QModelIndex();
-    }
-
-    KEduVocContainer *childItem = static_cast<KEduVocContainer*>(index.internalPointer());
-    if (!childItem) {
-        return QModelIndex();
-    }
-
-    KEduVocContainer *parentItem = childItem->parent();
-
-    if (!parentItem) {
-        return QModelIndex();
-    }
-
-    QModelIndex parentIndex = createIndex(parentItem->row(), 0, parentItem);
-    return parentIndex;
-}
-
-
-int ContainerModel::rowCount(const QModelIndex &parent) const
-{
-    if (parent.column() > 0) {
-        return 0;
-    }
-
-    KEduVocContainer *parentItem;
-    if (!parent.isValid()) {
-        // root element
-        if (!m_doc) {
-            return 0;
-        }
-        return 1;
-    } else {
-        parentItem =  static_cast<KEduVocContainer*>(parent.internalPointer());
-        return parentItem->childContainerCount();
-    }
-}
-
-
 QModelIndex ContainerModel::appendContainer(const QModelIndex& parent, const QString & containerName)
 {
     KEduVocContainer* parentContainer;
@@ -246,7 +147,7 @@
         // the root element, not editable for now
         if (index.parent() == QModelIndex()) {
             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable
-                        | Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled );
+                        | Qt::ItemIsUserCheckable );
         }
         // the name column
         if ( index.column() == 0 ) {
@@ -311,12 +212,6 @@
     endRemoveRows();
 }
 
-
-KEduVocContainer::EnumContainerType ContainerModel::containerType()
-{
-    return m_type;
-}
-
 Qt::DropActions ContainerModel::supportedDropActions() const
 {
     return Qt::MoveAction | Qt::CopyAction;
@@ -490,21 +385,5 @@
 
 */
 
-KEduVocContainer * ContainerModel::rootContainer() const
-{
-    if (!m_doc) {
-        return 0;
-    }
 
-    switch (m_type) {
-        case KEduVocContainer::Lesson:
-            return m_doc->lesson();
-        case KEduVocContainer::WordType:
-            return m_doc->wordTypeContainer();
-    }
-
-    return 0;
-}
-
-
 #include "containermodel.moc"
--- trunk/KDE/kdeedu/parley/src/vocabulary/containermodel.h #809159:809160
@@ -16,7 +16,7 @@
 #ifndef CONTAINERMODEL_H
 #define CONTAINERMODEL_H
 
-#include "containermodel.h"
+#include "basiccontainermodel.h"
 
 #include <QAbstractItemModel>
 #include <QModelIndex>
@@ -29,28 +29,19 @@
 /**
   * Model for the tree of containers (lessons, word types).
   */
-class ContainerModel : public QAbstractItemModel
+class ContainerModel : public BasicContainerModel
 {
     Q_OBJECT
 
 public:
-    explicit ContainerModel(KEduVocLesson::EnumContainerType type, QObject *parent = 0);
+    explicit ContainerModel(KEduVocContainer::EnumContainerType type, QObject *parent = 0);
 
     virtual QVariant data(const QModelIndex &index, int role) const;
     virtual Qt::ItemFlags flags(const QModelIndex &index) const;
     virtual QVariant headerData(int section, Qt::Orientation orientation,
                         int role = Qt::DisplayRole) const;
-    QModelIndex index(int row, int column,
-                    const QModelIndex &parent = QModelIndex()) const;
-
-    QModelIndex index(KEduVocContainer* container) const;
-
-    QModelIndex parent(const QModelIndex &index) const;
-    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
 
-    KEduVocContainer::EnumContainerType containerType();
-
     Qt::DropActions supportedDropActions () const;
     QStringList mimeTypes() const;
     QMimeData * mimeData(const QModelIndexList &indexes) const;
@@ -69,21 +60,11 @@
 
     void deleteContainer(const QModelIndex& containerIndex);
 
-public slots:
-    /** Set the new source kvtml file
-     * @param doc the new file */
-    void setDocument(KEduVocDocument *doc);
-
 signals:
     /**
      * emitted when the inPractice state or name of a lesson changed.
      */
     void documentModified();
-
-protected:
-    KEduVocContainer *rootContainer() const;
-    KEduVocContainer::EnumContainerType m_type;
-    KEduVocDocument *m_doc;
 };
 
 
--- trunk/KDE/kdeedu/parley/src/vocabulary/vocabularydelegate.cpp #809159:809160
@@ -16,25 +16,27 @@
 
 #include "vocabularydelegate.h"
 #include "vocabularymodel.h"
+#include "vocabularyfilter.h"
 
 #include "prefs.h"
 #include "languagesettings.h"
+#include "basiccontainermodel.h"
 
 #include <keduvocexpression.h>
-
+#include <keduvocwordtype.h>
 #include <KPassivePopup>
 #include <KComboBox>
 #include <KDebug>
-#include <KGlobalSettings>
 #include <KLineEdit>
 #include <KLocale>
-#include <KIconLoader>
-#include <KIcon>
-#include <QPainter>
+#include <QTreeView>
+#include <QHeaderView>
 #include <QDBusInterface>
 
 VocabularyDelegate::VocabularyDelegate(QObject *parent) : QItemDelegate(parent)
-{}
+{
+    m_doc = 0;
+}
 
 QWidget * VocabularyDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
 {
@@ -44,55 +46,104 @@
         return 0;
     }
 
-    KLineEdit *editor = new KLineEdit(parent);
-    editor->setFrame(false);
-    editor->setFont(index.model()->data(index, Qt::FontRole).value<QFont>());
-    editor->setText(index.model()->data(index, Qt::DisplayRole).toString());
+    switch (VocabularyModel::columnType(index.column())) {
+    case VocabularyModel::WordType: {
+        if (!m_doc) return 0;
+        KComboBox *wordTypeCombo = new KComboBox(parent);
 
-    QString locale = index.model()->data(index, VocabularyModel::LocaleRole).toString();
-    if(!locale.isEmpty()) {
-        LanguageSettings settings(locale);
-        settings.readConfig();
-        QString layout = settings.keyboardLayout();
-        if(!layout.isEmpty()) {
-            QDBusInterface kxkb( "org.kde.kxkb", "/kxkb", "org.kde.KXKB" );
-            if (kxkb.isValid()) {
-            kxkb.call( "setLayout", layout );
+        BasicContainerModel *basicWordTypeModel = new BasicContainerModel(KEduVocContainer::WordType, parent);
+        wordTypeCombo->setModel(basicWordTypeModel);
+        QTreeView *view = new QTreeView(parent);
+
+        view->setModel(basicWordTypeModel);
+        wordTypeCombo->setView(view);
+
+        view->header()->setVisible(false);
+        view->setRootIsDecorated(true);
+
+        basicWordTypeModel->setDocument(m_doc);
+        view->expandAll();
+
+        kDebug() << "index data" << index.data().toString();
+        //view->setCurrentItem();
+
+        return wordTypeCombo;
+    }
+
+    default: {
+
+        KLineEdit *editor = new KLineEdit(parent);
+        editor->setFrame(false);
+        editor->setFont(index.model()->data(index, Qt::FontRole).value<QFont>());
+        editor->setText(index.model()->data(index, Qt::DisplayRole).toString());
+
+        QString locale = index.model()->data(index, VocabularyModel::LocaleRole).toString();
+        if(!locale.isEmpty()) {
+            LanguageSettings settings(locale);
+            settings.readConfig();
+            QString layout = settings.keyboardLayout();
+            if(!layout.isEmpty()) {
+                QDBusInterface kxkb( "org.kde.kxkb", "/kxkb", "org.kde.KXKB" );
+                if (kxkb.isValid()) {
+                kxkb.call( "setLayout", layout );
+                }
             }
         }
+        connect(editor, SIGNAL(returnPressed()), this, SLOT(commitAndCloseEditor()));
+        return editor;
     }
-
-    connect(editor, SIGNAL(returnPressed()), this, SLOT(commitAndCloseEditor()));
-    return editor;
+    }
 }
 
-
 void VocabularyDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
 {
-    if (!index.isValid())
+    if (!index.isValid()) {
         return;
+    }
 
-    switch (index.column()) {
+    switch (VocabularyModel::columnType(index.column())) {
     default: {
         QString value = index.model()->data(index, Qt::DisplayRole).toString();
 
-        KLineEdit *lineEdit = static_cast<KLineEdit*>(editor);
-        lineEdit->setText(value);
+        KLineEdit *lineEdit = qobject_cast<KLineEdit*>(editor);
+        if (lineEdit) {
+            lineEdit->setText(value);
+        }
+        }
     }
-    }
 }
 
-
 void VocabularyDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
 {
     if (!index.isValid()) {
         return;
     }
 
-    int columnType = VocabularyModel::columnType(index.column());
+    switch (VocabularyModel::columnType(index.column())) {
+    case (VocabularyModel::WordType): {
+        kDebug() << "word type editor";
+        KComboBox *combo = qobject_cast<KComboBox*>(editor);
+        kDebug() << "combo" << combo->currentText();
+        QModelIndex comboIndex = combo->view()->currentIndex();
+        KEduVocWordType* wordType = static_cast<KEduVocWordType*>(comboIndex.internalPointer());
 
-    switch (columnType) {
+        // the root is the same as no word type
+        if (wordType && wordType->parent() == 0) {
+            wordType = 0;
+        }
 
+        VocabularyFilter *filter = qobject_cast<VocabularyFilter*>(model);
+        VocabularyModel *vocModel = qobject_cast<VocabularyModel*>((filter)->sourceModel());
+Q_ASSERT(vocModel);
+        QVariant data = vocModel->data(filter->mapToSource(index), VocabularyModel::EntryRole);
+
+        KEduVocExpression *expression = data.value<KEduVocExpression*>();
+Q_ASSERT(expression);
+        int translationId = VocabularyModel::translation(index.column());
+
+        expression->translation(translationId)->setWordType(wordType);
+
+    }
     default: {
         KLineEdit *lineEdit = static_cast<KLineEdit*>(editor);
         QString value = lineEdit->text();
@@ -111,7 +162,6 @@
     }
 }
 
-
 void VocabularyDelegate::commitAndCloseEditor()
 {
     QWidget *editor = qobject_cast<QWidget *>(sender());
@@ -120,12 +170,16 @@
     emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
 }
 
-
 void VocabularyDelegate::setCurrentIndex(const QModelIndex & index)
 {
     m_currentIndex = index;
 }
 
+void VocabularyDelegate::setDocument(KEduVocDocument * doc)
+{
+    m_doc = doc;
+}
+
 /*
 QPair< QString, QString > VocabularyDelegate::guessWordType(const QString & entry, int language) const
 {
@@ -142,4 +196,6 @@
     return qMakePair(QString(), QString());
 }
 */
+
+
 #include "vocabularydelegate.moc"
--- trunk/KDE/kdeedu/parley/src/vocabulary/vocabularydelegate.h #809159:809160
@@ -20,6 +20,8 @@
 #include <QItemDelegate>
 #include <QModelIndex>
 
+class KEduVocDocument;
+
 class VocabularyDelegate : public QItemDelegate
 {
     Q_OBJECT
@@ -32,6 +34,9 @@
     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
     void setCurrentIndex(const QModelIndex &index);
 
+public slots:
+    void setDocument(KEduVocDocument *doc);
+
 protected:
 //     void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const;
 
@@ -39,14 +44,8 @@
     void commitAndCloseEditor();
 
 private:
-    /**
-     * This should become a class of its own. It currently guesses the word type noun based on the articles.
-     * @param entry
-     * @param language
-     * @return
-     */
-//     QPair< QString, QString > guessWordType(const QString & entry, int language) const;
     QModelIndex m_currentIndex;
+    KEduVocDocument *m_doc;
 };
 
 #endif
--- trunk/KDE/kdeedu/parley/src/vocabulary/vocabularymodel.cpp #809159:809160
@@ -204,6 +204,13 @@
         v.setValue(m_container->entry(index.row(), m_recursive));
         return v;
         }
+    case Qt::ToolTipRole: {
+        ///@todo more tooltips?
+        switch (entryColumn) {
+        case WordType:
+            return i18n("You can drag and drop words onto their word type.");
+        }
+        }
     }
 
     return QVariant();
@@ -259,7 +266,7 @@
     case Translation:
         return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
     case WordType:
-        return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
+        return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
     default:
         return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDropEnabled;
     }
--- trunk/KDE/kdeedu/parley/src/vocabulary/vocabularyview.cpp #809159:809160
@@ -550,6 +550,7 @@
 {
     m_doc = doc;
     reset();
+    m_vocabularyDelegate->setDocument(doc);
 }
 
 void VocabularyView::checkSpelling()


More information about the Parley-devel mailing list