新增modelManager统一管理全部model,增加测试代码

This commit is contained in:
hewenfei 2022-12-12 14:15:15 +08:00
parent bf7362d85c
commit 6139108645
7 changed files with 149 additions and 6 deletions

View File

@ -69,6 +69,7 @@ set(SOURCE_FILES
src/settings/settings.cpp src/settings/settings.h
src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h
src/ukui-menu-application.cpp src/ukui-menu-application.h
src/model/model-manager.cpp src/model/model-manager.h
)
# qrc

View File

@ -10,6 +10,26 @@ Item {
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 {
x: 400
width: 200

View File

@ -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

44
src/model/model-manager.h Normal file
View File

@ -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

View File

@ -21,10 +21,34 @@
#include <QObject>
#include <QString>
#include <QDebug>
//#include <gio/gio.h>
#include <QGSettings>
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

View File

@ -19,14 +19,24 @@
#ifndef UKUI_MENU_MODEL_H
#define UKUI_MENU_MODEL_H
#include <QList>
#include <QAbstractListModel>
#include "commons.h"
namespace UkuiMenu {
class Model
class AppModel : public QAbstractListModel
{
Q_OBJECT
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

View File

@ -19,6 +19,7 @@
#include "ukui-menu-application.h"
#include "settings.h"
#include "commons.h"
#include "model-manager.h"
#include <QCoreApplication>
#include <QCommandLineParser>
@ -45,7 +46,7 @@ void UkuiMenuApplication::registerQmlTypes()
const char *uri = "org.ukui.menu.core";
int versionMajor = 1, versionMinor = 0;
CommonsModule::defineCommonsMod(uri, versionMajor, versionMinor);
ModelManager::registerMetaTypes();
}
void UkuiMenuApplication::initQmlEngine()
@ -56,6 +57,7 @@ void UkuiMenuApplication::initQmlEngine()
m_applicationEngine->addImportPath("qrc:/qml");
m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance());
m_applicationEngine->rootContext()->setContextProperty("modelManager", new ModelManager(this));
QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated,
this, [url](QObject *obj, const QUrl &objUrl) {