chalk

kis_clipboard.cpp
1/*
2 * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18#include <tqapplication.h>
19#include <tqclipboard.h>
20#include <tqobject.h>
21#include <tqimage.h>
22#include <tqmessagebox.h>
23#include <tqbuffer.h>
24#include <tdemultipledrag.h>
25#include <tdelocale.h>
26
27#include "kdebug.h"
28
29#include "KoStore.h"
30#include "KoStoreDrag.h"
31
32#include "kis_types.h"
33#include "kis_paint_device.h"
34#include "kis_config.h"
35#include "kis_colorspace_factory_registry.h"
36#include "kis_factory.h"
37#include <kis_meta_registry.h>
38#include "kis_clipboard.h"
39
40KisClipboard *KisClipboard::m_singleton = 0;
41
42KisClipboard::KisClipboard()
43{
44 Q_ASSERT(KisClipboard::m_singleton == 0);
45 KisClipboard::m_singleton = this;
46
47 m_pushedClipboard = false;
48 m_hasClip = false;
49 m_clip = 0;
50
51 // Check that we don't already have a clip ready
52 clipboardDataChanged();
53
54 // Make sure we are notified when clipboard changes
55 connect( TQApplication::clipboard(), TQ_SIGNAL( dataChanged() ),
56 this, TQ_SLOT( clipboardDataChanged() ) );
57}
58
59KisClipboard::~KisClipboard()
60{
61}
62
63KisClipboard* KisClipboard::instance()
64{
65 if(KisClipboard::m_singleton == 0)
66 {
67 KisClipboard::m_singleton = new KisClipboard();
68 TQ_CHECK_PTR(KisClipboard::m_singleton);
69 }
70 return KisClipboard::m_singleton;
71}
72
73void KisClipboard::setClip(KisPaintDeviceSP selection)
74{
75 m_clip = selection;
76
77 if (!selection)
78 return;
79
80 m_hasClip = true;
81
82 // We'll create a store (ZIP format) in memory
83 TQBuffer buffer;
84 TQCString mimeType("application/x-chalk-selection");
85 KoStore* store = KoStore::createStore( &buffer, KoStore::Write, mimeType );
86 Q_ASSERT( store );
87 Q_ASSERT( !store->bad() );
88
89 // Layer data
90 if (store->open("layerdata")) {
91 if (!selection->write(store)) {
92 selection->disconnect();
93 store->close();
94 return;
95 }
96 store->close();
97 }
98
99 // ColorSpace id of layer data
100 if (store->open("colorspace")) {
101 TQString csName = selection->colorSpace()->id().id();
102 store->write(csName.ascii(), strlen(csName.ascii()));
103 store->close();
104 }
105
106 if (selection->colorSpace()->getProfile()) {
107 KisAnnotationSP annotation = selection->colorSpace()->getProfile()->annotation();
108 if (annotation) {
109 // save layer profile
110 if (store->open("profile.icc")) {
111 store->write(annotation->annotation());
112 store->close();
113 }
114 }
115 }
116
117 delete store;
118
119 // We also create a TQImage so we can interchange with other applications
120 TQImage qimg;
121 KisConfig cfg;
122 TQString monitorProfileName = cfg.monitorProfile();
123 KisProfile * monitorProfile = KisMetaRegistry::instance()->csRegistry()->getProfileByName(monitorProfileName);
124 qimg = selection->convertToTQImage(monitorProfile);
125
126 TQImageDrag *qimgDrag = new TQImageDrag(qimg);
127 KMultipleDrag *multiDrag = new KMultipleDrag();
128 if ( !qimg.isNull() )
129 multiDrag->addDragObject( qimgDrag );
130 KoStoreDrag* storeDrag = new KoStoreDrag( mimeType, 0 );
131 storeDrag->setEncodedData( buffer.buffer() );
132 multiDrag->addDragObject( storeDrag );
133
134
135 TQClipboard *cb = TQApplication::clipboard();
136 cb->setData(multiDrag);
137 m_pushedClipboard = true;
138}
139
140KisPaintDeviceSP KisClipboard::clip()
141{
142 TQClipboard *cb = TQApplication::clipboard();
143 TQCString mimeType("application/x-chalk-selection");
144 TQMimeSource *cbData = cb->data();
145
146 if(cbData && cbData->provides(mimeType))
147 {
148 TQBuffer buffer(cbData->encodedData(mimeType));
149 KoStore* store = KoStore::createStore( &buffer, KoStore::Read, mimeType );
150 KisProfile *profile=0;
151
152 if (store->hasFile("profile.icc")) {
153 TQByteArray data;
154 store->open("profile.icc");
155 data = store->read(store->size());
156 store->close();
157 profile = new KisProfile(data);
158 }
159
160 TQString csName;
161 // ColorSpace id of layer data
162 if (store->hasFile("colorspace")) {
163 store->open("colorspace");
164 csName = TQString(store->read(store->size()));
165 store->close();
166 }
167
168 KisColorSpace *cs = KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID(csName, ""), profile);
169
170 m_clip = new KisPaintDevice(cs, "clip");
171
172 if (store->hasFile("layerdata")) {
173 store->open("layerdata");
174 m_clip->read(store);
175 store->close();
176 }
177 delete store;
178 }
179 else
180 {
181 TQImage qimg = cb->image();
182
183 if (qimg.isNull())
184 return 0;
185
186 KisConfig cfg;
187
188 TQ_UINT32 behaviour = cfg.pasteBehaviour();
189
190 if(behaviour==2)
191 {
192 // Ask user each time
193 behaviour = TQMessageBox::question(0,i18n("Pasting data from simple source"),i18n("The image data you are trying to paste has no colour profile information.\n\nOn the web and in simple applications the data are supposed to be in sRGB color format.\nImporting as web will show it as it is supposed to look.\nMost monitors are not perfect though so if you made the image yourself\nyou might want to import it as it looked on you monitor.\n\nHow do you want to interpret these data?"),i18n("As &Web"),i18n("As on &Monitor"));
194 }
195
196 KisColorSpace * cs;
197 TQString profileName("");
198 if(behaviour==1)
199 profileName = cfg.monitorProfile();
200
201 cs = KisMetaRegistry::instance()->csRegistry() ->getColorSpace(KisID("RGBA",""), profileName);
202 m_clip = new KisPaintDevice(cs, "from paste");
203 TQ_CHECK_PTR(m_clip);
204 m_clip->convertFromTQImage(qimg, profileName);
205 }
206
207 return m_clip;
208}
209
210void KisClipboard::clipboardDataChanged()
211{
212 if (!m_pushedClipboard) {
213 m_hasClip = false;
214 TQClipboard *cb = TQApplication::clipboard();
215 TQImage qimg = cb->image();
216 TQMimeSource *cbData = cb->data();
217 TQCString mimeType("application/x-chalk-selection");
218
219 if(cbData && cbData->provides(mimeType))
220 m_hasClip = true;
221
222 if (!qimg.isNull())
223 m_hasClip = true;
224 }
225
226 m_pushedClipboard = false;
227}
228
229
230bool KisClipboard::hasClip()
231{
232 return m_hasClip;
233}
234
235TQSize KisClipboard::clipSize()
236{
237
238 TQClipboard *cb = TQApplication::clipboard();
239 TQCString mimeType("application/x-chalk-selection");
240 TQMimeSource *cbData = cb->data();
241
242 KisPaintDeviceSP clip;
243
244 if(cbData && cbData->provides(mimeType)) {
245
246 TQBuffer buffer(cbData->encodedData(mimeType));
247 KoStore* store = KoStore::createStore( &buffer, KoStore::Read, mimeType );
248 KisProfile *profile=0;
249
250 if (store->hasFile("profile.icc")) {
251 TQByteArray data;
252 store->open("profile.icc");
253 data = store->read(store->size());
254 store->close();
255 profile = new KisProfile(data);
256 }
257
258 TQString csName;
259 // ColorSpace id of layer data
260 if (store->hasFile("colorspace")) {
261 store->open("colorspace");
262 csName = TQString(store->read(store->size()));
263 store->close();
264 }
265
266 KisColorSpace *cs = KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID(csName, ""), profile);
267
268 clip = new KisPaintDevice(cs, "clip");
269
270 if (store->hasFile("layerdata")) {
271 store->open("layerdata");
272 clip->read(store);
273 store->close();
274 }
275 delete store;
276
277 return clip->exactBounds().size();
278 }
279 else {
280 TQImage qimg = cb->image();
281 return qimg.size();
282 }
283;
284
285}
286
287#include "kis_clipboard.moc"
The Chalk clipboard is a clipboard that can store paint devices instead of just qimage's.
void setClip(KisPaintDeviceSP layer)
Sets the clipboard to the contents of the specified paint device; also set the system clipboard to a ...
KisPaintDeviceSP clip()
Get the contents of the clipboard in the form of a paint device.
KisColorSpace * getColorSpace(const KisID &csID, const TQString &profileName)
Return the colorspace + profile as named, or NULL if impossible combination.
KisProfile * getProfileByName(const TQString &name)
Return the profile associated with the given product name, or 0.
A colorspace is the definition of a certain color model in Chalk.
Chalk has a large number of extensible resources.
Definition kis_id.h:57
A paint device contains the actual pixel data and offers methods to read and write pixels.