Changeset View
Changeset View
Standalone View
Standalone View
layers/selections/kpSelectionDrag.cpp
Show All 19 Lines | 1 | /* | |||
---|---|---|---|---|---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | 26 | */ | ||
27 | 27 | | |||
28 | | ||||
29 | #define DEBUG_KP_SELECTION_DRAG 0 | | |||
30 | | ||||
31 | | ||||
32 | #include "kpSelectionDrag.h" | 28 | #include "kpSelectionDrag.h" | ||
33 | 29 | | |||
34 | #include <QDataStream> | 30 | #include <QDataStream> | ||
35 | #include <QImage> | 31 | #include <QImage> | ||
36 | #include <QUrl> | 32 | #include <QUrl> | ||
37 | 33 | | |||
38 | #include "kpLogCategories.h" | 34 | #include "kpLogCategories.h" | ||
39 | 35 | | |||
40 | #include "kpSelectionFactory.h" | 36 | #include "kpSelectionFactory.h" | ||
41 | #include "layers/selections/image/kpAbstractImageSelection.h" | 37 | #include "layers/selections/image/kpAbstractImageSelection.h" | ||
42 | #include "layers/selections/image/kpRectangularImageSelection.h" | 38 | #include "layers/selections/image/kpRectangularImageSelection.h" | ||
43 | 39 | | |||
44 | //--------------------------------------------------------------------- | 40 | //--------------------------------------------------------------------- | ||
45 | 41 | | |||
46 | // public static | 42 | // public static | ||
47 | const char * const kpSelectionDrag::SelectionMimeType = | 43 | const char * const kpSelectionDrag::SelectionMimeType = | ||
48 | "application/x-kolourpaint-selection-400"; | 44 | "application/x-kolourpaint-selection-400"; | ||
49 | 45 | | |||
50 | //--------------------------------------------------------------------- | 46 | //--------------------------------------------------------------------- | ||
51 | 47 | | |||
52 | kpSelectionDrag::kpSelectionDrag (const kpAbstractImageSelection &sel) | 48 | kpSelectionDrag::kpSelectionDrag (const kpAbstractImageSelection &sel) | ||
53 | { | 49 | { | ||
54 | #if DEBUG_KP_SELECTION_DRAG && 1 | | |||
55 | qCDebug(kpLogLayers) << "kpSelectionDrag() w=" << sel.width () | 50 | qCDebug(kpLogLayers) << "kpSelectionDrag() w=" << sel.width () | ||
56 | << " h=" << sel.height () | 51 | << " h=" << sel.height (); | ||
57 | << endl; | | |||
58 | #endif | | |||
59 | 52 | | |||
60 | Q_ASSERT (sel.hasContent ()); | 53 | Q_ASSERT (sel.hasContent ()); | ||
61 | 54 | | |||
62 | // Store as selection. | 55 | // Store as selection. | ||
63 | QByteArray ba; | 56 | QByteArray ba; | ||
64 | { | 57 | { | ||
65 | QDataStream stream (&ba, QIODevice::WriteOnly); | 58 | QDataStream stream (&ba, QIODevice::WriteOnly); | ||
66 | stream << sel; | 59 | stream << sel; | ||
67 | } | 60 | } | ||
68 | setData (kpSelectionDrag::SelectionMimeType, ba); | 61 | setData (kpSelectionDrag::SelectionMimeType, ba); | ||
69 | 62 | | |||
70 | // Store as image (so that QMimeData::hasImage()) works). | 63 | // Store as image (so that QMimeData::hasImage()) works). | ||
71 | // OPT: an awful waste of memory storing image in both selection and QImage | 64 | // OPT: an awful waste of memory storing image in both selection and QImage | ||
72 | const QImage image = sel.baseImage (); | 65 | const QImage image = sel.baseImage (); | ||
73 | #if DEBUG_KP_SELECTION_DRAG && 1 | | |||
74 | qCDebug(kpLogLayers) << "\timage: w=" << image.width () | 66 | qCDebug(kpLogLayers) << "\timage: w=" << image.width () | ||
75 | << " h=" << image.height () | 67 | << " h=" << image.height (); | ||
76 | << endl; | | |||
77 | #endif | | |||
78 | if (image.isNull ()) | 68 | if (image.isNull ()) | ||
79 | { | 69 | { | ||
80 | // TODO: proper error handling. | 70 | // TODO: proper error handling. | ||
81 | qCCritical(kpLogLayers) << "kpSelectionDrag::setSelection() could not convert to image" | 71 | qCCritical(kpLogLayers) << "kpSelectionDrag::setSelection() could not convert to image" | ||
82 | << endl; | 72 | << endl; | ||
83 | } | 73 | } | ||
84 | else | 74 | else | ||
85 | setImageData (image); | 75 | setImageData (image); | ||
86 | } | 76 | } | ||
87 | 77 | | |||
88 | //--------------------------------------------------------------------- | 78 | //--------------------------------------------------------------------- | ||
89 | // public static | 79 | // public static | ||
90 | 80 | | |||
91 | bool kpSelectionDrag::canDecode(const QMimeData *mimeData) | 81 | bool kpSelectionDrag::canDecode(const QMimeData *mimeData) | ||
92 | { | 82 | { | ||
93 | Q_ASSERT(mimeData); | 83 | Q_ASSERT(mimeData); | ||
94 | 84 | | |||
95 | #if DEBUG_KP_SELECTION_DRAG | | |||
96 | qCDebug(kpLogLayers) << "kpSelectionDrag::canDecode()" | 85 | qCDebug(kpLogLayers) << "kpSelectionDrag::canDecode()" | ||
97 | << "hasSel=" << mimeData->hasFormat(kpSelectionDrag::SelectionMimeType) | 86 | << "hasSel=" << mimeData->hasFormat(kpSelectionDrag::SelectionMimeType) | ||
98 | << "hasImage=" << mimeData->hasImage(); | 87 | << "hasImage=" << mimeData->hasImage(); | ||
99 | #endif | | |||
100 | 88 | | |||
101 | // mimeData->hasImage() would not check if the data is a valid image | 89 | // mimeData->hasImage() would not check if the data is a valid image | ||
102 | return mimeData->hasFormat(kpSelectionDrag::SelectionMimeType) || | 90 | return mimeData->hasFormat(kpSelectionDrag::SelectionMimeType) || | ||
103 | !qvariant_cast<QImage>(mimeData->imageData()).isNull(); | 91 | !qvariant_cast<QImage>(mimeData->imageData()).isNull(); | ||
104 | } | 92 | } | ||
105 | 93 | | |||
106 | //--------------------------------------------------------------------- | 94 | //--------------------------------------------------------------------- | ||
107 | // public static | 95 | // public static | ||
108 | 96 | | |||
109 | kpAbstractImageSelection *kpSelectionDrag::decode(const QMimeData *mimeData) | 97 | kpAbstractImageSelection *kpSelectionDrag::decode(const QMimeData *mimeData) | ||
110 | { | 98 | { | ||
111 | #if DEBUG_KP_SELECTION_DRAG | | |||
112 | qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection)"; | 99 | qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection)"; | ||
113 | #endif | | |||
114 | Q_ASSERT (mimeData); | 100 | Q_ASSERT (mimeData); | ||
115 | 101 | | |||
116 | if (mimeData->hasFormat (kpSelectionDrag::SelectionMimeType)) | 102 | if (mimeData->hasFormat (kpSelectionDrag::SelectionMimeType)) | ||
117 | { | 103 | { | ||
118 | #if DEBUG_KP_SELECTION_DRAG | | |||
119 | qCDebug(kpLogLayers) << "\tmimeSource hasFormat selection - just return it in QByteArray"; | 104 | qCDebug(kpLogLayers) << "\tmimeSource hasFormat selection - just return it in QByteArray"; | ||
120 | #endif | | |||
121 | QByteArray data = mimeData->data (kpSelectionDrag::SelectionMimeType); | 105 | QByteArray data = mimeData->data (kpSelectionDrag::SelectionMimeType); | ||
122 | QDataStream stream (&data, QIODevice::ReadOnly); | 106 | QDataStream stream (&data, QIODevice::ReadOnly); | ||
123 | 107 | | |||
124 | return kpSelectionFactory::FromStream (stream); | 108 | return kpSelectionFactory::FromStream (stream); | ||
125 | } | 109 | } | ||
126 | else | 110 | else | ||
127 | { | 111 | { | ||
128 | #if DEBUG_KP_SELECTION_DRAG | | |||
129 | qCDebug(kpLogLayers) << "\tmimeSource doesn't provide selection - try image"; | 112 | qCDebug(kpLogLayers) << "\tmimeSource doesn't provide selection - try image"; | ||
130 | #endif | | |||
131 | 113 | | |||
132 | QImage image = qvariant_cast <QImage> (mimeData->imageData ()); | 114 | QImage image = qvariant_cast <QImage> (mimeData->imageData ()); | ||
133 | if (!image.isNull ()) | 115 | if (!image.isNull ()) | ||
134 | { | 116 | { | ||
135 | #if DEBUG_KP_SELECTION_DRAG | | |||
136 | qCDebug(kpLogLayers) << "\tok w=" << image.width () << " h=" << image.height (); | 117 | qCDebug(kpLogLayers) << "\tok w=" << image.width () << " h=" << image.height (); | ||
137 | #endif | | |||
138 | 118 | | |||
139 | return new kpRectangularImageSelection ( | 119 | return new kpRectangularImageSelection ( | ||
140 | QRect (0, 0, image.width (), image.height ()), image); | 120 | QRect (0, 0, image.width (), image.height ()), image); | ||
141 | } | 121 | } | ||
142 | else if ( mimeData->hasUrls() ) // no image, check for path to local image file | 122 | else if ( mimeData->hasUrls() ) // no image, check for path to local image file | ||
143 | { | 123 | { | ||
144 | QList<QUrl> urls = mimeData->urls(); | 124 | QList<QUrl> urls = mimeData->urls(); | ||
145 | 125 | | |||
146 | if ( urls.count() && urls[0].isLocalFile() ) | 126 | if ( urls.count() && urls[0].isLocalFile() ) | ||
147 | { | 127 | { | ||
148 | image.load(urls[0].toLocalFile()); | 128 | image.load(urls[0].toLocalFile()); | ||
149 | 129 | | |||
150 | if ( !image.isNull() ) | 130 | if ( !image.isNull() ) | ||
151 | { | 131 | { | ||
152 | return new kpRectangularImageSelection( | 132 | return new kpRectangularImageSelection( | ||
153 | QRect(0, 0, image.width(), image.height()), image); | 133 | QRect(0, 0, image.width(), image.height()), image); | ||
154 | } | 134 | } | ||
155 | } | 135 | } | ||
156 | } | 136 | } | ||
157 | 137 | | |||
158 | #if DEBUG_KP_SELECTION_DRAG | | |||
159 | qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection) mimeSource had no sel " | 138 | qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection) mimeSource had no sel " | ||
160 | "and could not decode to image" << endl; | 139 | "and could not decode to image"; | ||
161 | #endif | | |||
162 | return nullptr; | 140 | return nullptr; | ||
163 | } | 141 | } | ||
164 | } | 142 | } | ||
165 | 143 | | |||
166 | //--------------------------------------------------------------------- | 144 | //--------------------------------------------------------------------- |