wrote viewmodel model for project browsing
This commit is contained in:
@@ -16,7 +16,13 @@
|
||||
|
||||
set(OLIVE_SOURCES
|
||||
${OLIVE_SOURCES}
|
||||
project/folder.h
|
||||
project/folder.cpp
|
||||
project/item.h
|
||||
project/item.cpp
|
||||
project/project.h
|
||||
project/project.cpp
|
||||
project/projectviewmodel.h
|
||||
project/projectviewmodel.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "folder.h"
|
||||
|
||||
Folder::Folder()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Item::Type Folder::type() const
|
||||
{
|
||||
return kFolder;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef FOLDER_H
|
||||
#define FOLDER_H
|
||||
|
||||
#include "item.h"
|
||||
|
||||
class Folder : public Item
|
||||
{
|
||||
public:
|
||||
Folder();
|
||||
|
||||
virtual Type type() const override;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif // FOLDER_H
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "item.h"
|
||||
|
||||
Item::Item() :
|
||||
parent_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
Item::~Item()
|
||||
{
|
||||
}
|
||||
|
||||
void Item::add_child(Item* c)
|
||||
{
|
||||
if (c->parent_ == this) {
|
||||
return;
|
||||
}
|
||||
|
||||
children_.append(c);
|
||||
c->parent_ = this;
|
||||
}
|
||||
|
||||
void Item::remove_child(Item *c)
|
||||
{
|
||||
if (c->parent_ != this) {
|
||||
return;
|
||||
}
|
||||
|
||||
children_.removeAll(c);
|
||||
c->parent_ = nullptr;
|
||||
}
|
||||
|
||||
int Item::child_count()
|
||||
{
|
||||
return children_.size();
|
||||
}
|
||||
|
||||
Item *Item::child(int i)
|
||||
{
|
||||
return children_.at(i);
|
||||
}
|
||||
|
||||
const QString &Item::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
void Item::set_name(const QString &n)
|
||||
{
|
||||
name_ = n;
|
||||
}
|
||||
|
||||
Item *Item::parent() const
|
||||
{
|
||||
return parent_;
|
||||
}
|
||||
|
||||
void Item::set_parent(Item *p)
|
||||
{
|
||||
if (parent_ != nullptr) {
|
||||
parent_->remove_child(this);
|
||||
}
|
||||
|
||||
p->add_child(this);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef ITEM_H
|
||||
#define ITEM_H
|
||||
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
|
||||
class Item
|
||||
{
|
||||
public:
|
||||
enum Type {
|
||||
kFolder,
|
||||
kFootage,
|
||||
kSequence
|
||||
};
|
||||
|
||||
Item();
|
||||
|
||||
// Required virtual destructor (it's empty)
|
||||
virtual ~Item();
|
||||
|
||||
virtual Type type() const = 0;
|
||||
|
||||
void add_child(Item *c);
|
||||
void remove_child(Item* c);
|
||||
int child_count();
|
||||
Item* child(int i);
|
||||
|
||||
const QString& name() const;
|
||||
void set_name(const QString& n);
|
||||
|
||||
Item *parent() const;
|
||||
void set_parent(Item *p);
|
||||
|
||||
private:
|
||||
QList<Item*> children_;
|
||||
|
||||
Item* parent_;
|
||||
|
||||
QString name_;
|
||||
};
|
||||
|
||||
#endif // ITEM_H
|
||||
@@ -2,5 +2,9 @@
|
||||
|
||||
Project::Project()
|
||||
{
|
||||
}
|
||||
|
||||
Folder *Project::root()
|
||||
{
|
||||
return &root_;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
#ifndef PROJECT_H
|
||||
#define PROJECT_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "folder.h"
|
||||
|
||||
class Project
|
||||
{
|
||||
public:
|
||||
Project();
|
||||
|
||||
Folder* root();
|
||||
|
||||
private:
|
||||
Folder root_;
|
||||
};
|
||||
|
||||
using ProjectPtr = std::shared_ptr<Project>;
|
||||
|
||||
#endif // PROJECT_H
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
#include "projectviewmodel.h"
|
||||
|
||||
ProjectViewModel::ProjectViewModel(QObject *parent) :
|
||||
QAbstractItemModel(parent),
|
||||
project_(nullptr)
|
||||
{
|
||||
// TODO make this configurable
|
||||
columns_.append(kName);
|
||||
columns_.append(kDuration);
|
||||
columns_.append(kRate);
|
||||
}
|
||||
|
||||
Project *ProjectViewModel::project()
|
||||
{
|
||||
return project_;
|
||||
}
|
||||
|
||||
void ProjectViewModel::set_project(Project *p)
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
project_ = p;
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QModelIndex ProjectViewModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
// I'm actually not 100% sure what this does, but it seems logical and was in the earlier code
|
||||
if (!hasIndex(row, column, parent)) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
// Get the parent object (project root if the index is invalid)
|
||||
Item* item_parent = (parent.isValid()) ? static_cast<Item*>(parent.internalPointer()) : project_->root();
|
||||
|
||||
// Return an index to this object
|
||||
return createIndex(row, column, item_parent->child(row));
|
||||
}
|
||||
|
||||
QModelIndex ProjectViewModel::parent(const QModelIndex &child) const
|
||||
{
|
||||
// Get the Item object from the index
|
||||
Item* item = static_cast<Item*>(child.internalPointer());
|
||||
|
||||
// Get Item's parent object
|
||||
Item* parent = item->parent();
|
||||
|
||||
// If the parent is the root, return an empty index
|
||||
if (parent == project_->root()) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
// Otherwise return a true index to its parent
|
||||
|
||||
// Find parent's index within its own parent
|
||||
// (TODO: this model should handle sorting, which means it'll have to "know" the indices)
|
||||
int child_index = -1;
|
||||
|
||||
Item* double_parent = parent->parent();
|
||||
for (int i=0;i<double_parent->child_count();i++) {
|
||||
if (double_parent->child(i) == parent) {
|
||||
child_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the index is valid (there's no reason it shouldn't be)
|
||||
Q_ASSERT(child_index > -1);
|
||||
|
||||
// Return an index to the parent
|
||||
return createIndex(child_index, 0, parent);
|
||||
}
|
||||
|
||||
int ProjectViewModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
// If there's no project, there are obviously no items to show
|
||||
if (project_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If the index is the root, return the root child count
|
||||
if (parent == QModelIndex()) {
|
||||
return project_->root()->child_count();
|
||||
}
|
||||
|
||||
// Otherwise, the index must contain a valid pointer, so we just return its child count
|
||||
return static_cast<Item*>(parent.internalPointer())->child_count();
|
||||
}
|
||||
|
||||
int ProjectViewModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
|
||||
// Not strictly necessary, but a decent visual cue that there's no project currently active
|
||||
if (project_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return columns_.size();
|
||||
}
|
||||
|
||||
QVariant ProjectViewModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole) {
|
||||
|
||||
ColumnType column_type = columns_.at(index.column());
|
||||
|
||||
switch (column_type) {
|
||||
case kName:
|
||||
return static_cast<Item*>(index.internalPointer())->name();
|
||||
case kDuration:
|
||||
return "00:00:00;00";
|
||||
case kRate:
|
||||
return "29.97 FPS";
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Add DecorationRole to column 1 for icons
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant ProjectViewModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
// Check if we need text data (DisplayRole) and orientation is horizontal
|
||||
// FIXME I'm not 100% sure what happens if the orientation is vertical/if that check is necessary
|
||||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
||||
ColumnType column_type = columns_.at(section);
|
||||
|
||||
// Return the name based on the column's current type
|
||||
switch (column_type) {
|
||||
case kName:
|
||||
return tr("Name");
|
||||
case kDuration:
|
||||
return tr("Duration");
|
||||
case kRate:
|
||||
return tr("Rate");
|
||||
}
|
||||
}
|
||||
|
||||
return QAbstractItemModel::headerData(section, orientation, role);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#ifndef VIEWMODEL_H
|
||||
#define VIEWMODEL_H
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
|
||||
#include "project.h"
|
||||
|
||||
/**
|
||||
* @brief The ProjectViewModel class
|
||||
*
|
||||
* An adapter that interprets the data in a Project into a Qt item model for usage in ViewModel Views.
|
||||
*/
|
||||
class ProjectViewModel : public QAbstractItemModel
|
||||
{
|
||||
public:
|
||||
enum ColumnType {
|
||||
/// Media name
|
||||
kName,
|
||||
|
||||
/// Media duration
|
||||
kDuration,
|
||||
|
||||
/// Media rate (frame rate for video, sample rate for audio)
|
||||
kRate
|
||||
};
|
||||
|
||||
ProjectViewModel(QObject* parent);
|
||||
|
||||
/**
|
||||
* @brief Get currently active project
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* Currently active project or nullptr if there is none
|
||||
*/
|
||||
Project* project();
|
||||
|
||||
/**
|
||||
* @brief Set the project to adapt
|
||||
*
|
||||
* Any views attached to this model will get updated by this function.
|
||||
*
|
||||
* @param p
|
||||
*
|
||||
* Project to adapt, can be set to nullptr to "close" the project (will show an empty model that cannot be modified)
|
||||
*/
|
||||
void set_project(Project* p);
|
||||
|
||||
/** Compulsory Qt QAbstractItemModel overrides */
|
||||
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
|
||||
virtual QModelIndex parent(const QModelIndex &child) const override;
|
||||
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
/** Optional Qt QAbstractItemModel overrides */
|
||||
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
|
||||
private:
|
||||
Project* project_;
|
||||
|
||||
QVector<ColumnType> columns_;
|
||||
};
|
||||
|
||||
#endif // VIEWMODEL_H
|
||||
Reference in New Issue
Block a user