Changeset View
Changeset View
Standalone View
Standalone View
layers/selections/kpSelectionDrag.cpp
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Line(s) | 47 | const char * const kpSelectionDrag::SelectionMimeType = | |||
---|---|---|---|---|---|
48 | "application/x-kolourpaint-selection-400"; | 48 | "application/x-kolourpaint-selection-400"; | ||
49 | 49 | | |||
50 | //--------------------------------------------------------------------- | 50 | //--------------------------------------------------------------------- | ||
51 | 51 | | |||
52 | kpSelectionDrag::kpSelectionDrag (const kpAbstractImageSelection &sel) | 52 | kpSelectionDrag::kpSelectionDrag (const kpAbstractImageSelection &sel) | ||
53 | { | 53 | { | ||
54 | #if DEBUG_KP_SELECTION_DRAG && 1 | 54 | #if DEBUG_KP_SELECTION_DRAG && 1 | ||
55 | qCDebug(kpLogLayers) << "kpSelectionDrag() w=" << sel.width () | 55 | qCDebug(kpLogLayers) << "kpSelectionDrag() w=" << sel.width () | ||
56 | << " h=" << sel.height () | 56 | << " h=" << sel.height (); | ||
57 | << endl; | | |||
58 | #endif | 57 | #endif | ||
59 | 58 | | |||
60 | Q_ASSERT (sel.hasContent ()); | 59 | Q_ASSERT (sel.hasContent ()); | ||
61 | 60 | | |||
62 | // Store as selection. | 61 | // Store as selection. | ||
63 | QByteArray ba; | 62 | QByteArray ba; | ||
64 | { | 63 | { | ||
65 | QDataStream stream (&ba, QIODevice::WriteOnly); | 64 | QDataStream stream (&ba, QIODevice::WriteOnly); | ||
66 | stream << sel; | 65 | stream << sel; | ||
67 | } | 66 | } | ||
68 | setData (kpSelectionDrag::SelectionMimeType, ba); | 67 | setData (kpSelectionDrag::SelectionMimeType, ba); | ||
69 | 68 | | |||
70 | // Store as image (so that QMimeData::hasImage()) works). | 69 | // Store as image (so that QMimeData::hasImage()) works). | ||
71 | // OPT: an awful waste of memory storing image in both selection and QImage | 70 | // OPT: an awful waste of memory storing image in both selection and QImage | ||
72 | const QImage image = sel.baseImage (); | 71 | const QImage image = sel.baseImage (); | ||
73 | #if DEBUG_KP_SELECTION_DRAG && 1 | 72 | #if DEBUG_KP_SELECTION_DRAG && 1 | ||
74 | qCDebug(kpLogLayers) << "\timage: w=" << image.width () | 73 | qCDebug(kpLogLayers) << "\timage: w=" << image.width () | ||
75 | << " h=" << image.height () | 74 | << " h=" << image.height (); | ||
76 | << endl; | | |||
77 | #endif | 75 | #endif | ||
78 | if (image.isNull ()) | 76 | if (image.isNull ()) | ||
79 | { | 77 | { | ||
80 | // TODO: proper error handling. | 78 | // TODO: proper error handling. | ||
81 | qCCritical(kpLogLayers) << "kpSelectionDrag::setSelection() could not convert to image" | 79 | qCCritical(kpLogLayers) << "kpSelectionDrag::setSelection() could not convert to image"; | ||
82 | << endl; | | |||
83 | } | 80 | } | ||
84 | else | 81 | else { | ||
85 | setImageData (image); | 82 | setImageData (image); | ||
86 | } | 83 | } | ||
84 | } | ||||
87 | 85 | | |||
88 | //--------------------------------------------------------------------- | 86 | //--------------------------------------------------------------------- | ||
89 | // public static | 87 | // public static | ||
90 | 88 | | |||
91 | bool kpSelectionDrag::canDecode(const QMimeData *mimeData) | 89 | bool kpSelectionDrag::canDecode(const QMimeData *mimeData) | ||
92 | { | 90 | { | ||
93 | Q_ASSERT(mimeData); | 91 | Q_ASSERT(mimeData); | ||
94 | 92 | | |||
Show All 23 Lines | 111 | #endif | |||
118 | #if DEBUG_KP_SELECTION_DRAG | 116 | #if DEBUG_KP_SELECTION_DRAG | ||
119 | qCDebug(kpLogLayers) << "\tmimeSource hasFormat selection - just return it in QByteArray"; | 117 | qCDebug(kpLogLayers) << "\tmimeSource hasFormat selection - just return it in QByteArray"; | ||
120 | #endif | 118 | #endif | ||
121 | QByteArray data = mimeData->data (kpSelectionDrag::SelectionMimeType); | 119 | QByteArray data = mimeData->data (kpSelectionDrag::SelectionMimeType); | ||
122 | QDataStream stream (&data, QIODevice::ReadOnly); | 120 | QDataStream stream (&data, QIODevice::ReadOnly); | ||
123 | 121 | | |||
124 | return kpSelectionFactory::FromStream (stream); | 122 | return kpSelectionFactory::FromStream (stream); | ||
125 | } | 123 | } | ||
126 | else | 124 | | ||
127 | { | 125 | | ||
128 | #if DEBUG_KP_SELECTION_DRAG | 126 | #if DEBUG_KP_SELECTION_DRAG | ||
129 | qCDebug(kpLogLayers) << "\tmimeSource doesn't provide selection - try image"; | 127 | qCDebug(kpLogLayers) << "\tmimeSource doesn't provide selection - try image"; | ||
130 | #endif | 128 | #endif | ||
131 | 129 | | |||
132 | QImage image = qvariant_cast <QImage> (mimeData->imageData ()); | 130 | QImage image = qvariant_cast <QImage> (mimeData->imageData ()); | ||
133 | if (!image.isNull ()) | 131 | if (!image.isNull ()) | ||
134 | { | 132 | { | ||
135 | #if DEBUG_KP_SELECTION_DRAG | 133 | #if DEBUG_KP_SELECTION_DRAG | ||
136 | qCDebug(kpLogLayers) << "\tok w=" << image.width () << " h=" << image.height (); | 134 | qCDebug(kpLogLayers) << "\tok w=" << image.width () << " h=" << image.height (); | ||
137 | #endif | 135 | #endif | ||
138 | 136 | | |||
139 | return new kpRectangularImageSelection ( | 137 | return new kpRectangularImageSelection ( | ||
140 | QRect (0, 0, image.width (), image.height ()), image); | 138 | QRect (0, 0, image.width (), image.height ()), image); | ||
141 | } | 139 | } | ||
142 | else if ( mimeData->hasUrls() ) // no image, check for path to local image file | 140 | | ||
141 | if ( mimeData->hasUrls() ) // no image, check for path to local image file | ||||
143 | { | 142 | { | ||
144 | QList<QUrl> urls = mimeData->urls(); | 143 | QList<QUrl> urls = mimeData->urls(); | ||
145 | 144 | | |||
146 | if ( urls.count() && urls[0].isLocalFile() ) | 145 | if ( urls.count() && urls[0].isLocalFile() ) | ||
147 | { | 146 | { | ||
148 | image.load(urls[0].toLocalFile()); | 147 | image.load(urls[0].toLocalFile()); | ||
149 | 148 | | |||
150 | if ( !image.isNull() ) | 149 | if ( !image.isNull() ) | ||
151 | { | 150 | { | ||
152 | return new kpRectangularImageSelection( | 151 | return new kpRectangularImageSelection( | ||
153 | QRect(0, 0, image.width(), image.height()), image); | 152 | QRect(0, 0, image.width(), image.height()), image); | ||
154 | } | 153 | } | ||
155 | } | 154 | } | ||
156 | } | 155 | } | ||
157 | 156 | | |||
158 | #if DEBUG_KP_SELECTION_DRAG | 157 | #if DEBUG_KP_SELECTION_DRAG | ||
159 | qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection) mimeSource had no sel " | 158 | qCDebug(kpLogLayers) << "kpSelectionDrag::decode(kpAbstractSelection) mimeSource had no sel " | ||
160 | "and could not decode to image" << endl; | 159 | "and could not decode to image"; | ||
161 | #endif | 160 | #endif | ||
162 | return nullptr; | 161 | return nullptr; | ||
163 | } | 162 | | ||
164 | } | 163 | } | ||
165 | 164 | | |||
166 | //--------------------------------------------------------------------- | 165 | //--------------------------------------------------------------------- |