2023-07-31 12:21:14 +00:00
|
|
|
#include "pagesmodel.h"
|
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
#include <QFileInfo>
|
2023-09-27 10:56:10 +00:00
|
|
|
#include <QRegularExpression>
|
2023-07-31 12:21:14 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
#include "directoryfileswatcher.h"
|
|
|
|
|
2023-07-31 12:21:14 +00:00
|
|
|
namespace {
|
|
|
|
const auto categoryUncategorized QStringLiteral("Uncategorized");
|
|
|
|
}
|
|
|
|
|
|
|
|
PagesModel::PagesModel(const QString &path, QObject *parent)
|
2023-09-27 10:56:10 +00:00
|
|
|
: QAbstractListModel{parent}, m_path{path},
|
2023-10-09 20:26:50 +00:00
|
|
|
m_pagesWatcher(new DirectoryFilesWatcher(
|
|
|
|
path, QStringLiteral("*Page.qml"), this))
|
2023-07-31 12:21:14 +00:00
|
|
|
{
|
2023-10-09 20:26:50 +00:00
|
|
|
m_items = readMetadata(m_pagesWatcher->files());
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
for (const auto& item : qAsConst(m_items))
|
2023-10-03 09:48:40 +00:00
|
|
|
setFigmaLinks(item.title, item.figmaLinks);
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
connect(m_pagesWatcher, &DirectoryFilesWatcher::filesChanged,
|
|
|
|
this, &PagesModel::onPagesChanged);
|
2023-09-27 10:56:10 +00:00
|
|
|
}
|
2023-07-31 12:21:14 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
PagesModelItem PagesModel::readMetadata(const QString& path)
|
|
|
|
{
|
|
|
|
PagesModelItem item;
|
|
|
|
item.path = path;
|
|
|
|
item.title = QFileInfo(path).fileName().chopped(
|
|
|
|
(QStringLiteral("Page.qml").size()));
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
QFile file(path);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
QByteArray content = file.readAll();
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2024-10-30 11:19:53 +00:00
|
|
|
item.category = extractCategory(content);
|
|
|
|
item.status = extractStatus(content);
|
|
|
|
item.figmaLinks = extractFigmaLinks(content);
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<PagesModelItem> PagesModel::readMetadata(const QStringList &paths)
|
|
|
|
{
|
|
|
|
QList<PagesModelItem> metadata;
|
|
|
|
metadata.reserve(paths.size());
|
|
|
|
|
|
|
|
std::transform(paths.begin(), paths.end(), std::back_inserter(metadata),
|
|
|
|
[](auto& path) {
|
|
|
|
return readMetadata(path);
|
|
|
|
});
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PagesModel::extractCategory(const QByteArray& content)
|
|
|
|
{
|
2023-09-27 10:56:10 +00:00
|
|
|
static QRegularExpression categoryRegex(
|
|
|
|
"^//(\\s)*category:(.+)$", QRegularExpression::MultilineOption);
|
|
|
|
|
|
|
|
QRegularExpressionMatch categoryMatch = categoryRegex.match(content);
|
|
|
|
QString category = categoryMatch.hasMatch()
|
|
|
|
? categoryMatch.captured(2).trimmed() : categoryUncategorized;
|
|
|
|
|
2024-10-30 11:19:53 +00:00
|
|
|
return category.isEmpty() ? categoryUncategorized : category;
|
|
|
|
}
|
|
|
|
|
|
|
|
PagesModel::Status PagesModel::extractStatus(const QByteArray& content)
|
|
|
|
{
|
|
|
|
static QRegularExpression statusRegex(
|
|
|
|
"^//(\\s)*status:(.+)$", QRegularExpression::MultilineOption);
|
2023-10-03 09:48:40 +00:00
|
|
|
|
2024-10-30 11:19:53 +00:00
|
|
|
QRegularExpressionMatch statusMatch = statusRegex.match(content);
|
|
|
|
QString status = statusMatch.hasMatch()
|
|
|
|
? statusMatch.captured(2).trimmed() : "";
|
|
|
|
|
|
|
|
if (status == QStringLiteral("bad"))
|
|
|
|
return Bad;
|
|
|
|
if (status == QStringLiteral("decent"))
|
|
|
|
return Decent;
|
|
|
|
if (status == QStringLiteral("good"))
|
|
|
|
return Good;
|
|
|
|
|
|
|
|
return Uncategorized;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PagesModel::extractFigmaLinks(const QByteArray& content)
|
|
|
|
{
|
2023-10-03 09:48:40 +00:00
|
|
|
static QRegularExpression figmaRegex(
|
|
|
|
"^(\\/\\/\\s*)((?:https:\\/\\/)?(?:www\\.)?figma\\.com\\/.*)$",
|
|
|
|
QRegularExpression::MultilineOption);
|
|
|
|
|
|
|
|
QRegularExpressionMatchIterator i = figmaRegex.globalMatch(content);
|
|
|
|
QStringList links;
|
|
|
|
|
|
|
|
while (i.hasNext()) {
|
|
|
|
QRegularExpressionMatch match = i.next();
|
|
|
|
QString link = match.captured(2);
|
|
|
|
links << link;
|
|
|
|
}
|
|
|
|
|
2024-10-30 11:19:53 +00:00
|
|
|
return links;
|
2023-10-09 20:26:50 +00:00
|
|
|
}
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
void PagesModel::onPagesChanged(const QStringList& added,
|
|
|
|
const QStringList& removed,
|
|
|
|
const QStringList& changed)
|
|
|
|
{
|
|
|
|
for (auto& path : removed) {
|
|
|
|
auto index = getIndexByPath(path);
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
beginRemoveRows({}, index, index);
|
2023-09-27 10:56:10 +00:00
|
|
|
m_items.removeAt(index);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
if (added.size()) {
|
|
|
|
beginInsertRows({}, rowCount(), rowCount() + added.size() - 1);
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
auto metadata = readMetadata(added);
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
for (const auto& item : qAsConst(metadata))
|
|
|
|
setFigmaLinks(item.title, item.figmaLinks);
|
|
|
|
|
|
|
|
m_items << metadata;
|
2023-09-27 10:56:10 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
for (auto& path : changed) {
|
|
|
|
auto index = getIndexByPath(path);
|
|
|
|
const auto& previous = m_items.at(index);
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
PagesModelItem metadata = readMetadata(path);
|
|
|
|
setFigmaLinks(metadata.title, metadata.figmaLinks);
|
2023-09-27 10:56:10 +00:00
|
|
|
|
2024-10-30 11:19:53 +00:00
|
|
|
// For simplicity category and status change is handled by removing and
|
|
|
|
// adding item. In the future it can be changed to regular dataChanged
|
|
|
|
// event and handled properly in upstream models like SectionSDecoratorModel.
|
|
|
|
if (previous.category != metadata.category
|
|
|
|
|| previous.status != metadata.status) {
|
2023-10-09 20:26:50 +00:00
|
|
|
beginRemoveRows({}, index, index);
|
2023-09-27 10:56:10 +00:00
|
|
|
m_items.removeAt(index);
|
|
|
|
endRemoveRows();
|
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
beginInsertRows({}, rowCount(), rowCount());
|
|
|
|
m_items << metadata;
|
2023-09-27 10:56:10 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 20:26:50 +00:00
|
|
|
int PagesModel::getIndexByPath(const QString& path) const
|
|
|
|
{
|
|
|
|
auto it = std::find_if(m_items.begin(), m_items.end(), [&path](auto& it) {
|
|
|
|
return it.path == path;
|
|
|
|
});
|
|
|
|
assert(it != m_items.end());
|
|
|
|
return std::distance(m_items.begin(), it);
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:21:14 +00:00
|
|
|
QHash<int, QByteArray> PagesModel::roleNames() const
|
|
|
|
{
|
|
|
|
static const QHash<int,QByteArray> roles {
|
|
|
|
{ TitleRole, QByteArrayLiteral("title") },
|
2023-10-03 09:48:40 +00:00
|
|
|
{ CategoryRole, QByteArrayLiteral("category") },
|
2024-10-30 11:19:53 +00:00
|
|
|
{ StatusRole, QByteArrayLiteral("status") },
|
2023-10-03 09:48:40 +00:00
|
|
|
{ FigmaRole, QByteArrayLiteral("figma") }
|
2023-07-31 12:21:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PagesModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return m_items.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant PagesModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (role == TitleRole)
|
|
|
|
return m_items.at(index.row()).title;
|
2023-10-03 09:48:40 +00:00
|
|
|
|
2023-07-31 12:21:14 +00:00
|
|
|
if (role == CategoryRole)
|
|
|
|
return m_items.at(index.row()).category;
|
|
|
|
|
2024-10-30 11:19:53 +00:00
|
|
|
if (role == StatusRole)
|
|
|
|
return m_items.at(index.row()).status;
|
|
|
|
|
2023-10-03 09:48:40 +00:00
|
|
|
if (role == FigmaRole) {
|
|
|
|
auto title = m_items.at(index.row()).title;
|
|
|
|
auto it = m_figmaSubmodels.find(title);
|
|
|
|
assert(it != m_figmaSubmodels.end());
|
|
|
|
|
|
|
|
return QVariant::fromValue(it.value());
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:21:14 +00:00
|
|
|
return {};
|
|
|
|
}
|
2023-10-03 09:48:40 +00:00
|
|
|
|
|
|
|
void PagesModel::setFigmaLinks(const QString& title, const QStringList& links)
|
|
|
|
{
|
|
|
|
auto it = m_figmaSubmodels.find(title);
|
|
|
|
|
|
|
|
if (it == m_figmaSubmodels.end()) {
|
|
|
|
m_figmaSubmodels.insert(title, new FigmaLinksModel(links, this));
|
|
|
|
} else {
|
|
|
|
it.value()->setContent(links);
|
|
|
|
}
|
|
|
|
}
|