karbon

karbon_drag.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "karbon_drag.h"
21
22#include <tqcstring.h>
23#include <tqdom.h>
24#include <tqtextstream.h>
25
26#include "vdocument.h"
27
28TQCString KarbonDrag::m_encodeFormats[NumEncodeFmts];
29TQCString KarbonDrag::m_decodeFormats[NumDecodeFmts];
30
31KarbonDrag::KarbonDrag( TQWidget *dragSource, const char *name )
32 : TQDragObject( dragSource, name )
33{
34 m_encodeFormats[0] = "application/vnd.kde.karbon";
35 m_decodeFormats[0] = "application/vnd.kde.karbon";
36}
37
38const char *
39KarbonDrag::format( int i ) const
40{
41 if( i < NumEncodeFmts ) {
42 return m_encodeFormats[i];
43 }
44
45 return 0L;
46}
47
48TQByteArray
49KarbonDrag::encodedData( const char* mimetype ) const
50{
51 TQCString result;
52
53 if( m_encodeFormats[0] == mimetype )
54 {
55 VObjectListIterator itr( m_objects );
56 // build a xml fragment containing the selection as karbon xml
57 TQDomDocument doc( "clip" );
58 TQDomElement elem = doc.createElement( "clip" );
59 TQTextStream ts( result, IO_WriteOnly );
60
61 for( ; itr.current() ; ++itr )
62 itr.current()->save( elem );
63
64 ts << elem;
65 }
66
67 return result;
68}
69
70bool
71KarbonDrag::canDecode( TQMimeSource* e)
72{
73 for( int i = 0; i < NumDecodeFmts; i++ )
74 {
75 if( e->provides( m_decodeFormats[i] ) )
76 return true;
77 }
78
79 return false;
80}
81
82bool
83KarbonDrag::decode( TQMimeSource* e, VObjectList& sl, VDocument& vdoc )
84{
85 if( e->provides( m_decodeFormats[0] ) )
86 {
87 TQDomDocument doc( "clip" );
88 TQByteArray data = e->encodedData( m_decodeFormats[0] );
89 doc.setContent( TQCString( data, data.size()+1 ) );
90 TQDomElement clip = doc.documentElement();
91 // Try to parse the clipboard data
92 if( clip.tagName() == "clip" )
93 {
94 VGroup grp( &vdoc );
95 grp.load( clip );
96 VObjectListIterator itr( grp.objects() );
97 for( ; itr.current() ; ++itr )
98 {
99 VObject *obj = itr.current()->clone();
100 obj->setParent( 0L );
101 sl.append( obj );
102 }
103
104 return true;
105 }
106 }
107
108 return false;
109}
110
111void
112KarbonDrag::setObjectList( VObjectList l )
113{
114 VObjectListIterator itr( l );
115 m_objects.clear();
116
117 for( ; itr.current() ; ++itr )
118 m_objects.append( itr.current()->clone() );
119}
120
121#include "karbon_drag.moc"
122
All non-visual, static doc info is in here.
Definition vdocument.h:53
void setParent(VObject *parent)
Sets a new parent object.
Definition vobject.h:140