1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Model_MetaInfo.h"
25 #include <QMessageBox>
26 #include <QInputDialog>
29 #define MODEL_ROW_COUNT 12
31 #define CHECK1(STR) (STR.isEmpty() ? (m_offset ? m_textNotSpecified : m_textUnknown) : STR)
32 #define CHECK2(VAL) ((VAL > 0) ? QString::number(VAL) : (m_offset ? m_textNotSpecified : m_textUnknown))
33 #define CHECK3(STR) (STR.isEmpty() ? Qt::darkGray : QVariant())
34 #define CHECK4(VAL) ((VAL == 0) ? Qt::darkGray : QVariant())
36 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
38 ////////////////////////////////////////////////////////////
39 // Constructor & Destructor
40 ////////////////////////////////////////////////////////////
42 MetaInfoModel::MetaInfoModel(AudioFileModel
*file
)
45 m_metaInfo(&file
->metaInfo()),
48 m_textUnknown
= QString("(%1)").arg(tr("Unknown"));
49 m_textNotSpecified
= QString("(%1)").arg(tr("Not Specified"));
52 MetaInfoModel::MetaInfoModel(AudioFileModel_MetaInfo
*metaInfo
)
58 m_textUnknown
= QString("(%1)").arg(tr("Unknown"));
59 m_textNotSpecified
= QString("(%1)").arg(tr("Not Specified"));
62 MetaInfoModel::~MetaInfoModel(void)
66 ////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////
70 int MetaInfoModel::columnCount(const QModelIndex
&parent
) const
75 int MetaInfoModel::rowCount(const QModelIndex
&parent
) const
77 return MODEL_ROW_COUNT
- m_offset
;
80 QVariant
MetaInfoModel::data(const QModelIndex
&index
, int role
) const
82 if(role
== Qt::DisplayRole
)
84 switch(index
.row() + m_offset
)
87 return (!index
.column()) ? tr("Full Path") : CHECK1(m_fullInfo
->filePath());
90 return (!index
.column()) ? tr("Format") : CHECK1(m_fullInfo
->audioBaseInfo());
93 return (!index
.column()) ? tr("Container") : CHECK1(m_fullInfo
->containerInfo());
96 return (!index
.column()) ? tr("Compression") : CHECK1(m_fullInfo
->audioCompressInfo());
99 return (!index
.column()) ? tr("Duration") : CHECK1(m_fullInfo
->durationInfo());
102 return (!index
.column()) ? tr("Title") : CHECK1(m_metaInfo
->title());
105 return (!index
.column()) ? tr("Artist") : CHECK1(m_metaInfo
->artist());
108 return (!index
.column()) ? tr("Album") : CHECK1(m_metaInfo
->album());
111 return (!index
.column()) ? tr("Genre") : CHECK1(m_metaInfo
->genre());
114 return (!index
.column()) ? tr("Year") : CHECK2(m_metaInfo
->year());
117 return (!index
.column()) ? tr("Position") : ((m_metaInfo
->position() == UINT_MAX
) ? tr("Generate from list position") : CHECK2(m_metaInfo
->position()));
120 return (!index
.column()) ? tr("Comment") : CHECK1(m_metaInfo
->comment());
127 else if(role
== Qt::DecorationRole
&& index
.column() == 0)
129 switch(index
.row() + m_offset
)
132 return QIcon(":/icons/folder_page.png");
135 return QIcon(":/icons/sound.png");
138 return QIcon(":/icons/package.png");
141 return QIcon(":/icons/compress.png");
144 return QIcon(":/icons/clock_play.png");
147 return QIcon(":/icons/music.png");
150 return QIcon(":/icons/user.png");
153 return QIcon(":/icons/cd.png");
156 return QIcon(":/icons/star.png");
159 return QIcon(":/icons/date.png");
162 return QIcon(":/icons/timeline_marker.png");
165 return QIcon(":/icons/comment.png");
172 else if(role
== Qt::TextColorRole
&& index
.column() == 1)
174 switch(index
.row() + m_offset
)
177 return CHECK3(m_fullInfo
->filePath());
180 return CHECK3(m_fullInfo
->audioBaseInfo());
183 return CHECK3(m_fullInfo
->containerInfo());
186 return CHECK3(m_fullInfo
->audioCompressInfo());
189 return CHECK4(m_fullInfo
->durationInfo());
192 return CHECK3(m_metaInfo
->title());
195 return CHECK3(m_metaInfo
->artist());
198 return CHECK3(m_metaInfo
->album());
201 return CHECK3(m_metaInfo
->genre());
204 return CHECK4(m_metaInfo
->year());
207 return CHECK4(m_metaInfo
->position());
210 return CHECK3(m_metaInfo
->comment());
223 QVariant
MetaInfoModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
225 if(role
== Qt::DisplayRole
)
227 if(orientation
== Qt::Horizontal
)
232 return QVariant(tr("Property"));
235 return QVariant(tr("Value"));
253 bool MetaInfoModel::setData (const QModelIndex
&index
, const QVariant
&value
, int role
)
255 if((role
!= Qt::EditRole
) || (index
.column() != 1) || !value
.isValid())
260 switch(index
.row() + m_offset
)
263 m_fullInfo
->setFilePath(value
.toString());
271 m_fullInfo
->techInfo().setDuration(value
.toUInt());
274 m_metaInfo
->setTitle(value
.toString());
277 m_metaInfo
->setArtist(value
.toString());
280 m_metaInfo
->setAlbum(value
.toString());
283 m_metaInfo
->setGenre(value
.toString());
286 m_metaInfo
->setYear(value
.toUInt());
289 m_metaInfo
->setPosition(value
.toUInt());
292 m_metaInfo
->setComment(value
.toString());
299 emit
dataChanged(index
, index
);
303 void MetaInfoModel::editItem(const QModelIndex
&index
, QWidget
*parent
)
307 QStringList
generes(QString("(%1)").arg(tr("Unspecified")));
310 QInputDialog
input(parent
);
311 input
.setOkButtonText(tr("OK"));
312 input
.setCancelButtonText(tr("Cancel"));
313 input
.setTextEchoMode(QLineEdit::Normal
);
315 switch(index
.row() + m_offset
)
318 input
.setWindowTitle(tr("Edit Title"));
319 input
.setLabelText(EXPAND(tr("Please enter the title for this file:")));
320 input
.setTextValue(m_metaInfo
->title());
321 if(input
.exec() != 0)
323 temp
= input
.textValue().simplified();
326 QMessageBox::warning(parent
, tr("Edit Title"), tr("The title must not be empty. Generating title from file name!"));
327 temp
= QFileInfo(m_fullInfo
->filePath()).completeBaseName().replace("_", " ").simplified();
328 int index
= temp
.lastIndexOf(" - ");
329 if(index
>= 0) temp
= temp
.mid(index
+ 3).trimmed();
332 m_metaInfo
->setTitle(temp
.isEmpty() ? QString() : temp
);
337 input
.setWindowTitle(tr("Edit Artist"));
338 input
.setLabelText(EXPAND(tr("Please enter the artist for this file:")));
339 input
.setTextValue(m_metaInfo
->artist());
340 if(input
.exec() != 0)
342 temp
= input
.textValue().simplified();
344 m_metaInfo
->setArtist(temp
.isEmpty() ? QString() : temp
);
349 input
.setWindowTitle(tr("Edit Album"));
350 input
.setLabelText(EXPAND(tr("Please enter the album for this file:")));
351 input
.setTextValue(m_metaInfo
->album());
352 if(input
.exec() != 0)
354 temp
= input
.textValue().simplified();
356 m_metaInfo
->setAlbum(temp
.isEmpty() ? QString() : temp
);
361 input
.setWindowTitle(tr("Edit Genre"));
362 input
.setLabelText(EXPAND(tr("Please enter the genre for this file:")));
363 for(int i
= 0; g_lamexp_generes
[i
]; i
++) generes
<< g_lamexp_generes
[i
];
364 input
.setComboBoxItems(generes
);
365 input
.setTextValue(m_metaInfo
->genre());
366 if(input
.exec() != 0)
368 temp
= input
.textValue().simplified();
370 m_metaInfo
->setGenre((temp
.isEmpty() || !temp
.compare(generes
.at(0), Qt::CaseInsensitive
)) ? QString() : temp
);
375 input
.setWindowTitle(tr("Edit Year"));
376 input
.setLabelText(EXPAND(tr("Please enter the year for this file:")));
377 input
.setIntRange(0, 2100);
378 input
.setIntValue((m_metaInfo
->year() ? m_metaInfo
->year() : 1900));
380 if(input
.exec() != 0)
382 val
= input
.intValue();
384 m_metaInfo
->setYear(val
);
391 input
.setWindowTitle(tr("Edit Position"));
392 input
.setLabelText(EXPAND(tr("Please enter the position (track no.) for this file:")));
393 input
.setIntRange(0, 99);
394 input
.setIntValue((m_metaInfo
->position() ? m_metaInfo
->position() : 1));
396 if(input
.exec() != 0)
398 val
= input
.intValue();
400 m_metaInfo
->setPosition(val
);
407 options
<< tr("Unspecified (copy from source file)") << tr("Generate from list position");
408 input
.setWindowTitle(tr("Edit Position"));
409 input
.setLabelText(EXPAND(tr("Please enter the position (track no.) for this file:")));
410 input
.setComboBoxItems(options
);
411 input
.setTextValue(options
.value((m_metaInfo
->position() == UINT_MAX
) ? 1 : 0));
412 if(input
.exec() != 0)
414 temp
= input
.textValue().simplified();
416 m_metaInfo
->setPosition((options
.indexOf(temp
) == 1) ? UINT_MAX
: 0);
422 input
.setWindowTitle(tr("Edit Comment"));
423 input
.setLabelText(EXPAND(tr("Please enter the comment for this file:")));
424 input
.setTextValue((m_metaInfo
->comment().isEmpty() ? tr("Encoded with LameXP") : m_metaInfo
->comment()));
425 if(input
.exec() != 0)
427 temp
= input
.textValue().simplified();
429 m_metaInfo
->setComment(temp
.isEmpty() ? QString() : temp
);
434 QMessageBox::warning(parent
, tr("Not editable"), tr("Sorry, this property of the source file cannot be edited!"));
439 void MetaInfoModel::editArtwork(const QString
&imagePath
)
441 m_metaInfo
->setCover(imagePath
, false);
444 void MetaInfoModel::clearData(bool clearMetaOnly
)
448 m_textUnknown
= QString("(%1)").arg(tr("Unknown"));
449 m_textNotSpecified
= QString("(%1)").arg(tr("Not Specified"));
451 if((!clearMetaOnly
) && m_fullInfo
)
453 m_fullInfo
->techInfo().reset();
459 m_metaInfo
->setComment(tr("Encoded with LameXP"));
460 m_metaInfo
->setPosition(m_offset
? UINT_MAX
: 0);
465 QString temp
= QFileInfo(m_fullInfo
->filePath()).baseName();
466 temp
= temp
.split("-", QString::SkipEmptyParts
).last().trimmed();
467 m_metaInfo
->setTitle(temp
);
473 Qt::ItemFlags
MetaInfoModel::flags(const QModelIndex
&index
) const
475 return QAbstractTableModel::flags(index
);
478 void MetaInfoModel::assignInfoFrom(const AudioFileModel
&file
)
481 m_metaInfo
->update(file
.metaInfo(), true);