kivio

kiviodragobject.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Peter Simonsson <psn@linux.se>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kiviodragobject.h"
00021 
00022 #include <qcstring.h>
00023 #include <qstringlist.h>
00024 #include <qdom.h>
00025 #include <qtextstream.h>
00026 #include <qpixmap.h>
00027 #include <qimage.h>
00028 
00029 #include <kdebug.h>
00030 
00031 #include <KoZoomHandler.h>
00032 
00033 #include "kivio_layer.h"
00034 #include "kivio_page.h"
00035 #include "kivio_screen_painter.h"
00036 #include "kivio_intra_stencil_data.h"
00037 #include "kivio_doc.h"
00038 #include "kivio_stencil_spawner.h"
00039 
00040 KivioDragObject::KivioDragObject(QWidget* dragSource, const char* name)
00041   : QDragObject(dragSource, name)
00042 {
00043   m_encodeMimeList[0] = "application/vnd.kde.kivio";
00044   m_encodeMimeList[1] = "text/xml";
00045   m_stencilList.setAutoDelete(true);
00046 }
00047 
00048 const char* KivioDragObject::format(int i) const
00049 {
00050   if(i < NumEncodeFormats) {
00051     return m_encodeMimeList[i];
00052   }
00053   
00054   QImageDrag id;
00055   id.setImage(QImage()); // We need the format list!!!
00056   return id.format(i - NumEncodeFormats);
00057 }
00058 
00059 QByteArray KivioDragObject::encodedData(const char* mimetype) const
00060 {
00061   if((m_encodeMimeList[0] == mimetype) ||
00062       (m_encodeMimeList[1] == mimetype))
00063   {
00064     return kivioEncoded();
00065   } else if(qstrnicmp(mimetype, "image/", 6) == 0) {
00066     return imageEncoded(mimetype);
00067   }
00068 
00069   return QByteArray();
00070 }
00071 
00072 bool KivioDragObject::canDecode(QMimeSource* e)
00073 {
00074   QValueList<QCString> decodeMimeList;
00075   decodeMimeList.append("application/vnd.kde.kivio");
00076   decodeMimeList.append("text/plain");
00077 
00078   for(QValueList<QCString>::Iterator it = decodeMimeList.begin(); it != decodeMimeList.end(); ++it) {
00079     if(e->provides(*it)) {
00080       return true;
00081     }
00082   }
00083 
00084   return false;
00085 }
00086 
00087 bool KivioDragObject::decode(QMimeSource* e, QPtrList<KivioStencil>& sl, KivioPage* page)
00088 {
00089   bool ok = false;
00090 
00091   if(e->provides("application/vnd.kde.kivio")) {
00092     QDomDocument doc("KivioSelection");
00093     QByteArray data = e->encodedData("application/vnd.kde.kivio");
00094     doc.setContent( QCString( data, data.size()+1 ) );
00095     KivioLayer l(page);
00096     ok = l.loadXML(doc.documentElement());
00097     KivioStencil* stencil = l.stencilList()->first();
00098     sl.clear();
00099 
00100     while(stencil) {
00101       sl.append(stencil->duplicate());
00102       stencil = l.stencilList()->next();
00103     }
00104   } else if(e->provides("text/plain")) {
00105     QString str;
00106     ok = QTextDrag::decode(e, str);
00107     KivioStencilSpawner* ss = page->doc()->findInternalStencilSpawner("Dave Marotti - Text");
00108     KivioStencil* stencil = ss->newStencil();
00109     stencil->setPosition(0, 0);
00110     stencil->setDimensions(100, 100);
00111     stencil->setText(str);
00112     stencil->setTextFont(page->doc()->defaultFont());
00113     sl.clear();
00114     sl.append(stencil);
00115   }
00116 
00117   return ok;
00118 }
00119 
00120 void KivioDragObject::setStencilList(QPtrList<KivioStencil> l)
00121 {
00122   KivioStencil* stencil = l.first();
00123   m_stencilList.clear();
00124 
00125   while(stencil) {
00126     m_stencilList.append(stencil->duplicate());
00127     stencil = l.next();
00128   }
00129 }
00130 
00131 void KivioDragObject::setStencilRect(KoRect r)
00132 {
00133   m_stencilRect = r;
00134 }
00135 
00136 QByteArray KivioDragObject::kivioEncoded() const
00137 {
00138   if(m_stencilList.count() <= 0)
00139     return QByteArray();
00140 
00141   QDomDocument doc("KivioSelection");
00142   QDomElement elem = doc.createElement( "KivioSelection" );
00143   doc.appendChild(elem);
00144   KivioStencil *stencil = 0;
00145   QPtrListIterator<KivioStencil> it(m_stencilList);
00146 
00147   while((stencil = it.current()) != 0) {
00148     ++it;
00149     kdDebug() << "Stencil: " << stencil->type() << endl;
00150     elem.appendChild(stencil->saveXML(doc));
00151   }
00152 
00153   return doc.toCString();
00154 }
00155 
00156 QByteArray KivioDragObject::imageEncoded(const char* mimetype) const
00157 {
00158   KoZoomHandler zoomHandler;
00159   zoomHandler.setZoomAndResolution(100, KoGlobal::dpiX(), KoGlobal::dpiY());
00160   QPixmap buffer(zoomHandler.zoomItX(m_stencilRect.width()), zoomHandler.zoomItY(m_stencilRect.height()));
00161   buffer.fill(Qt::white);
00162   KivioScreenPainter p;
00163   p.start( &buffer );
00164   p.setTranslation(-zoomHandler.zoomItX(m_stencilRect.x()), -zoomHandler.zoomItY(m_stencilRect.y()));
00165   KivioIntraStencilData data;
00166   data.painter = &p;
00167   data.zoomHandler = &zoomHandler;
00168   data.printing = true;
00169   KivioStencil *stencil = 0;
00170   QPtrListIterator<KivioStencil> it(m_stencilList);
00171 
00172   while((stencil = it.current()) != 0) {
00173     ++it;
00174     stencil->paint(&data);
00175   }
00176 
00177   p.stop();
00178 
00179   QImageDrag id;
00180   id.setImage(buffer.convertToImage());
00181   return id.encodedData(mimetype);
00182 }
00183 
00184 #include "kiviodragobject.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys