Permalink
Browse files

Allow files to be dropped on the Qt console widget.

The filename will be wrapped in single quotation marks and inserted in
the location of the text cursor before the drag was initiated.  (This
behavior is somewhat different than the drag and drop of text).

Multiple files will have their names joined with a space.
  • Loading branch information...
1 parent f5cbf8b commit 06d24efd926c44205e404126125902cc9e8015a5 @bfroehle committed Aug 10, 2012
Showing with 17 additions and 3 deletions.
  1. +17 −3 IPython/frontend/qt/console/console_widget.py
@@ -351,14 +351,21 @@ def __init__(self, parent=None, **kw):
#---------------------------------------------------------------------------
def dragEnterEvent(self, e):
- if e.mimeData().hasText():
+ if e.mimeData().hasUrls():
+ # The link action should indicate to that the drop will insert
+ # the file anme.
+ e.setDropAction(QtCore.Qt.LinkAction)
+ e.accept()
+ elif e.mimeData().hasText():
# By changing the action to copy we don't need to worry about
# the user accidentally moving text around in the widget.
e.setDropAction(QtCore.Qt.CopyAction)
e.accept()
def dragMoveEvent(self, e):
- if e.mimeData().hasText():
+ if e.mimeData().hasUrls():
+ pass
+ elif e.mimeData().hasText():
cursor = self._control.cursorForPosition(e.pos())
if self._in_buffer(cursor.position()):
e.setDropAction(QtCore.Qt.CopyAction)
@@ -368,7 +375,14 @@ def dragMoveEvent(self, e):
e.accept()
def dropEvent(self, e):
- if e.mimeData().hasText():
+ if e.mimeData().hasUrls():
+ self._keep_cursor_in_buffer()
+ cursor = self._control.textCursor()
+ filenames = [url.toLocalFile() for url in e.mimeData().urls()]
+ text = ', '.join("'" + f.replace("'", "'\"'\"'") + "'"
+ for f in filenames)
+ self._insert_plain_text_into_buffer(cursor, text)
+ elif e.mimeData().hasText():
cursor = self._control.cursorForPosition(e.pos())
if self._in_buffer(cursor.position()):
text = e.mimeData().text()

0 comments on commit 06d24ef

Please sign in to comment.