File: projectmodel.cpp

package info (click to toggle)
olive-editor 20181223-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,844 kB
  • sloc: cpp: 20,147; xml: 315; ansic: 16; makefile: 11
file content (178 lines) | stat: -rw-r--r-- 5,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "projectmodel.h"

#include "panels/panels.h"
#include "panels/viewer.h"
#include "ui/viewerwidget.h"
#include "project/media.h"
#include "debug.h"

ProjectModel::ProjectModel(QObject *parent) : QAbstractItemModel(parent), root_item(NULL) {
    root_item = new Media(0);
    root_item->root = true;
}

ProjectModel::~ProjectModel() {    
    destroy_root();
}

void ProjectModel::destroy_root() {
    if (panel_sequence_viewer != NULL) panel_sequence_viewer->viewer_widget->delete_function();
    if (panel_footage_viewer != NULL) panel_footage_viewer->viewer_widget->delete_function();

    if (root_item != NULL) {
        delete root_item;
    }
}

void ProjectModel::clear() {
    beginResetModel();
    destroy_root();
    root_item = new Media(0);
    root_item->root = true;
    endResetModel();
}

Media *ProjectModel::get_root() {
    return root_item;
}

QVariant ProjectModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid())
        return QVariant();

    return static_cast<Media*>(index.internalPointer())->data(index.column(), role);
}

Qt::ItemFlags ProjectModel::flags(const QModelIndex &index) const {
    if (!index.isValid())
        return Qt::ItemIsDropEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable;
}

QVariant ProjectModel::headerData(int section, Qt::Orientation orientation, int role) const {
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return root_item->data(section, role);

    return QVariant();
}

QModelIndex ProjectModel::index(int row, int column, const QModelIndex &parent) const {
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    Media *parentItem;

    if (!parent.isValid())
        parentItem = root_item;
    else
        parentItem = static_cast<Media*>(parent.internalPointer());

    Media *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
}

QModelIndex ProjectModel::parent(const QModelIndex &index) const {
    if (!index.isValid())
        return QModelIndex();

    Media *childItem = static_cast<Media*>(index.internalPointer());
    Media *parentItem = childItem->parentItem();

    if (parentItem == root_item)
        return QModelIndex();

    return createIndex(parentItem->row(), 0, parentItem);
}

bool ProjectModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if (role != Qt::EditRole)
        return false;

    Media *item = static_cast<Media*>(index.internalPointer());
    bool result = item->setData(index.column(), value);

    if (result)
        emit dataChanged(index, index);

    return result;
}

int ProjectModel::rowCount(const QModelIndex &parent) const {
    Media *parentItem;
    if (parent.column() > 0)
        return 0;

    if (!parent.isValid()) {
        parentItem = root_item;
    } else {
        parentItem = static_cast<Media*>(parent.internalPointer());
    }

    return parentItem->childCount();
}

int ProjectModel::columnCount(const QModelIndex &parent) const {
    if (parent.isValid())
        return static_cast<Media*>(parent.internalPointer())->columnCount();
    else
        return root_item->columnCount();
}

Media *ProjectModel::getItem(const QModelIndex &index) const {
    if (index.isValid()) {
        Media *item = static_cast<Media*>(index.internalPointer());
        if (item)
            return item;
    }
    return root_item;
}

void ProjectModel::set_icon(Media* m, const QIcon &ico) {
    QModelIndex index = createIndex(m->row(), 0, m);
    m->set_icon(ico);
    emit dataChanged(index, index);

}

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::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();
}

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();
}

Media* ProjectModel::child(int i, Media* parent) {
    if (parent == NULL) parent = root_item;
    return parent->child(i);
}

int ProjectModel::childCount(Media *parent) {
    if (parent == NULL) parent = root_item;
    return parent->childCount();
}