Skip to content

Commit 8fc7aa2

Browse files
committed
feat: [titlebar] connect qml with dpf events
Create crumbbar component, connent qml with dpf event, click item will send event and change titile. Log: Connect qml with dpf events
1 parent 47ff71b commit 8fc7aa2

File tree

12 files changed

+367
-7
lines changed

12 files changed

+367
-7
lines changed

src/plugins/filemanager/core/dfmplugin-titlebar/events/titlebareventcaller.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,16 @@ ViewMode TitleBarEventCaller::sendGetDefualtViewMode(const QString &scheme)
9898
int defaultViewMode = dpfSlotChannel->push("dfmplugin_workspace", "slot_View_GetDefaultViewMode", scheme).toInt();
9999
return static_cast<ViewMode>(defaultViewMode);
100100
}
101+
102+
void TitleBarEventCaller::sendCd(dfmgui::Applet *applet, const QUrl &url)
103+
{
104+
DFMBASE_USE_NAMESPACE
105+
quint64 id = TitleBarHelper::windowId(applet);
106+
Q_ASSERT(id > 0);
107+
if (!url.isValid()) {
108+
fmWarning() << "Invalid url: " << url;
109+
return;
110+
}
111+
112+
dpfSignalDispatcher->publish(DFMBASE_NAMESPACE::GlobalEventType::kChangeCurrentUrl, id, url);
113+
}

src/plugins/filemanager/core/dfmplugin-titlebar/events/titlebareventcaller.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <dfm-base/dfm_global_defines.h>
1111

12+
#include <dfm-gui/applet.h>
13+
1214
#include <QObject>
1315

1416
namespace dfmplugin_titlebar {
@@ -30,6 +32,8 @@ class TitleBarEventCaller
3032
static void sendCheckAddressInputStr(QWidget *sender, QString *str);
3133
static bool sendCheckTabAddable(quint64 windowId);
3234
static DFMGLOBAL_NAMESPACE::ViewMode sendGetDefualtViewMode(const QString &scheme);
35+
36+
static void sendCd(dfmgui::Applet *applet, const QUrl &url);
3337
};
3438

3539
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
#include "quickcrumbmodel.h"
6+
7+
namespace dfmplugin_titlebar {
8+
9+
QuickCrumbModel::QuickCrumbModel(QObject *parent)
10+
: QAbstractListModel { parent }
11+
{
12+
}
13+
14+
void QuickCrumbModel::setCurrentUrl(const QUrl &url, const QList<CrumbData> &crumbDataList)
15+
{
16+
beginResetModel();
17+
fileUrl = url;
18+
crumbData = crumbDataList;
19+
endResetModel();
20+
}
21+
22+
int QuickCrumbModel::rowCount(const QModelIndex &) const
23+
{
24+
return crumbData.size();
25+
}
26+
27+
QVariant QuickCrumbModel::data(const QModelIndex &index, int role) const
28+
{
29+
if (index.row() < 0 || index.row() >= crumbData.size()) {
30+
return {};
31+
}
32+
33+
const CrumbData &data = crumbData.at(index.row());
34+
switch (role) {
35+
case FileUrlRole:
36+
return data.url;
37+
case FullUrlRole:
38+
return fileUrl;
39+
case TextRole:
40+
return data.displayText;
41+
case IconRole:
42+
return data.iconName;
43+
case UseIconRole:
44+
return !data.iconName.isEmpty();
45+
}
46+
47+
return {};
48+
}
49+
50+
QHash<int, QByteArray> QuickCrumbModel::roleNames() const
51+
{
52+
QHash<int, QByteArray> roles;
53+
roles[FileUrlRole] = "fileUrl";
54+
roles[FullUrlRole] = "fullUrl";
55+
roles[TextRole] = "text";
56+
roles[IconRole] = "icon";
57+
roles[UseIconRole] = "useIcon";
58+
return roles;
59+
}
60+
61+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
#ifndef QUICKCRUMBMODEL_H
6+
#define QUICKCRUMBMODEL_H
7+
8+
#include "dfmplugin_titlebar_global.h"
9+
10+
#include <QAbstractListModel>
11+
#include <QUrl>
12+
13+
namespace dfmplugin_titlebar {
14+
15+
class QuickCrumbModel : public QAbstractListModel
16+
{
17+
Q_OBJECT
18+
public:
19+
enum Roles {
20+
FileUrlRole = Qt::UserRole + 1,
21+
FullUrlRole,
22+
TextRole,
23+
IconRole,
24+
UseIconRole,
25+
};
26+
Q_ENUM(Roles)
27+
28+
explicit QuickCrumbModel(QObject *parent = nullptr);
29+
30+
void setCurrentUrl(const QUrl &url, const QList<CrumbData> &crumbDataList);
31+
32+
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
33+
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
34+
QHash<int, QByteArray> roleNames() const override;
35+
36+
private:
37+
QUrl fileUrl;
38+
QList<CrumbData> crumbData;
39+
};
40+
41+
}
42+
43+
#endif // QUICKCRUMBMODEL_H
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
import QtQuick
6+
import QtQuick.Controls
7+
import org.dfm.base
8+
import org.deepin.dtk
9+
import org.deepin.dtk.private 1.0 as P
10+
11+
///! 面包屑导航栏
12+
Item {
13+
id: breadcrumbs
14+
15+
// 请求弹出同级文件夹列表
16+
signal requsetQuickJumpMenu(var url, real x, real y)
17+
18+
ListView {
19+
id: addressline
20+
21+
anchors.fill: parent
22+
model: Containment.crumbModel
23+
orientation: ListView.Horizontal
24+
25+
delegate: Button {
26+
id: addressBtn
27+
28+
enabled: index !== (ListView.view.count - 1)
29+
icon.name: model.useIcon ? model.icon : undefined
30+
leftInset: 0
31+
padding: 0
32+
rightInset: 0
33+
spacing: 0
34+
text: model.useIcon ? undefined : model.text
35+
36+
background: P.ButtonPanel {
37+
id: bkgPanel
38+
39+
button: addressBtn
40+
implicitWidth: addressBtn.contentItem.implicitWidth
41+
visible: addressBtn.enabled && addressBtn.hovered
42+
}
43+
indicator: Item {
44+
implicitHeight: 20
45+
implicitWidth: 10
46+
47+
Text {
48+
anchors.centerIn: parent
49+
font.bold: false
50+
font.family: "Noto Sans CJK TC"
51+
text: "/"
52+
visible: !bkgPanel.visible
53+
}
54+
55+
Loader {
56+
enabled: bkgPanel.visible
57+
58+
sourceComponent: Button {
59+
id: popupBtn
60+
61+
height: 16
62+
icon.height: 10
63+
icon.name: "combobox_arrow"
64+
icon.width: 10
65+
visible: bkgPanel.visible
66+
width: 16
67+
68+
onClicked: {
69+
breadcrumbs.requsetQuickJumpMenu(model.fileUrl, popupBtn.x, popupBtn.y + 20);
70+
}
71+
72+
anchors {
73+
right: parent.right
74+
rightMargin: 10
75+
verticalCenter: parent.verticalCenter
76+
}
77+
}
78+
}
79+
}
80+
81+
onClicked: {
82+
Containment.currentUrl = model.fileUrl;
83+
}
84+
}
85+
}
86+
}

src/plugins/filemanager/core/dfmplugin-titlebar/qml/Titlebar.qml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.dfm.base
1010
import org.deepin.dtk
1111
import org.deepin.dtk.style 1.0 as DS
1212

13-
AppletItem {
13+
ContainmentItem {
1414
id: titlebar
1515

1616
property int breadcrumbsHeight: 30
@@ -30,7 +30,6 @@ AppletItem {
3030

3131
RowLayout {
3232
Layout.fillHeight: true
33-
Layout.fillWidth: true
3433
layoutDirection: Qt.LeftToRight
3534

3635
CheckBox {
@@ -67,16 +66,20 @@ AppletItem {
6766

6867
IconButton {
6968
icon.name: "button_add"
69+
70+
onClicked: {
71+
console.warn("--- test", Containment.applets);
72+
Applet.currentUrl = "file:///home/uos/Downloads/GammaRay/build/bin";
73+
}
7074
}
7175
}
7276

7377
Loader {
74-
Layout.preferredWidth: item ? item.implicitWidth : 0
78+
Layout.fillWidth: true
7579
active: Window.window
7680
height: DS.Style.titleBar.height
7781

7882
sourceComponent: TitleBar {
79-
// replace Window.window.width
8083
width: parent.width
8184
}
8285
}
@@ -89,12 +92,11 @@ AppletItem {
8992
implicitHeight: breadcrumbsHeight
9093
spacing: 0
9194

92-
Rectangle {
95+
CrumbBar {
9396
id: breadcrumbs
9497

9598
Layout.fillHeight: true
9699
Layout.fillWidth: true
97-
color: "blue"
98100
}
99101

100102
Rectangle {

src/plugins/filemanager/core/dfmplugin-titlebar/titlebar.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

55
#include "titlebar.h"
6+
#include "titlebarcontainment.h"
67
#include "utils/titlebarhelper.h"
78
#include "utils/crumbinterface.h"
89
#include "utils/crumbmanager.h"
@@ -16,11 +17,29 @@
1617
#include <dfm-base/base/urlroute.h>
1718
#include <dfm-base/dfm_event_defines.h>
1819

20+
#include <dfm-gui/containment.h>
21+
#include <dfm-gui/appletfactory.h>
22+
1923
#include <dfm-framework/dpf.h>
2024

2125
namespace dfmplugin_titlebar {
2226
DFM_LOG_REISGER_CATEGORY(DPTITLEBAR_NAMESPACE)
2327

28+
static constexpr char kAppletUrl[] { "org.dfm.titlebar" };
29+
30+
static dfmgui::Applet *createTitlebarApplet(const QString &url, dfmgui::Containment *parent, QString *errorString)
31+
{
32+
if (kAppletUrl == url) {
33+
Q_ASSERT_X(parent && parent->flags().testFlag(dfmgui::Applet::kPanel),
34+
"Create titlebar applet", "Parent must based on panel");
35+
36+
auto titlebar = new TitlebarContainment(parent);
37+
QObject::connect(parent, &dfmgui::Applet::currentUrlChanged, titlebar, &TitlebarContainment::setCurrentUrl);
38+
return titlebar;
39+
}
40+
return nullptr;
41+
}
42+
2443
void TitleBar::initialize()
2544
{
2645
DFMBASE_USE_NAMESPACE
@@ -31,6 +50,14 @@ void TitleBar::initialize()
3150

3251
// event has been sended before the Window showed
3352
bindEvents();
53+
54+
// register qml component
55+
QString errorString;
56+
bool regSuccess = dfmgui::AppletFactory::instance()->regCreator(
57+
kAppletUrl, &createTitlebarApplet, &errorString);
58+
if (!regSuccess) {
59+
fmWarning() << QString("Register applet %1 failed.").arg(kAppletUrl) << errorString;
60+
}
3461
}
3562

3663
bool TitleBar::start()

src/plugins/filemanager/core/dfmplugin-titlebar/titlebar.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
{
1717
"Url" : "qml/Titlebar.qml",
1818
"Id" : "titlebar",
19-
"Parent" : "dfmplugin-core.filewindow"
19+
"Parent" : "dfmplugin-core.filewindow",
20+
"Applet" : "org.dfm.titlebar"
2021
}
2122
]
2223
}

0 commit comments

Comments
 (0)