1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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, unsigned int offset)
44 if(offset >= MODEL_ROW_COUNT)
46 throw "Offset is out of range!";
49 m_textUnknown = QString("(%1)").arg(tr("Unknown"));
50 m_textNotSpecified = QString("(%1)").arg(tr("Not Specified"));
56 MetaInfoModel::~MetaInfoModel(void)
60 ////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////
64 int MetaInfoModel::columnCount(const QModelIndex &parent) const
69 int MetaInfoModel::rowCount(const QModelIndex &parent) const
71 return MODEL_ROW_COUNT - m_offset;
74 QVariant MetaInfoModel::data(const QModelIndex &index, int role) const
76 if(role == Qt::DisplayRole)
78 switch(index.row() + m_offset)
81 return (!index.column()) ? tr("Full Path") : CHECK1(m_audioFile->filePath());
84 return (!index.column()) ? tr("Format") : CHECK1(m_audioFile->formatAudioBaseInfo());
87 return (!index.column()) ? tr("Container") : CHECK1(m_audioFile->formatContainerInfo());
90 return (!index.column()) ? tr("Compression") : CHECK1(m_audioFile->formatAudioCompressInfo());
93 return (!index.column()) ? tr("Duration") : CHECK1(m_audioFile->fileDurationInfo());
96 return (!index.column()) ? tr("Title") : CHECK1(m_audioFile->fileName());
99 return (!index.column()) ? tr("Artist") : CHECK1(m_audioFile->fileArtist());
102 return (!index.column()) ? tr("Album") : CHECK1(m_audioFile->fileAlbum());
105 return (!index.column()) ? tr("Genre") : CHECK1(m_audioFile->fileGenre());
108 return (!index.column()) ? tr("Year") : CHECK2(m_audioFile->fileYear());
111 return (!index.column()) ? tr("Position") : ((m_audioFile->filePosition() == UINT_MAX) ? tr("Generate from list position") : CHECK2(m_audioFile->filePosition()));
114 return (!index.column()) ? tr("Comment") : CHECK1(m_audioFile->fileComment());
121 else if(role == Qt::DecorationRole && index.column() == 0)
123 switch(index.row() + m_offset)
126 return QIcon(":/icons/folder_page.png");
129 return QIcon(":/icons/sound.png");
132 return QIcon(":/icons/package.png");
135 return QIcon(":/icons/compress.png");
138 return QIcon(":/icons/clock_play.png");
141 return QIcon(":/icons/music.png");
144 return QIcon(":/icons/user.png");
147 return QIcon(":/icons/cd.png");
150 return QIcon(":/icons/star.png");
153 return QIcon(":/icons/date.png");
156 return QIcon(":/icons/timeline_marker.png");
159 return QIcon(":/icons/comment.png");
166 else if(role == Qt::TextColorRole && index.column() == 1)
168 switch(index.row() + m_offset)
171 return CHECK3(m_audioFile->filePath());
174 return CHECK3(m_audioFile->formatAudioBaseInfo());
177 return CHECK3(m_audioFile->formatContainerInfo());
180 return CHECK3(m_audioFile->formatAudioCompressInfo());
183 return CHECK4(m_audioFile->fileDurationInfo());
186 return CHECK3(m_audioFile->fileName());
189 return CHECK3(m_audioFile->fileArtist());
192 return CHECK3(m_audioFile->fileAlbum());
195 return CHECK3(m_audioFile->fileGenre());
198 return CHECK4(m_audioFile->fileYear());
201 return CHECK4(m_audioFile->filePosition());
204 return CHECK3(m_audioFile->fileComment());
217 QVariant MetaInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
219 if(role == Qt::DisplayRole)
221 if(orientation == Qt::Horizontal)
226 return QVariant(tr("Property"));
229 return QVariant(tr("Value"));
247 bool MetaInfoModel::setData (const QModelIndex &index, const QVariant &value, int role)
249 if((role != Qt::EditRole) || (index.column() != 1) || !value.isValid())
254 switch(index.row() + m_offset)
257 m_audioFile->setFilePath(value.toString());
265 m_audioFile->setFileDuration(value.toUInt());
268 m_audioFile->setFileName(value.toString());
271 m_audioFile->setFileArtist(value.toString());
274 m_audioFile->setFileAlbum(value.toString());
277 m_audioFile->setFileGenre(value.toString());
280 m_audioFile->setFileYear(value.toUInt());
283 m_audioFile->setFilePosition(value.toUInt());
286 m_audioFile->setFileComment(value.toString());
293 emit dataChanged(index, index);
297 void MetaInfoModel::editItem(const QModelIndex &index, QWidget *parent)
301 QStringList generes(QString("(%1)").arg(tr("Unspecified")));
304 QInputDialog input(parent);
305 input.setOkButtonText(tr("OK"));
306 input.setCancelButtonText(tr("Cancel"));
307 input.setTextEchoMode(QLineEdit::Normal);
309 switch(index.row() + m_offset)
312 input.setWindowTitle(tr("Edit Title"));
313 input.setLabelText(EXPAND(tr("Please enter the title for this file:")));
314 input.setTextValue(m_audioFile->fileName());
315 if(input.exec() != 0)
317 temp = input.textValue().simplified();
320 QMessageBox::warning(parent, tr("Edit Title"), tr("The title must not be empty. Generating title from file name!"));
321 temp = QFileInfo(m_audioFile->filePath()).completeBaseName().replace("_", " ").simplified();
322 int index = temp.lastIndexOf(" - ");
323 if(index >= 0) temp = temp.mid(index + 3).trimmed();
326 m_audioFile->setFileName(temp.isEmpty() ? QString() : temp);
331 input.setWindowTitle(tr("Edit Artist"));
332 input.setLabelText(EXPAND(tr("Please enter the artist for this file:")));
333 input.setTextValue(m_audioFile->fileArtist());
334 if(input.exec() != 0)
336 temp = input.textValue().simplified();
338 m_audioFile->setFileArtist(temp.isEmpty() ? QString() : temp);
343 input.setWindowTitle(tr("Edit Album"));
344 input.setLabelText(EXPAND(tr("Please enter the album for this file:")));
345 input.setTextValue(m_audioFile->fileAlbum());
346 if(input.exec() != 0)
348 temp = input.textValue().simplified();
350 m_audioFile->setFileAlbum(temp.isEmpty() ? QString() : temp);
355 input.setWindowTitle(tr("Edit Genre"));
356 input.setLabelText(EXPAND(tr("Please enter the genre for this file:")));
357 for(int i = 0; g_lamexp_generes[i]; i++) generes << g_lamexp_generes[i];
358 input.setComboBoxItems(generes);
359 input.setTextValue(m_audioFile->fileGenre());
360 if(input.exec() != 0)
362 temp = input.textValue().simplified();
364 m_audioFile->setFileGenre((temp.isEmpty() || !temp.compare(generes.at(0), Qt::CaseInsensitive)) ? QString() : temp);
369 input.setWindowTitle(tr("Edit Year"));
370 input.setLabelText(EXPAND(tr("Please enter the year for this file:")));
371 input.setIntRange(0, 2100);
372 input.setIntValue((m_audioFile->fileYear() ? m_audioFile->fileYear() : 1900));
374 if(input.exec() != 0)
376 val = input.intValue();
378 m_audioFile->setFileYear(val);
385 input.setWindowTitle(tr("Edit Position"));
386 input.setLabelText(EXPAND(tr("Please enter the position (track no.) for this file:")));
387 input.setIntRange(0, 99);
388 input.setIntValue((m_audioFile->filePosition() ? m_audioFile->filePosition() : 1));
390 if(input.exec() != 0)
392 val = input.intValue();
394 m_audioFile->setFilePosition(val);
401 options << tr("Unspecified (copy from source file)") << tr("Generate from list position");
402 input.setWindowTitle(tr("Edit Position"));
403 input.setLabelText(EXPAND(tr("Please enter the position (track no.) for this file:")));
404 input.setComboBoxItems(options);
405 input.setTextValue(options.value((m_audioFile->filePosition() == UINT_MAX) ? 1 : 0));
406 if(input.exec() != 0)
408 temp = input.textValue().simplified();
410 m_audioFile->setFilePosition((options.indexOf(temp) == 1) ? UINT_MAX : 0);
416 input.setWindowTitle(tr("Edit Comment"));
417 input.setLabelText(EXPAND(tr("Please enter the comment for this file:")));
418 input.setTextValue((m_audioFile->fileComment().isEmpty() ? tr("Encoded with LameXP") : m_audioFile->fileComment()));
419 if(input.exec() != 0)
421 temp = input.textValue().simplified();
423 m_audioFile->setFileComment(temp.isEmpty() ? QString() : temp);
428 QMessageBox::warning(parent, tr("Not editable"), tr("Sorry, this property of the source file cannot be edited!"));
433 void MetaInfoModel::editArtwork(const QString &imagePath)
435 m_audioFile->setFileCover(imagePath, false);
438 void MetaInfoModel::clearData(bool clearMetaOnly)
442 m_textUnknown = QString("(%1)").arg(tr("Unknown"));
443 m_textNotSpecified = QString("(%1)").arg(tr("Not Specified"));
445 m_audioFile->setFileArtist(QString());
446 m_audioFile->setFileAlbum(QString());
447 m_audioFile->setFileGenre(QString());
448 m_audioFile->setFileComment(tr("Encoded with LameXP"));
449 m_audioFile->setFileCover(QString(), false);
450 m_audioFile->setFileYear(0);
451 m_audioFile->setFilePosition(m_offset ? UINT_MAX : 0);
455 m_audioFile->setFilePath(QString());
456 m_audioFile->setFileName(QString());
457 m_audioFile->setFileDuration(0);
458 m_audioFile->setFormatContainerType(QString());
459 m_audioFile->setFormatContainerProfile(QString());
460 m_audioFile->setFormatAudioType(QString());
461 m_audioFile->setFormatAudioProfile(QString());
462 m_audioFile->setFormatAudioVersion(QString());
463 m_audioFile->setFormatAudioSamplerate(0);
464 m_audioFile->setFormatAudioChannels(0);
465 m_audioFile->setFormatAudioBitdepth(0);
469 QString temp = QFileInfo(m_audioFile->filePath()).baseName();
470 temp = temp.split("-", QString::SkipEmptyParts).last().trimmed();
471 m_audioFile->setFileName(temp);
477 Qt::ItemFlags MetaInfoModel::flags(const QModelIndex &index) const
479 return QAbstractTableModel::flags(index);
482 void MetaInfoModel::assignInfoFrom(AudioFileModel &file)
485 m_audioFile->updateMetaInfo(file);