Commit 85cf5974 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Switch to a proper JSon format to store guide categories instead of hacky line...

Switch to a proper JSon format to store guide categories instead of hacky line feed separated format
parent da8d2d8b
Pipeline #266365 failed with stage
in 8 minutes and 47 seconds
......@@ -118,6 +118,66 @@ const QStringList MarkerListModel::categoriesToStringList() const
return categories;
}
QStringList MarkerListModel::guideCategoriesToStringList(const QString &categoriesData)
{
QStringList categories;
auto json = QJsonDocument::fromJson(categoriesData.toUtf8());
if (!json.isArray()) {
qDebug() << "Error : Json file should be an array";
return categories;
}
auto list = json.array();
for (const auto &entry : qAsConst(list)) {
if (!entry.isObject()) {
qDebug() << "Warning : Skipping invalid category data";
continue;
}
auto entryObj = entry.toObject();
if (!entryObj.contains(QLatin1String("index"))) {
qDebug() << "Warning : Skipping invalid category data (does not contain index)";
continue;
}
int ix = entryObj[QLatin1String("index")].toInt();
QString comment = entryObj[QLatin1String("comment")].toString();
QString color = entryObj[QLatin1String("color")].toString();
categories << QString("%1:%2:%3").arg(comment, QString::number(ix), color);
}
return categories;
}
const QString MarkerListModel::categoriesListToJSon(const QStringList categories) const
{
QJsonArray list;
for (auto &cat : categories) {
QJsonObject currentMarker;
const QColor color(cat.section(QLatin1Char(':'), -1));
const QString name = cat.section(QLatin1Char(':'), 0, -3);
int ix = cat.section(QLatin1Char(':'), -2, -2).toInt();
currentMarker.insert(QLatin1String("index"), QJsonValue(ix));
currentMarker.insert(QLatin1String("comment"), QJsonValue(name));
currentMarker.insert(QLatin1String("color"), QJsonValue(color.name()));
list.push_back(currentMarker);
}
QJsonDocument json(list);
return QString::fromUtf8(json.toJson());
}
const QString MarkerListModel::categoriesToJSon() const
{
QJsonArray list;
QMapIterator<int, Core::MarkerCategory> i(pCore->markerTypes);
while (i.hasNext()) {
i.next();
QJsonObject currentMarker;
currentMarker.insert(QLatin1String("index"), QJsonValue(i.key()));
currentMarker.insert(QLatin1String("comment"), QJsonValue(i.value().displayName));
currentMarker.insert(QLatin1String("color"), QJsonValue(i.value().color.name()));
list.push_back(currentMarker);
}
QJsonDocument json(list);
return QString::fromUtf8(json.toJson());
}
int MarkerListModel::markerIdAtFrame(int pos) const
{
if (m_markerPositions.contains(pos)) {
......
......@@ -158,8 +158,11 @@ public:
*/
void loadCategoriesWithUndo(const QStringList &categories, const QStringList &currentCategories, const QMap<int, int> remapCategories = {});
QList<int> loadCategories(const QStringList &categories);
QStringList guideCategoriesToStringList(const QString &categoriesData);
/** @brief Returns the marker categories in the form of a stringList for saving */
const QStringList categoriesToStringList() const;
const QString categoriesToJSon() const;
const QString categoriesListToJSon(const QStringList categories) const;
// Mandatory overloads
QVariant data(const QModelIndex &index, int role) const override;
......
......@@ -366,22 +366,23 @@ void KdenliveDoc::initializeProperties(bool newDocument)
m_documentProperties[QStringLiteral("seekOffset")] = QString::number(TimelineModel::seekDuration);
if (newDocument) {
// For existing documents, don't define guidesCategories, so that we can use the getDefaultGuideCategories() for backwards compatibility
m_documentProperties[QStringLiteral("guidesCategories")] = KdenliveSettings::guidesCategories().join(QLatin1Char('\n'));
m_documentProperties[QStringLiteral("guidesCategories")] = m_guideModel->categoriesListToJSon(KdenliveSettings::guidesCategories());
}
}
const QStringList KdenliveDoc::guidesCategories()
{
if (!m_documentProperties.contains(QStringLiteral("guidesCategories")) || m_documentProperties.value(QStringLiteral("guidesCategories")).isEmpty()) {
const QString defaultCategories = getDefaultGuideCategories().join(QLatin1Char('\n'));
m_documentProperties[QStringLiteral("guidesCategories")] = defaultCategories;
const QStringList defaultCategories = getDefaultGuideCategories();
m_documentProperties[QStringLiteral("guidesCategories")] = m_guideModel->categoriesListToJSon(defaultCategories);
return defaultCategories;
}
return m_documentProperties.value(QStringLiteral("guidesCategories")).split(QLatin1Char('\n'));
return m_guideModel->guideCategoriesToStringList(m_documentProperties.value(QStringLiteral("guidesCategories")));
}
void KdenliveDoc::updateGuideCategories(const QStringList &categories, const QMap<int, int> remapCategories)
{
const QStringList currentCategories = m_documentProperties.value(QStringLiteral("guidesCategories")).split(QLatin1Char('\n'));
const QStringList currentCategories = m_guideModel->guideCategoriesToStringList(m_documentProperties.value(QStringLiteral("guidesCategories")));
// Check if a guide category was removed
QList<int> currentIndexes;
QList<int> updatedIndexes;
......@@ -403,8 +404,8 @@ void KdenliveDoc::updateGuideCategories(const QStringList &categories, const QMa
void KdenliveDoc::saveGuideCategories()
{
const QStringList categories = m_guideModel->categoriesToStringList();
m_documentProperties[QStringLiteral("guidesCategories")] = categories.join(QLatin1Char('\n'));
const QString categories = m_guideModel->categoriesToJSon();
m_documentProperties[QStringLiteral("guidesCategories")] = categories;
}
int KdenliveDoc::updateClipsCount()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment