新增modelManager统一管理全部model,增加测试代码
This commit is contained in:
parent
bf7362d85c
commit
6139108645
|
@ -69,6 +69,7 @@ set(SOURCE_FILES
|
||||||
src/settings/settings.cpp src/settings/settings.h
|
src/settings/settings.cpp src/settings/settings.h
|
||||||
src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h
|
src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h
|
||||||
src/ukui-menu-application.cpp src/ukui-menu-application.h
|
src/ukui-menu-application.cpp src/ukui-menu-application.h
|
||||||
|
src/model/model-manager.cpp src/model/model-manager.h
|
||||||
)
|
)
|
||||||
|
|
||||||
# qrc文件
|
# qrc文件
|
||||||
|
|
|
@ -10,6 +10,26 @@ Item {
|
||||||
height: 480
|
height: 480
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ListView {
|
||||||
|
x: 200
|
||||||
|
width: 200
|
||||||
|
height: 480
|
||||||
|
clip: true
|
||||||
|
|
||||||
|
model: modelManager.getAppModel()
|
||||||
|
delegate: Rectangle {
|
||||||
|
width: ListView.view.width;
|
||||||
|
height: 40;
|
||||||
|
color: "lightblue"
|
||||||
|
Text {
|
||||||
|
anchors.fill: parent;
|
||||||
|
horizontalAlignment: Qt.AlignHCenter;
|
||||||
|
verticalAlignment: Qt.AlignVCenter;
|
||||||
|
text: model.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
AppControls2.AppTest {
|
AppControls2.AppTest {
|
||||||
x: 400
|
x: 400
|
||||||
width: 200
|
width: 200
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022, KylinSoft Co., Ltd.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "model-manager.h"
|
||||||
|
#include "model.h"
|
||||||
|
|
||||||
|
namespace UkuiMenu {
|
||||||
|
|
||||||
|
void ModelManager::registerMetaTypes()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<AppModel*>("AppModel*");
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelManager::ModelManager(QObject *parent) : QObject(parent)
|
||||||
|
{
|
||||||
|
appModel = new AppModel(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
AppModel *ModelManager::getAppModel()
|
||||||
|
{
|
||||||
|
if (appModel) {
|
||||||
|
return appModel;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // UkuiMenu
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022, KylinSoft Co., Ltd.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UKUI_MENU_MODEL_MANAGER_H
|
||||||
|
#define UKUI_MENU_MODEL_MANAGER_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
namespace UkuiMenu {
|
||||||
|
|
||||||
|
class AppModel;
|
||||||
|
|
||||||
|
class ModelManager : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
static void registerMetaTypes();
|
||||||
|
explicit ModelManager(QObject *parent = nullptr);
|
||||||
|
~ModelManager() override = default;
|
||||||
|
|
||||||
|
Q_INVOKABLE AppModel *getAppModel();
|
||||||
|
|
||||||
|
private:
|
||||||
|
AppModel *appModel{nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // UkuiMenu
|
||||||
|
|
||||||
|
#endif //UKUI_MENU_MODEL_MANAGER_H
|
|
@ -21,10 +21,34 @@
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
//#include <gio/gio.h>
|
|
||||||
|
|
||||||
#include <QGSettings>
|
|
||||||
|
|
||||||
namespace UkuiMenu {
|
namespace UkuiMenu {
|
||||||
|
|
||||||
|
AppModel::AppModel(QObject *parent) : QAbstractListModel(parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int AppModel::rowCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
return 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant AppModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
return QString("test data = %1.").arg(index.row());
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<int, QByteArray> AppModel::roleNames() const
|
||||||
|
{
|
||||||
|
QHash<int, QByteArray> names;
|
||||||
|
names.insert(0, "name");
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<DataEntity> AppModel::folderApps(const QString &folderName)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
} // UkuiMenu
|
} // UkuiMenu
|
||||||
|
|
|
@ -19,14 +19,24 @@
|
||||||
#ifndef UKUI_MENU_MODEL_H
|
#ifndef UKUI_MENU_MODEL_H
|
||||||
#define UKUI_MENU_MODEL_H
|
#define UKUI_MENU_MODEL_H
|
||||||
|
|
||||||
|
#include <QList>
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
|
|
||||||
|
#include "commons.h"
|
||||||
|
|
||||||
namespace UkuiMenu {
|
namespace UkuiMenu {
|
||||||
|
|
||||||
class Model
|
class AppModel : public QAbstractListModel
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Model() = default;
|
explicit AppModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
int rowCount(const QModelIndex &parent) const override;
|
||||||
|
QVariant data(const QModelIndex &index, int role) const override;
|
||||||
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
|
Q_INVOKABLE QList<DataEntity> folderApps(const QString &folderName);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // UkuiMenu
|
} // UkuiMenu
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "ukui-menu-application.h"
|
#include "ukui-menu-application.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "commons.h"
|
#include "commons.h"
|
||||||
|
#include "model-manager.h"
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
|
@ -45,7 +46,7 @@ void UkuiMenuApplication::registerQmlTypes()
|
||||||
const char *uri = "org.ukui.menu.core";
|
const char *uri = "org.ukui.menu.core";
|
||||||
int versionMajor = 1, versionMinor = 0;
|
int versionMajor = 1, versionMinor = 0;
|
||||||
CommonsModule::defineCommonsMod(uri, versionMajor, versionMinor);
|
CommonsModule::defineCommonsMod(uri, versionMajor, versionMinor);
|
||||||
|
ModelManager::registerMetaTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UkuiMenuApplication::initQmlEngine()
|
void UkuiMenuApplication::initQmlEngine()
|
||||||
|
@ -56,6 +57,7 @@ void UkuiMenuApplication::initQmlEngine()
|
||||||
m_applicationEngine->addImportPath("qrc:/qml");
|
m_applicationEngine->addImportPath("qrc:/qml");
|
||||||
|
|
||||||
m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance());
|
m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance());
|
||||||
|
m_applicationEngine->rootContext()->setContextProperty("modelManager", new ModelManager(this));
|
||||||
|
|
||||||
QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated,
|
QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated,
|
||||||
this, [url](QObject *obj, const QUrl &objUrl) {
|
this, [url](QObject *obj, const QUrl &objUrl) {
|
||||||
|
|
Loading…
Reference in New Issue