-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters