QmlDesigner: Add basic drag-n-drop support to textures

Dropping textures to different views to be handled separately.

Change-Id: I1e0ac5681e5e1b3dca8bdaf6819a6b699f629e79
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Mahmoud Badri
2022-11-07 16:49:17 +02:00
parent d5a7f25e60
commit 15dd073e0c
4 changed files with 28 additions and 5 deletions

View File

@@ -26,7 +26,7 @@ Image {
onPressed: (mouse) => {
if (mouse.button === Qt.LeftButton)
rootView.startDragTexture(modelData, mapToGlobal(mouse.x, mouse.y))
rootView.startDragTexture(index, mapToGlobal(mouse.x, mouse.y))
else if (mouse.button === Qt.RightButton)
root.showContextMenu()
}

View File

@@ -12,6 +12,7 @@
#include <documentmanager.h>
#include <qmldesignerconstants.h>
#include <qmldesignerplugin.h>
#include <variantproperty.h>
#include <theme.h>
@@ -93,23 +94,36 @@ bool MaterialBrowserWidget::eventFilter(QObject *obj, QEvent *event)
Model *model = document->currentModel();
QTC_ASSERT(model, return false);
if (m_materialToDrag.isValid()) {
if (m_materialToDrag.isValid() || m_textureToDrag.isValid()) {
QMouseEvent *me = static_cast<QMouseEvent *>(event);
if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 20) {
bool isMaterial = m_materialToDrag.isValid();
QByteArray data;
QMimeData *mimeData = new QMimeData;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << m_materialToDrag.internalId();
mimeData->setData(Constants::MIME_TYPE_MATERIAL, data);
mimeData->setData(isMaterial ? QString::fromLatin1(Constants::MIME_TYPE_MATERIAL)
: QString::fromLatin1(Constants::MIME_TYPE_TEXTURE),
data);
mimeData->removeFormat("text/plain");
model->startDrag(mimeData, m_previewImageProvider->requestPixmap(
QString::number(m_materialToDrag.internalId()), nullptr, {128, 128}));
if (isMaterial) {
model->startDrag(mimeData, m_previewImageProvider->requestPixmap(
QString::number(m_materialToDrag.internalId()), nullptr, {128, 128}));
} else {
QString iconPath = QLatin1String("%1/%2")
.arg(DocumentManager::currentResourcePath().path(),
m_textureToDrag.variantProperty("source").value().toString());
model->startDrag(mimeData, QPixmap(iconPath).scaled({128, 128}));
}
m_materialToDrag = {};
m_textureToDrag = {};
}
}
} else if (event->type() == QMouseEvent::MouseButtonRelease) {
m_materialToDrag = {};
m_textureToDrag = {};
}
return QObject::eventFilter(obj, event);
@@ -197,6 +211,12 @@ void MaterialBrowserWidget::startDragMaterial(int index, const QPointF &mousePos
m_dragStartPoint = mousePos.toPoint();
}
void MaterialBrowserWidget::startDragTexture(int index, const QPointF &mousePos)
{
m_textureToDrag = m_materialBrowserTexturesModel->textureAt(index);
m_dragStartPoint = mousePos.toPoint();
}
void MaterialBrowserWidget::acceptBundleMaterialDrop()
{
m_materialBrowserView->emitCustomNotification("drop_bundle_material", {}, {}); // To ContentLibraryView

View File

@@ -49,6 +49,7 @@ public:
Q_INVOKABLE void handleSearchFilterChanged(const QString &filterText);
Q_INVOKABLE void startDragMaterial(int index, const QPointF &mousePos);
Q_INVOKABLE void startDragTexture(int index, const QPointF &mousePos);
Q_INVOKABLE void acceptBundleMaterialDrop();
QQuickWidget *quickWidget() const;
@@ -72,6 +73,7 @@ private:
QString m_filterText;
ModelNode m_materialToDrag;
ModelNode m_textureToDrag;
QPoint m_dragStartPoint;
};

View File

@@ -75,6 +75,7 @@ const char MATERIAL_LIB_ID[] = "__materialLibrary__";
const char MIME_TYPE_ITEM_LIBRARY_INFO[] = "application/vnd.qtdesignstudio.itemlibraryinfo";
const char MIME_TYPE_ASSETS[] = "application/vnd.qtdesignstudio.assets";
const char MIME_TYPE_MATERIAL[] = "application/vnd.qtdesignstudio.material";
const char MIME_TYPE_TEXTURE[] = "application/vnd.qtdesignstudio.texture";
const char MIME_TYPE_BUNDLE_MATERIAL[] = "application/vnd.qtdesignstudio.bundlematerial";
const char MIME_TYPE_BUNDLE_TEXTURE[] = "application/vnd.qtdesignstudio.bundletexture";
const char MIME_TYPE_ASSET_IMAGE[] = "application/vnd.qtdesignstudio.asset.image";