reimplemented sorting and improved view-model

This commit is contained in:
itsmattkc
2018-12-14 14:45:42 +11:00
parent 76f1fa69fe
commit 94db640327
14 changed files with 156 additions and 189 deletions
+3 -12
View File
@@ -5,6 +5,8 @@
#include "undo.h"
#include "io/config.h"
#include "panels/viewer.h"
#include "panels/project.h"
#include "projectmodel.h"
#include "debug.h"
@@ -296,17 +298,6 @@ Media *Media::parentItem() {
return parent;
}
Media *Media::takeChild(int i) {
Media* ref = children.at(i);
void Media::removeChild(int i) {
children.removeAt(i);
return ref;
}
void Media::removeChild(Media* m) {
for (int i=0;i<children.size();i++) {
if (children.at(i) == m) {
children.removeAt(i);
return;
}
}
}
+1 -2
View File
@@ -46,8 +46,7 @@ public:
QVariant data(int column, int role);
int row() const;
Media *parentItem();
Media* takeChild(int i);
void removeChild(Media* m);
void removeChild(int i);
bool root;
int temp_id;
+37 -16
View File
@@ -1,4 +1,4 @@
#include "projectmodel.h"
#include "projectmodel.h"
#include "panels/panels.h"
#include "panels/viewer.h"
@@ -123,29 +123,50 @@ int ProjectModel::columnCount(const QModelIndex &parent) const {
return root_item->columnCount();
}
int ProjectModel::topLevelItemCount() {
return root_item->childCount();
Media *ProjectModel::getItem(const QModelIndex &index) const {
if (index.isValid()) {
Media *item = static_cast<Media*>(index.internalPointer());
if (item)
return item;
}
return root_item;
}
Media *ProjectModel::topLevelItem(int i) {
return root_item->child(i);
void ProjectModel::appendChild(Media *parent, Media *child) {
if (parent == NULL) parent = root_item;
beginInsertRows(parent == root_item ? QModelIndex() : createIndex(parent->row(), 0, parent), parent->childCount(), parent->childCount());
parent->appendChild(child);
endInsertRows();
}
void ProjectModel::addTopLevelItem(Media *m) {
root_item->appendChild(m);
void ProjectModel::moveChild(Media *child, Media *to) {
if (to == NULL) to = root_item;
Media* from = child->parentItem();
beginMoveRows(
from == root_item ? QModelIndex() : createIndex(from->row(), 0, from),
child->row(),
child->row(),
to == root_item ? QModelIndex() : createIndex(to->row(), 0, to),
to->childCount()
);
from->removeChild(child->row());
to->appendChild(child);
endMoveRows();
}
Media *ProjectModel::takeTopLevelItem(int i) {
Media* ref = root_item->child(i);
root_item->takeChild(i);
return ref;
void ProjectModel::removeChild(Media* parent, Media* m) {
if (parent == NULL) parent = root_item;
beginRemoveRows(parent == root_item ? QModelIndex() : createIndex(parent->row(), 0, parent), m->row(), m->row());
parent->removeChild(m->row());
endRemoveRows();
}
void ProjectModel::removeTopLevelItem(Media *m) {
root_item->removeChild(m);
Media* ProjectModel::child(int i, Media* parent) {
if (parent == NULL) parent = root_item;
return parent->child(i);
}
void ProjectModel::update_data() {
// emit dataChanged();
emit layoutChanged();
int ProjectModel::childCount(Media *parent) {
if (parent == NULL) parent = root_item;
return parent->childCount();
}
+8 -7
View File
@@ -1,4 +1,4 @@
#ifndef PROJECTMODEL_H
#ifndef PROJECTMODEL_H
#define PROJECTMODEL_H
#include <QAbstractItemModel>
@@ -25,12 +25,13 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int topLevelItemCount();
Media* topLevelItem(int i);
void addTopLevelItem(Media*);
Media* takeTopLevelItem(int i);
void removeTopLevelItem(Media* m);
void update_data();
Media *getItem(const QModelIndex &index) const;
void appendChild(Media* parent, Media* child);
void moveChild(Media *child, Media *to);
void removeChild(Media *parent, Media* m);
Media *child(int i, Media* parent = NULL);
int childCount(Media* parent = NULL);
private:
Media* root_item;
+20 -62
View File
@@ -346,40 +346,34 @@ NewSequenceCommand::NewSequenceCommand(Media *s, Media* iparent) :
parent(iparent),
done(false),
old_project_changed(mainWindow->isWindowModified())
{}
{
if (parent == NULL) parent = project_model.get_root();
}
NewSequenceCommand::~NewSequenceCommand() {
if (!done) delete seq;
}
void NewSequenceCommand::undo() {
if (parent == NULL) {
project_model.removeTopLevelItem(seq);
} else {
parent->removeChild(seq);
}
project_model.removeChild(parent, seq);
done = false;
mainWindow->setWindowModified(old_project_changed);
}
void NewSequenceCommand::redo() {
if (parent == NULL) {
project_model.addTopLevelItem(seq);
} else {
parent->appendChild(seq);
}
project_model.appendChild(parent, seq);
done = true;
mainWindow->setWindowModified(true);
}
AddMediaCommand::AddMediaCommand(Media* iitem, Media* iparent) :
AddMediaCommand::AddMediaCommand(Media* iitem, Media *iparent) :
item(iitem),
parent(iparent),
done(false),
old_project_changed(mainWindow->isWindowModified())
{
if (parent == NULL) parent = project_model.get_root();
}
{}
AddMediaCommand::~AddMediaCommand() {
if (!done) {
@@ -388,73 +382,40 @@ AddMediaCommand::~AddMediaCommand() {
}
void AddMediaCommand::undo() {
parent->removeChild(item);
project_model.removeChild(parent, item);
done = false;
mainWindow->setWindowModified(old_project_changed);
}
void AddMediaCommand::redo() {
parent->appendChild(item);
project_model.update_data();
/* Here we force the source_table to sort itself.
*
* For some reason, sometimes when you add items to the QTreeWidget,
* (usually upon first import) they appear at the bottom, regardless
* of where they should be placed alphabetically. Then when this
* function is "undone", and it tries to remove this item from the
* QTreeWidget, it immediately sorts and then removes THE WRONG ONE.
* If this happens to be a sequence, the sequence data doesn't save
* and is then lost forever (outside of autorecoveries).
*
* The following 2 lines seem to force the source_table to re-sort
* correctly and therefore works around this problem. But holy shit.
*
* I guess I'm "supposed" to use a Model–view–viewmodel instead,
* which I'll probably have to switch to soon anyway. So perhaps
* this will be a non-issue soon.
panel_project->source_table->setSortingEnabled(false);
panel_project->source_table->setSortingEnabled(true);*/
project_model.appendChild(parent, item);
done = true;
mainWindow->setWindowModified(true);
}
DeleteMediaCommand::DeleteMediaCommand(const QModelIndex& i) :
DeleteMediaCommand::DeleteMediaCommand(Media* i) :
item(i),
parent(i.parent()),
parent(i->parentItem()),
old_project_changed(mainWindow->isWindowModified())
{}
DeleteMediaCommand::~DeleteMediaCommand() {
if (done) {
Media* m = static_cast<Media*>(item.internalPointer());
delete m;
if (done) {
delete item;
}
}
void DeleteMediaCommand::undo() {
Media* m = static_cast<Media*>(item.internalPointer());
if (!parent.isValid()) {
project_model.addTopLevelItem(m);
} else {
static_cast<Media*>(parent.internalPointer())->appendChild(m);
}
project_model.appendChild(parent, item);
mainWindow->setWindowModified(old_project_changed);
done = false;
}
void DeleteMediaCommand::redo() {
Media* m = static_cast<Media*>(item.internalPointer());
if (!parent.isValid()) {
project_model.removeTopLevelItem(m);
} else {
static_cast<Media*>(parent.internalPointer())->removeChild(m);
}
project_model.removeChild(parent, item);
project_model.update_data();
mainWindow->setWindowModified(true);
done = true;
}
@@ -622,7 +583,7 @@ void ReplaceMediaCommand::replace(QString& filename) {
// replace media
QStringList files;
files.append(filename);
panel_project->process_file_list(false, files, NULL, item);
panel_project->process_file_list(files, false, item, NULL);
}
void ReplaceMediaCommand::undo() {
@@ -728,8 +689,7 @@ MediaMove::MediaMove(SourceTable *s) : table(s), old_project_changed(mainWindow-
void MediaMove::undo() {
for (int i=0;i<items.size();i++) {
to->removeChild(items.at(i));
froms.at(i)->appendChild(items.at(i));
project_model.moveChild(items.at(i), froms.at(i));
}
mainWindow->setWindowModified(old_project_changed);
}
@@ -740,10 +700,8 @@ void MediaMove::redo() {
for (int i=0;i<items.size();i++) {
Media* parent = items.at(i)->parentItem();
froms[i] = parent;
parent->removeChild(items.at(i));
to->appendChild(items.at(i));
project_model.moveChild(items.at(i), to);
}
project_model.update_data();
mainWindow->setWindowModified(true);
}
+4 -3
View File
@@ -22,6 +22,7 @@ struct EffectMeta;
#include <QUndoCommand>
#include <QVector>
#include <QVariant>
#include <QModelIndex>
extern QUndoStack undo_stack;
@@ -192,13 +193,13 @@ private:
class DeleteMediaCommand : public QUndoCommand {
public:
DeleteMediaCommand(const QModelIndex& i);
DeleteMediaCommand(Media *i);
~DeleteMediaCommand();
void undo();
void redo();
private:
const QModelIndex& item;
const QModelIndex& parent;
Media* item;
Media* parent;
bool old_project_changed;
bool done;
};