diff --git a/keduvocdocument/keduvocreadonlycontainermodel.cpp b/keduvocdocument/keduvocreadonlycontainermodel.cpp index 8a4ff80..d4372a7 100644 --- a/keduvocdocument/keduvocreadonlycontainermodel.cpp +++ b/keduvocdocument/keduvocreadonlycontainermodel.cpp @@ -1,212 +1,212 @@ /*************************************************************************** Copyright 2007-2008 Frederik Gladhorn ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "keduvocreadonlycontainermodel.h" #include #include #include "keduvoccontainermimedata.h" #include "keduvocvocabularymimedata.h" #include #include #include #include class KEduVocReadonlyContainerModel::Private { public: KEduVocContainer::EnumContainerType m_type; KEduVocDocument *m_doc; - Private(KEduVocContainer::EnumContainerType & type); + Private(KEduVocContainer::EnumContainerType type); }; -KEduVocReadonlyContainerModel::Private::Private(KEduVocContainer::EnumContainerType & type) +KEduVocReadonlyContainerModel::Private::Private(KEduVocContainer::EnumContainerType type) : m_type(type) , m_doc(0) { } /** @file * Implementation of ReadonlyContainerModel. * Functions to create the model from the lessons of the vocabulary document. */ -KEduVocReadonlyContainerModel::KEduVocReadonlyContainerModel(KEduVocContainer::EnumContainerType & type, +KEduVocReadonlyContainerModel::KEduVocReadonlyContainerModel(KEduVocContainer::EnumContainerType type, QObject * parent) : QAbstractItemModel( parent ) , d( new Private( type )) { } KEduVocReadonlyContainerModel::~KEduVocReadonlyContainerModel() { delete d; } KEduVocContainer::EnumContainerType KEduVocReadonlyContainerModel::containerType() const { return d->m_type; } KEduVocDocument* KEduVocReadonlyContainerModel::document() const { return d->m_doc; } void KEduVocReadonlyContainerModel::setDocument(KEduVocDocument * doc) { beginResetModel(); d->m_doc = doc; if (d->m_doc) { //qDebug() << "Set Document: " << d->m_doc->url(); } else { //qDebug() << "Set Invalid Document"; } endResetModel(); } QModelIndex KEduVocReadonlyContainerModel::index(int row, int column, const QModelIndex &parent) const { if (!d->m_doc || !hasIndex(row, column, parent)) { return QModelIndex(); } KEduVocContainer *parentContainer; if (!parent.isValid()) { parentContainer = 0; } else { parentContainer = static_cast(parent.internalPointer()); } KEduVocContainer *childContainer = 0; if (!parentContainer) { childContainer = rootContainer(); if (!childContainer) { return QModelIndex(); } } else { childContainer = parentContainer->childContainer(row); } return createIndex(row, column, childContainer); } QModelIndex KEduVocReadonlyContainerModel::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 KEduVocReadonlyContainerModel::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } KEduVocContainer *childItem = static_cast(index.internalPointer()); if (!childItem) { return QModelIndex(); } KEduVocContainer *parentItem = childItem->parent(); if (!parentItem) { return QModelIndex(); } QModelIndex parentIndex = createIndex(parentItem->row(), 0, parentItem); return parentIndex; } int KEduVocReadonlyContainerModel::rowCount(const QModelIndex &parent) const { if (parent.column() > 0) { return 0; } KEduVocContainer *parentItem; if (!parent.isValid()) { // root element if (!d->m_doc) { return 0; } return 1; } else { parentItem = static_cast(parent.internalPointer()); return parentItem->childContainerCount(); } } QVariant KEduVocReadonlyContainerModel::data(const QModelIndex & index, int role) const { if (!index.isValid()) { return QVariant(); } KEduVocContainer *container = static_cast(index.internalPointer()); switch (index.column()) { case 0: // Container name if (role == Qt::DisplayRole || role == Qt::EditRole) { if (index.parent() == QModelIndex()) { return i18n("None"); } return container->name(); } if (role == Qt::TextAlignmentRole) { return Qt::AlignLeft; } break; } return QVariant(); } Qt::ItemFlags KEduVocReadonlyContainerModel::flags(const QModelIndex &index) const { if (index.isValid()) { // the root element, not editable for now if (index.parent() == QModelIndex()) { return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); } // the name column if (index.column() == 0) { return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); } else { // every other element return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); } } return 0; } int KEduVocReadonlyContainerModel::columnCount(const QModelIndex & parent) const { Q_UNUSED(parent); if (!d->m_doc) { return 1; } return 1; } \ No newline at end of file diff --git a/keduvocdocument/keduvocreadonlycontainermodel.h b/keduvocdocument/keduvocreadonlycontainermodel.h index 2b0b7bf..c86f199 100644 --- a/keduvocdocument/keduvocreadonlycontainermodel.h +++ b/keduvocdocument/keduvocreadonlycontainermodel.h @@ -1,72 +1,72 @@ /*************************************************************************** Copyright 2007-2008 Frederik Gladhorn ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef KEDUVOCREADONLYCONTAINERMODEL_H #define KEDUVOCREADONLYCONTAINERMODEL_H #include #include #include #include #include /** * Model for the tree of containers (lessons, word types). * ReadonlyContainerModel is a read only tree model. * @see ContainerModel for its subclass that includes more options. */ class KEDUVOCDOCUMENT_EXPORT KEduVocReadonlyContainerModel : public QAbstractItemModel { Q_OBJECT public: - explicit KEduVocReadonlyContainerModel(KEduVocContainer::EnumContainerType & type, QObject *parent = 0); + explicit KEduVocReadonlyContainerModel(KEduVocContainer::EnumContainerType type, QObject *parent = 0); ~KEduVocReadonlyContainerModel(); virtual QVariant data(const QModelIndex &index, int role) const; virtual Qt::ItemFlags flags(const QModelIndex &index) const; virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex index(KEduVocContainer* container) const; virtual QModelIndex parent(const QModelIndex &index) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; // getter methods KEduVocContainer::EnumContainerType containerType() const; KEduVocDocument* document() const; public slots: /** Set the new source kvtml file * @param doc the new file */ virtual void setDocument(KEduVocDocument *doc); protected: virtual KEduVocContainer *rootContainer() const = 0; private: class Private; Private * const d; }; #endif