File indexing completed on 2024-04-21 03:51:02

0001 /*
0002     SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
0003     SPDX-FileCopyrightText: 2005-2007 Peter Hedlund <peter.hedlund@kdemail.net>
0004     SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "multiplechoicewidget.h"
0009 
0010 #include <KEduVocExpression>
0011 #include <KEduVocTranslation>
0012 #include <QDebug>
0013 
0014 #include <QDragEnterEvent>
0015 #include <QDropEvent>
0016 #include <QMimeData>
0017 #include <QStringListModel>
0018 
0019 using namespace Editor;
0020 
0021 MultipleChoiceWidget::MultipleChoiceWidget(QWidget *parent)
0022     : QWidget(parent)
0023     , m_choicesModel(new QStringListModel(this))
0024 {
0025     setupUi(this);
0026 
0027     connect(addChoiceButton, &QPushButton::clicked, this, &MultipleChoiceWidget::slotAddChoiceButton);
0028     connect(removeChoiceButton, &QPushButton::clicked, this, &MultipleChoiceWidget::slotRemoveChoiceButton);
0029 
0030     multipleChoiceListView->setModel(m_choicesModel);
0031 
0032     connect(m_choicesModel, &QStringListModel::dataChanged, this, &MultipleChoiceWidget::slotDataChanged);
0033 
0034     multipleChoiceListView->setAcceptDrops(true);
0035     multipleChoiceListView->installEventFilter(this);
0036 
0037     setEnabled(false);
0038 }
0039 
0040 void MultipleChoiceWidget::slotDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
0041 {
0042     Q_UNUSED(topLeft)
0043     Q_UNUSED(bottomRight)
0044     if (m_translation) {
0045         m_translation->setMultipleChoice(m_choicesModel->stringList());
0046         removeChoiceButton->setEnabled(m_translation->getMultipleChoice().count() > 0);
0047     } else {
0048         removeChoiceButton->setEnabled(false);
0049     }
0050 }
0051 
0052 void MultipleChoiceWidget::setTranslation(KEduVocExpression *entry, int translation)
0053 {
0054     if (entry) {
0055         m_translation = entry->translation(translation);
0056     } else {
0057         m_translation = nullptr;
0058     }
0059 
0060     if (m_translation) {
0061         setEnabled(true);
0062         m_choicesModel->setStringList(m_translation->getMultipleChoice());
0063         removeChoiceButton->setEnabled(m_translation->getMultipleChoice().count() > 0);
0064     } else {
0065         setEnabled(false);
0066     }
0067     removeChoiceButton->setEnabled(m_translation && m_translation->getMultipleChoice().count() > 0);
0068 }
0069 
0070 void MultipleChoiceWidget::slotAddChoiceButton()
0071 {
0072     m_choicesModel->insertRow(m_choicesModel->rowCount());
0073     QModelIndex index(m_choicesModel->index(m_choicesModel->rowCount() - 1));
0074     m_choicesModel->setData(index, "");
0075     multipleChoiceListView->scrollTo(index);
0076     multipleChoiceListView->setCurrentIndex(index);
0077     multipleChoiceListView->edit(index);
0078 }
0079 
0080 void MultipleChoiceWidget::slotRemoveChoiceButton()
0081 {
0082     QModelIndex index = multipleChoiceListView->selectionModel()->currentIndex();
0083     if (index.isValid()) {
0084         m_choicesModel->removeRows(index.row(), 1, QModelIndex());
0085     } else {
0086         m_choicesModel->removeRows(m_choicesModel->rowCount(QModelIndex()) - 1, 1, QModelIndex());
0087     }
0088     if (m_translation) {
0089         m_translation->setMultipleChoice(m_choicesModel->stringList());
0090         removeChoiceButton->setEnabled(m_translation->getMultipleChoice().count() > 0);
0091     } else {
0092         removeChoiceButton->setEnabled(false);
0093     }
0094 }
0095 
0096 bool MultipleChoiceWidget::eventFilter(QObject *obj, QEvent *event)
0097 {
0098     if (obj == multipleChoiceListView) {
0099         if (event->type() == QEvent::DragEnter) {
0100             QDragEnterEvent *dragEnterEvent = static_cast<QDragEnterEvent *>(event);
0101             // qDebug() << "DragEnter mime format: " << dragEnterEvent->format();
0102             if ((dragEnterEvent->mimeData() != nullptr) && dragEnterEvent->mimeData()->hasText()) {
0103                 event->accept();
0104             }
0105             return true;
0106         }
0107 
0108         if (event->type() == QEvent::DragMove) {
0109             event->accept();
0110             return true;
0111         }
0112 
0113         if (event->type() == QEvent::Drop) {
0114             QDropEvent *dropEvent = static_cast<QDropEvent *>(event);
0115             // qDebug() << "You dropped onto me: " << dropEvent->mimeData()->text();
0116             if ((dropEvent->mimeData() != nullptr) && dropEvent->mimeData()->hasText()) {
0117                 QStringList choices = dropEvent->mimeData()->text().split('\n');
0118                 for (const QString &choice : qAsConst(choices)) {
0119                     m_choicesModel->insertRow(multipleChoiceListView->model()->rowCount());
0120                     m_choicesModel->setData(m_choicesModel->index(multipleChoiceListView->model()->rowCount() - 1), choice);
0121                 }
0122 
0123                 return true;
0124             }
0125         }
0126     }
0127     return QObject::eventFilter(obj, event);
0128 }
0129 
0130 #include "moc_multiplechoicewidget.cpp"