File indexing completed on 2024-05-12 16:35:04

0001 /* This file is part of the KDE project
0002    Copyright 2007 Montel Laurent <montel@kde.org>
0003    Copyright 2011 Boudewijn Rempt <boud@valdyas.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "VectorTool.h"
0022 #include "VectorShape.h"
0023 #include "ChangeVectorDataCommand.h"
0024 
0025 #include <QToolButton>
0026 #include <QGridLayout>
0027 #include <QUrl>
0028 #include <QFileDialog>
0029 
0030 #include <klocalizedstring.h>
0031 #include <KIO/Job>
0032 
0033 #include <KoIcon.h>
0034 #include <KoCanvasBase.h>
0035 #include <KoImageCollection.h>
0036 #include <KoSelection.h>
0037 #include <KoShapeManager.h>
0038 #include <KoPointerEvent.h>
0039 
0040 VectorTool::VectorTool( KoCanvasBase* canvas )
0041     : KoToolBase( canvas ),
0042       m_shape(0)
0043 {
0044 }
0045 
0046 void VectorTool::activate(ToolActivation toolActivation, const QSet<KoShape*> &shapes)
0047 {
0048     Q_UNUSED(toolActivation);
0049 
0050     foreach (KoShape *shape, shapes) {
0051         m_shape = dynamic_cast<VectorShape*>( shape );
0052         if ( m_shape )
0053             break;
0054     }
0055     if ( !m_shape )
0056     {
0057         emit done();
0058         return;
0059     }
0060     useCursor(Qt::ArrowCursor);
0061 }
0062 
0063 void VectorTool::deactivate()
0064 {
0065   m_shape = 0;
0066 }
0067 
0068 QWidget * VectorTool::createOptionWidget()
0069 {
0070     QWidget *optionWidget = new QWidget();
0071     QGridLayout *layout = new QGridLayout(optionWidget);
0072 
0073     QToolButton *button = 0;
0074 
0075     button = new QToolButton(optionWidget);
0076     button->setIcon(koIcon("document-open"));
0077     button->setToolTip(i18n("Open Vector Image (EMF/WMF/SVM)"));
0078     layout->addWidget(button, 0, 0);
0079     connect(button, SIGNAL(clicked(bool)), this, SLOT(changeUrlPressed()));
0080 
0081     return optionWidget;
0082 }
0083 
0084 void VectorTool::changeUrlPressed()
0085 {
0086     if (m_shape == 0)
0087         return;
0088     const QUrl url = QFileDialog::getOpenFileUrl(/*QT5TODO: QLatin1String("image/x-emf image/x-wmf image/x-svm image/svg+xml")*/);
0089     if (!url.isEmpty()) {
0090         // TODO move this to an action in the libs, with a nice dialog or something.
0091         KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, 0);
0092         connect(job, SIGNAL(result(KJob*)), this, SLOT(setImageData(KJob*)));
0093     }
0094 
0095 }
0096 
0097 void VectorTool::mouseDoubleClickEvent( KoPointerEvent *event )
0098 {
0099     if(canvas()->shapeManager()->shapeAt(event->point) != m_shape) {
0100         event->ignore(); // allow the event to be used by another
0101         return;
0102     }
0103     changeUrlPressed();
0104 }
0105 
0106 void VectorTool::setImageData(KJob *job)
0107 {
0108     if (m_shape == 0) {
0109         return;
0110     }
0111     KIO::StoredTransferJob *transferJob = qobject_cast<KIO::StoredTransferJob*>(job);
0112     Q_ASSERT(transferJob);
0113 
0114     const QByteArray newData = transferJob->data();
0115     const VectorShape::VectorType vectorType = VectorShape::vectorType(newData);
0116     ChangeVectorDataCommand *cmd = new ChangeVectorDataCommand(m_shape, qCompress(newData), vectorType);
0117 
0118     canvas()->addCommand(cmd);
0119 }