2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2013-02-08 23:50:51 +01:00
// Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
2010-11-06 23:04:47 +01:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
2013-10-23 20:56:57 +02:00
// (at your option) any later version, but always including the *additional*
// restrictions defined in the "License.txt" file.
2010-11-06 23:04:47 +01:00
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// http://www.gnu.org/licenses/gpl-2.0.txt
///////////////////////////////////////////////////////////////////////////////
# include "Dialog_MetaInfo.h"
# include "Global.h"
# include "Model_MetaInfo.h"
# include <QFileInfo>
# include <QMessageBox>
# include <QTimer>
2011-03-22 17:33:03 +01:00
# include <QFileDialog>
# include <QMenu>
# define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
////////////////////////////////////////////////////////////
// Constructor & Destructor
////////////////////////////////////////////////////////////
2010-11-06 23:04:47 +01:00
MetaInfoDialog : : MetaInfoDialog ( QWidget * parent )
2011-03-22 17:33:03 +01:00
:
QDialog ( parent )
2010-11-06 23:04:47 +01:00
{
//Init the dialog, from the .ui file
setupUi ( this ) ;
2011-03-21 02:16:18 +01:00
//Hide artwork
frameArtwork - > hide ( ) ;
2010-11-06 23:04:47 +01:00
//Fix size
setMinimumSize ( this - > size ( ) ) ;
setMaximumHeight ( this - > height ( ) ) ;
//Setup table view
tableView - > verticalHeader ( ) - > setVisible ( false ) ;
tableView - > verticalHeader ( ) - > setResizeMode ( QHeaderView : : ResizeToContents ) ;
tableView - > horizontalHeader ( ) - > setResizeMode ( QHeaderView : : ResizeToContents ) ;
tableView - > horizontalHeader ( ) - > setStretchLastSection ( true ) ;
//Enable up/down button
connect ( upButton , SIGNAL ( clicked ( ) ) , this , SLOT ( upButtonClicked ( ) ) ) ;
connect ( downButton , SIGNAL ( clicked ( ) ) , this , SLOT ( downButtonClicked ( ) ) ) ;
connect ( editButton , SIGNAL ( clicked ( ) ) , this , SLOT ( editButtonClicked ( ) ) ) ;
2011-01-01 19:28:19 +01:00
2011-03-22 17:33:03 +01:00
//Create context menu
2011-05-31 19:04:45 +02:00
m_contextMenuInfo = new QMenu ( ) ;
m_contextMenuArtwork = new QMenu ( ) ;
QAction * editMetaInfoAction = m_contextMenuInfo - > addAction ( QIcon ( " :/icons/table_edit.png " ) , tr ( " Edit this Information " ) ) ;
QAction * copyMetaInfoAction = m_contextMenuInfo - > addAction ( QIcon ( " :/icons/page_white_copy.png " ) , tr ( " Copy everything to Meta Info tab " ) ) ;
QAction * clearMetaInfoAction = m_contextMenuInfo - > addAction ( QIcon ( " :/icons/bin.png " ) , tr ( " Clear all Meta Info " ) ) ;
QAction * loadArtworkAction = m_contextMenuArtwork - > addAction ( QIcon ( " :/icons/folder_image.png " ) , tr ( " Load Artwork From File " ) ) ;
QAction * clearArtworkAction = m_contextMenuArtwork - > addAction ( QIcon ( " :/icons/bin.png " ) , tr ( " Clear Artwork " ) ) ;
SET_FONT_BOLD ( editMetaInfoAction , true ) ;
2011-03-22 17:33:03 +01:00
SET_FONT_BOLD ( loadArtworkAction , true ) ;
2011-05-31 19:04:45 +02:00
connect ( tableView , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( infoContextMenuRequested ( QPoint ) ) ) ;
connect ( labelArtwork , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( artworkContextMenuRequested ( QPoint ) ) ) ;
connect ( frameArtwork , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( artworkContextMenuRequested ( QPoint ) ) ) ;
connect ( editMetaInfoAction , SIGNAL ( triggered ( bool ) ) , this , SLOT ( editButtonClicked ( ) ) ) ;
connect ( copyMetaInfoAction , SIGNAL ( triggered ( bool ) ) , this , SLOT ( copyMetaInfoActionTriggered ( ) ) ) ;
connect ( clearMetaInfoAction , SIGNAL ( triggered ( bool ) ) , this , SLOT ( clearMetaInfoActionTriggered ( ) ) ) ;
2011-03-22 17:33:03 +01:00
connect ( loadArtworkAction , SIGNAL ( triggered ( bool ) ) , this , SLOT ( editButtonClicked ( ) ) ) ;
connect ( clearArtworkAction , SIGNAL ( triggered ( bool ) ) , this , SLOT ( clearArtworkActionTriggered ( ) ) ) ;
2011-12-18 15:19:28 +01:00
//Install event filter
labelArtwork - > installEventFilter ( this ) ;
2011-01-01 19:28:19 +01:00
//Translate
labelHeaderText - > setText ( QString ( " <b>%1</b><br>%2 " ) . arg ( tr ( " Meta Information " ) , tr ( " The following meta information have been extracted from the original file. " ) ) ) ;
2010-11-06 23:04:47 +01:00
}
MetaInfoDialog : : ~ MetaInfoDialog ( void )
{
2011-05-31 19:04:45 +02:00
LAMEXP_DELETE ( m_contextMenuInfo ) ;
LAMEXP_DELETE ( m_contextMenuArtwork ) ;
2010-11-06 23:04:47 +01:00
}
2011-03-22 17:33:03 +01:00
////////////////////////////////////////////////////////////
// Slots
////////////////////////////////////////////////////////////
2010-11-06 23:04:47 +01:00
int MetaInfoDialog : : exec ( AudioFileModel & audioFile , bool allowUp , bool allowDown )
{
MetaInfoModel * model = new MetaInfoModel ( & audioFile ) ;
tableView - > setModel ( model ) ;
2011-03-21 02:16:18 +01:00
tableView - > show ( ) ;
frameArtwork - > hide ( ) ;
2011-12-15 14:28:03 +01:00
setWindowTitle ( tr ( " Meta Information: %1 " ) . arg ( QFileInfo ( audioFile . filePath ( ) ) . fileName ( ) ) ) ;
2011-03-21 02:16:18 +01:00
editButton - > setEnabled ( true ) ;
2010-11-06 23:04:47 +01:00
upButton - > setEnabled ( allowUp ) ;
downButton - > setEnabled ( allowDown ) ;
2011-03-21 02:16:18 +01:00
buttonArtwork - > setChecked ( false ) ;
2013-10-12 22:55:41 +02:00
if ( ! audioFile . metaInfo ( ) . cover ( ) . isEmpty ( ) )
2011-03-21 02:16:18 +01:00
{
QImage artwork ;
2013-10-12 22:55:41 +02:00
if ( artwork . load ( audioFile . metaInfo ( ) . cover ( ) ) )
2011-03-21 02:16:18 +01:00
{
2011-03-22 17:33:03 +01:00
if ( ( artwork . width ( ) > 256 ) | | ( artwork . height ( ) > 256 ) )
2011-03-21 02:16:18 +01:00
{
2011-03-22 17:33:03 +01:00
artwork = artwork . scaled ( 256 , 256 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ;
2011-03-21 02:16:18 +01:00
}
labelArtwork - > setPixmap ( QPixmap : : fromImage ( artwork ) ) ;
}
else
{
qWarning ( " Error: Failed to load cover art! " ) ;
2011-03-22 17:33:03 +01:00
labelArtwork - > setPixmap ( QPixmap : : fromImage ( QImage ( " :/images/CD.png " ) ) ) ;
2011-03-21 02:16:18 +01:00
}
}
2011-03-22 17:33:03 +01:00
else
{
labelArtwork - > setPixmap ( QPixmap : : fromImage ( QImage ( " :/images/CD.png " ) ) ) ;
}
2010-11-06 23:04:47 +01:00
int iResult = QDialog : : exec ( ) ;
tableView - > setModel ( NULL ) ;
LAMEXP_DELETE ( model ) ;
return iResult ;
}
void MetaInfoDialog : : upButtonClicked ( void )
{
done ( - 1 ) ;
}
void MetaInfoDialog : : downButtonClicked ( void )
{
done ( + 1 ) ;
}
void MetaInfoDialog : : editButtonClicked ( void )
{
2011-03-22 17:33:03 +01:00
if ( ! buttonArtwork - > isChecked ( ) )
{
dynamic_cast < MetaInfoModel * > ( tableView - > model ( ) ) - > editItem ( tableView - > currentIndex ( ) , this ) ;
return ;
}
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Load Artwork " ) , QString ( ) , QString : : fromLatin1 ( " JPEG (*.jpg);;PNG (*.png);;GIF (*.gif) " ) ) ;
if ( ! fileName . isEmpty ( ) )
{
QImage artwork ;
if ( artwork . load ( fileName ) )
{
if ( ( artwork . width ( ) > 256 ) | | ( artwork . height ( ) > 256 ) )
{
artwork = artwork . scaled ( 256 , 256 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ;
}
dynamic_cast < MetaInfoModel * > ( tableView - > model ( ) ) - > editArtwork ( fileName ) ;
labelArtwork - > setPixmap ( QPixmap : : fromImage ( artwork ) ) ;
}
else
{
qWarning ( " Error: Failed to load cover art! " ) ;
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 ) ) ) ;
}
}
}
2011-05-31 19:04:45 +02:00
void MetaInfoDialog : : infoContextMenuRequested ( const QPoint & pos )
{
QAbstractScrollArea * scrollArea = dynamic_cast < QAbstractScrollArea * > ( QObject : : sender ( ) ) ;
QWidget * sender = scrollArea ? scrollArea - > viewport ( ) : dynamic_cast < QWidget * > ( QObject : : sender ( ) ) ;
2011-03-22 17:33:03 +01:00
2011-05-31 19:04:45 +02:00
if ( sender )
{
if ( pos . x ( ) < = sender - > width ( ) & & pos . y ( ) < = sender - > height ( ) & & pos . x ( ) > = 0 & & pos . y ( ) > = 0 )
{
m_contextMenuInfo - > popup ( sender - > mapToGlobal ( pos ) ) ;
}
}
}
void MetaInfoDialog : : artworkContextMenuRequested ( const QPoint & pos )
2011-03-22 17:33:03 +01:00
{
QAbstractScrollArea * scrollArea = dynamic_cast < QAbstractScrollArea * > ( QObject : : sender ( ) ) ;
QWidget * sender = scrollArea ? scrollArea - > viewport ( ) : dynamic_cast < QWidget * > ( QObject : : sender ( ) ) ;
if ( sender )
{
if ( pos . x ( ) < = sender - > width ( ) & & pos . y ( ) < = sender - > height ( ) & & pos . x ( ) > = 0 & & pos . y ( ) > = 0 )
{
2011-05-31 19:04:45 +02:00
m_contextMenuArtwork - > popup ( sender - > mapToGlobal ( pos ) ) ;
2011-03-22 17:33:03 +01:00
}
}
}
2011-05-31 19:04:45 +02:00
void MetaInfoDialog : : copyMetaInfoActionTriggered ( void )
{
done ( INT_MAX ) ;
}
void MetaInfoDialog : : clearMetaInfoActionTriggered ( void )
{
if ( MetaInfoModel * model = dynamic_cast < MetaInfoModel * > ( tableView - > model ( ) ) )
{
model - > clearData ( true ) ;
}
}
2011-03-22 17:33:03 +01:00
void MetaInfoDialog : : clearArtworkActionTriggered ( void )
{
labelArtwork - > setPixmap ( QPixmap : : fromImage ( QImage ( " :/images/CD.png " ) ) ) ;
dynamic_cast < MetaInfoModel * > ( tableView - > model ( ) ) - > editArtwork ( QString ( ) ) ;
2010-11-06 23:04:47 +01:00
}
2011-12-18 15:19:28 +01:00
bool MetaInfoDialog : : eventFilter ( QObject * obj , QEvent * event )
{
if ( ( obj = = labelArtwork ) & & ( event - > type ( ) = = QEvent : : MouseButtonDblClick ) )
{
editButtonClicked ( ) ;
return true ;
}
return QDialog : : eventFilter ( obj , event ) ;
}