parley
containermodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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:
00077 if (role == Qt::DisplayRole || role == Qt::EditRole) {
00078 return container->name();
00079 }
00080
00081 if (role == Qt::CheckStateRole) {
00082 if (container->inPractice())
00083 return Qt::Checked;
00084 else
00085 return Qt::Unchecked;
00086 }
00087
00088
00089
00090 if (role == Qt::TextAlignmentRole) {
00091 return Qt::AlignLeft;
00092 }
00093 break;
00094 case 1:
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
00121 if (role == Qt::EditRole) {
00122 container->setName(value.toString());
00123 emit dataChanged(index, index);
00124 emit documentModified();
00125 return true;
00126 }
00127
00128
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
00148 if (index.parent() == QModelIndex()) {
00149 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable
00150 | Qt::ItemIsUserCheckable );
00151 }
00152
00153 if ( index.column() == 0 ) {
00154 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable
00155 | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled );
00156 } else {
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
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
00196 return 2;
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
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
00230
00231 foreach (const QModelIndex &index, indexes) {
00232 mimeData->addContainer(static_cast<KEduVocContainer*>(index.internalPointer()));
00233 }
00234 mimeData->setText("Parley lesson");
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
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
00259 const ContainerMimeData * containerData =
00260 qobject_cast<const ContainerMimeData *>(data);
00261
00262 if (containerData) {
00263 foreach (KEduVocContainer* container, containerData->containerList()) {
00264
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
00278 parentContainer = rootContainer();
00279 } else {
00280
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
00300 if (row < 0) {
00301 row = parentContainer->childContainerCount();
00302 }
00303
00304
00305 beginInsertRows(index(parentContainer), row, row);
00306 parentContainer->insertChildContainer(row, container);
00307 endInsertRows();
00308
00309 return true;
00310 }
00311 }
00312 }
00313
00314
00315
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
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
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361 return false;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 #include "containermodel.moc"