kivio

kiviodragobject.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Peter Simonsson <psn@linux.se>
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 "kiviodragobject.h"
21
22#include <tqcstring.h>
23#include <tqstringlist.h>
24#include <tqdom.h>
25#include <tqtextstream.h>
26#include <tqpixmap.h>
27#include <tqimage.h>
28
29#include <kdebug.h>
30
31#include <KoZoomHandler.h>
32
33#include "kivio_layer.h"
34#include "kivio_page.h"
35#include "kivio_screen_painter.h"
36#include "kivio_intra_stencil_data.h"
37#include "kivio_doc.h"
38#include "kivio_stencil_spawner.h"
39
40KivioDragObject::KivioDragObject(TQWidget* dragSource, const char* name)
41 : TQDragObject(dragSource, name)
42{
43 m_encodeMimeList[0] = "application/vnd.kde.kivio";
44 m_encodeMimeList[1] = "text/xml";
45 m_stencilList.setAutoDelete(true);
46}
47
48const char* KivioDragObject::format(int i) const
49{
50 if(i < NumEncodeFormats) {
51 return m_encodeMimeList[i];
52 }
53
54 TQImageDrag id;
55 id.setImage(TQImage()); // We need the format list!!!
56 return id.format(i - NumEncodeFormats);
57}
58
59TQByteArray KivioDragObject::encodedData(const char* mimetype) const
60{
61 if((m_encodeMimeList[0] == mimetype) ||
62 (m_encodeMimeList[1] == mimetype))
63 {
64 return kivioEncoded();
65 } else if(tqstrnicmp(mimetype, "image/", 6) == 0) {
66 return imageEncoded(mimetype);
67 }
68
69 return TQByteArray();
70}
71
72bool KivioDragObject::canDecode(TQMimeSource* e)
73{
74 TQValueList<TQCString> decodeMimeList;
75 decodeMimeList.append("application/vnd.kde.kivio");
76 decodeMimeList.append("text/plain");
77
78 for(TQValueList<TQCString>::Iterator it = decodeMimeList.begin(); it != decodeMimeList.end(); ++it) {
79 if(e->provides(*it)) {
80 return true;
81 }
82 }
83
84 return false;
85}
86
87bool KivioDragObject::decode(TQMimeSource* e, TQPtrList<KivioStencil>& sl, KivioPage* page)
88{
89 bool ok = false;
90
91 if(e->provides("application/vnd.kde.kivio")) {
92 TQDomDocument doc("KivioSelection");
93 TQByteArray data = e->encodedData("application/vnd.kde.kivio");
94 doc.setContent( TQCString( data, data.size()+1 ) );
95 KivioLayer l(page);
96 ok = l.loadXML(doc.documentElement());
97 KivioStencil* stencil = l.stencilList()->first();
98 sl.clear();
99
100 while(stencil) {
101 sl.append(stencil->duplicate());
102 stencil = l.stencilList()->next();
103 }
104 } else if(e->provides("text/plain")) {
105 TQString str;
106 ok = TQTextDrag::decode(e, str);
107 KivioStencilSpawner* ss = page->doc()->findInternalStencilSpawner("Dave Marotti - Text");
108 KivioStencil* stencil = ss->newStencil();
109 stencil->setPosition(0, 0);
110 stencil->setDimensions(100, 100);
111 stencil->setText(str);
112 stencil->setTextFont(page->doc()->defaultFont());
113 sl.clear();
114 sl.append(stencil);
115 }
116
117 return ok;
118}
119
120void KivioDragObject::setStencilList(TQPtrList<KivioStencil> l)
121{
122 KivioStencil* stencil = l.first();
123 m_stencilList.clear();
124
125 while(stencil) {
126 m_stencilList.append(stencil->duplicate());
127 stencil = l.next();
128 }
129}
130
131void KivioDragObject::setStencilRect(KoRect r)
132{
133 m_stencilRect = r;
134}
135
136TQByteArray KivioDragObject::kivioEncoded() const
137{
138 if(m_stencilList.count() <= 0)
139 return TQByteArray();
140
141 TQDomDocument doc("KivioSelection");
142 TQDomElement elem = doc.createElement( "KivioSelection" );
143 doc.appendChild(elem);
144 KivioStencil *stencil = 0;
145 TQPtrListIterator<KivioStencil> it(m_stencilList);
146
147 while((stencil = it.current()) != 0) {
148 ++it;
149 kdDebug() << "Stencil: " << stencil->type() << endl;
150 elem.appendChild(stencil->saveXML(doc));
151 }
152
153 return doc.toCString();
154}
155
156TQByteArray KivioDragObject::imageEncoded(const char* mimetype) const
157{
158 KoZoomHandler zoomHandler;
159 zoomHandler.setZoomAndResolution(100, KoGlobal::dpiX(), KoGlobal::dpiY());
160 TQPixmap buffer(zoomHandler.zoomItX(m_stencilRect.width()), zoomHandler.zoomItY(m_stencilRect.height()));
161 buffer.fill(TQt::white);
162 KivioScreenPainter p;
163 p.start( &buffer );
164 p.setTranslation(-zoomHandler.zoomItX(m_stencilRect.x()), -zoomHandler.zoomItY(m_stencilRect.y()));
165 KivioIntraStencilData data;
166 data.painter = &p;
167 data.zoomHandler = &zoomHandler;
168 data.printing = true;
169 KivioStencil *stencil = 0;
170 TQPtrListIterator<KivioStencil> it(m_stencilList);
171
172 while((stencil = it.current()) != 0) {
173 ++it;
174 stencil->paint(&data);
175 }
176
177 p.stop();
178
179 TQImageDrag id;
180 id.setImage(buffer.convertToImage());
181 return id.encodedData(mimetype);
182}
183
184#include "kiviodragobject.moc"