1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Model_MetaInfo.h"
26 #include <QMessageBox>
27 #include <QInputDialog>
31 #define MODEL_ROW_COUNT 12
33 #define CHECK1(STR) (STR.isEmpty() ? (m_offset ? m_textNotSpecified : m_textUnknown) : STR)
34 #define CHECK2(VAL) ((VAL > 0) ? QString::number(VAL) : (m_offset ? m_textNotSpecified : m_textUnknown))
35 #define CHECK3(STR) (STR.isEmpty() ? Qt::darkGray : QVariant())
36 #define CHECK4(VAL) ((VAL == 0) ? Qt::darkGray : QVariant())
38 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
40 ////////////////////////////////////////////////////////////
41 // Constructor & Destructor
42 ////////////////////////////////////////////////////////////
44 MetaInfoModel::MetaInfoModel(AudioFileModel
*file
)
47 m_metaInfo(&file
->metaInfo()),
50 m_textUnknown
= QString("(%1)").arg(tr("Unknown"));
51 m_textNotSpecified
= QString("(%1)").arg(tr("Not Specified"));
54 MetaInfoModel::MetaInfoModel(AudioFileModel_MetaInfo
*metaInfo
)
60 m_textUnknown
= QString("(%1)").arg(tr("Unknown"));
61 m_textNotSpecified
= QString("(%1)").arg(tr("Not Specified"));
64 MetaInfoModel::~MetaInfoModel(void)
68 ////////////////////////////////////////////////////////////
70 ////////////////////////////////////////////////////////////
72 int MetaInfoModel::columnCount(const QModelIndex
&parent
) const
77 int MetaInfoModel::rowCount(const QModelIndex
&parent
) const
79 return MODEL_ROW_COUNT
- m_offset
;
82 QVariant
MetaInfoModel::data(const QModelIndex
&index
, int role
) const
84 if(role
== Qt::DisplayRole
)
86 switch(index
.row() + m_offset
)
89 return (!index
.column()) ? tr("Full Path") : CHECK1(QDir::toNativeSeparators(m_fullInfo
->filePath()));
92 return (!index
.column()) ? tr("Format") : CHECK1(m_fullInfo
->audioBaseInfo());
95 return (!index
.column()) ? tr("Container") : CHECK1(m_fullInfo
->containerInfo());
98 return (!index
.column()) ? tr("Compression") : CHECK1(m_fullInfo
->audioCompressInfo());
101 return (!index
.column()) ? tr("Duration") : CHECK1(m_fullInfo
->durationInfo());
104 return (!index
.column()) ? tr("Title") : CHECK1(m_metaInfo
->title());
107 return (!index
.column()) ? tr("Artist") : CHECK1(m_metaInfo
->artist());
110 return (!index
.column()) ? tr("Album") : CHECK1(m_metaInfo
->album());
113 return (!index
.column()) ? tr("Genre") : CHECK1(m_metaInfo
->genre());
116 return (!index
.column()) ? tr("Year") : CHECK2(m_metaInfo
->year());
119 return (!index
.column()) ? tr("Position") : ((m_metaInfo
->position() == UINT_MAX
) ? tr("Generate from list position") : CHECK2(m_metaInfo
->position()));
122 return (!index
.column()) ? tr("Comment") : CHECK1(m_metaInfo
->comment());
129 else if(role
== Qt::DecorationRole
&& index
.column() == 0)
131 switch(index
.row() + m_offset
)
134 return QIcon(":/icons/folder_page.png");
137 return QIcon(":/icons/sound.png");
140 return QIcon(":/icons/package.png");
143 return QIcon(":/icons/compress.png");
146 return QIcon(":/icons/clock_play.png");
149 return QIcon(":/icons/music.png");
152 return QIcon(":/icons/user.png");
155 return QIcon(":/icons/cd.png");
158 return QIcon(":/icons/star.png");
161 return QIcon(":/icons/date.png");
164 return QIcon(":/icons/timeline_marker.png");
167 return QIcon(":/icons/comment.png");
174 else if(role
== Qt::TextColorRole
&& index
.column() == 1)
176 switch(index
.row() + m_offset
)
179 return CHECK3(m_fullInfo
->filePath());
182 return CHECK3(m_fullInfo
->audioBaseInfo());
185 return CHECK3(m_fullInfo
->containerInfo());
188 return CHECK3(m_fullInfo
->audioCompressInfo());
191 return CHECK4(m_fullInfo
->durationInfo());
194 return CHECK3(m_metaInfo
->title());
197 return CHECK3(m_metaInfo
->artist());
200 return CHECK3(m_metaInfo
->album());
203 return CHECK3(m_metaInfo
->genre());
206 return CHECK4(m_metaInfo
->year());
209 return CHECK4(m_metaInfo
->position());
212 return CHECK3(m_metaInfo
->comment());
225 QVariant
MetaInfoModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
227 if(role
== Qt::DisplayRole
)
229 if(orientation
== Qt::Horizontal
)
234 return QVariant(tr("Property"));
237 return QVariant(tr("Value"));
255 bool MetaInfoModel::setData (const QModelIndex
&index
, const QVariant
&value
, int role
)
257 if((role
!= Qt::EditRole
) || (index
.column() != 1) || !value
.isValid())
262 switch(index
.row() + m_offset
)
265 m_fullInfo
->setFilePath(value
.toString());
273 m_fullInfo
->techInfo().setDuration(value
.toUInt());
276 m_metaInfo
->setTitle(value
.toString());
279 m_metaInfo
->setArtist(value
.toString());
282 m_metaInfo
->setAlbum(value
.toString());
285 m_metaInfo
->setGenre(value
.toString());
288 m_metaInfo
->setYear(value
.toUInt());
291 m_metaInfo
->setPosition(value
.toUInt());
294 m_metaInfo
->setComment(value
.toString());
301 emit
dataChanged(index
, index
);
305 void MetaInfoModel::editItem(const QModelIndex
&index
, QWidget
*parent
)
309 QStringList
generes(QString("(%1)").arg(tr("Unspecified")));
312 QInputDialog
input(parent
);
313 input
.setOkButtonText(tr("OK"));
314 input
.setCancelButtonText(tr("Cancel"));
315 input
.setTextEchoMode(QLineEdit::Normal
);
317 switch(index
.row() + m_offset
)
320 input
.setWindowTitle(tr("Edit Title"));
321 input
.setLabelText(EXPAND(tr("Please enter the title for this file:")));
322 input
.setTextValue(m_metaInfo
->title());
323 if(input
.exec() != 0)
325 temp
= input
.textValue().simplified();
328 QMessageBox::warning(parent
, tr("Edit Title"), tr("The title must not be empty. Generating title from file name!"));
329 temp
= QFileInfo(m_fullInfo
->filePath()).completeBaseName().replace("_", " ").simplified();
330 int index
= temp
.lastIndexOf(" - ");
331 if(index
>= 0) temp
= temp
.mid(index
+ 3).trimmed();
334 m_metaInfo
->setTitle(temp
.isEmpty() ? QString() : temp
);
339 input
.setWindowTitle(tr("Edit Artist"));
340 input
.setLabelText(EXPAND(tr("Please enter the artist for this file:")));
341 input
.setTextValue(m_metaInfo
->artist());
342 if(input
.exec() != 0)
344 temp
= input
.textValue().simplified();
346 m_metaInfo
->setArtist(temp
.isEmpty() ? QString() : temp
);
351 input
.setWindowTitle(tr("Edit Album"));
352 input
.setLabelText(EXPAND(tr("Please enter the album for this file:")));
353 input
.setTextValue(m_metaInfo
->album());
354 if(input
.exec() != 0)
356 temp
= input
.textValue().simplified();
358 m_metaInfo
->setAlbum(temp
.isEmpty() ? QString() : temp
);
363 input
.setWindowTitle(tr("Edit Genre"));
364 input
.setLabelText(EXPAND(tr("Please enter the genre for this file:")));
365 for(int i
= 0; g_lamexp_generes
[i
]; i
++) generes
<< g_lamexp_generes
[i
];
366 input
.setComboBoxItems(generes
);
367 input
.setTextValue(m_metaInfo
->genre());
368 if(input
.exec() != 0)
370 temp
= input
.textValue().simplified();
372 m_metaInfo
->setGenre((temp
.isEmpty() || !temp
.compare(generes
.at(0), Qt::CaseInsensitive
)) ? QString() : temp
);
377 input
.setWindowTitle(tr("Edit Year"));
378 input
.setLabelText(EXPAND(tr("Please enter the year for this file:")));
379 input
.setIntRange(0, 2100);
380 input
.setIntValue((m_metaInfo
->year() ? m_metaInfo
->year() : 1900));
382 if(input
.exec() != 0)
384 val
= input
.intValue();
386 m_metaInfo
->setYear(val
);
393 input
.setWindowTitle(tr("Edit Position"));
394 input
.setLabelText(EXPAND(tr("Please enter the position (track no.) for this file:")));
395 input
.setIntRange(0, 99);
396 input
.setIntValue((m_metaInfo
->position() ? m_metaInfo
->position() : 1));
398 if(input
.exec() != 0)
400 val
= input
.intValue();
402 m_metaInfo
->setPosition(val
);
409 options
<< tr("Unspecified (copy from source file)") << tr("Generate from list position");
410 input
.setWindowTitle(tr("Edit Position"));
411 input
.setLabelText(EXPAND(tr("Please enter the position (track no.) for this file:")));
412 input
.setComboBoxItems(options
);
413 input
.setTextValue(options
.value((m_metaInfo
->position() == UINT_MAX
) ? 1 : 0));
414 if(input
.exec() != 0)
416 temp
= input
.textValue().simplified();
418 m_metaInfo
->setPosition((options
.indexOf(temp
) == 1) ? UINT_MAX
: 0);
424 input
.setWindowTitle(tr("Edit Comment"));
425 input
.setLabelText(EXPAND(tr("Please enter the comment for this file:")));
426 input
.setTextValue((m_metaInfo
->comment().isEmpty() ? tr("Encoded with LameXP") : m_metaInfo
->comment()));
427 if(input
.exec() != 0)
429 temp
= input
.textValue().simplified();
431 m_metaInfo
->setComment(temp
.isEmpty() ? QString() : temp
);
436 QMessageBox::warning(parent
, tr("Not editable"), tr("Sorry, this property of the source file cannot be edited!"));
441 void MetaInfoModel::editArtwork(const QString
&imagePath
)
443 m_metaInfo
->setCover(imagePath
, false);
446 void MetaInfoModel::clearData(bool clearMetaOnly
)
450 m_textUnknown
= QString("(%1)").arg(tr("Unknown"));
451 m_textNotSpecified
= QString("(%1)").arg(tr("Not Specified"));
453 if((!clearMetaOnly
) && m_fullInfo
)
455 m_fullInfo
->techInfo().reset();
461 m_metaInfo
->setComment(tr("Encoded with LameXP"));
462 m_metaInfo
->setPosition(m_offset
? UINT_MAX
: 0);
467 QString temp
= QFileInfo(m_fullInfo
->filePath()).baseName();
468 temp
= temp
.split("-", QString::SkipEmptyParts
).last().trimmed();
469 m_metaInfo
->setTitle(temp
);
475 Qt::ItemFlags
MetaInfoModel::flags(const QModelIndex
&index
) const
477 return QAbstractTableModel::flags(index
);
480 void MetaInfoModel::assignInfoFrom(const AudioFileModel
&file
)
483 const unsigned int position
= m_metaInfo
->position();
484 m_metaInfo
->update(file
.metaInfo(), true);
487 m_metaInfo
->setTitle(QString());
488 m_metaInfo
->setPosition(position
? UINT_MAX
: 0);
489 m_metaInfo
->setCover(QString(), false);