OSDN Git Service

67ebd4d1aaa9576d934e13224af72b53db7fb323
[lamexp/LameXP.git] / src / Dialog_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 "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         //Install event filter
84         labelArtwork->installEventFilter(this);
85
86         //Translate
87         labelHeaderText->setText(QString("<b>%1</b><br>%2").arg(tr("Meta Information"), tr("The following meta information have been extracted from the original file.")));
88 }
89
90 MetaInfoDialog::~MetaInfoDialog(void)
91 {
92         LAMEXP_DELETE(m_contextMenuInfo);
93         LAMEXP_DELETE(m_contextMenuArtwork);
94 }
95
96 ////////////////////////////////////////////////////////////
97 // Slots
98 ////////////////////////////////////////////////////////////
99
100 int MetaInfoDialog::exec(AudioFileModel &audioFile, bool allowUp, bool allowDown)
101 {
102         MetaInfoModel *model = new MetaInfoModel(&audioFile);
103         tableView->setModel(model);
104         tableView->show();
105         frameArtwork->hide();
106         setWindowTitle(tr("Meta Information: %1").arg(QFileInfo(audioFile.filePath()).fileName()));
107         editButton->setEnabled(true);
108         upButton->setEnabled(allowUp);
109         downButton->setEnabled(allowDown);
110         buttonArtwork->setChecked(false);
111
112         if(!audioFile.fileCover().isEmpty())
113         {
114                 QImage artwork;
115                 if(artwork.load(audioFile.fileCover()))
116                 {
117                         if((artwork.width() > 256) || (artwork.height() > 256))
118                         {
119                                 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
120                         }
121                         labelArtwork->setPixmap(QPixmap::fromImage(artwork));
122                 }
123                 else
124                 {
125                         qWarning("Error: Failed to load cover art!");
126                         labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
127                 }
128         }
129         else
130         {
131                 labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
132         }
133
134         int iResult = QDialog::exec();
135         
136         tableView->setModel(NULL);
137         LAMEXP_DELETE(model);
138
139         return iResult;
140 }
141
142 void MetaInfoDialog::upButtonClicked(void)
143 {
144         done(-1);
145 }
146
147 void MetaInfoDialog::downButtonClicked(void)
148 {
149         done(+1);
150 }
151
152 void MetaInfoDialog::editButtonClicked(void)
153 {
154         if(!buttonArtwork->isChecked())
155         {
156                 dynamic_cast<MetaInfoModel*>(tableView->model())->editItem(tableView->currentIndex(), this);
157                 return;
158         }
159
160         QString fileName = QFileDialog::getOpenFileName(this, tr("Load Artwork"), QString(), QString::fromLatin1("JPEG (*.jpg);;PNG (*.png);;GIF (*.gif)"));
161         if(!fileName.isEmpty())
162         {
163                 QImage artwork;
164                 if(artwork.load(fileName))
165                 {
166                         if((artwork.width() > 256) || (artwork.height() > 256))
167                         {
168                                 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
169                         }
170                         dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(fileName);
171                         labelArtwork->setPixmap(QPixmap::fromImage(artwork));
172                 }
173                 else
174                 {
175                         qWarning("Error: Failed to load cover art!");
176                         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)));
177                 }
178         }
179 }
180
181 void MetaInfoDialog::infoContextMenuRequested(const QPoint &pos)
182 {
183         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
184         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
185
186         if(sender)
187         {
188                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
189                 {
190                         m_contextMenuInfo->popup(sender->mapToGlobal(pos));
191                 }
192         }
193 }
194
195 void MetaInfoDialog::artworkContextMenuRequested(const QPoint &pos)
196 {
197         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
198         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
199
200         if(sender)
201         {
202                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
203                 {
204                         m_contextMenuArtwork->popup(sender->mapToGlobal(pos));
205                 }
206         }
207 }
208
209 void MetaInfoDialog::copyMetaInfoActionTriggered(void)
210 {
211         done(INT_MAX);
212 }
213
214 void MetaInfoDialog::clearMetaInfoActionTriggered(void)
215 {
216         if(MetaInfoModel *model = dynamic_cast<MetaInfoModel*>(tableView->model()))
217         {
218                 model->clearData(true);
219         }
220 }
221
222 void MetaInfoDialog::clearArtworkActionTriggered(void)
223 {
224         labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
225         dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(QString());
226 }
227
228 bool MetaInfoDialog::eventFilter(QObject *obj, QEvent *event)
229 {
230         if((obj == labelArtwork) && (event->type() == QEvent::MouseButtonDblClick))
231         {
232                 editButtonClicked();
233                 return true;
234         }
235
236         return QDialog::eventFilter(obj, event);
237 }