2010-12-22 01:01:01 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2018-01-17 21:18:10 +01:00
// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
2010-12-22 01:01:01 +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-12-22 01:01:01 +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_DropBox.h"
2014-11-24 18:28:53 +01:00
# include "UIC_DropBox.h"
2012-12-11 22:51:18 +01:00
2014-11-25 02:14:42 +01:00
//Internal
2010-12-22 01:01:01 +01:00
# include "Global.h"
# include "Model_Settings.h"
2014-11-25 02:14:42 +01:00
//MUtils
# include <MUtils/Global.h>
//Qt
2010-12-22 01:01:01 +01:00
# include <QThread>
# include <QMovie>
# include <QKeyEvent>
# include <QDesktopWidget>
2010-12-22 22:59:00 +01:00
# include <QToolTip>
# include <QTimer>
2010-12-22 01:01:01 +01:00
# define EPS (1.0E-5)
2012-12-11 22:51:18 +01:00
# define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = (WIDGET)->font(); _font.setBold(BOLD); (WIDGET)->setFont(_font); }
2010-12-22 01:01:01 +01:00
2013-11-01 00:08:40 +01:00
static QRect SCREEN_GEOMETRY ( void )
{
QDesktopWidget * desktop = QApplication : : desktop ( ) ;
return ( desktop - > isVirtualDesktop ( ) ? desktop - > screen ( ) - > geometry ( ) : desktop - > availableGeometry ( ) ) ;
}
static const double LOW_OPACITY = 0.85 ;
2010-12-22 01:01:01 +01:00
////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////
DropBox : : DropBox ( QWidget * parent , QAbstractItemModel * model , SettingsModel * settings )
:
2013-11-01 00:08:40 +01:00
QDialog ( parent , Qt : : FramelessWindowHint | Qt : : WindowStaysOnTopHint ) ,
2012-12-11 22:51:18 +01:00
ui ( new Ui : : DropBox ) ,
2010-12-22 01:01:01 +01:00
m_model ( model ) ,
m_settings ( settings ) ,
2010-12-22 22:59:00 +01:00
m_moving ( false ) ,
2013-11-01 00:08:40 +01:00
m_screenGeometry ( SCREEN_GEOMETRY ( ) ) ,
2010-12-22 01:01:01 +01:00
m_firstShow ( true )
{
//Init the dialog, from the .ui file
2012-12-11 22:51:18 +01:00
ui - > setupUi ( this ) ;
2010-12-22 01:01:01 +01:00
//Init counter
2012-12-11 22:51:18 +01:00
m_counterLabel = new QLabel ( this ) ;
m_counterLabel - > setParent ( ui - > dropBoxLabel ) ;
m_counterLabel - > setText ( " 0 " ) ;
m_counterLabel - > setAlignment ( Qt : : AlignHCenter | Qt : : AlignTop ) ;
2010-12-22 01:01:01 +01:00
SET_FONT_BOLD ( m_counterLabel , true ) ;
2012-12-11 22:51:18 +01:00
m_windowReferencePoint = new QPoint ;
m_mouseReferencePoint = new QPoint ;
2010-12-22 01:01:01 +01:00
//Prevent close
m_canClose = false ;
//Make transparent
2013-11-01 00:08:40 +01:00
setAttribute ( Qt : : WA_TranslucentBackground ) ;
setWindowOpacity ( LOW_OPACITY ) ;
2010-12-30 17:34:19 +01:00
//Translate UI
2011-01-01 19:28:19 +01:00
QEvent languageChangeEvent ( QEvent : : LanguageChange ) ;
changeEvent ( & languageChangeEvent ) ;
2010-12-22 01:01:01 +01:00
}
////////////////////////////////////////////////////////////
// Destructor
////////////////////////////////////////////////////////////
DropBox : : ~ DropBox ( void )
{
2013-11-02 01:07:23 +01:00
if ( ! m_firstShow )
{
m_settings - > dropBoxWidgetPositionX ( this - > x ( ) ) ;
m_settings - > dropBoxWidgetPositionY ( this - > y ( ) ) ;
}
2014-11-25 02:14:42 +01:00
MUTILS_DELETE ( m_counterLabel ) ;
MUTILS_DELETE ( m_windowReferencePoint ) ;
MUTILS_DELETE ( m_mouseReferencePoint ) ;
MUTILS_DELETE ( ui ) ;
2010-12-22 01:01:01 +01:00
}
////////////////////////////////////////////////////////////
// PUBLIC SLOTS
////////////////////////////////////////////////////////////
void DropBox : : modelChanged ( void )
{
if ( m_model )
{
2012-12-11 22:51:18 +01:00
m_counterLabel - > setText ( QString : : number ( m_model - > rowCount ( ) ) ) ;
2010-12-22 01:01:01 +01:00
}
}
////////////////////////////////////////////////////////////
// EVENTS
////////////////////////////////////////////////////////////
2010-12-30 17:34:19 +01:00
/*
* Re - translate the UI
*/
void DropBox : : changeEvent ( QEvent * e )
{
if ( e - > type ( ) = = QEvent : : LanguageChange )
{
2012-12-11 22:51:18 +01:00
ui - > retranslateUi ( this ) ;
ui - > dropBoxLabel - > setToolTip ( QString ( " <b>%1</b><br><nobr>%2</nobr><br><nobr>%3</nobr> " ) . arg ( tr ( " LameXP DropBox " ) , tr ( " You can add files to LameXP via Drag&Drop here! " ) , tr ( " (Right-click to close the DropBox) " ) ) ) ;
2010-12-30 17:34:19 +01:00
}
}
2010-12-22 01:01:01 +01:00
void DropBox : : showEvent ( QShowEvent * event )
{
2013-11-01 00:08:40 +01:00
m_screenGeometry = SCREEN_GEOMETRY ( ) ;
setFixedSize ( ui - > dropBoxLabel - > pixmap ( ) - > size ( ) ) ;
2010-12-22 01:01:01 +01:00
2012-12-11 22:51:18 +01:00
m_counterLabel - > setGeometry ( 0 , ui - > dropBoxLabel - > height ( ) - 30 , ui - > dropBoxLabel - > width ( ) , 25 ) ;
2010-12-22 01:01:01 +01:00
if ( m_firstShow )
{
m_firstShow = false ;
2013-11-02 01:07:23 +01:00
int pos_x = m_settings - > dropBoxWidgetPositionX ( ) ;
int pos_y = m_settings - > dropBoxWidgetPositionY ( ) ;
if ( ( pos_x < 0 ) & & ( pos_y < 0 ) )
{
QWidget * const parentWidget = dynamic_cast < QWidget * > ( this - > parent ( ) ) ;
QWidget * const targetWidget = parentWidget ? parentWidget : this ;
QRect availGeometry = QApplication : : desktop ( ) - > availableGeometry ( targetWidget ) ;
pos_x = availGeometry . width ( ) - frameGeometry ( ) . width ( ) + availGeometry . left ( ) ;
pos_y = availGeometry . height ( ) - frameGeometry ( ) . height ( ) + availGeometry . top ( ) ;
}
else
{
pos_x = qBound ( 0 , pos_x , m_screenGeometry . width ( ) - frameGeometry ( ) . width ( ) ) ;
pos_y = qBound ( 0 , pos_y , m_screenGeometry . height ( ) - frameGeometry ( ) . height ( ) ) ;
}
move ( pos_x , pos_y ) ;
2010-12-22 01:01:01 +01:00
}
2010-12-22 22:59:00 +01:00
2011-05-03 20:17:33 +02:00
if ( m_moving )
{
QApplication : : restoreOverrideCursor ( ) ;
m_moving = false ;
2013-11-01 00:08:40 +01:00
setWindowOpacity ( LOW_OPACITY ) ;
2011-05-03 20:17:33 +02:00
}
2013-11-02 01:07:23 +01:00
boundWidget ( this ) ;
2010-12-22 01:01:01 +01:00
}
void DropBox : : keyPressEvent ( QKeyEvent * event )
{
event - > ignore ( ) ;
}
void DropBox : : keyReleaseEvent ( QKeyEvent * event )
{
event - > ignore ( ) ;
}
void DropBox : : closeEvent ( QCloseEvent * event )
{
2013-11-02 01:07:23 +01:00
if ( ! m_canClose )
{
event - > ignore ( ) ;
}
2010-12-22 01:01:01 +01:00
}
void DropBox : : mousePressEvent ( QMouseEvent * event )
{
2010-12-22 22:59:00 +01:00
if ( m_moving )
{
event - > ignore ( ) ;
return ;
}
2010-12-22 01:01:01 +01:00
if ( event - > button ( ) = = Qt : : RightButton )
{
hide ( ) ;
if ( m_settings ) m_settings - > dropBoxWidgetEnabled ( false ) ;
return ;
}
2013-11-01 00:08:40 +01:00
m_screenGeometry = SCREEN_GEOMETRY ( ) ;
2010-12-22 01:01:01 +01:00
QApplication : : setOverrideCursor ( Qt : : SizeAllCursor ) ;
2012-12-11 22:51:18 +01:00
* m_windowReferencePoint = this - > pos ( ) ;
* m_mouseReferencePoint = event - > globalPos ( ) ;
2010-12-22 22:59:00 +01:00
m_moving = true ;
2013-11-01 00:08:40 +01:00
setWindowOpacity ( 1.0 ) ;
2010-12-22 01:01:01 +01:00
}
void DropBox : : mouseReleaseEvent ( QMouseEvent * event )
{
2010-12-22 22:59:00 +01:00
if ( m_moving & & event - > button ( ) ! = Qt : : RightButton )
{
2013-11-02 01:07:23 +01:00
boundWidget ( this ) ;
2010-12-22 22:59:00 +01:00
QApplication : : restoreOverrideCursor ( ) ;
2013-11-01 00:08:40 +01:00
setWindowOpacity ( LOW_OPACITY ) ;
2013-11-02 01:07:23 +01:00
m_settings - > dropBoxWidgetPositionX ( this - > x ( ) ) ;
m_settings - > dropBoxWidgetPositionY ( this - > y ( ) ) ;
m_moving = false ;
2010-12-22 22:59:00 +01:00
}
2010-12-22 01:01:01 +01:00
}
void DropBox : : mouseMoveEvent ( QMouseEvent * event )
{
2010-12-22 22:59:00 +01:00
if ( ! m_moving )
{
return ;
}
2013-11-01 00:08:40 +01:00
2012-12-11 22:51:18 +01:00
const int delta_x = m_mouseReferencePoint - > x ( ) - event - > globalX ( ) ;
const int delta_y = m_mouseReferencePoint - > y ( ) - event - > globalY ( ) ;
2010-12-22 01:01:01 +01:00
2013-11-01 00:08:40 +01:00
const int max_x = m_screenGeometry . width ( ) - frameGeometry ( ) . width ( ) + m_screenGeometry . left ( ) ;
const int max_y = m_screenGeometry . height ( ) - frameGeometry ( ) . height ( ) + m_screenGeometry . top ( ) ;
2010-12-22 22:59:00 +01:00
2013-11-01 00:08:40 +01:00
const int new_x = qBound ( m_screenGeometry . left ( ) , m_windowReferencePoint - > x ( ) - delta_x , max_x ) ;
const int new_y = qBound ( m_screenGeometry . top ( ) , m_windowReferencePoint - > y ( ) - delta_y , max_y ) ;
2010-12-22 22:59:00 +01:00
2010-12-22 01:01:01 +01:00
move ( new_x , new_y ) ;
}
2010-12-22 22:59:00 +01:00
void DropBox : : showToolTip ( void )
{
2012-12-11 22:51:18 +01:00
QToolTip : : showText ( ui - > dropBoxLabel - > mapToGlobal ( ui - > dropBoxLabel - > pos ( ) ) , ui - > dropBoxLabel - > toolTip ( ) ) ;
2010-12-22 22:59:00 +01:00
}
2011-05-03 20:17:33 +02:00
bool DropBox : : event ( QEvent * event )
{
switch ( event - > type ( ) )
{
case QEvent : : Leave :
case QEvent : : WindowDeactivate :
if ( m_moving )
{
QApplication : : restoreOverrideCursor ( ) ;
m_moving = false ;
}
break ;
}
return QDialog : : event ( event ) ;
}
2013-11-02 01:07:23 +01:00
////////////////////////////////////////////////////////////
// PRIVATE
////////////////////////////////////////////////////////////
void DropBox : : boundWidget ( QWidget * widget )
{
static const int magnetic = 24 ;
QRect availGeometry = QApplication : : desktop ( ) - > availableGeometry ( widget ) ;
const int max_x = availGeometry . width ( ) - widget - > frameGeometry ( ) . width ( ) + availGeometry . left ( ) ;
const int max_y = availGeometry . height ( ) - widget - > frameGeometry ( ) . height ( ) + availGeometry . top ( ) ;
int new_x = qBound ( availGeometry . left ( ) , widget - > x ( ) , max_x ) ;
int new_y = qBound ( availGeometry . top ( ) , widget - > y ( ) , max_y ) ;
if ( new_x - availGeometry . left ( ) < magnetic ) new_x = availGeometry . left ( ) ;
if ( new_y - availGeometry . top ( ) < magnetic ) new_y = availGeometry . top ( ) ;
if ( max_x - new_x < magnetic ) new_x = max_x ;
if ( max_y - new_y < magnetic ) new_y = max_y ;
if ( ( widget - > x ( ) ! = new_x ) | | ( widget - > y ( ) ! = new_y ) )
{
widget - > move ( new_x , new_y ) ;
}
}