File indexing completed on 2024-04-14 03:49:04
0001 /* 0002 SPDX-FileCopyrightText: 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "readonlycontainermodel.h" 0007 0008 #include <KLocalizedString> 0009 #include <QDebug> 0010 0011 #include "containermimedata.h" 0012 #include "vocabularymimedata.h" 0013 0014 #include <KEduVocDocument> 0015 #include <KEduVocExpression> 0016 #include <KEduVocWordtype> 0017 0018 /** @file 0019 * Implementation of ReadonlyContainerModel. 0020 * Functions to create the model from the lessons of the vocabulary document. 0021 */ 0022 0023 ReadonlyContainerModel::ReadonlyContainerModel(KEduVocContainer::EnumContainerType type, QObject *parent) 0024 : QAbstractItemModel(parent) 0025 , m_type(type) 0026 { 0027 } 0028 0029 void ReadonlyContainerModel::setDocument(const std::shared_ptr<KEduVocDocument> &doc) 0030 { 0031 beginResetModel(); 0032 m_doc = doc; 0033 if (m_doc) { 0034 // qDebug() << "Set Document: " << m_doc->url(); 0035 } else { 0036 // qDebug() << "Set Invalid Document"; 0037 } 0038 endResetModel(); 0039 } 0040 0041 QModelIndex ReadonlyContainerModel::index(int row, int column, const QModelIndex &parent) const 0042 { 0043 if (!m_doc || !hasIndex(row, column, parent)) { 0044 return QModelIndex(); 0045 } 0046 0047 KEduVocContainer *parentContainer; 0048 0049 if (!parent.isValid()) { 0050 parentContainer = nullptr; 0051 } else { 0052 parentContainer = static_cast<KEduVocContainer *>(parent.internalPointer()); 0053 } 0054 0055 KEduVocContainer *childContainer = nullptr; 0056 if (!parentContainer) { 0057 childContainer = rootContainer(); 0058 if (!childContainer) { 0059 return QModelIndex(); 0060 } 0061 } else { 0062 childContainer = parentContainer->childContainer(row); 0063 } 0064 return createIndex(row, column, childContainer); 0065 } 0066 0067 QModelIndex ReadonlyContainerModel::index(KEduVocContainer *container) const 0068 { 0069 if (!container) { 0070 return QModelIndex(); 0071 } 0072 0073 QModelIndex currentIndex = index(container->row(), 0, index(container->parent())); 0074 Q_ASSERT(container == currentIndex.internalPointer()); 0075 0076 return currentIndex; 0077 } 0078 0079 QModelIndex ReadonlyContainerModel::parent(const QModelIndex &index) const 0080 { 0081 if (!index.isValid()) { 0082 return QModelIndex(); 0083 } 0084 0085 KEduVocContainer *childItem = static_cast<KEduVocContainer *>(index.internalPointer()); 0086 if (!childItem) { 0087 return QModelIndex(); 0088 } 0089 0090 KEduVocContainer *parentItem = childItem->parent(); 0091 0092 if (!parentItem) { 0093 return QModelIndex(); 0094 } 0095 0096 QModelIndex parentIndex = createIndex(parentItem->row(), 0, parentItem); 0097 return parentIndex; 0098 } 0099 0100 int ReadonlyContainerModel::rowCount(const QModelIndex &parent) const 0101 { 0102 if (parent.column() > 0) { 0103 return 0; 0104 } 0105 0106 KEduVocContainer *parentItem; 0107 if (!parent.isValid()) { 0108 // root element 0109 if (!m_doc) { 0110 return 0; 0111 } 0112 return 1; 0113 } else { 0114 parentItem = static_cast<KEduVocContainer *>(parent.internalPointer()); 0115 return parentItem->childContainerCount(); 0116 } 0117 } 0118 0119 QVariant ReadonlyContainerModel::data(const QModelIndex &index, int role) const 0120 { 0121 if (!index.isValid()) { 0122 return QVariant(); 0123 } 0124 0125 KEduVocContainer *container = static_cast<KEduVocContainer *>(index.internalPointer()); 0126 0127 switch (index.column()) { 0128 case 0: // Container name 0129 if (role == Qt::DisplayRole || role == Qt::EditRole) { 0130 if (index.parent() == QModelIndex()) { 0131 return i18n("None"); 0132 } 0133 return container->name(); 0134 } 0135 if (role == Qt::TextAlignmentRole) { 0136 return Qt::AlignLeft; 0137 } 0138 break; 0139 } 0140 0141 return QVariant(); 0142 } 0143 0144 Qt::ItemFlags ReadonlyContainerModel::flags(const QModelIndex &index) const 0145 { 0146 if (index.isValid()) { 0147 // the root element, not editable for now 0148 if (index.parent() == QModelIndex()) { 0149 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); 0150 } 0151 // the name column 0152 if (index.column() == 0) { 0153 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); 0154 } else { // every other element 0155 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); 0156 } 0157 } 0158 return Qt::NoItemFlags; 0159 } 0160 0161 int ReadonlyContainerModel::columnCount(const QModelIndex &parent) const 0162 { 0163 Q_UNUSED(parent); 0164 if (!m_doc) { 0165 return 1; 0166 } 0167 0168 return 1; 0169 } 0170 0171 KEduVocContainer::EnumContainerType ReadonlyContainerModel::containerType() 0172 { 0173 return m_type; 0174 } 0175 0176 KEduVocDocument *ReadonlyContainerModel::document() const 0177 { 0178 return m_doc.get(); 0179 } 0180 0181 #include "moc_readonlycontainermodel.cpp"