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

parley

containermodel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 
00003     Copyright 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
00004 
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "containermodel.h"
00017 
00018 #include "containermimedata.h"
00019 #include "vocabularymimedata.h"
00020 
00021 #include <keduvocdocument.h>
00022 #include <keduvoclesson.h>
00023 #include <keduvocwordtype.h>
00024 #include <keduvocexpression.h>
00025 
00026 #include <KDebug>
00027 #include <KLocalizedString>
00028 
00034 ContainerModel::ContainerModel(KEduVocContainer::EnumContainerType type, QObject * parent) : BasicContainerModel(type, parent)
00035 {
00036     m_type = type;
00037     m_doc = 0;
00038 
00039     setSupportedDragActions(Qt::CopyAction | Qt::MoveAction);
00040 }
00041 
00042 QModelIndex ContainerModel::appendContainer(const QModelIndex& parent, const QString & containerName)
00043 {
00044     KEduVocContainer* parentContainer;
00045     if (parent.isValid()) {
00046         parentContainer = static_cast<KEduVocContainer*>(parent.internalPointer());
00047     } else {
00048         return QModelIndex();
00049     }
00050 
00051     beginInsertRows(parent, parentContainer->childContainerCount(),
00052                     parentContainer->childContainerCount() );
00053     switch (m_type){
00054     case (KEduVocContainer::Lesson):
00055         parentContainer->appendChildContainer(new KEduVocLesson(containerName, static_cast<KEduVocLesson*>(parentContainer)));
00056         break;
00057     case (KEduVocContainer::WordType):
00058         parentContainer->appendChildContainer(new KEduVocWordType(containerName, static_cast<KEduVocWordType*>(parentContainer)));
00059     break;
00060     }
00061     endInsertRows();
00062 
00063     return index(parentContainer->childContainerCount() - 1, 0, parent);
00064 }
00065 
00066 
00067 QVariant ContainerModel::data(const QModelIndex & index, int role) const
00068 {
00069     if (!index.isValid()) {
00070          return QVariant();
00071     }
00072 
00073     KEduVocContainer *container = static_cast<KEduVocContainer*>(index.internalPointer());
00074 
00075     switch (index.column()){
00076     case 0: // Container name
00077         if (role == Qt::DisplayRole || role == Qt::EditRole) {
00078             return container->name();
00079         }
00080         // checkboxes
00081         if (role == Qt::CheckStateRole) {
00082             if (container->inPractice())
00083                 return Qt::Checked;
00084             else
00085                 return Qt::Unchecked;
00086         }
00087 //         if (role == Qt::DecorationRole) {
00088 //             return KIcon("favorites");
00089 //         }
00090         if (role == Qt::TextAlignmentRole) {
00091             return Qt::AlignLeft;
00092         }
00093         break;
00094     case 1: // Total count
00095         if (role == Qt::DisplayRole) {
00096             return container->entryCount(KEduVocContainer::Recursive);
00097         }
00098         if (role == Qt::TextAlignmentRole) {
00099             return Qt::AlignRight;
00100         }
00101         break;
00102     default:
00103         if (role == Qt::TextAlignmentRole) {
00104             return Qt::AlignRight;
00105         }
00106     }
00107 
00108     return QVariant();
00109 }
00110 
00111 
00112 bool ContainerModel::setData(const QModelIndex &index, const QVariant &value, int role)
00113 {
00114     if (!index.isValid()) {
00115         return false;
00116     }
00117 
00118     if ( index.column() == 0 ) {
00119         KEduVocContainer *container = static_cast<KEduVocContainer*>(index.internalPointer());
00120         // rename a lesson
00121         if (role == Qt::EditRole) {
00122             container->setName(value.toString());
00123             emit dataChanged(index, index);
00124             emit documentModified();
00125             return true;
00126         }
00127 
00128         // checkboxes
00129         if (role == Qt::CheckStateRole) {
00130             bool newState = value.toBool();
00131             for (int i = 0; i < rowCount(index); i++) {
00132                 setData(index.child(i, 0), newState, Qt::CheckStateRole);
00133             }
00134             container->setInPractice(newState);
00135             emit dataChanged(index, index);
00136             emit documentModified();
00137             return true;
00138         }
00139     }
00140     return false;
00141 }
00142 
00143 
00144 Qt::ItemFlags ContainerModel::flags(const QModelIndex &index) const
00145 {
00146     if (index.isValid()) {
00147         // the root element, not editable for now
00148         if (index.parent() == QModelIndex()) {
00149             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable
00150                         | Qt::ItemIsUserCheckable );
00151         }
00152         // the name column
00153         if ( index.column() == 0 ) {
00154             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable
00155                     | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled );
00156         } else { // every other element
00157             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled |  Qt::ItemIsDropEnabled );
00158         }
00159     }
00160     return  Qt::ItemIsDropEnabled;
00161 }
00162 
00163 
00164 QVariant ContainerModel::headerData(int section, Qt::Orientation orientation, int role) const
00165 {
00166     // statically two columns for now
00167     if (orientation == Qt::Horizontal) {
00168         switch (section) {
00169         case 0:
00170             if(role == Qt::DisplayRole) {
00171                 return i18n("Lesson");
00172             }
00173             break;
00174         case 1:
00175             if(role == Qt::DisplayRole) {
00176                 return QVariant();
00177             }
00178             if(role == Qt::ToolTipRole) {
00179                 return i18n("Number of entries in this lesson.");
00180             }
00181             break;
00182         }
00183     }
00184     return QVariant();
00185 }
00186 
00187 
00188 int ContainerModel::columnCount(const QModelIndex & parent) const
00189 {
00190     Q_UNUSED(parent);
00191     if (!m_doc) {
00192         return 2;
00193     }
00194 
00195     // for now one grade per language
00196     return 2; // + m_doc->identifierCount();
00197 }
00198 
00199 
00200 void ContainerModel::deleteContainer(const QModelIndex & containerIndex)
00201 {
00202     KEduVocContainer* container = static_cast<KEduVocContainer*>(containerIndex.internalPointer());
00203     KEduVocContainer* parent = container->parent();
00204 
00205     if (!parent) {
00206         // never delete the root container
00207         return;
00208     }
00209 
00210     beginRemoveRows(containerIndex.parent(), containerIndex.row(), containerIndex.row());
00211     parent->deleteChildContainer(container->row());
00212     endRemoveRows();
00213 }
00214 
00215 Qt::DropActions ContainerModel::supportedDropActions() const
00216 {
00217     return Qt::MoveAction | Qt::CopyAction;
00218 }
00219 
00220 QStringList ContainerModel::mimeTypes() const
00221 {
00222     return QStringList() << "text/plain";
00223 }
00224 
00225 
00226 QMimeData * ContainerModel::mimeData(const QModelIndexList &indexes) const
00227 {
00228      ContainerMimeData *mimeData = new ContainerMimeData();
00229 //      QByteArray encodedData;
00230 
00231     foreach (const QModelIndex &index, indexes) {
00232         mimeData->addContainer(static_cast<KEduVocContainer*>(index.internalPointer()));
00233     }
00234     mimeData->setText("Parley lesson");
00235 
00236 //      QDataStream stream(&encodedData, QIODevice::WriteOnly);
00237 // stream << "Parley lesson";
00238 //      foreach (const QModelIndex &index, indexes) {
00239 //          if (index.isValid()) {
00240 //              QString text = data(index, Qt::DisplayRole).toString();
00241 //              stream << text;
00242 //          }
00243 //      }
00244 // // kDebug() << "mimeData:" << encodedData;
00245 //      mimeData->setData("text/plain", encodedData);
00246     return mimeData;
00247 }
00248 
00249 
00250 bool ContainerModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
00251 {
00252     Q_UNUSED(column)
00253 
00254     if (action == Qt::IgnoreAction) {
00255          return true;
00256     }
00257 
00258     // if it's internal, get the pointers
00259     const ContainerMimeData * containerData =
00260              qobject_cast<const ContainerMimeData *>(data);
00261 
00262     if (containerData) {
00263         foreach (KEduVocContainer* container, containerData->containerList()) {
00264             // no way to move a word type to a lesson for now
00265             if (container->containerType() != m_type) {
00266                 return false;
00267             }
00268 
00269             if (action == Qt::MoveAction || action == Qt::CopyAction) {
00270                 kDebug() << "Move container: " << container->name();
00271                 KEduVocContainer* parentContainer = 0;
00272 
00273                 if (parent.isValid()) {
00274                     parentContainer = static_cast<KEduVocContainer*>(parent.internalPointer());
00275                 }
00276                 if (!parentContainer) {
00277                     // drop into root
00278                     parentContainer = rootContainer();
00279                 } else {
00280                     //make sure a container cannot be dropped into one of its child containers!
00281                     KEduVocContainer* childTest = parentContainer;
00282                     while (childTest != 0) {
00283                         if (childTest == container) {
00284                             kDebug() << "Cannot drop a container into one of its child containers!";
00285                             return false;
00286                         }
00287                         childTest = childTest->parent();
00288                     }
00289                 }
00290 
00291                 QModelIndex oldParent = index(container->parent());
00292 
00293 
00294 
00295                 beginRemoveRows(oldParent, container->row(), container->row());
00296                 container->parent()->removeChildContainer(container->row());
00297                 endRemoveRows();
00298 
00299                 // if we get to choose, append seems sane.
00300                 if (row < 0) {
00301                     row = parentContainer->childContainerCount();
00302                 }
00303 
00304                 // use index because we sometimes reparent to the root container instead of dropping into nowhere
00305                 beginInsertRows(index(parentContainer), row, row);
00306                 parentContainer->insertChildContainer(row, container);
00307                 endInsertRows();
00308 
00309                 return true;
00310             }
00311         }
00312     }
00313 
00314 
00315     // if it's a translation, get the pointers
00316     const VocabularyMimeData * translationData =
00317              qobject_cast<const VocabularyMimeData *>(data);
00318 
00319     if (translationData) {
00320         if(!parent.isValid()) {
00321             return false;
00322         }
00323         if (containerType() == KEduVocContainer::Lesson) {
00324             // Create a list of the entries associated with the translations being copied. This prevents duplicates if they highlighted several columns.
00325             QList<KEduVocExpression*> entries;
00326 
00327             foreach (KEduVocTranslation* translation, translationData->translationList()) {
00328                 if (!entries.contains(translation->entry())) {
00329                     entries << translation->entry();
00330                 }
00331             }
00332 
00333             foreach (KEduVocExpression* entry, entries) {
00334                 static_cast<KEduVocLesson*>(parent.internalPointer())->appendEntry(new KEduVocExpression(*entry));
00335             }
00336         }
00337 
00338         if (containerType() == KEduVocContainer::WordType) {
00339             foreach (KEduVocTranslation* translation, translationData->translationList()) {
00340                 translation->setWordType(
00341                     static_cast<KEduVocWordType*>(parent.internalPointer()));
00342             }
00343         }
00344         return false;
00345     }
00346 
00347 
00348     kDebug() << data->formats();
00349 /*
00350     if (data->hasText()) {
00351         if (action == Qt::CopyAction | action == Qt::MoveAction) {
00352             QString name;
00353             name = data->text();
00354             kDebug() << "Copy lesson " << name;
00355 
00356             appendLesson(parent, name);
00357             return true;
00358         }
00359     }*/
00360 
00361     return false;
00362 }
00363 
00364 /*
00365 bool ContainerModel::removeRows(int row, int count, const QModelIndex & parent)
00366 {
00367 // BIG FAT WARNING this code works, but it gets called by the drag and drop implementation automatically, so either this gets used or the other (dropMimeData) to remove the containers. If both are active, containers get deleted without warning or visible signs.
00368 
00369     KEduVocContainer* parentContainer;
00370     if (!parent.internalPointer()) {
00371         parentContainer = m_container;
00372     } else {
00373         parentContainer = static_cast<KEduVocContainer*>(parent.internalPointer());
00374     }
00375     kDebug() << "removeRows from " << parentContainer->name() << " row " << row << "count" << count;
00376 
00377     beginRemoveRows ( parent, row, row+count );
00378     for (int i = 0; i<count; i++) {
00379         parentContainer->removeChildContainer(row);
00380     }
00381     endRemoveRows();
00382 
00383     return true;
00384 }
00385 
00386 */
00387 
00388 
00389 #include "containermodel.moc"

parley

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

kdeedu

Skip menu "kdeedu"
  •     lib
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal