File indexing completed on 2024-05-19 04:22:59
0001 /* 0002 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org> 0003 Copyright (c) 2011 Martin Koller <kollix@aon.at> 0004 All rights reserved. 0005 0006 Redistribution and use in source and binary forms, with or without 0007 modification, are permitted provided that the following conditions 0008 are met: 0009 0010 1. Redistributions of source code must retain the above copyright 0011 notice, this list of conditions and the following disclaimer. 0012 2. Redistributions in binary form must reproduce the above copyright 0013 notice, this list of conditions and the following disclaimer in the 0014 documentation and/or other materials provided with the distribution. 0015 0016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0017 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0018 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0019 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0020 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0021 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0022 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0023 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0025 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0026 */ 0027 0028 0029 #define DEBUG_KP_SELECTION_DRAG 0 0030 0031 0032 #include "kpSelectionDrag.h" 0033 0034 #include <QDataStream> 0035 #include <QImage> 0036 #include <QIODevice> 0037 #include <QUrl> 0038 0039 #include "kpLogCategories.h" 0040 0041 #include "kpSelectionFactory.h" 0042 #include "layers/selections/image/kpAbstractImageSelection.h" 0043 #include "layers/selections/image/kpRectangularImageSelection.h" 0044 0045 //--------------------------------------------------------------------- 0046 0047 // public static 0048 const char * const kpSelectionDrag::SelectionMimeType = 0049 "application/x-kolourpaint-selection-400"; 0050 0051 //--------------------------------------------------------------------- 0052 0053 kpSelectionDrag::kpSelectionDrag (const kpAbstractImageSelection &sel) 0054 { 0055 #if DEBUG_KP_SELECTION_DRAG && 1 0056 qCDebug(kpLogLayers) << "kpSelectionDrag() w=" << sel.width () 0057 << " h=" << sel.height (); 0058 #endif 0059 0060 Q_ASSERT (sel.hasContent ()); 0061 0062 // Store as selection. 0063 QByteArray ba; 0064 { 0065 QDataStream stream (&ba, QIODevice::WriteOnly); 0066 stream << sel; 0067 } 0068 setData (QLatin1String(kpSelectionDrag::SelectionMimeType), ba); 0069 0070 // Store as image (so that QMimeData::hasImage()) works). 0071 // OPT: an awful waste of memory storing image in both selection and QImage 0072 const QImage image = sel.baseImage (); 0073 #if DEBUG_KP_SELECTION_DRAG && 1 0074 qCDebug(kpLogLayers) << "\timage: w=" << image.width () 0075 << " h=" << image.height (); 0076 #endif 0077 if (image.isNull ()) 0078 { 0079 // TODO: proper error handling. 0080 qCCritical(kpLogLayers) << "kpSelectionDrag::setSelection() could not convert to image"; 0081 } 0082 else { 0083 setImageData (image); 0084 } 0085 } 0086 0087 //--------------------------------------------------------------------- 0088 // public static 0089 0090 bool kpSelectionDrag::canDecode(const QMimeData *mimeData) 0091 { 0092 Q_ASSERT(mimeData); 0093 0094 #if DEBUG_KP_SELECTION_DRAG 0095 qCDebug(kpLogLayers) << "kpSelectionDrag::canDecode()" 0096 << "hasSel=" << mimeData->hasFormat(QLatin1String(kpSelectionDrag::SelectionMimeType)) 0097 << "hasImage=" << mimeData->hasImage(); 0098 #endif 0099 0100 // mimeData->hasImage() would not check if the data is a valid image 0101 return mimeData->hasFormat(QLatin1String(kpSelectionDrag::SelectionMimeType)) || 0102 !qvariant_cast<QImage>(mimeData->imageData()).isNull(); 0103 } 0104 0105 //--------------------------------------------------------------------- 0106 // public static 0107 0108 kpAbstractImageSelection *kpSelectionDrag::decode(const QMimeData *mimeData) 0109 { 0110 #if DEBUG_KP_SELECTION_DRAG 0111 qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection)"; 0112 #endif 0113 Q_ASSERT (mimeData); 0114 0115 if (mimeData->hasFormat (QLatin1String(kpSelectionDrag::SelectionMimeType))) 0116 { 0117 #if DEBUG_KP_SELECTION_DRAG 0118 qCDebug(kpLogLayers) << "\tmimeSource hasFormat selection - just return it in QByteArray"; 0119 #endif 0120 QByteArray data = mimeData->data (QLatin1String(kpSelectionDrag::SelectionMimeType)); 0121 QDataStream stream (&data, QIODevice::ReadOnly); 0122 0123 return kpSelectionFactory::FromStream (stream); 0124 } 0125 0126 0127 #if DEBUG_KP_SELECTION_DRAG 0128 qCDebug(kpLogLayers) << "\tmimeSource doesn't provide selection - try image"; 0129 #endif 0130 0131 QImage image = qvariant_cast <QImage> (mimeData->imageData ()); 0132 if (!image.isNull ()) 0133 { 0134 #if DEBUG_KP_SELECTION_DRAG 0135 qCDebug(kpLogLayers) << "\tok w=" << image.width () << " h=" << image.height (); 0136 #endif 0137 0138 return new kpRectangularImageSelection ( 0139 QRect (0, 0, image.width (), image.height ()), image); 0140 } 0141 0142 if ( mimeData->hasUrls() ) // no image, check for path to local image file 0143 { 0144 QList<QUrl> urls = mimeData->urls(); 0145 0146 if ( urls.count() && urls[0].isLocalFile() ) 0147 { 0148 image.load(urls[0].toLocalFile()); 0149 0150 if ( !image.isNull() ) 0151 { 0152 return new kpRectangularImageSelection( 0153 QRect(0, 0, image.width(), image.height()), image); 0154 } 0155 } 0156 } 0157 0158 #if DEBUG_KP_SELECTION_DRAG 0159 qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection) mimeSource had no sel " 0160 "and could not decode to image"; 0161 #endif 0162 return nullptr; 0163 0164 } 0165 0166 //--------------------------------------------------------------------- 0167 0168 #include "moc_kpSelectionDrag.cpp"