Commit 5be2288f authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Delete guides if their category is deleted

parent 87aa85e9
......@@ -54,8 +54,9 @@ void MarkerListModel::setup()
connect(this, &MarkerListModel::dataChanged, this, &MarkerListModel::modelChanged);
}
void MarkerListModel::loadCategories(const QStringList &categories)
QList<int> MarkerListModel::loadCategories(const QStringList &categories)
{
QList<int> previousCategories = pCore->markerTypes.keys();
pCore->markerTypes.clear();
for (const QString &cat : categories) {
if (cat.count(QLatin1Char(':')) < 2) {
......@@ -66,10 +67,11 @@ void MarkerListModel::loadCategories(const QStringList &categories)
const QColor color(cat.section(QLatin1Char(':'), -1));
const QString name = cat.section(QLatin1Char(':'), 0, -3);
int ix = cat.section(QLatin1Char(':'), -2, -2).toInt();
previousCategories.removeAll(ix);
pCore->markerTypes.insert(ix, {color, name});
}
emit categoriesChanged();
// TODO: delete markers if their category was deleted
return previousCategories;
}
int MarkerListModel::markerIdAtFrame(int pos) const
......
......@@ -144,8 +144,10 @@ public:
*/
bool addMultipleMarkersGui(const GenTime &pos, QWidget *parent, bool createIfNotFound, ClipController *clip = nullptr);
void exportGuidesGui(QWidget *parent, GenTime projectDuration) const;
/** @brief Load the marker categories from a stringList */
void loadCategories(const QStringList &categories);
/** @brief Load the marker categories from a stringList
* @return the list of deleted categories ids (if any)
*/
QList<int> loadCategories(const QStringList &categories);
// Mandatory overloads
QVariant data(const QModelIndex &index, int role) const override;
......
......@@ -373,8 +373,31 @@ const QStringList KdenliveDoc::guidesCategories() const
void KdenliveDoc::updateGuideCategories(const QStringList &categories)
{
m_guideModel->loadCategories(categories);
m_documentProperties[QStringLiteral("guidesCategories")] = categories.join(QLatin1Char('\n'));
Fun local_undo = []() { return true; };
Fun local_redo = []() { return true; };
const QStringList currentCategories = m_documentProperties.value(QStringLiteral("guidesCategories")).split(QLatin1Char('\n'));
QList<int> deletedCategories = m_guideModel->loadCategories(categories);
// Remove all markers of deleted category
while (!deletedCategories.isEmpty()) {
int ix = deletedCategories.takeFirst();
QList<CommentedTime> toDelete = m_guideModel->getAllMarkers(ix);
for (CommentedTime c : toDelete) {
m_guideModel->removeMarker(c.time(), local_undo, local_redo);
}
}
Fun undo = [this, currentCategories]() {
m_guideModel->loadCategories(currentCategories);
m_documentProperties[QStringLiteral("guidesCategories")] = currentCategories.join(QLatin1Char('\n'));
return true;
};
Fun redo = [this, categories]() {
m_guideModel->loadCategories(categories);
m_documentProperties[QStringLiteral("guidesCategories")] = categories.join(QLatin1Char('\n'));
return true;
};
PUSH_FRONT_LAMBDA(local_redo, redo);
PUSH_LAMBDA(local_undo, undo);
pCore->pushUndo(undo, redo, i18n("Update guides categories"));
}
int KdenliveDoc::updateClipsCount()
......@@ -1439,7 +1462,6 @@ void KdenliveDoc::loadDocumentGuides()
if (!guides.isEmpty()) {
m_guideModel->importFromJson(guides, true, false);
}
m_guideModel->loadCategories(m_documentProperties.value(QStringLiteral("guides")).split(QLatin1Char('\n')));
}
void KdenliveDoc::loadDocumentProperties()
......@@ -1520,6 +1542,8 @@ void KdenliveDoc::loadDocumentProperties()
if (!profileFound) {
qDebug() << "ERROR, no matching profile found";
}
const QStringList guideCategories = m_documentProperties.value(QStringLiteral("guidesCategories")).split(QLatin1Char('\n'));
m_guideModel->loadCategories(guideCategories);
updateProjectProfile(false);
}
......
......@@ -6,6 +6,7 @@ SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "guidecategories.h"
#include "bin/bin.h"
#include "bin/model/markerlistmodel.hpp"
#include "core.h"
#include "kdenlive_debug.h"
#include "kdenlivesettings.h"
......@@ -14,6 +15,7 @@ SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include <KIconEffect>
#include <KLineEdit>
#include <KLocalizedString>
#include <KMessageBox>
#include <KStandardAction>
#include <QDialog>
......@@ -92,6 +94,7 @@ GuideCategories::GuideCategories(KdenliveDoc *doc, QWidget *parent)
return true;
};
QStringList guidesCategories = doc ? doc->guidesCategories() : KdenliveSettings::guidesCategories();
QList<int> existingCategories;
for (auto &g : guidesCategories) {
if (g.count(QLatin1Char(':')) < 2) {
// Invalid guide data found
......@@ -101,12 +104,20 @@ GuideCategories::GuideCategories(KdenliveDoc *doc, QWidget *parent)
const QColor color(g.section(QLatin1Char(':'), -1));
const QString name = g.section(QLatin1Char(':'), 0, -3);
int ix = g.section(QLatin1Char(':'), -2, -2).toInt();
existingCategories << ix;
QIcon ic = buildIcon(color);
auto *item = new QListWidgetItem(ic, name);
item->setData(Qt::UserRole, color);
item->setData(Qt::UserRole + 1, ix);
// Check usage
if (doc) {
int count = doc->getGuideModel()->getAllMarkers(ix).count();
item->setData(Qt::UserRole + 2, count);
}
guides_list->addItem(item);
}
std::sort(existingCategories.begin(), existingCategories.end());
m_categoryIndex = existingCategories.last() + 1;
QAction *a = KStandardAction::renameFile(this, editItem, this);
guides_list->addAction(a);
connect(guides_list, &QListWidget::itemDoubleClicked, this, [=]() { editItem(); });
......@@ -114,15 +125,24 @@ GuideCategories::GuideCategories(KdenliveDoc *doc, QWidget *parent)
connect(guide_add, &QPushButton::clicked, this, [=]() {
QIcon ic = buildIcon(Qt::white);
auto *item = new QListWidgetItem(ic, i18n("Category %1", guides_list->count() + 1));
item->setData(Qt::UserRole + 1, m_categoryIndex++);
guides_list->addItem(item);
guides_list->setCurrentItem(item);
editItem();
});
connect(guide_delete, &QPushButton::clicked, this, [=]() {
auto *item = guides_list->currentItem();
if (item) {
delete item;
if (!item) {
return;
}
int count = item->data(Qt::UserRole + 2).toInt();
if (count > 0) {
// There are existing guides in this category, warn
if (KMessageBox::warningContinueCancel(this, i18n("This will delete the %1 guides using this category", count)) != KMessageBox::Continue) {
return;
}
}
delete item;
});
}
......@@ -148,7 +168,8 @@ const QStringList GuideCategories::updatedGuides() const
auto item = guides_list->item(i);
QString color = item->data(Qt::UserRole).toString();
QString name = item->text();
categories << QString("%1:%2:%3").arg(name, QString::number(i + 1), color);
int ix = item->data(Qt::UserRole + 1).toInt();
categories << QString("%1:%2:%3").arg(name, QString::number(ix), color);
}
return categories;
}
......@@ -27,6 +27,8 @@ public slots:
private:
/** @brief Create a colored guide icon. */
QIcon buildIcon(const QColor &col);
/** @brief The incremental index for newly created categories. */
int m_categoryIndex;
signals:
};
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