OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / Dialog_MetaInfo.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Dialog_MetaInfo.h"
24
25 //Internal
26 #include "Global.h"
27 #include "Model_MetaInfo.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31
32 //Qt
33 #include <QFileInfo>
34 #include <QMessageBox>
35 #include <QTimer>
36 #include <QFileDialog>
37 #include <QMenu>
38
39 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
40
41 ////////////////////////////////////////////////////////////
42 // Constructor & Destructor
43 ////////////////////////////////////////////////////////////
44
45 MetaInfoDialog::MetaInfoDialog(QWidget *parent)
46 :
47         QDialog(parent)
48 {
49         //Init the dialog, from the .ui file
50         setupUi(this);
51         
52         //Hide artwork
53         frameArtwork->hide();
54
55         //Fix size
56         setMinimumSize(this->size());
57         setMaximumHeight(this->height());
58
59         //Setup table view
60         tableView->verticalHeader()->setVisible(false);
61         tableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
62         tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
63         tableView->horizontalHeader()->setStretchLastSection(true);
64
65         //Enable up/down button
66         connect(upButton, SIGNAL(clicked()), this, SLOT(upButtonClicked()));
67         connect(downButton, SIGNAL(clicked()), this, SLOT(downButtonClicked()));
68         connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
69
70         //Create context menu
71         m_contextMenuInfo = new QMenu();
72         m_contextMenuArtwork = new QMenu();
73         QAction *editMetaInfoAction = m_contextMenuInfo->addAction(QIcon(":/icons/table_edit.png"), tr("Edit this Information"));
74         QAction *copyMetaInfoAction = m_contextMenuInfo->addAction(QIcon(":/icons/page_white_copy.png"), tr("Copy everything to Meta Info tab"));
75         QAction *clearMetaInfoAction = m_contextMenuInfo->addAction(QIcon(":/icons/bin.png"), tr("Clear all Meta Info"));
76         QAction *loadArtworkAction = m_contextMenuArtwork->addAction(QIcon(":/icons/folder_image.png"), tr("Load Artwork From File"));
77         QAction *clearArtworkAction = m_contextMenuArtwork->addAction(QIcon(":/icons/bin.png"), tr("Clear Artwork"));
78         SET_FONT_BOLD(editMetaInfoAction, true);
79         SET_FONT_BOLD(loadArtworkAction, true);
80         connect(tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(infoContextMenuRequested(QPoint)));
81         connect(labelArtwork, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(artworkContextMenuRequested(QPoint)));
82         connect(frameArtwork, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(artworkContextMenuRequested(QPoint)));
83         connect(editMetaInfoAction, SIGNAL(triggered(bool)), this, SLOT(editButtonClicked()));
84         connect(copyMetaInfoAction, SIGNAL(triggered(bool)), this, SLOT(copyMetaInfoActionTriggered()));
85         connect(clearMetaInfoAction, SIGNAL(triggered(bool)), this, SLOT(clearMetaInfoActionTriggered()));
86         connect(loadArtworkAction, SIGNAL(triggered(bool)), this, SLOT(editButtonClicked()));
87         connect(clearArtworkAction, SIGNAL(triggered(bool)), this, SLOT(clearArtworkActionTriggered()));
88
89         //Install event filter
90         labelArtwork->installEventFilter(this);
91
92         //Translate
93         labelHeaderText->setText(QString("<b>%1</b><br>%2").arg(tr("Meta Information"), tr("The following meta information have been extracted from the original file.")));
94 }
95
96 MetaInfoDialog::~MetaInfoDialog(void)
97 {
98         MUTILS_DELETE(m_contextMenuInfo);
99         MUTILS_DELETE(m_contextMenuArtwork);
100 }
101
102 ////////////////////////////////////////////////////////////
103 // Slots
104 ////////////////////////////////////////////////////////////
105
106 int MetaInfoDialog::exec(AudioFileModel &audioFile, bool allowUp, bool allowDown)
107 {
108         MetaInfoModel *model = new MetaInfoModel(&audioFile);
109         tableView->setModel(model);
110         tableView->show();
111         frameArtwork->hide();
112         setWindowTitle(tr("Meta Information: %1").arg(QFileInfo(audioFile.filePath()).fileName()));
113         editButton->setEnabled(true);
114         upButton->setEnabled(allowUp);
115         downButton->setEnabled(allowDown);
116         buttonArtwork->setChecked(false);
117
118         if(!audioFile.metaInfo().cover().isEmpty())
119         {
120                 QImage artwork;
121                 if(artwork.load(audioFile.metaInfo().cover()))
122                 {
123                         if((artwork.width() > 256) || (artwork.height() > 256))
124                         {
125                                 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
126                         }
127                         labelArtwork->setPixmap(QPixmap::fromImage(artwork));
128                 }
129                 else
130                 {
131                         qWarning("Error: Failed to load cover art!");
132                         labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
133                 }
134         }
135         else
136         {
137                 labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
138         }
139
140         int iResult = QDialog::exec();
141         
142         tableView->setModel(NULL);
143         MUTILS_DELETE(model);
144
145         return iResult;
146 }
147
148 void MetaInfoDialog::upButtonClicked(void)
149 {
150         done(-1);
151 }
152
153 void MetaInfoDialog::downButtonClicked(void)
154 {
155         done(+1);
156 }
157
158 void MetaInfoDialog::editButtonClicked(void)
159 {
160         if(!buttonArtwork->isChecked())
161         {
162                 dynamic_cast<MetaInfoModel*>(tableView->model())->editItem(tableView->currentIndex(), this);
163                 return;
164         }
165
166         QString fileName = QFileDialog::getOpenFileName(this, tr("Load Artwork"), QString(), QString::fromLatin1("JPEG (*.jpg);;PNG (*.png);;GIF (*.gif)"));
167         if(!fileName.isEmpty())
168         {
169                 QImage artwork;
170                 if(artwork.load(fileName))
171                 {
172                         if((artwork.width() > 256) || (artwork.height() > 256))
173                         {
174                                 artwork = artwork.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
175                         }
176                         dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(fileName);
177                         labelArtwork->setPixmap(QPixmap::fromImage(artwork));
178                 }
179                 else
180                 {
181                         qWarning("Error: Failed to load cover art!");
182                         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)));
183                 }
184         }
185 }
186
187 void MetaInfoDialog::infoContextMenuRequested(const QPoint &pos)
188 {
189         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
190         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
191
192         if(sender)
193         {
194                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
195                 {
196                         m_contextMenuInfo->popup(sender->mapToGlobal(pos));
197                 }
198         }
199 }
200
201 void MetaInfoDialog::artworkContextMenuRequested(const QPoint &pos)
202 {
203         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
204         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
205
206         if(sender)
207         {
208                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
209                 {
210                         m_contextMenuArtwork->popup(sender->mapToGlobal(pos));
211                 }
212         }
213 }
214
215 void MetaInfoDialog::copyMetaInfoActionTriggered(void)
216 {
217         done(INT_MAX);
218 }
219
220 void MetaInfoDialog::clearMetaInfoActionTriggered(void)
221 {
222         if(MetaInfoModel *model = dynamic_cast<MetaInfoModel*>(tableView->model()))
223         {
224                 model->clearData(true);
225         }
226 }
227
228 void MetaInfoDialog::clearArtworkActionTriggered(void)
229 {
230         labelArtwork->setPixmap(QPixmap::fromImage(QImage(":/images/CD.png")));
231         dynamic_cast<MetaInfoModel*>(tableView->model())->editArtwork(QString());
232 }
233
234 bool MetaInfoDialog::eventFilter(QObject *obj, QEvent *event)
235 {
236         if((obj == labelArtwork) && (event->type() == QEvent::MouseButtonDblClick))
237         {
238                 editButtonClicked();
239                 return true;
240         }
241
242         return QDialog::eventFilter(obj, event);
243 }