OSDN Git Service

Slightly tweaked how the progress bar is updated.
[lamexp/LameXP.git] / src / Dialog_MetaInfo.cpp
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.
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 "Dialog_MetaInfo.h"
23
24 #include "Global.h"
25 #include "Model_MetaInfo.h"
26
27 #include <QFileInfo>
28 #include <QMessageBox>
29 #include <QTimer>
30 #include <QFileDialog>
31 #include <QMenu>
32
33 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
34
35 ////////////////////////////////////////////////////////////
36 // Constructor & Destructor
37 ////////////////////////////////////////////////////////////
38
39 MetaInfoDialog::MetaInfoDialog(QWidget *parent)
40 :
41         QDialog(parent)
42 {
43         //Init the dialog, from the .ui file
44         setupUi(this);
45         
46         //Hide artwork
47         frameArtwork->hide();
48
49         //Fix size
50         setMinimumSize(this->size());
51         setMaximumHeight(this->height());
52
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);
58
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()));
63
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()));
82
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.")));
85 }
86
87 MetaInfoDialog::~MetaInfoDialog(void)
88 {
89         LAMEXP_DELETE(m_contextMenuInfo);
90         LAMEXP_DELETE(m_contextMenuArtwork);
91 }
92
93 ////////////////////////////////////////////////////////////
94 // Slots
95 ////////////////////////////////////////////////////////////
96
97 int MetaInfoDialog::exec(AudioFileModel &audioFile, bool allowUp, bool allowDown)
98 {
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);
108
109         if(!audioFile.fileCover().isEmpty())
110         {
111                 QImage artwork;
112                 if(artwork.load(audioFile.fileCover()))
113                 {
114                         if((artwork.width() > 256) || (artwork.height() > 256))
115                         {
116                                 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
117                         }
118                         labelArtwork->setPixmap(QPixmap::fromImage(artwork));
119                 }
120                 else
121                 {
122                         qWarning("Error: Failed to load cover art!");
123                         labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
124                 }
125         }
126         else
127         {
128                 labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
129         }
130
131         int iResult = QDialog::exec();
132         
133         tableView->setModel(NULL);
134         LAMEXP_DELETE(model);
135
136         return iResult;
137 }
138
139 void MetaInfoDialog::upButtonClicked(void)
140 {
141         done(-1);
142 }
143
144 void MetaInfoDialog::downButtonClicked(void)
145 {
146         done(+1);
147 }
148
149 void MetaInfoDialog::editButtonClicked(void)
150 {
151         if(!buttonArtwork->isChecked())
152         {
153                 dynamic_cast<MetaInfoModel*>(tableView->model())->editItem(tableView->currentIndex(), this);
154                 return;
155         }
156
157         QString fileName = QFileDialog::getOpenFileName(this, tr("Load Artwork"), QString(), QString::fromLatin1("JPEG (*.jpg);;PNG (*.png);;GIF (*.gif)"));
158         if(!fileName.isEmpty())
159         {
160                 QImage artwork;
161                 if(artwork.load(fileName))
162                 {
163                         if((artwork.width() > 256) || (artwork.height() > 256))
164                         {
165                                 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
166                         }
167                         dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(fileName);
168                         labelArtwork->setPixmap(QPixmap::fromImage(artwork));
169                 }
170                 else
171                 {
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)));
174                 }
175         }
176 }
177
178 void MetaInfoDialog::infoContextMenuRequested(const QPoint &pos)
179 {
180         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
181         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
182
183         if(sender)
184         {
185                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
186                 {
187                         m_contextMenuInfo->popup(sender->mapToGlobal(pos));
188                 }
189         }
190 }
191
192 void MetaInfoDialog::artworkContextMenuRequested(const QPoint &pos)
193 {
194         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
195         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
196
197         if(sender)
198         {
199                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
200                 {
201                         m_contextMenuArtwork->popup(sender->mapToGlobal(pos));
202                 }
203         }
204 }
205
206 void MetaInfoDialog::copyMetaInfoActionTriggered(void)
207 {
208         done(INT_MAX);
209 }
210
211 void MetaInfoDialog::clearMetaInfoActionTriggered(void)
212 {
213         if(MetaInfoModel *model = dynamic_cast<MetaInfoModel*>(tableView->model()))
214         {
215                 model->clearData(true);
216         }
217 }
218
219 void MetaInfoDialog::clearArtworkActionTriggered(void)
220 {
221         labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
222         dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(QString());
223 }