summaryrefslogtreecommitdiff
path: root/src/filelist.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2024-07-27 13:39:32 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2024-07-27 13:39:32 +0200
commitfc29354d86e6a8b5601e92405b89a1da27406ce7 (patch)
treee31069154ad5bd9497764b6205dd4d3a411b40eb /src/filelist.cc
parent329110e3230a6518f024c612842b04afdea1cd03 (diff)
WIP
Diffstat (limited to 'src/filelist.cc')
-rw-r--r--src/filelist.cc652
1 files changed, 348 insertions, 304 deletions
diff --git a/src/filelist.cc b/src/filelist.cc
index e1467e3..b746450 100644
--- a/src/filelist.cc
+++ b/src/filelist.cc
@@ -35,7 +35,6 @@
#include "itemeditor.h"
#include "project.h"
-#include <QAbstractItemModel>
#include <QTreeView>
#include <iostream>
@@ -43,7 +42,6 @@
#include <QComboBox>
#include <QLineEdit>
#include <QCheckBox>
-#include <QStyledItemDelegate>
#include <QPainter>
#include <QHeaderView>
#include <QStyleOptionButton>
@@ -51,378 +49,225 @@
#include <sndfile.h>
-class ChannelMapDeligate
- : public QStyledItemDelegate
+FileDataModel::FileDataModel(Instrument& instrument, FileList* fileList)
+ : instrument(instrument)
+ , fileList(fileList)
+{
+}
+
+QModelIndex FileDataModel::index(int row, int column, const QModelIndex &parent) const
{
-public:
- ChannelMapDeligate(Instrument& instrument)
- : instrument(instrument)
+ if(!hasIndex(row, column, parent))
{
+ return QModelIndex();
}
- QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option,
- const QModelIndex &index) const override
+ auto audiofile_ids = instrument.getAudioFileList();
+ if(!parent.isValid())
{
- // Main channel:
- if(index.column() == 2)
- {
- auto w = new QCheckBox(parent);
- auto audiofile_ids = instrument.getAudioFileList();
- auto audiofile_id = audiofile_ids.begin() + index.row();
- auto& audiofile = instrument.getAudioFile(*audiofile_id);
-
- connect(w, &QCheckBox::stateChanged,
- [&audiofile](int state)
- {
- audiofile.setMainChannel(state == Qt::Checked);
- });
-
- return w;
- }
-
- // Name:
- if(index.column() == 3)
+ if(row < audiofile_ids.size())
{
- auto w = new QLineEdit(parent);
-
- auto audiofile_ids = instrument.getAudioFileList();
- auto audiofile_id = audiofile_ids.begin() + index.row();
- auto& audiofile = instrument.getAudioFile(*audiofile_id);
-
- connect(w, &QLineEdit::textEdited,
- [&audiofile](const QString& name)
- {
- audiofile.setName(name);
- });
- return w;
+ return createIndex(row, column, &instrument);
}
-
- // Channel Map ID:
- if(index.column() == 4)
+ else
{
- auto w = new QComboBox(parent);
- w->addItem(tr("<None>"), -1);
- auto channel_ids = instrument.getProject().getChannelList();
- for(auto channel_id : channel_ids)
- {
- const auto& channel = instrument.getProject().getChannel(channel_id);
- w->addItem(channel.getChannelName(), channel.getId());
- }
-
- auto audiofile_ids = instrument.getAudioFileList();
- auto audiofile_id = audiofile_ids.begin() + index.row();
- auto& audiofile = instrument.getAudioFile(*audiofile_id);
-
- connect(w,
- // This wierd line points the compiler to the correct overloaded
- // version of QComboBox::currentIndexChanged
- // ie. chooses the (int) version over the (const QString&) version
- QOverload<int>::of(&QComboBox::currentIndexChanged),
- [w, &audiofile](int idx)
- {
- audiofile.setChannelMapId(w->itemData(idx).toInt());
- });
- return w;
+ return QModelIndex(); // row is out of bounds.
}
-
- return QStyledItemDelegate::createEditor(parent, option, index);
}
- void setEditorData(QWidget *editor, const QModelIndex &index) const override
- {
- // Main channel:
- if(index.column() == 2)
- {
- auto w = static_cast<QCheckBox*>(editor);
- auto b = index.data(Qt::EditRole).toBool();
- w->setCheckState(b ? Qt::Checked : Qt::Unchecked);
- }
-
- // Name:
- if(index.column() == 3)
- {
- auto w = static_cast<QLineEdit*>(editor);
- auto s = index.data(Qt::EditRole).toString();
- w->setText(s);
- }
+ return QModelIndex();
+}
- // Channel Map ID:
- if(index.column() == 4)
- {
- auto w = static_cast<QComboBox*>(editor);
- auto i = w->findData(index.data(Qt::EditRole).toInt());
- w->setCurrentIndex(i);
- }
+QModelIndex FileDataModel::parent(const QModelIndex &index) const
+{
+ return QModelIndex(); // no parent
}
- void setModelData(QWidget *editor, QAbstractItemModel *model,
- const QModelIndex &index) const override
+int FileDataModel::rowCount(const QModelIndex &parent) const
+{
+ if(parent.column() > 0) // only return row count on first column
{
- // Main channel:
- if(index.column() == 2)
- {
- model->setData(index,
- static_cast<QCheckBox*>(editor)->checkState() == Qt::Checked,
- Qt::EditRole);
- }
-
- // Name:
- if(index.column() == 3)
- {
- model->setData(index, static_cast<QLineEdit*>(editor)->text(),
- Qt::EditRole);
- }
-
- // Channel Map ID:
- if(index.column() == 4)
- {
- model->setData(index, static_cast<QComboBox*>(editor)->currentData(),
- Qt::EditRole);
- }
+ return 0;
}
- void paint(QPainter* painter, const QStyleOptionViewItem& option,
- const QModelIndex& index) const override
+ if(!parent.isValid())
{
- if(index.column() == 2) // Main Channel
- {
- auto audiofile_ids = instrument.getAudioFileList();
- auto audiofile_id = audiofile_ids.begin() + index.row();
- const auto& audiofile = instrument.getAudioFile(*audiofile_id);
-
- bool checked = audiofile.getMainChannel();
- QStyleOptionButton butOpt;
- butOpt.state = QStyle::State_Enabled;
- butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off;
- butOpt.rect = option.rect;
- QApplication::style()->drawControl( QStyle::CE_CheckBox,
- &butOpt, painter );
- }
- else
- {
- QStyledItemDelegate::paint(painter, option, index);
- }
+ auto audiofile_ids = instrument.getAudioFileList();
+ return audiofile_ids.size(); // root level
}
-private:
- Instrument& instrument;
-};
+ return 0; // no children
+}
-class FileDataModel
- : public QAbstractItemModel
+int FileDataModel::columnCount(const QModelIndex &parent) const
{
-public:
- FileDataModel(Instrument& instrument)
- : instrument(instrument)
+ return 6;
+}
+
+QVariant FileDataModel::data(const QModelIndex &index, int role) const
+{
+ if(!index.isValid())
{
+ return QVariant();
}
- QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
- {
- if(!hasIndex(row, column, parent))
- {
- return QModelIndex();
- }
+ auto audiofile_ids = instrument.getAudioFileList();
- auto audiofile_ids = instrument.getAudioFileList();
- if(!parent.isValid())
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ const auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ if(role == Qt::DecorationRole )
+ {
+ if(index.column() == 0)
{
- if(row < audiofile_ids.size())
+ if(audiofile.getAbsoluteFile() == instrument.getMasterFile())
{
- return createIndex(row, column, &instrument);
+ return QPixmap(":/icons/master.png");
}
else
{
- return QModelIndex(); // row is out of bounds.
+ return QPixmap(":/icons/file.png");
}
}
-
- return QModelIndex();
- }
-
- QModelIndex parent(const QModelIndex &index) const override
- {
- return QModelIndex(); // no parent
- }
-
- int rowCount(const QModelIndex &parent = QModelIndex()) const override
- {
- if(parent.column() > 0) // only return row count on first column
- {
- return 0;
- }
-
- if(!parent.isValid())
- {
- auto audiofile_ids = instrument.getAudioFileList();
- return audiofile_ids.size(); // root level
- }
-
- return 0; // no children
}
- int columnCount(const QModelIndex &parent = QModelIndex()) const override
+ if(role == Qt::DisplayRole)
{
- return 5;
- }
-
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
- {
- if(!index.isValid())
- {
- return QVariant();
- }
-
- auto audiofile_ids = instrument.getAudioFileList();
-
- auto audiofile_id = audiofile_ids.begin() + index.row();
- const auto& audiofile = instrument.getAudioFile(*audiofile_id);
-
- if(role == Qt::DecorationRole )
- {
- if(index.column() == 0)
- {
- if(audiofile.getAbsoluteFile() == instrument.getMasterFile())
- {
- return QPixmap(":/icons/master.png");
- }
- else
- {
- return QPixmap(":/icons/file.png");
- }
- }
- }
-
- if(role == Qt::DisplayRole)
+ switch(index.column())
{
- switch(index.column())
- {
- case 0: return QVariant(); // Master
- case 1: return audiofile.getFile();
- case 2: return audiofile.getMainChannel() ? "x" : " ";
- case 3: return audiofile.getName();
- case 4:
- {
- auto channelMapId = audiofile.getChannelMapId();
- if(channelMapId == -1)
- {
- return tr("<None>");
- }
- return instrument.getProject().getChannel(channelMapId).getChannelName();
- }
- default: return QVariant();
- }
- }
- else if(role == Qt::EditRole)
+ case 0: return QVariant(); // Master
+ case 1: return audiofile.getFile();
+ case 2: return audiofile.getMainChannel() ? "x" : " ";
+ case 3: return audiofile.getName();
+ case 4:
{
- switch(index.column())
+ auto channelMapId = audiofile.getChannelMapId();
+ if(channelMapId == -1)
{
- case 0: return QVariant(); // Master
- case 1: return audiofile.getFile();
- case 2: return audiofile.getMainChannel();
- case 3: return audiofile.getName();
- case 4: return audiofile.getChannelMapId();
- default: return QVariant();
+ return tr("<None>");
}
+ return instrument.getProject().getChannel(channelMapId).getChannelName();
}
- else
-
- {
- return QVariant();
+ case 5: return audiofile.getPositionChannel() ? "x" : " ";
+ default: return QVariant();
}
}
-
- QVariant headerData(int section, Qt::Orientation orientation, int role) const override
+ else if(role == Qt::EditRole)
{
- if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
+ switch(index.column())
{
- switch(section)
- {
- case 0: return tr("M");
- case 1: return tr("Filename");
- case 2: return tr("m");
- case 3: return tr("Name");
- case 4: return tr("Kit Channel");
- default: return QVariant();
- }
+ case 0: return QVariant(); // Master
+ case 1: return audiofile.getFile();
+ case 2: return audiofile.getMainChannel();
+ case 3: return audiofile.getName();
+ case 4: return audiofile.getChannelMapId();
+ case 5: return audiofile.getPositionChannel();
+ default: return QVariant();
}
+ }
+ else
+ {
return QVariant();
}
+}
- Qt::ItemFlags flags(const QModelIndex &index) const override
+QVariant FileDataModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
- if(!index.isValid())
+ switch(section)
{
- return 0;
- }
-
- switch(index.column())
- {
- case 0: // Master
- return QAbstractItemModel::flags(index);
- case 1: // File
- return QAbstractItemModel::flags(index);
- case 2: // Main channel
- return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
- case 3: // Name
- return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
- case 4: // Channel map id
- return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
+ case 0: return tr("M");
+ case 1: return tr("Filename");
+ case 2: return tr("m");
+ case 3: return tr("Name");
+ case 4: return tr("Kit Channel");
+ case 5: return tr("p");
+ default: return QVariant();
}
}
- bool setData(const QModelIndex &index, const QVariant &value,
- int role = Qt::EditRole) override
+ return QVariant();
+}
+
+Qt::ItemFlags FileDataModel::flags(const QModelIndex &index) const
+{
+ if(!index.isValid())
{
- auto audiofile_ids = instrument.getAudioFileList();
+ return 0;
+ }
- if(index.row() > audiofile_ids.size() ||
- index.column() > (columnCount() - 1))
- {
- return false;
- }
+ switch(index.column())
+ {
+ case 0: // Master
+ return QAbstractItemModel::flags(index);
+ case 1: // File
+ return QAbstractItemModel::flags(index);
+ case 2: // Main channel
+ return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
+ case 3: // Name
+ return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
+ case 4: // Channel map id
+ return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
+ case 5: // Pos channel
+ return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
+ }
- auto audiofile_id = audiofile_ids.begin() + index.row();
- auto& audiofile = instrument.getAudioFile(*audiofile_id);
+ return 0;
+}
- switch(index.column())
- {
- case 0: // Master
- break;
- case 1: // File
- break;
- case 2: // Main Channel
- audiofile.setMainChannel(value.toBool());
- break;
- case 3: // Name
- audiofile.setName(value.toString());
- break;
- case 4: // Channel map id
- audiofile.setChannelMapId(value.toInt());
- break;
- default: break;
- }
+bool FileDataModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ auto audiofile_ids = instrument.getAudioFileList();
- return true;
+ if(index.row() > audiofile_ids.size() ||
+ index.column() > (columnCount() - 1))
+ {
+ return false;
}
- void refresh()
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ switch(index.column())
{
- beginResetModel();
- endResetModel();
+ case 0: // Master
+ break;
+ case 1: // File
+ break;
+ case 2: // Main Channel
+ audiofile.setMainChannel(value.toBool());
+ break;
+ case 3: // Name
+ audiofile.setName(value.toString());
+ break;
+ case 4: // Channel map id
+ audiofile.setChannelMapId(value.toInt());
+ break;
+ case 5: // Pos Channel
+ audiofile.setPositionChannel(value.toBool());
+ fileList->positionFilesChanged();
+ break;
+ default: break;
}
-private:
- Instrument& instrument;
-};
+ return true;
+}
+
+void FileDataModel::refresh()
+{
+ beginResetModel();
+ endResetModel();
+}
+
FileList::FileList(Instrument& instrument)
: instrument(instrument)
{
- model = new FileDataModel(instrument);
+ model = new FileDataModel(instrument, this);
setModel(model);
- setItemDelegate(new ChannelMapDeligate(instrument));
+ setItemDelegate(new ChannelMapDeligate(instrument, this));
setEditTriggers(QAbstractItemView::AllEditTriggers); // show list on click
setContextMenuPolicy(Qt::CustomContextMenu); // enable context menu
@@ -584,3 +429,202 @@ void FileList::removeAllFiles()
emit masterFileChanged(""); // Clear canvas
emit allFilesRemoved();
}
+
+
+ChannelMapDeligate::ChannelMapDeligate(Instrument& instrument, FileList* fileList)
+ : instrument(instrument)
+ , fileList(fileList)
+{
+}
+
+QWidget* ChannelMapDeligate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
+ const QModelIndex &index) const
+{
+ // Main channel:
+ if(index.column() == 2)
+ {
+ auto w = new QCheckBox(parent);
+ auto audiofile_ids = instrument.getAudioFileList();
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ connect(w, &QCheckBox::stateChanged,
+ [&audiofile](int state)
+ {
+ audiofile.setMainChannel(state == Qt::Checked);
+ });
+
+ return w;
+ }
+
+ // Name:
+ if(index.column() == 3)
+ {
+ auto w = new QLineEdit(parent);
+
+ auto audiofile_ids = instrument.getAudioFileList();
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ connect(w, &QLineEdit::textEdited,
+ [&audiofile](const QString& name)
+ {
+ audiofile.setName(name);
+ });
+ return w;
+ }
+
+ // Channel Map ID:
+ if(index.column() == 4)
+ {
+ auto w = new QComboBox(parent);
+ w->addItem(tr("<None>"), -1);
+ auto channel_ids = instrument.getProject().getChannelList();
+ for(auto channel_id : channel_ids)
+ {
+ const auto& channel = instrument.getProject().getChannel(channel_id);
+ w->addItem(channel.getChannelName(), channel.getId());
+ }
+
+ auto audiofile_ids = instrument.getAudioFileList();
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ connect(w,
+ // This wierd line points the compiler to the correct overloaded
+ // version of QComboBox::currentIndexChanged
+ // ie. chooses the (int) version over the (const QString&) version
+ QOverload<int>::of(&QComboBox::currentIndexChanged),
+ [w, &audiofile](int idx)
+ {
+ audiofile.setChannelMapId(w->itemData(idx).toInt());
+ });
+ return w;
+ }
+
+ // Pos channel:
+ if(index.column() == 5)
+ {
+ auto w = new QCheckBox(parent);
+ auto audiofile_ids = instrument.getAudioFileList();
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ connect(w, &QCheckBox::stateChanged,
+ [&](int state)
+ {
+ audiofile.setPositionChannel(state == Qt::Checked);
+ fileList->positionFilesChanged();
+ });
+
+ return w;
+ }
+
+ return QStyledItemDelegate::createEditor(parent, option, index);
+}
+
+void ChannelMapDeligate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+ // Main channel:
+ if(index.column() == 2)
+ {
+ auto w = static_cast<QCheckBox*>(editor);
+ auto b = index.data(Qt::EditRole).toBool();
+ w->setCheckState(b ? Qt::Checked : Qt::Unchecked);
+ }
+
+ // Name:
+ if(index.column() == 3)
+ {
+ auto w = static_cast<QLineEdit*>(editor);
+ auto s = index.data(Qt::EditRole).toString();
+ w->setText(s);
+ }
+
+ // Channel Map ID:
+ if(index.column() == 4)
+ {
+ auto w = static_cast<QComboBox*>(editor);
+ auto i = w->findData(index.data(Qt::EditRole).toInt());
+ w->setCurrentIndex(i);
+ }
+
+ // Pos channel:
+ if(index.column() == 5)
+ {
+ auto w = static_cast<QCheckBox*>(editor);
+ auto b = index.data(Qt::EditRole).toBool();
+ w->setCheckState(b ? Qt::Checked : Qt::Unchecked);
+ }
+}
+
+void ChannelMapDeligate::setModelData(QWidget *editor, QAbstractItemModel *model,
+ const QModelIndex &index) const
+{
+ // Main channel:
+ if(index.column() == 2)
+ {
+ model->setData(index,
+ static_cast<QCheckBox*>(editor)->checkState() == Qt::Checked,
+ Qt::EditRole);
+ }
+
+ // Name:
+ if(index.column() == 3)
+ {
+ model->setData(index, static_cast<QLineEdit*>(editor)->text(),
+ Qt::EditRole);
+ }
+
+ // Channel Map ID:
+ if(index.column() == 4)
+ {
+ model->setData(index, static_cast<QComboBox*>(editor)->currentData(),
+ Qt::EditRole);
+ }
+
+ // Pos channel:
+ if(index.column() == 5)
+ {
+ model->setData(index,
+ static_cast<QCheckBox*>(editor)->checkState() == Qt::Checked,
+ Qt::EditRole);
+ }
+}
+
+void ChannelMapDeligate::paint(QPainter* painter, const QStyleOptionViewItem& option,
+ const QModelIndex& index) const
+{
+ if(index.column() == 2) // Main Channel
+ {
+ auto audiofile_ids = instrument.getAudioFileList();
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ const auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ bool checked = audiofile.getMainChannel();
+ QStyleOptionButton butOpt;
+ butOpt.state = QStyle::State_Enabled;
+ butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off;
+ butOpt.rect = option.rect;
+ QApplication::style()->drawControl( QStyle::CE_CheckBox,
+ &butOpt, painter );
+ }
+ else if(index.column() == 5) // Pos Channel
+ {
+ auto audiofile_ids = instrument.getAudioFileList();
+ auto audiofile_id = audiofile_ids.begin() + index.row();
+ const auto& audiofile = instrument.getAudioFile(*audiofile_id);
+
+ bool checked = audiofile.getPositionChannel();
+ QStyleOptionButton butOpt;
+ butOpt.state = QStyle::State_Enabled;
+ butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off;
+ butOpt.rect = option.rect;
+ QApplication::style()->drawControl( QStyle::CE_CheckBox,
+ &butOpt, painter );
+ }
+ else
+ {
+ QStyledItemDelegate::paint(painter, option, index);
+ }
+}