Skip to content

Commit

Permalink
Move pasteImage to a separate action
Browse files Browse the repository at this point in the history
Fixes #385.
  • Loading branch information
mitya57 committed Sep 18, 2018
1 parent 4c24ee3 commit a19d6e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
46 changes: 24 additions & 22 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from PyQt5.QtCore import pyqtSignal, QFileInfo, QPoint, QRect, QSize, Qt
from PyQt5.QtGui import QColor, QImage, QKeyEvent, QMouseEvent, QPainter, \
QPalette, QTextCursor, QTextFormat, QWheelEvent, QGuiApplication
from PyQt5.QtWidgets import QFileDialog, QLabel, QTextEdit, QWidget
from PyQt5.QtWidgets import QAction, QApplication, QFileDialog, QLabel, QTextEdit, QWidget

try:
from ReText.fakevimeditor import ReTextFakeVimHandler
Expand Down Expand Up @@ -175,6 +175,12 @@ def scrollContentsBy(self, dx, dy):
def contextMenuEvent(self, event):
# Create base menu
menu = self.createStandardContextMenu()
if self.parent.actionPasteImage.isEnabled():
actions = menu.actions()
actionPaste = menu.findChild(QAction, "edit-paste")
actionNextAfterPaste = actions[actions.index(actionPaste) + 1]
menu.insertAction(actionNextAfterPaste, self.parent.actionPasteImage)

text = self.toPlainText()
if not text:
menu.exec(event.globalPos())
Expand Down Expand Up @@ -362,9 +368,6 @@ def contentsChange(self, pos, removed, added):
self.lineNumberArea.update()
self.updateTextStatistics()

def canInsertFromMimeData(self, mimeData):
return mimeData.hasText() or mimeData.hasImage()

def findNextImageName(self, filenames):
highestNumber = 0
for filename in filenames:
Expand Down Expand Up @@ -401,24 +404,23 @@ def getImageFilenameAndLink(self):

return chosenFileName, link

def insertFromMimeData(self, mimeData):
if mimeData.hasImage():
fileName, link = self.getImageFilenameAndLink()
if fileName:
image = QImage(mimeData.imageData())
image.save(fileName)

markupClass = self.tab.getActiveMarkupClass()
if markupClass == MarkdownMarkup:
imageText = '![%s](%s)' % (QFileInfo(link).baseName(), link)
elif markupClass == ReStructuredTextMarkup:
imageText = '.. image:: %s' % link
elif markupClass == TextileMarkup:
imageText = '!%s!' % link

self.textCursor().insertText(imageText)
else:
QTextEdit.insertFromMimeData(self, mimeData)
def pasteImage(self):
mimeData = QApplication.instance().clipboard().mimeData()
fileName, link = self.getImageFilenameAndLink()
if not fileName or not mimeData.hasImage():
return
image = QImage(mimeData.imageData())
image.save(fileName)

markupClass = self.tab.getActiveMarkupClass()
if markupClass == MarkdownMarkup:
imageText = '![%s](%s)' % (QFileInfo(link).baseName(), link)
elif markupClass == ReStructuredTextMarkup:
imageText = '.. image:: %s' % link
elif markupClass == TextileMarkup:
imageText = '!%s!' % link

self.textCursor().insertText(imageText)

def installFakeVimHandler(self):
if ReTextFakeVimHandler:
Expand Down
6 changes: 5 additions & 1 deletion ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def __init__(self, parent=None):
lambda: self.currentTab.editBox.cut(), shct=QKeySequence.Cut)
self.actionPaste = self.act(self.tr('Paste'), 'edit-paste',
lambda: self.currentTab.editBox.paste(), shct=QKeySequence.Paste)
self.actionPasteImage = self.act(self.tr('Paste image'), 'edit-paste',
lambda: self.currentTab.editBox.pasteImage(), shct=Qt.CTRL+Qt.SHIFT+Qt.Key_V)
self.actionMoveUp = self.act(self.tr('Move line up'), 'go-up',
lambda: self.currentTab.editBox.moveLineUp(), shct=Qt.ALT+Qt.Key_Up)
self.actionMoveDown = self.act(self.tr('Move line down'), 'go-down',
Expand Down Expand Up @@ -294,6 +296,7 @@ def __init__(self, parent=None):
menuEdit.addAction(self.actionCut)
menuEdit.addAction(self.actionCopy)
menuEdit.addAction(self.actionPaste)
menuEdit.addAction(self.actionPasteImage)
menuEdit.addSeparator()
menuEdit.addAction(self.actionMoveUp)
menuEdit.addAction(self.actionMoveDown)
Expand Down Expand Up @@ -1071,7 +1074,8 @@ def autoSaveActive(self, tab=None):
def clipboardDataChanged(self):
mimeData = QApplication.instance().clipboard().mimeData()
if mimeData is not None:
self.actionPaste.setEnabled(mimeData.hasText() or mimeData.hasImage())
self.actionPaste.setEnabled(mimeData.hasText())
self.actionPasteImage.setEnabled(mimeData.hasImage())

def insertFormatting(self, formatting):
cursor = self.currentTab.editBox.textCursor()
Expand Down
16 changes: 4 additions & 12 deletions tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,6 @@ def _create_image(self):
image.fill(Qt.green)
return image

def test_allowTextOnClipboard(self):
mimeData = QMimeData()
mimeData.setText('hello')
self.assertTrue(self.editor.canInsertFromMimeData(mimeData))

def test_allowImageOnClipboard(self):
mimeData = QMimeData()
mimeData.setImageData(self._create_image())
self.assertTrue(self.editor.canInsertFromMimeData(mimeData))

def test_pasteText(self):
mimeData = QMimeData()
mimeData.setText('pasted text')
Expand All @@ -126,19 +116,21 @@ def test_pasteText(self):
def test_pasteImage_Markdown(self, _mock_image, _mock_editor):
mimeData = QMimeData()
mimeData.setImageData(self._create_image())
app.clipboard().setMimeData(mimeData)
self.dummytab.markupClass = MarkdownMarkup

self.editor.insertFromMimeData(mimeData)
self.editor.pasteImage()
self.assertTrue('![myimage](myimage.jpg)' in self.editor.toPlainText())

@patch.object(ReTextEdit, 'getImageFilenameAndLink', return_value=('/tmp/myimage.jpg', 'myimage.jpg'))
@patch.object(QImage, 'save')
def test_pasteImage_RestructuredText(self, _mock_image, _mock_editor):
mimeData = QMimeData()
mimeData.setImageData(self._create_image())
app.clipboard().setMimeData(mimeData)
self.dummytab.markupClass = ReStructuredTextMarkup

self.editor.insertFromMimeData(mimeData)
self.editor.pasteImage()
self.assertTrue('.. image:: myimage.jpg' in self.editor.toPlainText())

if __name__ == '__main__':
Expand Down

0 comments on commit a19d6e6

Please sign in to comment.