SqlTextEdit: Support opening files by drag and drop

When dropping a file on the editor widget try to open and read it.
This commit is contained in:
Martin Kleusberg
2013-05-17 15:05:21 +02:00
parent c0e6d251d5
commit 929bb9026d
2 changed files with 18 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
#include <QScrollBar>
#include <QPainter>
#include <QTextBlock>
#include <QUrl>
SqlTextEdit::SqlTextEdit(QWidget* parent) :
QPlainTextEdit(parent), m_Completer(0), m_defaultCompleterModel(0)
@@ -181,6 +182,22 @@ void SqlTextEdit::LineNumberArea::paintEvent(QPaintEvent* event)
}
}
void SqlTextEdit::dropEvent(QDropEvent* e)
{
QList<QUrl> urls = e->mimeData()->urls();
if(urls.isEmpty())
return QPlainTextEdit::dropEvent(e);
QString file = urls.first().toLocalFile();
if(!QFile::exists(file))
return;
QFile f(file);
f.open(QIODevice::ReadOnly);
setPlainText(f.readAll());
f.close();
}
QSize SqlTextEdit::LineNumberArea::sizeHint() const
{
return QSize(parent->lineNumberAreaWidth(), 0);

View File

@@ -32,6 +32,7 @@ protected:
void keyPressEvent(QKeyEvent *e);
void focusInEvent(QFocusEvent *e);
void resizeEvent(QResizeEvent* event);
void dropEvent(QDropEvent* e);
void lineNumberAreaPaintEvent(QPaintEvent* event);
int lineNumberAreaWidth();