Skip to content

Commit

Permalink
add model
Browse files Browse the repository at this point in the history
  • Loading branch information
AMOSOMNUM committed Dec 16, 2023
1 parent 93a9dac commit 9ca023a
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 13 deletions.
43 changes: 32 additions & 11 deletions qml/page/T_LessonPic.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FluScrollablePage {
Rectangle {
id: lesson_pic
width: 1080
height: 1920 + (lesson_detail.num - 2) * 430
height: 1920 + (LessonModel.num - 2) * 430
color: "#EAEAEA"

Image {
Expand Down Expand Up @@ -84,12 +84,9 @@ FluScrollablePage {
spacing: 100

Repeater {
id: lesson_detail
property int num: 2
property var dep: ["C++", "软件研发中心"]

model: num
delegate: Rectangle {
model: LessonModel
delegate: Item {
height: 330
anchors {
left: parent.left
Expand Down Expand Up @@ -122,31 +119,55 @@ FluScrollablePage {
bottom: parent.bottom
bottomMargin: 5
}
source: "qrc:/res/image/department/cpp_1.svg"
source: icon
fillMode: Image.PreserveAspectFit
}

Text {
leftPadding: 20
rightPadding: 40
height: dep_title_block_rect.height
text: lesson_detail.dep[index]
text: title
font.pixelSize: 50
verticalAlignment: Text.AlignVCenter
font.bold: true
}
}

Text {
id: txt
id: time_txt
anchors {
top: dep_title_block_rect.bottom
topMargin: 20
left: dep_title_block_rect.left
right: parent.right
bottom: parent.bottom
}
text: qsTr("%1").arg(index)
text: qsTr("时间:%1").arg(time)
font.pixelSize: 36
}

Text {
id: loc_txt
anchors {
top: time_txt.bottom
topMargin: 10
left: dep_title_block_rect.left
right: parent.right
}
text: qsTr("地点:%1").arg(loc)
font.pixelSize: 36
}

Text {
id: topic_txt
anchors {
top: loc_txt.bottom
topMargin: 10
left: dep_title_block_rect.left
right: parent.right
}
text: qsTr("主题:%1").arg(topic)
font.pixelSize: 36
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/controller/evento_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "feedback_service.h"
#include "image.h"
#include "latest_evento_model.h"
#include "lesson.h"
#include "my_page.h"
#include "plaza.h"
#include "repository.h"
Expand Down Expand Up @@ -454,3 +455,9 @@ EventoBlock::EventoBlock(const DTO_Evento& src, const std::set<EventoID>& permit
editable(permitted.count(src.id)) {
init();
}

EventoLesson::EventoLesson(const DTO_Evento& src)
: id(src.id), topic(src.description), time(src.gmtEventStart.toString("ddd h:m")) {
for (const auto& i : src.departments)
departments << i.name;
}
18 changes: 18 additions & 0 deletions src/controller/ui/calendar.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#include "calendar.h"
#include "evento_block_model.h"
#include "evento_service.h"
#include "lesson_model.h"

void CalendarController::generateLessonPic(DepartmentEnum dep) {
std::vector<EventoLesson> data;
auto& test = data.emplace_back();
test.departments.push_back("C++组");
test.time = "周日 17:00";
test.topic = "?";
test.image = "qrc:/res/image/department/cpp_1.svg";
{
auto& test = data.emplace_back();
test.departments.push_back("C++组");
test.time = "周日 17:00";
test.topic = "?";
test.image = "qrc:/res/image/department/cpp_1.svg";
}
LessonModel::getInstance()->resetModel(std::move(data));
}

void CalendarController::loadAllEventoInfo(QString date) {
EventoService::getInstance().load_Block(QDate::fromString(date, "yyyy-M-d"));
Expand Down
2 changes: 1 addition & 1 deletion src/controller/ui/calendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CalendarController : public QObject {
Q_INVOKABLE void deleteEvento(EventoID eventId);
Q_INVOKABLE void cancelEvento(EventoID eventId);
Q_INVOKABLE void loadCheckCode(EventoID eventId);
Q_INVOKABLE void generateLessonPic(DepartmentEnum dep) {}
Q_INVOKABLE void generateLessonPic(DepartmentEnum dep);

signals:
void loadAllEventoSuccessEvent();
Expand Down
21 changes: 21 additions & 0 deletions src/domain/entity/lesson.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef LESSON_H
#define LESSON_H

#include "types.h"

struct DTO_Evento;

struct EventoLesson {
EventoID id;
QStringList departments;
QString topic;
QString time;
QString location;
Image image;

EventoLesson() = default;
EventoLesson(const DTO_Evento&);
};


#endif // LESSON_H
74 changes: 74 additions & 0 deletions src/domain/model/lesson_model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "lesson_model.h"

#include "movable_lambda.h"

int LessonModel::rowCount(const QModelIndex& parent) const {
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;

return m_data.size();
}

QVariant LessonModel::data(const QModelIndex& index, int role) const {
if (!index.isValid() || index.row() >= m_data.size())
return QVariant();

const auto& element = m_data.at(index.row());

switch (role) {
case Role::Id:
return element.id;
case Role::DepTitle:
return element.departments.size() == 1 ? element.departments.front() : element.departments.join('/') + "联合授课";
case Role::Topic:
return element.topic;
case Role::Time:
return element.time;
case Role::Location:
return element.location;
case Role::Icon:
return element.image;
default:
break;
}

return QVariant();
}

QHash<int, QByteArray> LessonModel::roleNames() const {
static QHash<int, QByteArray> roles;
if (roles.isEmpty()) {
roles.insert(Id, "id");
roles.insert(DepTitle, "title");
roles.insert(Topic, "topic");
roles.insert(Time, "time");
roles.insert(Location, "loc");
roles.insert(Icon, "icon");
}
return roles;
}

void LessonModel::resetModel(std::vector<EventoLesson>&& model) {
QMetaObject::invokeMethod(
this,
MovableLambda(std::move(model), [this](std::vector<EventoLesson>&& data) {
beginResetModel();
m_data = std::move(data);
emit numChanged();
endResetModel();
})
);
}

LessonModel* LessonModel::create(QQmlEngine* qmlEngine, QJSEngine* jsEngine) {
auto pInstance = getInstance();
QJSEngine::setObjectOwnership(pInstance, QQmlEngine::CppOwnership);
return pInstance;
}

LessonModel* LessonModel::getInstance() {
static LessonModel instance;
return &instance;
}
47 changes: 47 additions & 0 deletions src/domain/model/lesson_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef LESSON_MODEL_H
#define LESSON_MODEL_H

#include <QAbstractListModel>
#include <QtQml>

#include "lesson.h"

class LessonModel : public QAbstractListModel {
Q_OBJECT
QML_SINGLETON
QML_NAMED_ELEMENT(LessonModel)

Q_PROPERTY(int num READ rowCount NOTIFY numChanged)

signals:
void numChanged();

public:
enum Role {
Id = Qt::UserRole + 1,
DepTitle,
Topic,
Time,
Location,
Icon,
};

int rowCount(const QModelIndex& parent = QModelIndex()) const override;

QVariant data(const QModelIndex& index, int role) const override;

QHash<int, QByteArray> roleNames() const override;

void resetModel(std::vector<EventoLesson>&& model);

private:
LessonModel() = default;

std::vector<EventoLesson> m_data;

public:
static LessonModel* create(QQmlEngine* qmlEngine, QJSEngine* jsEngine);
static LessonModel* getInstance();
};

#endif // LESSON_MODEL_H
20 changes: 20 additions & 0 deletions src/include/movable_lambda.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef MOVABLE_LAMBDA_H
#define MOVABLE_LAMBDA_H

#include <functional>
#include <type_traits>

template <typename T, typename ReturnType = void>
class MovableLambda {
std::remove_reference_t<T> stored;
std::function<ReturnType(std::remove_reference_t<T>&&)> func;
public:
template<typename Fun>
MovableLambda(T&& arg, Fun&& f) : stored(std::move(arg)), func(f) {}

ReturnType operator()() {
return func(std::move(stored));
}
};

#endif // MOVABLE_LAMBDA_H
3 changes: 2 additions & 1 deletion src/include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using UserID = QString;
using EventoID = int;
using EventTypeID = int;
using LocationID = int;
using Image = QString;
using Tag = QString;
Expand All @@ -26,7 +27,7 @@ enum class EventState : int {
};

struct EventType {
int id;
EventTypeID id;
QString name;
bool allowConflict;
};
Expand Down

0 comments on commit 9ca023a

Please sign in to comment.