Commit 936d1897 authored by Matthieu Gallien's avatar Matthieu Gallien 🎵
Browse files

implement removal of metadata fields

not working because the writer from KFileMetaData is not able to do that

remove metadata tags by using an invalid QVariant when saving to file
parent e0a2a944
......@@ -9,6 +9,8 @@
#include "filescanner.h"
#include "filewriter.h"
#include <QFileInfo>
class ModelDataLoaderPrivate
{
public:
......@@ -449,15 +451,27 @@ void ModelDataLoader::databaseAlbumsAdded(const ListAlbumDataType &newData)
}
}
void ModelDataLoader::trackHasBeenModified(const ModelDataLoader::ListTrackDataType &trackDataType, const QHash<QString, QUrl> &covers)
void ModelDataLoader::trackHasBeenModified(ModelDataLoader::ListTrackDataType trackDataType, const QHash<QString, QUrl> &covers)
{
Q_UNUSED(covers)
for(const auto &oneTrack : trackDataType) {
for(auto &oneTrack : trackDataType) {
if (oneTrack.elementType() == ElisaUtils::Track) {
d->mFileWriter.writeAllMetaDataToFile(oneTrack.resourceURI(), oneTrack);
QFileInfo trackFile{oneTrack.resourceURI().toLocalFile()};
oneTrack[DataTypes::FileModificationTime] = trackFile.fileTime(QFileDevice::FileModificationTime);
for (auto itData = oneTrack.begin(); itData != oneTrack.end();) {
if (itData->isNull()) {
itData = oneTrack.erase(itData);
} else {
++itData;
}
}
}
}
Q_EMIT saveTrackModified(trackDataType, covers);
}
void ModelDataLoader::updateFileMetaData(const DataTypes::TrackDataType &trackDataType, const QUrl &url)
......
......@@ -118,7 +118,7 @@ public Q_SLOTS:
void updateSingleFileMetaData(const QUrl &url, DataTypes::ColumnsRoles role, const QVariant &data);
void trackHasBeenModified(const ModelDataLoader::ListTrackDataType &trackDataType, const QHash<QString, QUrl> &covers);
void trackHasBeenModified(ListTrackDataType trackDataType, const QHash<QString, QUrl> &covers);
private Q_SLOTS:
......
......@@ -15,6 +15,186 @@ EditableTrackMetadataModel::EditableTrackMetadataModel(QObject *parent)
{
}
QVariant EditableTrackMetadataModel::data(const QModelIndex &index, int role) const
{
auto result = QVariant{};
const auto currentKey = trackKey(index.row());
switch (role)
{
case ReadOnlyRole:
switch (currentKey)
{
case DataTypes::TitleRole:
result = false;
break;
case DataTypes::ResourceRole:
switch (allTrackData().elementType())
{
case ElisaUtils::Track:
result = true;
break;
case ElisaUtils::Radio:
result = false;
break;
case ElisaUtils::Album:
case ElisaUtils::Artist:
case ElisaUtils::Composer:
case ElisaUtils::Container:
case ElisaUtils::FileName:
case ElisaUtils::Genre:
case ElisaUtils::Lyricist:
case ElisaUtils::Unknown:
result = true;
break;
}
break;
case DataTypes::ImageUrlRole:
result = false;
break;
case DataTypes::ArtistRole:
result = false;
break;
case DataTypes::AlbumRole:
result = false;
break;
case DataTypes::AlbumArtistRole:
result = false;
break;
case DataTypes::TrackNumberRole:
result = false;
break;
case DataTypes::DiscNumberRole:
result = false;
break;
case DataTypes::RatingRole:
result = false;
break;
case DataTypes::GenreRole:
result = false;
break;
case DataTypes::LyricistRole:
result = false;
break;
case DataTypes::ComposerRole:
result = false;
break;
case DataTypes::CommentRole:
result = false;
break;
case DataTypes::YearRole:
result = false;
break;
case DataTypes::LastPlayDate:
result = true;
break;
case DataTypes::PlayCounter:
result = true;
break;
case DataTypes::LyricsRole:
result = false;
break;
case DataTypes::DurationRole:
case DataTypes::SampleRateRole:
case DataTypes::BitRateRole:
case DataTypes::ChannelsRole:
case DataTypes::SecondaryTextRole:
case DataTypes::ShadowForImageRole:
case DataTypes::ChildModelRole:
case DataTypes::StringDurationRole:
case DataTypes::IsValidAlbumArtistRole:
case DataTypes::AllArtistsRole:
case DataTypes::HighestTrackRating:
case DataTypes::IdRole:
case DataTypes::ParentIdRole:
case DataTypes::DatabaseIdRole:
case DataTypes::IsSingleDiscAlbumRole:
case DataTypes::ContainerDataRole:
case DataTypes::IsPartialDataRole:
case DataTypes::AlbumIdRole:
case DataTypes::HasEmbeddedCover:
case DataTypes::FileModificationTime:
case DataTypes::FirstPlayDate:
case DataTypes::PlayFrequency:
case DataTypes::ElementTypeRole:
case DataTypes::FullDataRole:
case DataTypes::IsDirectoryRole:
case DataTypes::IsPlayListRole:
case DataTypes::FilePathRole:
break;
}
break;
case RemovableFieldRole:
switch (currentKey)
{
case DataTypes::TitleRole:
case DataTypes::BitRateRole:
case DataTypes::ChannelsRole:
case DataTypes::ArtistRole:
case DataTypes::AlbumRole:
case DataTypes::AlbumArtistRole:
case DataTypes::TrackNumberRole:
case DataTypes::DiscNumberRole:
case DataTypes::RatingRole:
case DataTypes::GenreRole:
case DataTypes::LyricistRole:
case DataTypes::ComposerRole:
case DataTypes::CommentRole:
case DataTypes::YearRole:
case DataTypes::LastPlayDate:
case DataTypes::PlayCounter:
case DataTypes::LyricsRole:
result = true;
break;
case DataTypes::ImageUrlRole:
case DataTypes::DurationRole:
case DataTypes::SampleRateRole:
case DataTypes::SecondaryTextRole:
case DataTypes::ShadowForImageRole:
case DataTypes::ChildModelRole:
case DataTypes::StringDurationRole:
case DataTypes::IsValidAlbumArtistRole:
case DataTypes::AllArtistsRole:
case DataTypes::HighestTrackRating:
case DataTypes::IdRole:
case DataTypes::ParentIdRole:
case DataTypes::DatabaseIdRole:
case DataTypes::IsSingleDiscAlbumRole:
case DataTypes::ContainerDataRole:
case DataTypes::IsPartialDataRole:
case DataTypes::AlbumIdRole:
case DataTypes::HasEmbeddedCover:
case DataTypes::FileModificationTime:
case DataTypes::FirstPlayDate:
case DataTypes::PlayFrequency:
case DataTypes::ElementTypeRole:
case DataTypes::FullDataRole:
case DataTypes::IsDirectoryRole:
case DataTypes::IsPlayListRole:
case DataTypes::FilePathRole:
case DataTypes::ResourceRole:
result = false;
break;
}
break;
default:
result = TrackMetadataModel::data(index, role);
}
return result;
}
QHash<int, QByteArray> EditableTrackMetadataModel::roleNames() const
{
auto names = TrackMetadataModel::roleNames();
names[EditableColumnRoles::ReadOnlyRole] = "isReadOnly";
names[EditableColumnRoles::RemovableFieldRole] = "isRemovable";
return names;
}
bool EditableTrackMetadataModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
auto result = TrackMetadataModel::setData(index, value, role);
......@@ -60,6 +240,20 @@ void EditableTrackMetadataModel::deleteRadio()
}
}
void EditableTrackMetadataModel::removeData(int index)
{
beginRemoveRows({}, index, index);
removeDataByIndex(index);
endRemoveRows();
if (!mIsDirty) {
mIsDirty = true;
Q_EMIT isDirtyChanged();
}
validData();
}
void EditableTrackMetadataModel::fillDataFromTrackData(const TrackMetadataModel::TrackDataType &trackData,
const QList<DataTypes::ColumnsRoles> &fieldsForTrack)
{
......@@ -92,8 +286,6 @@ void EditableTrackMetadataModel::initialize(MusicListenersManager *newManager, D
{
TrackMetadataModel::initialize(newManager, trackDatabase);
connect(this, &EditableTrackMetadataModel::saveTrackModified,
&modelDataLoader(), &ModelDataLoader::saveTrackModified);
connect(this, &EditableTrackMetadataModel::saveTrackModified,
&modelDataLoader(), &ModelDataLoader::trackHasBeenModified);
connect(this, &EditableTrackMetadataModel::deleteRadioData,
......
......@@ -28,8 +28,18 @@ class ELISALIB_EXPORT EditableTrackMetadataModel : public TrackMetadataModel
NOTIFY isDirtyChanged)
public:
enum EditableColumnRoles
{
ReadOnlyRole = TrackMetadataModel::ItemTypeRole + 1,
RemovableFieldRole,
};
Q_ENUM(EditableColumnRoles)
explicit EditableTrackMetadataModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool isDataValid() const
{
return mIsDataValid;
......@@ -48,6 +58,8 @@ public:
return mErrorMessage;
}
QHash<int, QByteArray> roleNames() const override;
Q_SIGNALS:
void isDataValidChanged();
......@@ -65,6 +77,8 @@ public Q_SLOTS:
void deleteRadio();
void removeData(int index);
protected:
void fillDataFromTrackData(const TrackMetadataModel::TrackDataType &trackData,
......
......@@ -32,7 +32,7 @@ int TrackMetadataModel::rowCount(const QModelIndex &parent) const
return 0;
}
return mTrackData.count();
return mTrackKeys.count();
}
QVariant TrackMetadataModel::data(const QModelIndex &index, int role) const
......@@ -265,108 +265,6 @@ QVariant TrackMetadataModel::data(const QModelIndex &index, int role) const
break;
}
break;
case ReadOnlyRole:
switch (currentKey)
{
case DataTypes::TitleRole:
result = false;
break;
case DataTypes::ResourceRole:
switch (mFullData.elementType())
{
case ElisaUtils::Track:
result = true;
break;
case ElisaUtils::Radio:
result = false;
break;
case ElisaUtils::Album:
case ElisaUtils::Artist:
case ElisaUtils::Composer:
case ElisaUtils::Container:
case ElisaUtils::FileName:
case ElisaUtils::Genre:
case ElisaUtils::Lyricist:
case ElisaUtils::Unknown:
result = true;
break;
}
break;
case DataTypes::ImageUrlRole:
result = false;
break;
case DataTypes::ArtistRole:
result = false;
break;
case DataTypes::AlbumRole:
result = false;
break;
case DataTypes::AlbumArtistRole:
result = false;
break;
case DataTypes::TrackNumberRole:
result = false;
break;
case DataTypes::DiscNumberRole:
result = false;
break;
case DataTypes::RatingRole:
result = false;
break;
case DataTypes::GenreRole:
result = false;
break;
case DataTypes::LyricistRole:
result = false;
break;
case DataTypes::ComposerRole:
result = false;
break;
case DataTypes::CommentRole:
result = false;
break;
case DataTypes::YearRole:
result = false;
break;
case DataTypes::LastPlayDate:
result = true;
break;
case DataTypes::PlayCounter:
result = true;
break;
case DataTypes::LyricsRole:
result = false;
break;
case DataTypes::DurationRole:
case DataTypes::SampleRateRole:
case DataTypes::BitRateRole:
case DataTypes::ChannelsRole:
case DataTypes::SecondaryTextRole:
case DataTypes::ShadowForImageRole:
case DataTypes::ChildModelRole:
case DataTypes::StringDurationRole:
case DataTypes::IsValidAlbumArtistRole:
case DataTypes::AllArtistsRole:
case DataTypes::HighestTrackRating:
case DataTypes::IdRole:
case DataTypes::ParentIdRole:
case DataTypes::DatabaseIdRole:
case DataTypes::IsSingleDiscAlbumRole:
case DataTypes::ContainerDataRole:
case DataTypes::IsPartialDataRole:
case DataTypes::AlbumIdRole:
case DataTypes::HasEmbeddedCover:
case DataTypes::FileModificationTime:
case DataTypes::FirstPlayDate:
case DataTypes::PlayFrequency:
case DataTypes::ElementTypeRole:
case DataTypes::FullDataRole:
case DataTypes::IsDirectoryRole:
case DataTypes::IsPlayListRole:
case DataTypes::FilePathRole:
break;
}
break;
}
return result;
......@@ -392,7 +290,6 @@ QHash<int, QByteArray> TrackMetadataModel::roleNames() const
names[ItemNameRole] = "name";
names[ItemTypeRole] = "type";
names[ReadOnlyRole] = "isReadOnly";
return names;
}
......@@ -584,6 +481,20 @@ const TrackMetadataModel::TrackDataType &TrackMetadataModel::displayedTrackData(
return mTrackData;
}
DataTypes::ColumnsRoles TrackMetadataModel::trackKey(int index) const
{
return mTrackKeys[index];
}
void TrackMetadataModel::removeDataByIndex(int index)
{
auto dataKey = mTrackKeys[index];
mTrackData[dataKey] = {};
mFullData[dataKey] = {};
mTrackKeys.removeAt(index);
}
void TrackMetadataModel::fetchLyrics()
{
auto lyricicsValue = QtConcurrent::run(QThreadPool::globalInstance(), [=]() {
......
......@@ -52,9 +52,10 @@ public:
{
ItemNameRole = Qt::UserRole + 1,
ItemTypeRole,
ReadOnlyRole,
};
Q_ENUM(ColumnRoles)
enum ItemType
{
TextEntry,
......@@ -148,6 +149,10 @@ protected:
const TrackDataType& displayedTrackData() const;
DataTypes::ColumnsRoles trackKey(int index) const;
void removeDataByIndex(int index);
private Q_SLOTS:
void lyricsValueIsReady();
......
......@@ -17,10 +17,12 @@ RowLayout {
property int index
property var type
property string display
property bool isRemovable
spacing: 0
signal edited()
signal deleteField()
Label {
id: metaDataLabels
......@@ -105,4 +107,17 @@ RowLayout {
}
}
}
Button {
Layout.fillHeight: true
Layout.preferredWidth: height
flat: true
display: AbstractButton.IconOnly
icon.name: 'entry-delete'
visible: isRemovable
onClicked: deleteField()
}
}
......@@ -123,8 +123,11 @@ Window {
name: model.name
display: model.display
type: model.type
isRemovable: model.isRemovable
onEdited: model.display = display
onDeleteField: realModel.removeData(model.index)
}
}
......
Markdown is supported
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