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

parley

basiccontainermodel.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 BasicContainerModel::BasicContainerModel(KEduVocContainer::EnumContainerType type, QObject * parent) : QAbstractItemModel(parent)
00035 {
00036     m_type = type;
00037     m_doc = 0;
00038 }
00039 
00040 void BasicContainerModel::setDocument(KEduVocDocument * doc)
00041 {
00042     m_doc=doc;
00043     reset();
00044 }
00045 
00046 QModelIndex BasicContainerModel::index(int row, int column, const QModelIndex &parent) const
00047 {
00048     if (!m_doc || !hasIndex(row, column, parent)) {
00049         return QModelIndex();
00050     }
00051 
00052     KEduVocContainer *parentContainer;
00053 
00054     if (!parent.isValid()) {
00055         parentContainer = 0;
00056     } else {
00057         parentContainer = static_cast<KEduVocContainer*>(parent.internalPointer());
00058     }
00059 
00060     KEduVocContainer *childContainer;
00061     if (!parentContainer) {
00062         childContainer = rootContainer();
00063     } else {
00064         childContainer = parentContainer->childContainer(row);
00065     }
00066     return createIndex(row, column, childContainer);
00067 }
00068 
00069 QModelIndex BasicContainerModel::index(KEduVocContainer * container) const
00070 {
00071     if(!container) {
00072         return QModelIndex();
00073     }
00074 
00075     QModelIndex currentIndex = index(container->row(), 0, index(container->parent()));
00076     Q_ASSERT(container == currentIndex.internalPointer());
00077 
00078     return currentIndex;
00079 }
00080 
00081 QModelIndex BasicContainerModel::parent(const QModelIndex &index) const
00082 {
00083     if (!index.isValid()) {
00084         return QModelIndex();
00085     }
00086 
00087     KEduVocContainer *childItem = static_cast<KEduVocContainer*>(index.internalPointer());
00088     if (!childItem) {
00089         return QModelIndex();
00090     }
00091 
00092     KEduVocContainer *parentItem = childItem->parent();
00093 
00094     if (!parentItem) {
00095         return QModelIndex();
00096     }
00097 
00098     QModelIndex parentIndex = createIndex(parentItem->row(), 0, parentItem);
00099     return parentIndex;
00100 }
00101 
00102 int BasicContainerModel::rowCount(const QModelIndex &parent) const
00103 {
00104     if (parent.column() > 0) {
00105         return 0;
00106     }
00107 
00108     KEduVocContainer *parentItem;
00109     if (!parent.isValid()) {
00110         // root element
00111         if (!m_doc) {
00112             return 0;
00113         }
00114         return 1;
00115     } else {
00116         parentItem =  static_cast<KEduVocContainer*>(parent.internalPointer());
00117         return parentItem->childContainerCount();
00118     }
00119 }
00120 
00121 QVariant BasicContainerModel::data(const QModelIndex & index, int role) const
00122 {
00123     if (!index.isValid()) {
00124          return QVariant();
00125     }
00126 
00127     KEduVocContainer *container = static_cast<KEduVocContainer*>(index.internalPointer());
00128 
00129     switch (index.column()){
00130     case 0: // Container name
00131         if (role == Qt::DisplayRole || role == Qt::EditRole) {
00132             if (index.parent() == QModelIndex()) {
00133                 return i18n("None");
00134             }
00135             return container->name();
00136         }
00137         if (role == Qt::TextAlignmentRole) {
00138             return Qt::AlignLeft;
00139         }
00140         break;
00141     }
00142 
00143     return QVariant();
00144 }
00145 
00146 Qt::ItemFlags BasicContainerModel::flags(const QModelIndex &index) const
00147 {
00148     if (index.isValid()) {
00149         // the root element, not editable for now
00150         if (index.parent() == QModelIndex()) {
00151             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00152         }
00153         // the name column
00154         if ( index.column() == 0 ) {
00155             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00156         } else { // every other element
00157             return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00158         }
00159     }
00160     return  0;
00161 }
00162 
00163 int BasicContainerModel::columnCount(const QModelIndex & parent) const
00164 {
00165     Q_UNUSED(parent);
00166     if (!m_doc) {
00167         return 1;
00168     }
00169 
00170     return 1;
00171 }
00172 
00173 KEduVocContainer::EnumContainerType BasicContainerModel::containerType()
00174 {
00175     return m_type;
00176 }
00177 
00178 
00179 #include "basiccontainermodel.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"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
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