File indexing completed on 2024-04-21 03:50:59

0001 /*
0002     SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "vocabularymimedata.h"
0008 
0009 #include <KEduVocTranslation>
0010 
0011 void VocabularyMimeData::setTranslations(const QList<KEduVocTranslation *> &translations)
0012 {
0013     // list of pointers for drag and drop - for example to assign word types
0014     m_translations = translations;
0015 
0016     // sort the translations into entries to make deep copies for real copy and paste
0017     // to only include each expression once
0018     QList<KEduVocExpression *> expressions;
0019     for (KEduVocTranslation *translation : qAsConst(m_translations)) {
0020         if (!expressions.contains(translation->entry())) {
0021             expressions.append(translation->entry());
0022         }
0023     }
0024 
0025     for (KEduVocExpression *expression : qAsConst(expressions)) {
0026         MimeExpression exp;
0027         // deep copy
0028         exp.expression = KEduVocExpression(*expression);
0029 
0030         // copy word types
0031         // this sucks but there is not really a better was. copying pointers is not a good idea because copy and paste can be done between different documents.
0032         const QList<int> translationIndices = expression->translationIndices();
0033         for (int i : translationIndices) {
0034             // generate text string representation
0035             m_text.append(expression->translation(i)->text());
0036             m_text.append(" - ");
0037 
0038             // fill in word types independent of pointers
0039             KEduVocWordType *type = expression->translation(i)->wordType();
0040 
0041             if (type) { // check if it has a type != 0
0042                 exp.wordTypes[i].grammarType = expression->translation(i)->wordType()->wordType();
0043 
0044                 // this may seem weird, but the root element is "word types" so no need to copy that one.
0045                 while (type->parent()) {
0046                     exp.wordTypes[i].wordType.prepend(type->name());
0047                     type = static_cast<KEduVocWordType *>(type->parent());
0048                 }
0049             }
0050         }
0051         m_expressions.append(exp);
0052         m_text.append('\n');
0053     }
0054 }
0055 
0056 QList<KEduVocTranslation *> VocabularyMimeData::translationList() const
0057 {
0058     return m_translations;
0059 }
0060 
0061 QVariant VocabularyMimeData::retrieveData(const QString &mimeType, QMetaType type) const
0062 {
0063     Q_UNUSED(type)
0064     // only use the expression list.expressions
0065     // the translation list may be invalid (eg when cut it is no longer valid.
0066     // translations can only be used internally for drag and drop!!!
0067 
0068     if (mimeType == QLatin1String("text/plain")) {
0069         return m_text;
0070     }
0071     return QVariant();
0072 }
0073 
0074 QStringList VocabularyMimeData::formats() const
0075 {
0076     return QStringList() << QStringLiteral("text/plain");
0077 }
0078 
0079 QList<VocabularyMimeData::MimeExpression> VocabularyMimeData::expressionList() const
0080 {
0081     return m_expressions;
0082 }
0083 
0084 #include "moc_vocabularymimedata.cpp"