Commit 2145ce73 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[ADD #253] drag and drop support for filename properties

parent 1e98860b
......@@ -25,7 +25,6 @@
#include <cmath>
#include <string>
#include <QtGui/QFileDialog>
#include "core/common/WLogger.h"
......@@ -58,6 +57,9 @@ WPropertyFilenameWidget::WPropertyFilenameWidget( WPropFilename property, QGridL
m_infoLayout.setSpacing( WGLOBAL_SPACING );
m_informationWidgets.setLayout( &m_infoLayout );
// accept drag and drop
setAcceptDrops( true );
// set the initial values
update();
......@@ -112,3 +114,50 @@ void WPropertyFilenameWidget::buttonReleased()
m_button.setText( QString::fromStdString( m_fnProperty->get().filename().string() ) );
}
void WPropertyFilenameWidget::dragEnterEvent( QDragEnterEvent* event )
{
if( event->mimeData()->hasUrls() && ( event->mimeData()->urls().size() == 1 ) )
{
// accept only a single url to a local file
QUrl url = *( event->mimeData()->urls().begin() );
QString path = url.toLocalFile();
QFileInfo info( path );
bool shouldBeDir = ( m_fnProperty->countConstraint( PC_ISDIRECTORY ) != 0 );
bool shouldExist = ( m_fnProperty->countConstraint( PC_PATHEXISTS ) != 0 );
// needs to be a directory?
bool accept = ( shouldBeDir && info.isDir() ) || !shouldBeDir;
accept &= ( shouldExist && info.exists() ) || !shouldExist;
event->setAccepted( accept );
}
}
void WPropertyFilenameWidget::dropEvent( QDropEvent* event )
{
if( event->mimeData()->hasUrls() && ( event->mimeData()->urls().size() == 1 ) )
{
// accept only a single url to a local file
QUrl url = *( event->mimeData()->urls().begin() );
QString path = url.toLocalFile();
QFileInfo info( path );
bool shouldBeDir = ( m_fnProperty->countConstraint( PC_ISDIRECTORY ) != 0 );
bool shouldExist = ( m_fnProperty->countConstraint( PC_PATHEXISTS ) != 0 );
// needs to be a directory?
bool accept = ( shouldBeDir && info.isDir() ) || !shouldBeDir;
accept &= ( shouldExist && info.exists() ) || !shouldExist;
// use new path/filename
// convert it back to a filename property
invalidate( !m_fnProperty->set( boost::filesystem::path( path.toStdString() ) ) ); // NOTE: set automatically checks the validity of the value
// set button
m_button.setText( QString::fromStdString( m_fnProperty->get().filename().string() ) );
}
}
......@@ -29,6 +29,10 @@
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
#include <QtCore/QMimeData>
#include <QtCore/QUrl>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDropEvent>
#include "../guiElements/WScaleLabel.h"
......@@ -86,6 +90,18 @@ protected:
*/
QHBoxLayout m_infoLayout;
/**
* Reimplemented to accept color drops
* \param event the handled event
*/
virtual void dragEnterEvent( QDragEnterEvent* event );
/**
* Reimplemented to accept color drops
* \param event the handled event
*/
virtual void dropEvent( QDropEvent* event );
private:
public slots:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment