Fixed a typo, thanks to Rub3n CT for reporting.
[LameXP.git] / src / Dialog_MetaInfo.cpp
blobed05a38aee1c7bc74d88908e53b37e65f09254c3
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 "Dialog_MetaInfo.h"
24 #include "Global.h"
25 #include "Model_MetaInfo.h"
27 #include <QFileInfo>
28 #include <QMessageBox>
29 #include <QTimer>
30 #include <QFileDialog>
31 #include <QMenu>
33 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
35 ////////////////////////////////////////////////////////////
36 // Constructor & Destructor
37 ////////////////////////////////////////////////////////////
39 MetaInfoDialog::MetaInfoDialog(QWidget *parent)
41 QDialog(parent)
43 //Init the dialog, from the .ui file
44 setupUi(this);
46 //Hide artwork
47 frameArtwork->hide();
49 //Fix size
50 setMinimumSize(this->size());
51 setMaximumHeight(this->height());
53 //Setup table view
54 tableView->verticalHeader()->setVisible(false);
55 tableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
56 tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
57 tableView->horizontalHeader()->setStretchLastSection(true);
59 //Enable up/down button
60 connect(upButton, SIGNAL(clicked()), this, SLOT(upButtonClicked()));
61 connect(downButton, SIGNAL(clicked()), this, SLOT(downButtonClicked()));
62 connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
64 //Create context menu
65 m_contextMenuInfo = new QMenu();
66 m_contextMenuArtwork = new QMenu();
67 QAction *editMetaInfoAction = m_contextMenuInfo->addAction(QIcon(":/icons/table_edit.png"), tr("Edit this Information"));
68 QAction *copyMetaInfoAction = m_contextMenuInfo->addAction(QIcon(":/icons/page_white_copy.png"), tr("Copy everything to Meta Info tab"));
69 QAction *clearMetaInfoAction = m_contextMenuInfo->addAction(QIcon(":/icons/bin.png"), tr("Clear all Meta Info"));
70 QAction *loadArtworkAction = m_contextMenuArtwork->addAction(QIcon(":/icons/folder_image.png"), tr("Load Artwork From File"));
71 QAction *clearArtworkAction = m_contextMenuArtwork->addAction(QIcon(":/icons/bin.png"), tr("Clear Artwork"));
72 SET_FONT_BOLD(editMetaInfoAction, true);
73 SET_FONT_BOLD(loadArtworkAction, true);
74 connect(tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(infoContextMenuRequested(QPoint)));
75 connect(labelArtwork, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(artworkContextMenuRequested(QPoint)));
76 connect(frameArtwork, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(artworkContextMenuRequested(QPoint)));
77 connect(editMetaInfoAction, SIGNAL(triggered(bool)), this, SLOT(editButtonClicked()));
78 connect(copyMetaInfoAction, SIGNAL(triggered(bool)), this, SLOT(copyMetaInfoActionTriggered()));
79 connect(clearMetaInfoAction, SIGNAL(triggered(bool)), this, SLOT(clearMetaInfoActionTriggered()));
80 connect(loadArtworkAction, SIGNAL(triggered(bool)), this, SLOT(editButtonClicked()));
81 connect(clearArtworkAction, SIGNAL(triggered(bool)), this, SLOT(clearArtworkActionTriggered()));
83 //Translate
84 labelHeaderText->setText(QString("<b>%1</b><br>%2").arg(tr("Meta Information"), tr("The following meta information have been extracted from the original file.")));
87 MetaInfoDialog::~MetaInfoDialog(void)
89 LAMEXP_DELETE(m_contextMenuInfo);
90 LAMEXP_DELETE(m_contextMenuArtwork);
93 ////////////////////////////////////////////////////////////
94 // Slots
95 ////////////////////////////////////////////////////////////
97 int MetaInfoDialog::exec(AudioFileModel &audioFile, bool allowUp, bool allowDown)
99 MetaInfoModel *model = new MetaInfoModel(&audioFile);
100 tableView->setModel(model);
101 tableView->show();
102 frameArtwork->hide();
103 setWindowTitle(QString("Meta Information: %1").arg(QFileInfo(audioFile.filePath()).fileName()));
104 editButton->setEnabled(true);
105 upButton->setEnabled(allowUp);
106 downButton->setEnabled(allowDown);
107 buttonArtwork->setChecked(false);
109 if(!audioFile.fileCover().isEmpty())
111 QImage artwork;
112 if(artwork.load(audioFile.fileCover()))
114 if((artwork.width() > 256) || (artwork.height() > 256))
116 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
118 labelArtwork->setPixmap(QPixmap::fromImage(artwork));
120 else
122 qWarning("Error: Failed to load cover art!");
123 labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
126 else
128 labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
131 int iResult = QDialog::exec();
133 tableView->setModel(NULL);
134 LAMEXP_DELETE(model);
136 return iResult;
139 void MetaInfoDialog::upButtonClicked(void)
141 done(-1);
144 void MetaInfoDialog::downButtonClicked(void)
146 done(+1);
149 void MetaInfoDialog::editButtonClicked(void)
151 if(!buttonArtwork->isChecked())
153 dynamic_cast<MetaInfoModel*>(tableView->model())->editItem(tableView->currentIndex(), this);
154 return;
157 QString fileName = QFileDialog::getOpenFileName(this, tr("Load Artwork"), QString(), QString::fromLatin1("JPEG (*.jpg);;PNG (*.png);;GIF (*.gif)"));
158 if(!fileName.isEmpty())
160 QImage artwork;
161 if(artwork.load(fileName))
163 if((artwork.width() > 256) || (artwork.height() > 256))
165 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
167 dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(fileName);
168 labelArtwork->setPixmap(QPixmap::fromImage(artwork));
170 else
172 qWarning("Error: Failed to load cover art!");
173 QMessageBox::warning(this, tr("Artwork Error"), QString("<nobr>%1</nobr><br><br><nobr>%2</nobr>").arg(tr("Sorry, failed to load artwork from selected file!"), QDir::toNativeSeparators(fileName)));
178 void MetaInfoDialog::infoContextMenuRequested(const QPoint &pos)
180 QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
181 QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
183 if(sender)
185 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
187 m_contextMenuInfo->popup(sender->mapToGlobal(pos));
192 void MetaInfoDialog::artworkContextMenuRequested(const QPoint &pos)
194 QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
195 QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
197 if(sender)
199 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
201 m_contextMenuArtwork->popup(sender->mapToGlobal(pos));
206 void MetaInfoDialog::copyMetaInfoActionTriggered(void)
208 done(INT_MAX);
211 void MetaInfoDialog::clearMetaInfoActionTriggered(void)
213 if(MetaInfoModel *model = dynamic_cast<MetaInfoModel*>(tableView->model()))
215 model->clearData(true);
219 void MetaInfoDialog::clearArtworkActionTriggered(void)
221 labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
222 dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(QString());