Skip to content

Commit c1ecc61

Browse files
authored
Add dnd support
1 parent 58a53d5 commit c1ecc61

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/mainwindow.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <QDesktopServices>
3030
#include <QDialog>
3131
#include <QDir>
32+
#include <QDragEnterEvent>
33+
#include <QDropEvent>
3234
#include <QEvent>
3335
#include <QFile>
3436
#include <QFileDialog>
@@ -41,6 +43,7 @@
4143
#include <QMenu>
4244
#include <QMenuBar>
4345
#include <QMessageBox>
46+
#include <QMimeData>
4447
#include <QObject>
4548
#include <QPixmap>
4649
#include <QPrinter>
@@ -104,6 +107,8 @@ MainWindow::MainWindow( const QStringList& arguments )
104107
// UIC stuff
105108
setupUi( this );
106109

110+
setAcceptDrops( true );
111+
107112
// Set up layout direction
108113
if ( pConfig->m_advLayoutDirectionRL )
109114
qApp->setLayoutDirection( Qt::RightToLeft );
@@ -608,6 +613,45 @@ void MainWindow::closeEvent( QCloseEvent* e )
608613
QMainWindow::closeEvent( e );
609614
}
610615

616+
void MainWindow::dragEnterEvent( QDragEnterEvent* e )
617+
{
618+
if ( e->mimeData()->hasUrls() )
619+
{
620+
QUrl url = e->mimeData()->urls().first();
621+
622+
if ( url.isLocalFile() )
623+
{
624+
QString fileName = url.toLocalFile();
625+
626+
if ( fileName.endsWith( ".chm", Qt::CaseInsensitive ) || fileName.endsWith( ".epub", Qt::CaseInsensitive ) )
627+
{
628+
e->acceptProposedAction();
629+
return;
630+
}
631+
}
632+
}
633+
634+
e->ignore();
635+
}
636+
637+
void MainWindow::dropEvent( QDropEvent* e )
638+
{
639+
if ( e->mimeData()->hasUrls() )
640+
{
641+
QUrl url = e->mimeData()->urls().first();
642+
643+
if ( url.isLocalFile() )
644+
{
645+
QString fileName = url.toLocalFile();
646+
647+
if ( fileName.endsWith( ".chm", Qt::CaseInsensitive ) || fileName.endsWith( ".epub", Qt::CaseInsensitive ) )
648+
loadFile( fileName );
649+
}
650+
651+
e->acceptProposedAction();
652+
}
653+
}
654+
611655
void MainWindow::printHelpAndExit()
612656
{
613657
fprintf( stderr, "Usage: %s [options] [helpfile]\n"

src/mainwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ class MainWindow : public QMainWindow, public Ui::MainWindow
172172
protected:
173173
// Reimplemented functions
174174
void closeEvent( QCloseEvent* e );
175+
void dragEnterEvent( QDragEnterEvent* e );
176+
void dropEvent( QDropEvent* e );
175177
bool event( QEvent* e );
176178

177179
private:

src/viewwindowmgr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ ViewWindow* ViewWindowMgr::current()
160160
ViewWindow* ViewWindowMgr::addNewTab( bool set_active )
161161
{
162162
ViewWindow* browser = new ViewWindow( m_tabWidget );
163+
browser->setAcceptDrops( false );
163164

164165
editFind->installEventFilter( this );
165166

0 commit comments

Comments
 (0)