karbon

karbon_drag.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, 2002, 2003 The Karbon Developers
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 "karbon_drag.h"
00021 
00022 #include <qcstring.h>
00023 #include <qdom.h>
00024 #include <qtextstream.h>
00025 
00026 #include "vdocument.h"
00027 
00028 QCString KarbonDrag::m_encodeFormats[NumEncodeFmts];
00029 QCString KarbonDrag::m_decodeFormats[NumDecodeFmts];
00030 
00031 KarbonDrag::KarbonDrag( QWidget *dragSource, const char *name )
00032  : QDragObject( dragSource, name )
00033 {
00034     m_encodeFormats[0] = "application/vnd.kde.karbon";
00035     m_decodeFormats[0] = "application/vnd.kde.karbon";
00036 }
00037 
00038 const char *
00039 KarbonDrag::format( int i ) const
00040 {
00041     if( i < NumEncodeFmts ) {
00042         return m_encodeFormats[i];
00043     }
00044 
00045     return 0L;
00046 }
00047 
00048 QByteArray
00049 KarbonDrag::encodedData( const char* mimetype ) const
00050 {
00051     QCString result;
00052 
00053     if( m_encodeFormats[0] == mimetype )
00054     {
00055         VObjectListIterator itr( m_objects );
00056         // build a xml fragment containing the selection as karbon xml
00057         QDomDocument doc( "clip" );
00058         QDomElement elem = doc.createElement( "clip" );
00059         QTextStream ts( result, IO_WriteOnly );
00060 
00061         for( ; itr.current() ; ++itr )
00062             itr.current()->save( elem );
00063 
00064         ts << elem;
00065     }
00066 
00067     return result;
00068 }
00069 
00070 bool
00071 KarbonDrag::canDecode( QMimeSource* e)
00072 {
00073     for( int i = 0; i < NumDecodeFmts; i++ )
00074     {
00075         if( e->provides( m_decodeFormats[i] ) )
00076             return true;
00077     }
00078 
00079     return false;
00080 }
00081 
00082 bool
00083 KarbonDrag::decode( QMimeSource* e, VObjectList& sl, VDocument& vdoc )
00084 {
00085     if( e->provides( m_decodeFormats[0] ) )
00086     {
00087         QDomDocument doc( "clip" );
00088         QByteArray data = e->encodedData( m_decodeFormats[0] );
00089         doc.setContent( QCString( data, data.size()+1 ) );
00090         QDomElement clip = doc.documentElement();
00091         // Try to parse the clipboard data
00092         if( clip.tagName() == "clip" )
00093         {
00094             VGroup grp( &vdoc );
00095             grp.load( clip );
00096             VObjectListIterator itr( grp.objects() );
00097             for( ; itr.current() ; ++itr )
00098             {
00099                 VObject *obj = itr.current()->clone();
00100                 obj->setParent( 0L );
00101                 sl.append( obj );
00102             }
00103 
00104             return true;
00105         }
00106     }
00107 
00108     return false;
00109 }
00110 
00111 void
00112 KarbonDrag::setObjectList( VObjectList l )
00113 {
00114     VObjectListIterator itr( l );
00115     m_objects.clear();
00116 
00117     for( ; itr.current() ; ++itr )
00118         m_objects.append( itr.current()->clone() );
00119 }
00120 
00121 #include "karbon_drag.moc"
00122 
KDE Home | KDE Accessibility Home | Description of Access Keys