Updated CueImportDialog and CueSheetModel as well as the CueSheet helper classes...
[LameXP.git] / src / Model_MetaInfo.cpp
blobb9b6250734c43ec1f2cf016006683fde2fd98f28
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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)
44 m_fullInfo(file),
45 m_metaInfo(&file->metaInfo()),
46 m_offset(0)
48 m_textUnknown = QString("(%1)").arg(tr("Unknown"));
49 m_textNotSpecified = QString("(%1)").arg(tr("Not Specified"));
52 MetaInfoModel::MetaInfoModel(AudioFileModel_MetaInfo *metaInfo)
54 m_fullInfo(NULL),
55 m_metaInfo(metaInfo),
56 m_offset(6)
58 m_textUnknown = QString("(%1)").arg(tr("Unknown"));
59 m_textNotSpecified = QString("(%1)").arg(tr("Not Specified"));
62 MetaInfoModel::~MetaInfoModel(void)
66 ////////////////////////////////////////////////////////////
67 // Public Functions
68 ////////////////////////////////////////////////////////////
70 int MetaInfoModel::columnCount(const QModelIndex &parent) const
72 return 2;
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)
86 case 0:
87 return (!index.column()) ? tr("Full Path") : CHECK1(m_fullInfo->filePath());
88 break;
89 case 1:
90 return (!index.column()) ? tr("Format") : CHECK1(m_fullInfo->audioBaseInfo());
91 break;
92 case 2:
93 return (!index.column()) ? tr("Container") : CHECK1(m_fullInfo->containerInfo());
94 break;
95 case 3:
96 return (!index.column()) ? tr("Compression") : CHECK1(m_fullInfo->audioCompressInfo());
97 break;
98 case 4:
99 return (!index.column()) ? tr("Duration") : CHECK1(m_fullInfo->durationInfo());
100 break;
101 case 5:
102 return (!index.column()) ? tr("Title") : CHECK1(m_metaInfo->title());
103 break;
104 case 6:
105 return (!index.column()) ? tr("Artist") : CHECK1(m_metaInfo->artist());
106 break;
107 case 7:
108 return (!index.column()) ? tr("Album") : CHECK1(m_metaInfo->album());
109 break;
110 case 8:
111 return (!index.column()) ? tr("Genre") : CHECK1(m_metaInfo->genre());
112 break;
113 case 9:
114 return (!index.column()) ? tr("Year") : CHECK2(m_metaInfo->year());
115 break;
116 case 10:
117 return (!index.column()) ? tr("Position") : ((m_metaInfo->position() == UINT_MAX) ? tr("Generate from list position") : CHECK2(m_metaInfo->position()));
118 break;
119 case 11:
120 return (!index.column()) ? tr("Comment") : CHECK1(m_metaInfo->comment());
121 break;
122 default:
123 return QVariant();
124 break;
127 else if(role == Qt::DecorationRole && index.column() == 0)
129 switch(index.row() + m_offset)
131 case 0:
132 return QIcon(":/icons/folder_page.png");
133 break;
134 case 1:
135 return QIcon(":/icons/sound.png");
136 break;
137 case 2:
138 return QIcon(":/icons/package.png");
139 break;
140 case 3:
141 return QIcon(":/icons/compress.png");
142 break;
143 case 4:
144 return QIcon(":/icons/clock_play.png");
145 break;
146 case 5:
147 return QIcon(":/icons/music.png");
148 break;
149 case 6:
150 return QIcon(":/icons/user.png");
151 break;
152 case 7:
153 return QIcon(":/icons/cd.png");
154 break;
155 case 8:
156 return QIcon(":/icons/star.png");
157 break;
158 case 9:
159 return QIcon(":/icons/date.png");
160 break;
161 case 10:
162 return QIcon(":/icons/timeline_marker.png");
163 break;
164 case 11:
165 return QIcon(":/icons/comment.png");
166 break;
167 default:
168 return QVariant();
169 break;
172 else if(role == Qt::TextColorRole && index.column() == 1)
174 switch(index.row() + m_offset)
176 case 0:
177 return CHECK3(m_fullInfo->filePath());
178 break;
179 case 1:
180 return CHECK3(m_fullInfo->audioBaseInfo());
181 break;
182 case 2:
183 return CHECK3(m_fullInfo->containerInfo());
184 break;
185 case 3:
186 return CHECK3(m_fullInfo->audioCompressInfo());
187 break;
188 case 4:
189 return CHECK4(m_fullInfo->durationInfo());
190 break;
191 case 5:
192 return CHECK3(m_metaInfo->title());
193 break;
194 case 6:
195 return CHECK3(m_metaInfo->artist());
196 break;
197 case 7:
198 return CHECK3(m_metaInfo->album());
199 break;
200 case 8:
201 return CHECK3(m_metaInfo->genre());
202 break;
203 case 9:
204 return CHECK4(m_metaInfo->year());
205 break;
206 case 10:
207 return CHECK4(m_metaInfo->position());
208 break;
209 case 11:
210 return CHECK3(m_metaInfo->comment());
211 break;
212 default:
213 return QVariant();
214 break;
217 else
219 return QVariant();
223 QVariant MetaInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
225 if(role == Qt::DisplayRole)
227 if(orientation == Qt::Horizontal)
229 switch(section)
231 case 0:
232 return QVariant(tr("Property"));
233 break;
234 case 1:
235 return QVariant(tr("Value"));
236 break;
237 default:
238 return QVariant();
239 break;
242 else
244 return QVariant();
247 else
249 return QVariant();
253 bool MetaInfoModel::setData (const QModelIndex &index, const QVariant &value, int role)
255 if((role != Qt::EditRole) || (index.column() != 1) || !value.isValid())
257 return false;
260 switch(index.row() + m_offset)
262 case 0:
263 m_fullInfo->setFilePath(value.toString());
264 break;
265 case 1:
266 case 2:
267 case 3:
268 return false;
269 break;
270 case 4:
271 m_fullInfo->techInfo().setDuration(value.toUInt());
272 break;
273 case 5:
274 m_metaInfo->setTitle(value.toString());
275 break;
276 case 6:
277 m_metaInfo->setArtist(value.toString());
278 break;
279 case 7:
280 m_metaInfo->setAlbum(value.toString());
281 break;
282 case 8:
283 m_metaInfo->setGenre(value.toString());
284 break;
285 case 9:
286 m_metaInfo->setYear(value.toUInt());
287 break;
288 case 10:
289 m_metaInfo->setPosition(value.toUInt());
290 break;
291 case 11:
292 m_metaInfo->setComment(value.toString());
293 break;
294 default:
295 return false;
296 break;
299 emit dataChanged(index, index);
300 return true;
303 void MetaInfoModel::editItem(const QModelIndex &index, QWidget *parent)
305 bool ok = false;
306 int val = -1;
307 QStringList generes(QString("(%1)").arg(tr("Unspecified")));
308 QString temp;
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)
317 case 5:
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();
324 if(temp.isEmpty())
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();
331 beginResetModel();
332 m_metaInfo->setTitle(temp.isEmpty() ? QString() : temp);
333 endResetModel();
335 break;
336 case 6:
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();
343 beginResetModel();
344 m_metaInfo->setArtist(temp.isEmpty() ? QString() : temp);
345 endResetModel();
347 break;
348 case 7:
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();
355 beginResetModel();
356 m_metaInfo->setAlbum(temp.isEmpty() ? QString() : temp);
357 endResetModel();
359 break;
360 case 8:
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();
369 beginResetModel();
370 m_metaInfo->setGenre((temp.isEmpty() || !temp.compare(generes.at(0), Qt::CaseInsensitive)) ? QString() : temp);
371 endResetModel();
373 break;
374 case 9:
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));
379 input.setIntStep(1);
380 if(input.exec() != 0)
382 val = input.intValue();
383 beginResetModel();
384 m_metaInfo->setYear(val);
385 endResetModel();
387 break;
388 case 10:
389 if(!m_offset)
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));
395 input.setIntStep(1);
396 if(input.exec() != 0)
398 val = input.intValue();
399 beginResetModel();
400 m_metaInfo->setPosition(val);
401 endResetModel();
404 else
406 QStringList options;
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();
415 beginResetModel();
416 m_metaInfo->setPosition((options.indexOf(temp) == 1) ? UINT_MAX : 0);
417 endResetModel();
420 break;
421 case 11:
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();
428 beginResetModel();
429 m_metaInfo->setComment(temp.isEmpty() ? QString() : temp);
430 endResetModel();
432 break;
433 default:
434 QMessageBox::warning(parent, tr("Not editable"), tr("Sorry, this property of the source file cannot be edited!"));
435 break;
439 void MetaInfoModel::editArtwork(const QString &imagePath)
441 m_metaInfo->setCover(imagePath, false);
444 void MetaInfoModel::clearData(bool clearMetaOnly)
446 beginResetModel();
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();
456 if(m_metaInfo)
458 m_metaInfo->reset();
459 m_metaInfo->setComment(tr("Encoded with LameXP"));
460 m_metaInfo->setPosition(m_offset ? UINT_MAX : 0);
463 if(m_fullInfo)
465 QString temp = QFileInfo(m_fullInfo->filePath()).baseName();
466 temp = temp.split("-", QString::SkipEmptyParts).last().trimmed();
467 m_metaInfo->setTitle(temp);
470 endResetModel();
473 Qt::ItemFlags MetaInfoModel::flags(const QModelIndex &index) const
475 return QAbstractTableModel::flags(index);
478 void MetaInfoModel::assignInfoFrom(const AudioFileModel &file)
480 beginResetModel();
481 m_metaInfo->update(file.metaInfo(), true);
482 endResetModel();