Skip to content

Commit

Permalink
Drag and drop to open files
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Oct 28, 2024
1 parent ab6e79b commit e981d68
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/ui/qt/include/ui/qt/widget/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace VTX::UI::QT::Widget

inline void setClosing( const bool p_closing ) { _closing = p_closing; }

void changeEvent( QEvent * const p_event ) override;
void closeEvent( QCloseEvent * p_event ) override;
void changeEvent( QEvent * ) override;
void closeEvent( QCloseEvent * ) override;

template<typename M>
M * const createMenu()
Expand Down Expand Up @@ -80,6 +80,10 @@ namespace VTX::UI::QT::Widget
void save() override;
void restore() override;

protected:
void dragEnterEvent( QDragEnterEvent * );
void dropEvent( QDropEvent * );

private:
QPointer<OpenGLWidget> _openGLWidget;
QPointer<StatusBar> _statusBar;
Expand Down
3 changes: 3 additions & 0 deletions lib/ui/qt/include/ui/qt/widget/opengl_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace VTX::UI::QT::Widget
void setVSync( const bool );
bool isVSync() const { return _context->format().swapInterval() == 1; }

protected:
bool eventFilter( QObject *, QEvent * ) override;

private:
QPointer<QOpenGLContext> _context;
QPointer<Window::EventCatchWindow> _window;
Expand Down
15 changes: 15 additions & 0 deletions lib/ui/qt/src/ui/qt/widget/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "ui/qt/tool_bar/file.hpp"
#include "ui/qt/tool_bar/snapshot.hpp"
#include <QApplication>
#include <QMimeData>
#include <app/action/application.hpp>
#include <app/action/scene.hpp>

namespace VTX::UI::QT::Widget
{
Expand All @@ -35,6 +37,7 @@ namespace VTX::UI::QT::Widget
setDockOptions( ForceTabbedDocks );
setTabPosition( Qt::AllDockWidgetAreas, QTabWidget::North );
setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
setAcceptDrops( true );

// Features.
setTabShape( QTabWidget::Rounded );
Expand Down Expand Up @@ -184,6 +187,18 @@ namespace VTX::UI::QT::Widget
}
}

void MainWindow::dragEnterEvent( QDragEnterEvent * p_event ) { p_event->acceptProposedAction(); }

void MainWindow::dropEvent( QDropEvent * p_event )
{
for ( const auto & url : p_event->mimeData()->urls() )
{
App::ACTION_SYSTEM().execute<App::Action::Scene::LoadMolecule>( url.toLocalFile().toStdString() );
}

p_event->acceptProposedAction();
}

void MainWindow::save()
{
SETTINGS.setValue( "geometry", saveGeometry() );
Expand Down
24 changes: 24 additions & 0 deletions lib/ui/qt/src/ui/qt/widget/opengl_widget.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "ui/qt/widget/opengl_widget.hpp"
#include "ui/qt/application.hpp"
#include <app/action/application.hpp>

namespace VTX::UI::QT::Widget
{

OpenGLWidget::OpenGLWidget( QWidget * p_parent ) : BaseWidget( p_parent )
{
setAcceptDrops( true );

// Create surface.
QSurfaceFormat format;
format.setVersion( 4, 5 );
Expand Down Expand Up @@ -34,6 +37,8 @@ namespace VTX::UI::QT::Widget

// Use a widget container to embed the window.
_container = createWindowContainer( _window, this );
//_container->setAcceptDrops( true );
_container->installEventFilter( this );

// Set context.
_context->makeCurrent( _window );
Expand Down Expand Up @@ -79,4 +84,23 @@ namespace VTX::UI::QT::Widget
}
}

bool OpenGLWidget::eventFilter( QObject * p_watched, QEvent * p_event )
{
if ( p_watched == _container )
{
auto * e = p_event->clone();
if ( p_event->type() == QEvent::DragEnter )
{
QCoreApplication::sendEvent( APP_QT::getMainWindow(), e );
return true;
}
else if ( p_event->type() == QEvent::Drop )
{
QCoreApplication::sendEvent( APP_QT::getMainWindow(), e );
return true;
}
}
return QWidget::eventFilter( p_watched, p_event );
}

} // namespace VTX::UI::QT::Widget

0 comments on commit e981d68

Please sign in to comment.