TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kpselectiondrag.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #define DEBUG_KP_SELECTION_DRAG 0
  24. #include <kpselectiondrag.h>
  25. #include <tqdatastream.h>
  26. #include <kdebug.h>
  27. #include <kppixmapfx.h>
  28. #include <kpselection.h>
  29. // public static
  30. const char * const kpSelectionDrag::selectionMimeType = "application/x-kolourpaint-selection";
  31. kpSelectionDrag::kpSelectionDrag (TQWidget *dragSource, const char *name)
  32. : TQImageDrag (dragSource, name)
  33. {
  34. }
  35. kpSelectionDrag::kpSelectionDrag (const TQImage &image, TQWidget *dragSource, const char *name)
  36. : TQImageDrag (image, dragSource, name)
  37. {
  38. }
  39. kpSelectionDrag::kpSelectionDrag (const kpSelection &sel, TQWidget *dragSource, const char *name)
  40. : TQImageDrag (dragSource, name)
  41. {
  42. setSelection (sel);
  43. }
  44. kpSelectionDrag::~kpSelectionDrag ()
  45. {
  46. }
  47. // public
  48. void kpSelectionDrag::setSelection (const kpSelection &sel)
  49. {
  50. if (!sel.pixmap ())
  51. {
  52. kdError () << "kpSelectionDrag::setSelection() without pixmap" << endl;
  53. return;
  54. }
  55. m_selection = sel;
  56. // OPT: an awful waste of memory storing image in both selection and TQImage
  57. // HACK: need to set image else TQImageDrag::format() lies
  58. setImage (kpPixmapFX::convertToImage (*m_selection.pixmap ()));
  59. }
  60. // protected
  61. bool kpSelectionDrag::holdsSelection () const
  62. {
  63. return bool (m_selection.pixmap ());
  64. }
  65. // public virtual [base TQMimeSource]
  66. const char *kpSelectionDrag::format (int which) const
  67. {
  68. #if DEBUG_KP_SELECTION_DRAG && 0
  69. kdDebug () << "kpSelectionDrag::format(" << which << ")" << endl;
  70. #endif
  71. const char *ret = TQImageDrag::format (which);
  72. if (ret)
  73. {
  74. #if DEBUG_KP_SELECTION_DRAG && 0
  75. kdDebug () << "\tTQImageDrag reports " << ret << endl;
  76. #endif
  77. return ret;
  78. }
  79. int i;
  80. for (i = 0; TQImageDrag::format (i); i++)
  81. ;
  82. #if DEBUG_KP_SELECTION_DRAG && 0
  83. kdDebug () << "\tend of TQImageDrag format list at " << i << endl;
  84. #endif
  85. if (i == which)
  86. {
  87. #if DEBUG_KP_SELECTION_DRAG && 0
  88. kdDebug () << "\treturning own mimetype" << endl;
  89. #endif
  90. return kpSelectionDrag::selectionMimeType;
  91. }
  92. else
  93. {
  94. #if DEBUG_KP_SELECTION_DRAG && 0
  95. kdDebug () << "\treturning non-existent" << endl;
  96. #endif
  97. return 0;
  98. }
  99. }
  100. // public virtual [base TQMimeSource]
  101. // Don't need to override in TQt 3.2 (the TQMimeSource base will work)
  102. // but we do so just in case TQImageDrag later decides to override it.
  103. bool kpSelectionDrag::provides (const char *mimeType) const
  104. {
  105. #if DEBUG_KP_SELECTION_DRAG
  106. kdDebug () << "kpSelectionDrag::provides(" << mimeType << ")" << endl;
  107. #endif
  108. if (!mimeType)
  109. return false;
  110. return (!strcmp (mimeType, kpSelectionDrag::selectionMimeType) ||
  111. TQImageDrag::provides (mimeType));
  112. }
  113. // public virtual [base TQMimeSource]
  114. TQByteArray kpSelectionDrag::encodedData (const char *mimeType) const
  115. {
  116. #if DEBUG_KP_SELECTION_DRAG
  117. kdDebug () << "kpSelectionDrag::encodedData(" << mimeType << ")" << endl;
  118. #endif
  119. if (!mimeType)
  120. return TQByteArray ();
  121. if (!strcmp (mimeType, kpSelectionDrag::selectionMimeType))
  122. {
  123. TQByteArray ba;
  124. TQDataStream stream (ba, IO_WriteOnly);
  125. #if DEBUG_KP_SELECTION_DRAG
  126. kdDebug () << "\twant it as kpSelection in TQByteArray" << endl;
  127. #endif
  128. if (holdsSelection ())
  129. {
  130. #if DEBUG_KP_SELECTION_DRAG
  131. kdDebug () << "\t\thave selection - return it" << endl;
  132. #endif
  133. stream << m_selection;
  134. }
  135. else
  136. {
  137. #if DEBUG_KP_SELECTION_DRAG
  138. kdDebug () << "\t\thave image - call kpSelectionDrag::decode(TQImage)" << endl;
  139. #endif
  140. TQImage image;
  141. if (kpSelectionDrag::decode (this, image/*ref*/))
  142. {
  143. #if DEBUG_KP_SELECTION_DRAG
  144. kdDebug () << "\t\t\tok - returning sel with image w="
  145. << image.width ()
  146. << " h="
  147. << image.height ()
  148. << endl;
  149. #endif
  150. TQPixmap pixmap = kpPixmapFX::convertToPixmapAsLosslessAsPossible (image);
  151. stream << kpSelection (kpSelection::Rectangle,
  152. TQRect (0, 0, pixmap.width (), pixmap.height ()),
  153. pixmap);
  154. }
  155. else
  156. {
  157. kdError () << "kpSelectionDrag::encodedData(" << mimeType << ")"
  158. << " kpSelectionDrag(TQImage) could not decode data into TQImage"
  159. << endl;
  160. stream << kpSelection ();
  161. }
  162. }
  163. return ba;
  164. }
  165. else
  166. {
  167. #if DEBUG_KP_SELECTION_DRAG
  168. kdDebug () << "\twant it as TQImage in TQByteArray" << endl;
  169. #endif
  170. return TQImageDrag::encodedData (mimeType);
  171. }
  172. }
  173. // public static
  174. bool kpSelectionDrag::canDecode (const TQMimeSource *e)
  175. {
  176. #if DEBUG_KP_SELECTION_DRAG
  177. kdDebug () << "kpSelectionDrag::canDecode()" << endl;
  178. #endif
  179. if (!e)
  180. return false;
  181. return (e->provides (kpSelectionDrag::selectionMimeType) ||
  182. TQImageDrag::canDecode (e));
  183. }
  184. // public static
  185. bool kpSelectionDrag::decode (const TQMimeSource *e, TQImage &img)
  186. {
  187. #if DEBUG_KP_SELECTION_DRAG
  188. kdDebug () << "kpSelectionDrag::decode(TQImage)" << endl;
  189. #endif
  190. if (!e)
  191. return false;
  192. return (TQImageDrag::canDecode (e) && // prevents X errors, jumps based on unitialised values...
  193. TQImageDrag::decode (e, img/*ref*/));
  194. }
  195. // public static
  196. bool kpSelectionDrag::decode (const TQMimeSource *e, kpSelection &sel,
  197. const kpPixmapFX::WarnAboutLossInfo &wali)
  198. {
  199. #if DEBUG_KP_SELECTION_DRAG
  200. kdDebug () << "kpSelectionDrag::decode(kpSelection)" << endl;
  201. #endif
  202. if (!e)
  203. return false;
  204. if (e->provides (kpSelectionDrag::selectionMimeType))
  205. {
  206. #if DEBUG_KP_SELECTION_DRAG
  207. kdDebug () << "\tmimeSource provides selection - just return it in TQByteArray" << endl;
  208. #endif
  209. TQByteArray data = e->encodedData (kpSelectionDrag::selectionMimeType);
  210. TQDataStream stream (data, IO_ReadOnly);
  211. // (no need for wali as kpSelection's by definition only support TQPixmap's)
  212. stream >> sel;
  213. }
  214. else
  215. {
  216. #if DEBUG_KP_SELECTION_DRAG
  217. kdDebug () << "\tmimeSource doesn't provide selection - try image" << endl;
  218. #endif
  219. TQImage image;
  220. if (kpSelectionDrag::decode (e, image/*ref*/))
  221. {
  222. #if DEBUG_KP_SELECTION_DRAG
  223. kdDebug () << "\tok w=" << image.width () << " h=" << image.height () << endl;
  224. #endif
  225. sel = kpSelection (kpSelection::Rectangle,
  226. TQRect (0, 0, image.width (), image.height ()),
  227. kpPixmapFX::convertToPixmapAsLosslessAsPossible (image, wali));
  228. }
  229. else
  230. {
  231. #if DEBUG_KP_SELECTION_DRAG
  232. kdDebug () << "kpSelectionDrag::decode(kpSelection) mimeSource had no sel "
  233. "and could not decode to image" << endl;
  234. #endif
  235. return false;
  236. }
  237. }
  238. return true;
  239. }
  240. #include <kpselectiondrag.moc>