Updated MediaInfo binary to v0.7.57 (2012-05-02), compiled with ICL 12.1.7 and MSVC...
[LameXP.git] / src / Model_MetaInfo.cpp
blob5ff2fee9adaec70bdf745a369889d75eb284d394
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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"
23 #include "Genres.h"
25 #include <QMessageBox>
26 #include <QInputDialog>
27 #include <QFileInfo>
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"));
52 m_audioFile = file;
53 m_offset = offset;
56 MetaInfoModel::~MetaInfoModel(void)
60 ////////////////////////////////////////////////////////////
61 // Public Functions
62 ////////////////////////////////////////////////////////////
64 int MetaInfoModel::columnCount(const QModelIndex &parent) const
66 return 2;
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)
80 case 0:
81 return (!index.column()) ? tr("Full Path") : CHECK1(m_audioFile->filePath());
82 break;
83 case 1:
84 return (!index.column()) ? tr("Format") : CHECK1(m_audioFile->formatAudioBaseInfo());
85 break;
86 case 2:
87 return (!index.column()) ? tr("Container") : CHECK1(m_audioFile->formatContainerInfo());
88 break;
89 case 3:
90 return (!index.column()) ? tr("Compression") : CHECK1(m_audioFile->formatAudioCompressInfo());
91 break;
92 case 4:
93 return (!index.column()) ? tr("Duration") : CHECK1(m_audioFile->fileDurationInfo());
94 break;
95 case 5:
96 return (!index.column()) ? tr("Title") : CHECK1(m_audioFile->fileName());
97 break;
98 case 6:
99 return (!index.column()) ? tr("Artist") : CHECK1(m_audioFile->fileArtist());
100 break;
101 case 7:
102 return (!index.column()) ? tr("Album") : CHECK1(m_audioFile->fileAlbum());
103 break;
104 case 8:
105 return (!index.column()) ? tr("Genre") : CHECK1(m_audioFile->fileGenre());
106 break;
107 case 9:
108 return (!index.column()) ? tr("Year") : CHECK2(m_audioFile->fileYear());
109 break;
110 case 10:
111 return (!index.column()) ? tr("Position") : ((m_audioFile->filePosition() == UINT_MAX) ? tr("Generate from list position") : CHECK2(m_audioFile->filePosition()));
112 break;
113 case 11:
114 return (!index.column()) ? tr("Comment") : CHECK1(m_audioFile->fileComment());
115 break;
116 default:
117 return QVariant();
118 break;
121 else if(role == Qt::DecorationRole && index.column() == 0)
123 switch(index.row() + m_offset)
125 case 0:
126 return QIcon(":/icons/folder_page.png");
127 break;
128 case 1:
129 return QIcon(":/icons/sound.png");
130 break;
131 case 2:
132 return QIcon(":/icons/package.png");
133 break;
134 case 3:
135 return QIcon(":/icons/compress.png");
136 break;
137 case 4:
138 return QIcon(":/icons/clock_play.png");
139 break;
140 case 5:
141 return QIcon(":/icons/music.png");
142 break;
143 case 6:
144 return QIcon(":/icons/user.png");
145 break;
146 case 7:
147 return QIcon(":/icons/cd.png");
148 break;
149 case 8:
150 return QIcon(":/icons/star.png");
151 break;
152 case 9:
153 return QIcon(":/icons/date.png");
154 break;
155 case 10:
156 return QIcon(":/icons/timeline_marker.png");
157 break;
158 case 11:
159 return QIcon(":/icons/comment.png");
160 break;
161 default:
162 return QVariant();
163 break;
166 else if(role == Qt::TextColorRole && index.column() == 1)
168 switch(index.row() + m_offset)
170 case 0:
171 return CHECK3(m_audioFile->filePath());
172 break;
173 case 1:
174 return CHECK3(m_audioFile->formatAudioBaseInfo());
175 break;
176 case 2:
177 return CHECK3(m_audioFile->formatContainerInfo());
178 break;
179 case 3:
180 return CHECK3(m_audioFile->formatAudioCompressInfo());
181 break;
182 case 4:
183 return CHECK4(m_audioFile->fileDurationInfo());
184 break;
185 case 5:
186 return CHECK3(m_audioFile->fileName());
187 break;
188 case 6:
189 return CHECK3(m_audioFile->fileArtist());
190 break;
191 case 7:
192 return CHECK3(m_audioFile->fileAlbum());
193 break;
194 case 8:
195 return CHECK3(m_audioFile->fileGenre());
196 break;
197 case 9:
198 return CHECK4(m_audioFile->fileYear());
199 break;
200 case 10:
201 return CHECK4(m_audioFile->filePosition());
202 break;
203 case 11:
204 return CHECK3(m_audioFile->fileComment());
205 break;
206 default:
207 return QVariant();
208 break;
211 else
213 return QVariant();
217 QVariant MetaInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
219 if(role == Qt::DisplayRole)
221 if(orientation == Qt::Horizontal)
223 switch(section)
225 case 0:
226 return QVariant(tr("Property"));
227 break;
228 case 1:
229 return QVariant(tr("Value"));
230 break;
231 default:
232 return QVariant();
233 break;
236 else
238 return QVariant();
241 else
243 return QVariant();
247 bool MetaInfoModel::setData (const QModelIndex &index, const QVariant &value, int role)
249 if((role != Qt::EditRole) || (index.column() != 1) || !value.isValid())
251 return false;
254 switch(index.row() + m_offset)
256 case 0:
257 m_audioFile->setFilePath(value.toString());
258 break;
259 case 1:
260 case 2:
261 case 3:
262 return false;
263 break;
264 case 4:
265 m_audioFile->setFileDuration(value.toUInt());
266 break;
267 case 5:
268 m_audioFile->setFileName(value.toString());
269 break;
270 case 6:
271 m_audioFile->setFileArtist(value.toString());
272 break;
273 case 7:
274 m_audioFile->setFileAlbum(value.toString());
275 break;
276 case 8:
277 m_audioFile->setFileGenre(value.toString());
278 break;
279 case 9:
280 m_audioFile->setFileYear(value.toUInt());
281 break;
282 case 10:
283 m_audioFile->setFilePosition(value.toUInt());
284 break;
285 case 11:
286 m_audioFile->setFileComment(value.toString());
287 break;
288 default:
289 return false;
290 break;
293 emit dataChanged(index, index);
294 return true;
297 void MetaInfoModel::editItem(const QModelIndex &index, QWidget *parent)
299 bool ok = false;
300 int val = -1;
301 QStringList generes(QString("(%1)").arg(tr("Unspecified")));
302 QString temp;
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)
311 case 5:
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();
318 if(temp.isEmpty())
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();
325 beginResetModel();
326 m_audioFile->setFileName(temp.isEmpty() ? QString() : temp);
327 endResetModel();
329 break;
330 case 6:
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();
337 beginResetModel();
338 m_audioFile->setFileArtist(temp.isEmpty() ? QString() : temp);
339 endResetModel();
341 break;
342 case 7:
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();
349 beginResetModel();
350 m_audioFile->setFileAlbum(temp.isEmpty() ? QString() : temp);
351 endResetModel();
353 break;
354 case 8:
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();
363 beginResetModel();
364 m_audioFile->setFileGenre((temp.isEmpty() || !temp.compare(generes.at(0), Qt::CaseInsensitive)) ? QString() : temp);
365 endResetModel();
367 break;
368 case 9:
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));
373 input.setIntStep(1);
374 if(input.exec() != 0)
376 val = input.intValue();
377 beginResetModel();
378 m_audioFile->setFileYear(val);
379 endResetModel();
381 break;
382 case 10:
383 if(!m_offset)
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));
389 input.setIntStep(1);
390 if(input.exec() != 0)
392 val = input.intValue();
393 beginResetModel();
394 m_audioFile->setFilePosition(val);
395 endResetModel();
398 else
400 QStringList options;
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();
409 beginResetModel();
410 m_audioFile->setFilePosition((options.indexOf(temp) == 1) ? UINT_MAX : 0);
411 endResetModel();
414 break;
415 case 11:
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();
422 beginResetModel();
423 m_audioFile->setFileComment(temp.isEmpty() ? QString() : temp);
424 endResetModel();
426 break;
427 default:
428 QMessageBox::warning(parent, tr("Not editable"), tr("Sorry, this property of the source file cannot be edited!"));
429 break;
433 void MetaInfoModel::editArtwork(const QString &imagePath)
435 m_audioFile->setFileCover(imagePath, false);
438 void MetaInfoModel::clearData(bool clearMetaOnly)
440 beginResetModel();
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);
453 if(!clearMetaOnly)
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);
467 else
469 QString temp = QFileInfo(m_audioFile->filePath()).baseName();
470 temp = temp.split("-", QString::SkipEmptyParts).last().trimmed();
471 m_audioFile->setFileName(temp);
474 endResetModel();
477 Qt::ItemFlags MetaInfoModel::flags(const QModelIndex &index) const
479 return QAbstractTableModel::flags(index);
482 void MetaInfoModel::assignInfoFrom(AudioFileModel &file)
484 beginResetModel();
485 m_audioFile->updateMetaInfo(file);
486 endResetModel();