forked from qt-creator/qt-creator
QmlDesigner: Modernize ownership for MimeData
Using raw pointer for ownership is hard to track. std::unqiue_ptr and Utils::UniqueObjectPtr take care of it. Change-Id: I163a97db0d8aabc7b9a75659e94d6562ea8dc969 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -58,16 +58,17 @@ bool AssetsLibraryView::hasWidget() const
|
||||
|
||||
WidgetInfo AssetsLibraryView::widgetInfo()
|
||||
{
|
||||
if (m_widget.isNull()) {
|
||||
m_widget = new AssetsLibraryWidget{imageCacheData()->asynchronousFontImageCache,
|
||||
imageCacheData()->synchronousFontImageCache,
|
||||
this};
|
||||
if (!m_widget) {
|
||||
m_widget = Utils::makeUniqueObjectPtr<AssetsLibraryWidget>(
|
||||
imageCacheData()->asynchronousFontImageCache,
|
||||
imageCacheData()->synchronousFontImageCache,
|
||||
this);
|
||||
|
||||
auto context = new Internal::AssetsLibraryContext(m_widget.data());
|
||||
auto context = new Internal::AssetsLibraryContext(m_widget.get());
|
||||
Core::ICore::addContextObject(context);
|
||||
}
|
||||
|
||||
return createWidgetInfo(m_widget.data(), "Assets", WidgetInfo::LeftPane, tr("Assets"));
|
||||
return createWidgetInfo(m_widget.get(), "Assets", WidgetInfo::LeftPane, tr("Assets"));
|
||||
}
|
||||
|
||||
void AssetsLibraryView::customNotification(const AbstractView * /*view*/,
|
||||
@@ -101,10 +102,11 @@ void AssetsLibraryView::setResourcePath(const QString &resourcePath)
|
||||
|
||||
m_lastResourcePath = resourcePath;
|
||||
|
||||
if (m_widget.isNull()) {
|
||||
m_widget = new AssetsLibraryWidget{imageCacheData()->asynchronousFontImageCache,
|
||||
imageCacheData()->synchronousFontImageCache,
|
||||
this};
|
||||
if (!m_widget) {
|
||||
m_widget = Utils::makeUniqueObjectPtr<AssetsLibraryWidget>(
|
||||
imageCacheData()->asynchronousFontImageCache,
|
||||
imageCacheData()->synchronousFontImageCache,
|
||||
this);
|
||||
}
|
||||
|
||||
m_widget->setResourcePath(resourcePath);
|
||||
|
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "abstractview.h"
|
||||
|
||||
#include <utils/uniqueobjectptr.h>
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
#include <mutex>
|
||||
@@ -40,7 +42,7 @@ private:
|
||||
|
||||
std::once_flag imageCacheFlag;
|
||||
std::unique_ptr<ImageCacheData> m_imageCacheData;
|
||||
QPointer<AssetsLibraryWidget> m_widget;
|
||||
Utils::UniqueObjectPtr<AssetsLibraryWidget> m_widget;
|
||||
QString m_lastResourcePath;
|
||||
};
|
||||
|
||||
|
@@ -43,6 +43,8 @@
|
||||
#include <QToolButton>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
static QString propertyEditorResourcesPath()
|
||||
@@ -63,7 +65,7 @@ bool AssetsLibraryWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
if (!m_assetsToDrag.isEmpty() && m_assetsView->model()) {
|
||||
QMouseEvent *me = static_cast<QMouseEvent *>(event);
|
||||
if ((me->globalPosition().toPoint() - m_dragStartPoint).manhattanLength() > 10) {
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
auto mimeData = std::make_unique<QMimeData>();
|
||||
mimeData->setData(Constants::MIME_TYPE_ASSETS, m_assetsToDrag.join(',').toUtf8());
|
||||
|
||||
QList<QUrl> urlsToDrag = Utils::transform(m_assetsToDrag, [](const QString &path) {
|
||||
@@ -72,8 +74,11 @@ bool AssetsLibraryWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
|
||||
mimeData->setUrls(urlsToDrag);
|
||||
|
||||
m_assetsView->model()->startDrag(mimeData, m_assetsIconProvider->requestPixmap(
|
||||
m_assetsToDrag[0], nullptr, {128, 128}));
|
||||
m_assetsView->model()->startDrag(std::move(mimeData),
|
||||
m_assetsIconProvider->requestPixmap(m_assetsToDrag[0],
|
||||
nullptr,
|
||||
{128, 128}),
|
||||
this);
|
||||
|
||||
m_assetsToDrag.clear();
|
||||
}
|
||||
@@ -160,6 +165,8 @@ AssetsLibraryWidget::AssetsLibraryWidget(AsynchronousImageCache &asynchronousFon
|
||||
setFocusProxy(m_assetsWidget->quickWidget());
|
||||
}
|
||||
|
||||
AssetsLibraryWidget::~AssetsLibraryWidget() = default;
|
||||
|
||||
void AssetsLibraryWidget::contextHelp(const Core::IContext::HelpCallback &callback) const
|
||||
{
|
||||
if (m_assetsView)
|
||||
|
@@ -53,7 +53,7 @@ class AssetsLibraryWidget : public QFrame
|
||||
public:
|
||||
AssetsLibraryWidget(AsynchronousImageCache &asynchronousFontImageCache,
|
||||
SynchronousImageCache &synchronousFontImageCache, AssetsLibraryView *view);
|
||||
~AssetsLibraryWidget() = default;
|
||||
~AssetsLibraryWidget();
|
||||
|
||||
QList<QToolButton *> createToolBarWidgets();
|
||||
void contextHelp(const Core::IContext::HelpCallback &callback) const;
|
||||
|
@@ -72,13 +72,13 @@ bool ContentLibraryWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
QMouseEvent *me = static_cast<QMouseEvent *>(event);
|
||||
if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 20) {
|
||||
QByteArray data;
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
auto mimeData = std::make_unique<QMimeData>();
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
stream << m_itemToDrag->type();
|
||||
mimeData->setData(Constants::MIME_TYPE_BUNDLE_ITEM, data);
|
||||
|
||||
emit bundleItemDragStarted(m_itemToDrag);
|
||||
model->startDrag(mimeData, m_itemToDrag->icon().toLocalFile());
|
||||
model->startDrag(std::move(mimeData), m_itemToDrag->icon().toLocalFile(), this);
|
||||
m_itemToDrag = nullptr;
|
||||
}
|
||||
} else if (m_materialToDrag) {
|
||||
@@ -86,21 +86,21 @@ bool ContentLibraryWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
if ((me->globalPosition().toPoint() - m_dragStartPoint).manhattanLength() > 20
|
||||
&& m_materialsModel->isMaterialDownloaded(m_materialToDrag)) {
|
||||
QByteArray data;
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
auto mimeData = std::make_unique<QMimeData>();
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
stream << m_materialToDrag->type();
|
||||
mimeData->setData(Constants::MIME_TYPE_BUNDLE_MATERIAL, data);
|
||||
mimeData->removeFormat("text/plain");
|
||||
|
||||
emit bundleMaterialDragStarted(m_materialToDrag);
|
||||
model->startDrag(mimeData, m_materialToDrag->icon().toLocalFile());
|
||||
model->startDrag(std::move(mimeData), m_materialToDrag->icon().toLocalFile(), this);
|
||||
m_materialToDrag = nullptr;
|
||||
}
|
||||
} else if (m_textureToDrag) {
|
||||
QMouseEvent *me = static_cast<QMouseEvent *>(event);
|
||||
if ((me->globalPosition().toPoint() - m_dragStartPoint).manhattanLength() > 20
|
||||
&& m_textureToDrag->isDownloaded()) {
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
auto mimeData = std::make_unique<QMimeData>();
|
||||
mimeData->setData(Constants::MIME_TYPE_BUNDLE_TEXTURE,
|
||||
{m_textureToDrag->texturePath().toUtf8()});
|
||||
|
||||
@@ -108,7 +108,7 @@ bool ContentLibraryWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
mimeData->setUrls({QUrl::fromLocalFile(m_textureToDrag->texturePath())});
|
||||
|
||||
emit bundleTextureDragStarted(m_textureToDrag);
|
||||
model->startDrag(mimeData, m_textureToDrag->icon().toLocalFile());
|
||||
model->startDrag(std::move(mimeData), m_textureToDrag->icon().toLocalFile(), this);
|
||||
m_textureToDrag = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -438,8 +438,6 @@ QStringList ContentLibraryWidget::saveNewTextures(const QDir &bundleDir, const Q
|
||||
|
||||
bool hasFile = (o["file"] == file);
|
||||
return hasFile;
|
||||
|
||||
return false;
|
||||
});
|
||||
return !contains;
|
||||
});
|
||||
|
@@ -486,9 +486,9 @@ void ItemLibraryModel::update(Model *model)
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QMimeData *ItemLibraryModel::getMimeData(const ItemLibraryEntry &itemLibraryEntry)
|
||||
std::unique_ptr<QMimeData> ItemLibraryModel::getMimeData(const ItemLibraryEntry &itemLibraryEntry)
|
||||
{
|
||||
auto mimeData = new QMimeData();
|
||||
auto mimeData = std::make_unique<QMimeData>();
|
||||
|
||||
QByteArray data;
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
|
@@ -40,7 +40,7 @@ public:
|
||||
void update(Model *model);
|
||||
void updateUsedImports(const Imports &usedImports);
|
||||
|
||||
QMimeData *getMimeData(const ItemLibraryEntry &itemLibraryEntry);
|
||||
std::unique_ptr<QMimeData> getMimeData(const ItemLibraryEntry &itemLibraryEntry);
|
||||
|
||||
void setSearchText(const QString &searchText);
|
||||
void setFlowMode(bool);
|
||||
|
@@ -96,7 +96,8 @@ bool ItemLibraryWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
if (model) {
|
||||
model->startDrag(m_itemLibraryModel->getMimeData(entry),
|
||||
::Utils::StyleHelper::dpiSpecificImageFile(
|
||||
entry.libraryEntryIconPath()));
|
||||
entry.libraryEntryIconPath()),
|
||||
this);
|
||||
}
|
||||
|
||||
m_itemToDrag = {};
|
||||
|
@@ -104,14 +104,18 @@ bool MaterialBrowserWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
QMouseEvent *me = static_cast<QMouseEvent *>(event);
|
||||
if ((me->globalPosition().toPoint() - m_dragStartPoint).manhattanLength() > 20) {
|
||||
bool isMaterial = m_materialToDrag.isValid();
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
auto mimeData = std::make_unique<QMimeData>();
|
||||
QByteArray internalId;
|
||||
|
||||
if (isMaterial) {
|
||||
internalId.setNum(m_materialToDrag.internalId());
|
||||
mimeData->setData(Constants::MIME_TYPE_MATERIAL, internalId);
|
||||
model->startDrag(mimeData, m_previewImageProvider->requestPixmap(
|
||||
QString::number(m_materialToDrag.internalId()), nullptr, {128, 128}));
|
||||
model->startDrag(std::move(mimeData),
|
||||
m_previewImageProvider->requestPixmap(
|
||||
QString::number(m_materialToDrag.internalId()),
|
||||
nullptr,
|
||||
{128, 128}),
|
||||
this);
|
||||
} else {
|
||||
internalId.setNum(m_textureToDrag.internalId());
|
||||
mimeData->setData(Constants::MIME_TYPE_TEXTURE, internalId);
|
||||
@@ -129,7 +133,7 @@ bool MaterialBrowserWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
pixmap = Utils::StyleHelper::dpiSpecificImageFile(iconPath);
|
||||
if (pixmap.isNull())
|
||||
pixmap = Utils::StyleHelper::dpiSpecificImageFile(":/textureeditor/images/texture_default.png");
|
||||
model->startDrag(mimeData, pixmap.scaled({128, 128}));
|
||||
model->startDrag(std::move(mimeData), pixmap.scaled({128, 128}), this);
|
||||
}
|
||||
m_materialToDrag = {};
|
||||
m_textureToDrag = {};
|
||||
|
@@ -12,6 +12,7 @@
|
||||
#include <projectstorage/projectstorageinfotypes.h>
|
||||
#include <projectstorage/projectstorageobserver.h>
|
||||
#include <projectstorageids.h>
|
||||
#include <utils/uniqueobjectptr.h>
|
||||
|
||||
#include <QMimeData>
|
||||
#include <QObject>
|
||||
@@ -258,7 +259,7 @@ public:
|
||||
|
||||
QString generateNewId(const QString &prefixName, const QString &fallbackPrefix = "element") const;
|
||||
|
||||
void startDrag(QMimeData *mimeData, const QPixmap &icon);
|
||||
void startDrag(std::unique_ptr<QMimeData> mimeData, const QPixmap &icon, QWidget *sourceWidget);
|
||||
void endDrag();
|
||||
|
||||
void setCurrentStateNode(const ModelNode &node);
|
||||
|
@@ -1906,22 +1906,21 @@ QString Model::generateNewId(const QString &prefixName, const QString &fallbackP
|
||||
});
|
||||
}
|
||||
|
||||
void Model::startDrag(QMimeData *mimeData, const QPixmap &icon)
|
||||
void Model::startDrag(std::unique_ptr<QMimeData> mimeData, const QPixmap &icon, QWidget *dragSource)
|
||||
{
|
||||
d->notifyDragStarted(mimeData);
|
||||
d->notifyDragStarted(mimeData.get());
|
||||
|
||||
auto drag = new QDrag(this);
|
||||
drag->setPixmap(icon);
|
||||
drag->setMimeData(mimeData);
|
||||
if (drag->exec() == Qt::IgnoreAction)
|
||||
d->drag = Utils::makeUniqueObjectPtr<QDrag>(dragSource);
|
||||
d->drag->setPixmap(icon);
|
||||
d->drag->setMimeData(mimeData.release());
|
||||
if (d->drag->exec() == Qt::IgnoreAction)
|
||||
endDrag();
|
||||
|
||||
drag->deleteLater();
|
||||
}
|
||||
|
||||
void Model::endDrag()
|
||||
{
|
||||
d->notifyDragEnded();
|
||||
d->drag.reset();
|
||||
}
|
||||
|
||||
void Model::setCurrentStateNode(const ModelNode &node)
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
#include <nodemetainfo.h>
|
||||
|
||||
#include <utils/uniqueobjectptr.h>
|
||||
|
||||
#include <QDrag>
|
||||
#include <QList>
|
||||
#include <QPointer>
|
||||
#include <QSet>
|
||||
@@ -381,7 +384,7 @@ private:
|
||||
QPointer<NodeInstanceView> m_nodeInstanceView;
|
||||
QPointer<Model> m_metaInfoProxyModel;
|
||||
QHash<TypeName, std::shared_ptr<NodeMetaInfoPrivate>> m_nodeMetaInfoCache;
|
||||
|
||||
Utils::UniqueObjectPtr<QDrag> drag;
|
||||
bool m_writeLock = false;
|
||||
qint32 m_internalIdCounter = 1;
|
||||
};
|
||||
|
Reference in New Issue
Block a user