KGuiAddons

kcolormimedata.cpp
1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 1999 Steffen Hansen <hansen@kde.org>
3 SPDX-FileCopyrightText: 2005 Joseph Wenninger <jowenn@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kcolormimedata.h"
9
10#include <QColor>
11#include <QDrag>
12#include <QMimeData>
13#include <QPainter>
14
16{
17 mimeData->setColorData(color);
18 mimeData->setText(color.name());
19}
20
22{
23 if (mimeData->hasColor()) {
24 return true;
25 }
26 if (mimeData->hasText()) {
27 const QString colorName = mimeData->text();
28 if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) {
29 return true;
30 }
31 }
32 return false;
33}
34
36{
37 if (mimeData->hasColor()) {
38 return mimeData->colorData().value<QColor>();
39 }
40 if (canDecode(mimeData)) {
41 return QColor(mimeData->text());
42 }
43 return QColor();
44}
45
47{
48 QDrag *drag = new QDrag(dragsource);
49 QMimeData *mime = new QMimeData;
50 populateMimeData(mime, color);
51 drag->setMimeData(mime);
52 QPixmap colorpix(25, 20);
53 colorpix.fill(color);
54 QPainter p(&colorpix);
56 p.drawRect(0, 0, 24, 19);
57 p.end();
58 drag->setPixmap(colorpix);
59 drag->setHotSpot(QPoint(-5, -7));
60 return drag;
61}
KGUIADDONS_EXPORT QDrag * createDrag(const QColor &color, QObject *dragsource)
Creates a color drag object.
KGUIADDONS_EXPORT QColor fromMimeData(const QMimeData *mimeData)
Decodes the MIME data mimeData and returns the resulting color.
KGUIADDONS_EXPORT bool canDecode(const QMimeData *mimeData)
Returns true if the MIME data mimeData contains a color object.
KGUIADDONS_EXPORT void populateMimeData(QMimeData *mimeData, const QColor &color)
Sets the color and text representation fields for the specified color in the mimedata object: applica...
QString name() const const
void setHotSpot(const QPoint &hotspot)
void setMimeData(QMimeData *data)
void setPixmap(const QPixmap &pixmap)
QVariant colorData() const const
bool hasColor() const const
bool hasText() const const
void setColorData(const QVariant &color)
void setText(const QString &text)
QString text() const const
void drawRect(const QRectF &rectangle)
bool end()
void setPen(const QColor &color)
void fill(const QColor &color)
int length() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:40:22 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.