File indexing completed on 2022-11-29 13:00:35

0001 /*
0002     SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "imagechooserwidget.h"
0007 
0008 #include <KEduVocExpression>
0009 #include <QDebug>
0010 
0011 #include <QDragEnterEvent>
0012 #include <QMimeData>
0013 
0014 using namespace Editor;
0015 
0016 ImageChooserWidget::ImageChooserWidget(QWidget *parent)
0017     : QWidget(parent)
0018 {
0019     setupUi(this);
0020 
0021     connect(imageUrlRequester, &KUrlRequester::textChanged, this, &ImageChooserWidget::slotImageChanged);
0022 
0023     imageWidget->setAcceptDrops(true);
0024     imageWidget->installEventFilter(this);
0025     setEnabled(false);
0026 }
0027 
0028 void ImageChooserWidget::setTranslation(KEduVocExpression *entry, int translation)
0029 {
0030     m_currentTranslation = translation;
0031     m_entry = entry;
0032 
0033     if (m_entry) {
0034         setEnabled(true);
0035         imageUrlRequester->setUrl(m_entry->translation(m_currentTranslation)->imageUrl());
0036         //         slotImageChanged( m_entry->translation(m_currentTranslation)->imageUrl().toLocalFile() );
0037     } else {
0038         setEnabled(false);
0039         imageUrlRequester->clear();
0040         //         imageLabel->setText(i18nc("@label image preview is empty", "No Preview"));
0041         imageWidget->setPixmap(QPixmap());
0042     }
0043 }
0044 
0045 void ImageChooserWidget::slotImageChanged(const QString &urlStr)
0046 {
0047     QUrl url = QUrl::fromUserInput(urlStr);
0048     if (!url.isEmpty() && url.isLocalFile()) {
0049         QPixmap pixmap(url.toLocalFile());
0050         imageWidget->setPixmap(pixmap);
0051     } else {
0052         //         imageLabel->setText(i18nc("@label image preview is empty", "No Image"));
0053         imageWidget->setPixmap(QPixmap());
0054     }
0055 
0056     if (m_entry) {
0057         m_entry->translation(m_currentTranslation)->setImageUrl(url);
0058         const QList<int> translationIndices = m_entry->translationIndices();
0059         for (int j : translationIndices) {
0060             if (m_entry->translation(j)->imageUrl().isEmpty()) {
0061                 m_entry->translation(j)->setImageUrl(imageUrlRequester->url());
0062             }
0063         }
0064     }
0065 }
0066 
0067 bool ImageChooserWidget::eventFilter(QObject *obj, QEvent *event)
0068 {
0069     if (obj == imageWidget) {
0070         //         if (event->type() == QEvent::KeyPress) {
0071         //              QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
0072         //              qDebug() << "Ate key press " << keyEvent->key();
0073         //              return true;
0074         //         }
0075         //         if (event->type() == QEvent::Resize) {
0076         //             QPixmap pixmap(imageUrlRequester->url().toLocalFile());
0077         //             imageLabel->setPixmap(pixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio));
0078         //             return false;
0079         //         }
0080 
0081         if (event->type() == QEvent::DragMove) {
0082             event->accept();
0083             return true;
0084         }
0085 
0086         // Events with drop data
0087         if (event->type() == QEvent::DragEnter || event->type() == QEvent::Drop) {
0088             if (!m_entry) {
0089                 return true;
0090             }
0091             QDropEvent *dropEvent = dynamic_cast<QDropEvent *>(event);
0092             if ((dropEvent != NULL) && (dropEvent->mimeData() != NULL)) {
0093                 const QMimeData *mimeData(dropEvent->mimeData());
0094                 if (mimeData->hasUrls() && mimeData->hasUrls() && (mimeData->urls().size() == 1)) {
0095                     switch (event->type()) {
0096                     case QEvent::DragEnter:
0097                         event->accept();
0098                         return true;
0099                     case QEvent::Drop:
0100                         imageUrlRequester->setUrl(mimeData->urls().constFirst());
0101                         return true;
0102                         break;
0103                     default:
0104                         break;
0105                     }
0106                 }
0107             }
0108         }
0109     }
0110 
0111     return QObject::eventFilter(obj, event);
0112 }