Skip to content

Commit

Permalink
fix: [framework] improve appletitem management
Browse files Browse the repository at this point in the history
Add debugging assistance function.
Fix the bug that appletitem cannot be released correctly.

Log: Improve item management.
  • Loading branch information
rb-union committed Jun 18, 2024
1 parent b8f52bc commit 4b863d1
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 46 deletions.
1 change: 1 addition & 0 deletions include/dfm-framework/lifecycle/pluginquickmetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class PluginQuickMetaDataCreator final
QT_BEGIN_NAMESPACE
#ifndef QT_NO_DEBUG_STREAM
Q_CORE_EXPORT QDebug operator<<(QDebug, const DPF_NAMESPACE::PluginQuickMetaData &);
Q_CORE_EXPORT QDebug operator<<(QDebug, const DPF_NAMESPACE::PluginQuickMetaPtr &);
#endif // QT_NO_DEBUG_STREAM
QT_END_NAMESPACE

Expand Down
2 changes: 2 additions & 0 deletions include/dfm-gui/applet.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Applet : public QObject
void setComponentUrl(const QUrl &url);
Q_SIGNAL void componentUrlChanged(const QUrl &url);

void dumpAppletTree();

protected:
explicit Applet(AppletPrivate &dd, QObject *parent = nullptr);
QScopedPointer<AppletPrivate> dptr;
Expand Down
2 changes: 2 additions & 0 deletions include/dfm-gui/appletitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AppletItem : public QQuickItem
Applet *applet() const;
void setApplet(Applet *applet);

bool isCreateComplete() const;

Q_INVOKABLE QQuickWindow *itemWindow() const;
Q_SIGNAL void itemWindowChanged(QQuickWindow *window);

Expand Down
4 changes: 2 additions & 2 deletions include/dfm-gui/windowhandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class QQuickWindow;
DFMGUI_BEGIN_NAMESPACE

class Panel;
class FileManagerWindowHandleData;
class WindowHandleData;

class WindowHandle
{
Expand All @@ -32,7 +32,7 @@ class WindowHandle

private:
Q_DISABLE_COPY(WindowHandle);
QScopedPointer<FileManagerWindowHandleData> data;
QScopedPointer<WindowHandleData> data;
};

using WindowHandlePtr = QSharedPointer<WindowHandle>;
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/utils/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void onClipboardDataChanged()
return;
}
const QString &data = mimeData->data(kGnomeCopyKey);
const static QRegExp regCut("cut\nfile://"), regCopy("copy\nfile://");
const static QRegularExpression regCut("cut\nfile://"), regCopy("copy\nfile://");
if (data.contains(regCut)) {
clipboardAction = ClipBoard::kCutAction;
} else if (data.contains(regCopy)) {
Expand Down
25 changes: 17 additions & 8 deletions src/dfm-framework/lifecycle/pluginmetaobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,20 @@ Q_CORE_EXPORT QDebug operator<<(QDebug out, const DPF_NAMESPACE::PluginMetaObjec
}

/*!
* \brief 重定向全局Debug打印 PluginQuickInfo 对象的函数
* \brief operator <<
* 重定向全局Debug入口函数
* \param out
* \param pointer
* \return
*/
Q_CORE_EXPORT QDebug operator<<(QDebug out, const DPF_NAMESPACE::PluginMetaObjectPointer &pointer)
{
out << *pointer;
return out;
}

/*!
* \brief 重定向全局Debug打印 PluginQuickMetaData 对象的函数
*/
Q_CORE_EXPORT QDebug operator<<(QDebug out, const DPF_NAMESPACE::PluginQuickMetaData &quickMeta)
{
Expand All @@ -237,15 +250,11 @@ Q_CORE_EXPORT QDebug operator<<(QDebug out, const DPF_NAMESPACE::PluginQuickMeta
}

/*!
* \brief operator <<
* 重定向全局Debug入口函数
* \param out
* \param pointer
* \return
* \brief 重定向全局Debug打印 PluginQuickMetaPtr 的函数
*/
Q_CORE_EXPORT QDebug operator<<(QDebug out, const DPF_NAMESPACE::PluginMetaObjectPointer &pointer)
Q_CORE_EXPORT QDebug operator<<(QDebug out, const DPF_NAMESPACE::PluginQuickMetaPtr &quickMetaPtr)
{
out << *pointer;
out << *quickMetaPtr;
return out;
}

Expand Down
42 changes: 32 additions & 10 deletions src/dfm-gui/applet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <dfm-gui/applet.h>
#include <dfm-gui/containment.h>
#include "applet_p.h"
#include "containment_p.h"

#include <QJSEngine>

Expand All @@ -19,6 +20,7 @@ AppletPrivate::~AppletPrivate()
{
// 释放关联的 QQuickItem
if (rootObject && QJSEngine::CppOwnership == QJSEngine::objectOwnership(rootObject)) {
Q_ASSERT_X(rootObject->parent() == q_ptr, "AppletItem memory management", "Undefined behaviour, unmanaged QQuickItem");
rootObject->deleteLater();
}
}
Expand All @@ -34,6 +36,24 @@ void AppletPrivate::setRootObject(QObject *item)
Q_EMIT q_func()->rootObjectChanged(item);
}

/*!
* \brief 按层级递归打印当前 Applet 树信息
*/
void AppletPrivate::dumpAppletTreeImpl(int level)
{
QString indent = QString(' ').repeated(level * 4);
qCDebug(logDFMGui) << indent << metaPtr;

if (flag.testFlag(Applet::kContainment)) {
if (auto containment = dynamic_cast<ContainmentPrivate *>(this)) {
qCDebug(logDFMGui) << indent << QStringLiteral("Applet chiledren:");
for (Applet *child : containment->applets) {
child->dptr->dumpAppletTreeImpl(level + 1);
}
}
}
}

/*!
* \class Applet
* \brief 管理插件 QML 组件的最小单元,包含基础的组件信息.
Expand Down Expand Up @@ -94,22 +114,16 @@ QObject *Applet::rootObject() const
}

/*!
* \brief 返回当前 Applet 的容器 Containment , 若自身是容器则会返回当前对象
* \return 容器指针
* \brief 返回当前 Applet 的容器 Containment , 会递归向上查找
* \return 容器指针,若父对象没有容器,返回 nullptr
*/
Containment *Applet::containment() const
{
Containment *contain = qobject_cast<Containment *>(const_cast<Applet *>(this));
if (contain) {
return contain;
}
contain = nullptr;

Containment *contain = nullptr;
QObject *parent = this->parent();

while (parent) {
Containment *tmp = qobject_cast<Containment *>(parent);
if (tmp) {
if (Containment *tmp = qobject_cast<Containment *>(parent)) {
contain = tmp;
break;
}
Expand Down Expand Up @@ -155,4 +169,12 @@ void Applet::setComponentUrl(const QUrl &url)
}
}

/*!
* \brief 打印当前 Applet 树信息
*/
void Applet::dumpAppletTree()
{
d_func()->dumpAppletTreeImpl();
}

DFMGUI_END_NAMESPACE
1 change: 1 addition & 0 deletions src/dfm-gui/applet_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AppletPrivate
virtual ~AppletPrivate();

void setRootObject(QObject *item);
void dumpAppletTreeImpl(int level = 0);

Applet *q_ptr;
QUrl currentUrl; // 当前 Applet 操作的文件 Url
Expand Down
11 changes: 11 additions & 0 deletions src/dfm-gui/appletitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ void AppletItem::setApplet(Applet *applet)
d_func()->applet = applet;
}

/*!
* \return 返回当前 Item 是否被初始化完成,目前是对 QQuickItem::isComponentComplete
* 的封装,用于判断是否被 QQmlEngine 完成创建
*/
bool AppletItem::isCreateComplete() const
{
Q_D(const AppletItem);
return isComponentComplete() && d->applet;
}

/*!
* \return 当前 Item 所在的顶层窗体
*/
Expand Down Expand Up @@ -81,6 +91,7 @@ AppletItem *AppletItem::itemForApplet(Applet *applet)
QObject *rootObject = engine.rootObject();
if (rootObject) {
if (auto item = qobject_cast<AppletItem *>(rootObject)) {
item->setApplet(applet);
applet->dptr->setRootObject(item);
return item;
}
Expand Down
8 changes: 6 additions & 2 deletions src/dfm-gui/appletmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ Applet *AppletManagerPrivate::createAppletFromNode(const AppletTemplateNode::Ptr
}

if (applet->flags().testFlag(Applet::kContainment) && !node->childNode.isEmpty()) {
Containment *cotainment = applet->containment();
Containment *containment = qobject_cast<Containment *>(applet);
if (!containment) {
return nullptr;
}

for (auto childNode : std::as_const(node->childNode)) {
Applet *childApplet = createAppletFromNode(childNode, nullptr);
cotainment->appendApplet(childApplet);
containment->appendApplet(childApplet);
}
}

Expand Down
21 changes: 2 additions & 19 deletions src/dfm-gui/attachedproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@ AppletAttached::~AppletAttached() { }

Applet *AppletAttached::qmlAttachedProperties(QObject *object)
{
AppletItem *item = qobject_cast<AppletItem *>(object);
if (item) {
return item->applet();
}
Applet *applet = qobject_cast<Applet *>(object);
if (applet) {
return applet;
}

QObject *parent = object->parent();
while (parent) {
if (auto parItem = qobject_cast<AppletItem *>(parent)) {
return parItem->applet();
}

parent = parent->parent();
}

if (auto context = qmlContext(object)) {
return context->contextProperty("_dfm_applet").value<Applet *>();
}
Expand All @@ -62,7 +44,8 @@ ContainmentAttached::~ContainmentAttached() { }

Containment *ContainmentAttached::qmlAttachedProperties(QObject *object)
{
return qobject_cast<Containment *>(AppletAttached::qmlAttachedProperties(object));
auto contain = qobject_cast<Containment *>(AppletAttached::qmlAttachedProperties(object));
return contain;
}

DFMGUI_END_NAMESPACE
8 changes: 4 additions & 4 deletions src/dfm-gui/windowhandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

DFMGUI_BEGIN_NAMESPACE

class FileManagerWindowHandleData : public QSharedData
class WindowHandleData : public QSharedData
{
public:
QPointer<Panel> panel;
Expand All @@ -22,18 +22,18 @@ class FileManagerWindowHandleData : public QSharedData
* \brief 提供用于操作的窗口和Applet指针
*/
WindowHandle::WindowHandle()
: data(new FileManagerWindowHandleData)
: data(new WindowHandleData)
{
}

WindowHandle::WindowHandle(const QString &error)
: data(new FileManagerWindowHandleData)
: data(new WindowHandleData)
{
data->errorString = error;
}

WindowHandle::WindowHandle(Panel *p, QQuickWindow *w)
: data(new FileManagerWindowHandleData)
: data(new WindowHandleData)
{
data->panel = p;
data->window = w;
Expand Down
1 change: 1 addition & 0 deletions src/dfm-gui/windowmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void WindowManagerPrivate::asyncLoadQuickItem(Applet *applet)
tmpEngine->completeCreation();
QObject *rootObject = tmpEngine->rootObject();
if (AppletItem *item = qobject_cast<AppletItem *>(rootObject)) {
item->setApplet(applet);
applet->dptr->setRootObject(item);

if (auto containment = applet->containment()) {
Expand Down

0 comments on commit 4b863d1

Please sign in to comment.