OSDN Git Service

Update MediaInfo binaries to v0.7.53 (2012-01-24), compiled with ICL 12.1.6 and MSVC...
[lamexp/LameXP.git] / src / Model_MetaInfo.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Model_MetaInfo.h"
23 #include "Genres.h"
24
25 #include <QMessageBox>
26 #include <QInputDialog>
27 #include <QFileInfo>
28
29 #define MODEL_ROW_COUNT 12
30
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())
35
36 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
37
38 ////////////////////////////////////////////////////////////
39 // Constructor & Destructor
40 ////////////////////////////////////////////////////////////
41
42 MetaInfoModel::MetaInfoModel(AudioFileModel *file, unsigned int offset)
43 {
44         if(offset >= MODEL_ROW_COUNT)
45         {
46                 throw "Offset is out of range!";
47         }
48
49         m_textUnknown = QString("(%1)").arg(tr("Unknown"));
50         m_textNotSpecified = QString("(%1)").arg(tr("Not Specified"));
51
52         m_audioFile = file;
53         m_offset = offset;
54 }
55
56 MetaInfoModel::~MetaInfoModel(void)
57 {
58 }
59
60 ////////////////////////////////////////////////////////////
61 // Public Functions
62 ////////////////////////////////////////////////////////////
63
64 int MetaInfoModel::columnCount(const QModelIndex &parent) const
65 {
66         return 2;
67 }
68
69 int MetaInfoModel::rowCount(const QModelIndex &parent) const
70 {
71         return MODEL_ROW_COUNT - m_offset;
72 }
73
74 QVariant MetaInfoModel::data(const QModelIndex &index, int role) const
75 {
76         if(role == Qt::DisplayRole)
77         {
78                 switch(index.row() + m_offset)
79                 {
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;
119                 }
120         }
121         else if(role == Qt::DecorationRole && index.column() == 0)
122         {
123                 switch(index.row() + m_offset)
124                 {
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;
164                 }
165         }
166         else if(role == Qt::TextColorRole && index.column() == 1)
167         {
168                 switch(index.row() + m_offset)
169                 {
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;
209                 }
210         }
211         else
212         {
213                 return QVariant();
214         }
215 }
216
217 QVariant MetaInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
218 {
219         if(role == Qt::DisplayRole)
220         {
221                 if(orientation == Qt::Horizontal)
222                 {
223                         switch(section)
224                         {
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;
234                         }
235                 }
236                 else
237                 {
238                         return QVariant();
239                 }
240         }
241         else
242         {
243                 return QVariant();
244         }
245 }
246
247 bool MetaInfoModel::setData (const QModelIndex &index, const QVariant &value, int role)
248 {
249         if((role != Qt::EditRole) || (index.column() != 1) || !value.isValid())
250         {
251                 return false;
252         }
253
254         switch(index.row() + m_offset)
255         {
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;
291         }
292
293         emit dataChanged(index, index);
294         return true;
295 }
296
297 void MetaInfoModel::editItem(const QModelIndex &index, QWidget *parent)
298 {
299         bool ok = false;
300         int val = -1;
301         QStringList generes(QString("(%1)").arg(tr("Unspecified")));
302         QString temp;
303
304         QInputDialog input(parent);
305         input.setOkButtonText(tr("OK"));
306         input.setCancelButtonText(tr("Cancel"));
307         input.setTextEchoMode(QLineEdit::Normal);
308
309         switch(index.row() + m_offset)
310         {
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)
316                 {
317                         temp = input.textValue().simplified();
318                         if(temp.isEmpty())
319                         {
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();
324                         }
325                         beginResetModel();
326                         m_audioFile->setFileName(temp.isEmpty() ? QString() : temp);
327                         endResetModel();
328                 }
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)
335                 {
336                         temp = input.textValue().simplified();
337                         beginResetModel();
338                         m_audioFile->setFileArtist(temp.isEmpty() ? QString() : temp);
339                         endResetModel();
340                 }
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)
347                 {
348                         temp = input.textValue().simplified();
349                         beginResetModel();
350                         m_audioFile->setFileAlbum(temp.isEmpty() ? QString() : temp);
351                         endResetModel();
352                 }
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)
361                 {
362                         temp = input.textValue().simplified();
363                         beginResetModel();
364                         m_audioFile->setFileGenre((temp.isEmpty() || !temp.compare(generes.at(0), Qt::CaseInsensitive)) ? QString() : temp);
365                         endResetModel();
366                 }
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)
375                 {
376                         val = input.intValue();
377                         beginResetModel();
378                         m_audioFile->setFileYear(val);
379                         endResetModel();
380                 }
381                 break;
382         case 10:
383                 if(!m_offset)
384                 {
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)
391                         {
392                                 val = input.intValue();
393                                 beginResetModel();
394                                 m_audioFile->setFilePosition(val);
395                                 endResetModel();
396                         }
397                 }
398                 else
399                 {
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)
407                         {
408                                 temp = input.textValue().simplified();
409                                 beginResetModel();
410                                 m_audioFile->setFilePosition((options.indexOf(temp) == 1) ? UINT_MAX : 0);
411                                 endResetModel();
412                         }
413                 }
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)
420                 {
421                         temp = input.textValue().simplified();
422                         beginResetModel();
423                         m_audioFile->setFileComment(temp.isEmpty() ? QString() : temp);
424                         endResetModel();
425                 }
426                 break;
427         default:
428                 QMessageBox::warning(parent, tr("Not editable"), tr("Sorry, this property of the source file cannot be edited!"));
429                 break;
430         }
431 }
432
433 void MetaInfoModel::editArtwork(const QString &imagePath)
434 {
435         m_audioFile->setFileCover(imagePath, false);
436 }
437
438 void MetaInfoModel::clearData(bool clearMetaOnly)
439 {
440         beginResetModel();
441
442         m_textUnknown = QString("(%1)").arg(tr("Unknown"));
443         m_textNotSpecified = QString("(%1)").arg(tr("Not Specified"));
444
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);
452
453         if(!clearMetaOnly)
454         {
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);
466         }
467         else
468         {
469                 QString temp = QFileInfo(m_audioFile->filePath()).baseName();
470                 temp = temp.split("-", QString::SkipEmptyParts).last().trimmed();
471                 m_audioFile->setFileName(temp);
472         }
473
474         endResetModel();
475 }
476
477 Qt::ItemFlags MetaInfoModel::flags(const QModelIndex &index) const
478 {
479         return QAbstractTableModel::flags(index);
480 }
481
482 void MetaInfoModel::assignInfoFrom(AudioFileModel &file)
483 {
484         beginResetModel();
485         m_audioFile->updateMetaInfo(file);
486         endResetModel();
487 }