File indexing completed on 2024-05-19 16:07:20
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "PictureShapeConfigWidget.h" 0021 #include "PictureShape.h" 0022 #include "PictureDebug.h" 0023 0024 #include <KoImageData.h> 0025 #include <KoImageCollection.h> 0026 0027 #include <kfilewidget.h> 0028 #include <kjobuidelegate.h> 0029 #include <KIO/Job> 0030 0031 #include <QGridLayout> 0032 #include <QImageReader> 0033 #include <QUrl> 0034 0035 void PictureShapeLoadWaiter::setImageData(KJob *job) 0036 { 0037 if (job->error()) { // e.g. file not found 0038 job->uiDelegate()->showErrorMessage(); 0039 if (m_pictureShape && !m_pictureShape->imageData()) { 0040 // Don't leave an empty broken shape, the rest of the code isn't ready for null imageData 0041 if (m_pictureShape->parent()) { 0042 m_pictureShape->parent()->removeShape(m_pictureShape); 0043 } 0044 delete m_pictureShape; 0045 } 0046 deleteLater(); 0047 return; 0048 } 0049 0050 deleteLater(); 0051 0052 if (m_pictureShape == 0) 0053 return; // ugh, the shape got deleted meanwhile (## err, who would set the pointer to null?) 0054 0055 KIO::StoredTransferJob *transferJob = qobject_cast<KIO::StoredTransferJob*>(job); 0056 Q_ASSERT(transferJob); 0057 0058 if (m_pictureShape->imageCollection()) { 0059 KoImageData *data = m_pictureShape->imageCollection()->createImageData(transferJob->data()); 0060 if (data) { 0061 m_pictureShape->setUserData(data); 0062 // check if the shape still size of the default shape and resize in that case 0063 if (qFuzzyCompare(m_pictureShape->size().width(), 50.0)) { 0064 m_pictureShape->setSize(data->imageSize()); 0065 } 0066 // trigger repaint as the userData was changed 0067 m_pictureShape->update(); 0068 } 0069 } 0070 } 0071 0072 // ---------------------------------------------------- // 0073 0074 PictureShapeConfigWidget::PictureShapeConfigWidget() 0075 : m_shape(0), 0076 m_fileWidget(0) 0077 { 0078 } 0079 0080 PictureShapeConfigWidget::~PictureShapeConfigWidget() 0081 { 0082 delete m_fileWidget; 0083 } 0084 0085 void PictureShapeConfigWidget::open(KoShape *shape) 0086 { 0087 m_shape = dynamic_cast<PictureShape*>(shape); 0088 Q_ASSERT(m_shape); 0089 delete m_fileWidget; 0090 QVBoxLayout *layout = new QVBoxLayout(this); 0091 m_fileWidget = new KFileWidget(QUrl(/* QT5TODO:"kfiledialog:///OpenDialog"*/), this); 0092 m_fileWidget->setMode(KFile::Files | KFile::ExistingOnly); 0093 m_fileWidget->setOperationMode(KFileWidget::Opening); 0094 QStringList imageFilters; 0095 foreach(const QByteArray &mimeType, QImageReader::supportedMimeTypes()) { 0096 imageFilters << QLatin1String(mimeType); 0097 } 0098 m_fileWidget->setMimeFilter(imageFilters); 0099 layout->addWidget(m_fileWidget); 0100 setLayout(layout); 0101 connect(m_fileWidget, &KFileWidget::accepted, this, &PictureShapeConfigWidget::slotAccept); 0102 } 0103 0104 // The page dialog's own accept() is called by the OK button 0105 // This makes exec() return, then we get here. 0106 // For KFileWidget, nothing happened yet. It still needs to process things in slotOk. 0107 void PictureShapeConfigWidget::save() 0108 { 0109 if (!m_shape) 0110 return; 0111 m_fileWidget->slotOk(); // emits accepted, possibly async 0112 } 0113 0114 // Called by slotOk, possibly async 0115 void PictureShapeConfigWidget::slotAccept() 0116 { 0117 m_fileWidget->accept(); 0118 const QUrl url = m_fileWidget->selectedUrl(); 0119 if (!url.isEmpty()) { 0120 KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, 0); 0121 PictureShapeLoadWaiter *waiter = new PictureShapeLoadWaiter(m_shape); 0122 connect(job, SIGNAL(result(KJob*)), waiter, SLOT(setImageData(KJob*))); 0123 } 0124 Q_EMIT accept(); 0125 } 0126 0127 bool PictureShapeConfigWidget::showOnShapeCreate() 0128 { 0129 return true; 0130 } 0131 0132 bool PictureShapeConfigWidget::showOnShapeSelect() 0133 { 0134 return false; 0135 }