Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert an image directly from filesystem #500

Merged
merged 16 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ def getImageFilenameAndLink(self):

return chosenFileName, link

# this function is also accessed in window.insertImage
@staticmethod
def getCorrectSyntaxLink(markupClass, link):
if markupClass == MarkdownMarkup:
return '![%s](%s)' % (QFileInfo(link).baseName(), link)
elif markupClass == ReStructuredTextMarkup:
return '.. image:: %s' % link
elif markupClass == TextileMarkup:
return '!%s!' % link

def pasteImage(self):
mimeData = QApplication.instance().clipboard().mimeData()
fileName, link = self.getImageFilenameAndLink()
Expand All @@ -414,12 +424,7 @@ def pasteImage(self):
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
imageText = self.getCorrectSyntaxLink(markupClass, link)

self.textCursor().insertText(imageText)

Expand Down
26 changes: 26 additions & 0 deletions ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from ReText.dialogs import HtmlDialog, LocaleDialog
from ReText.config import ConfigDialog, setIconThemeFromSettings
from ReText.tabledialog import InsertTableDialog
from ReText.editor import ReTextEdit

try:
from ReText.fakevimeditor import ReTextFakeVimHandler, FakeVimMode
Expand Down Expand Up @@ -146,6 +147,8 @@ def __init__(self, parent=None):
self.actionTableMode = self.act(self.tr('Table editing mode'),
shct=Qt.CTRL+Qt.Key_T,
trigbool=lambda x: self.currentTab.editBox.enableTableMode(x))
self.actionInsertImage = self.act(self.tr('Insert images by file path'),
trig=lambda: self.insertImage())
if ReTextFakeVimHandler:
self.actionFakeVimMode = self.act(self.tr('FakeVim mode'),
shct=Qt.CTRL+Qt.ALT+Qt.Key_V, trigbool=self.enableFakeVimMode)
Expand Down Expand Up @@ -326,6 +329,7 @@ def __init__(self, parent=None):
menuEdit.addAction(self.actionPreview)
menuEdit.addAction(self.actionInsertTable)
menuEdit.addAction(self.actionTableMode)
menuEdit.addAction(self.actionInsertImage)
if ReTextFakeVimHandler:
menuEdit.addAction(self.actionFakeVimMode)
menuEdit.addSeparator()
Expand Down Expand Up @@ -1105,6 +1109,9 @@ def insertFormatting(self, formatting):
dialog.show()
self.formattingBox.setCurrentIndex(0)
return
elif formatting == 'image':
self.insertImage()
return
mitya57 marked this conversation as resolved.
Show resolved Hide resolved

cursor = self.currentTab.editBox.textCursor()
text = cursor.selectedText()
Expand Down Expand Up @@ -1239,6 +1246,25 @@ def viewHtml(self):
htmlDlg.raise_()
htmlDlg.activateWindow()

def insertImage(self):
supportedExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp']
fileFilter = ' (' + str.join(' ', ['*' + ext for ext in supportedExtensions]) + ');;'
fileNames = QFileDialog.getOpenFileNames(self,
self.tr("Select one or several images to open"), QDir.currentPath(),
self.tr("Supported files") + fileFilter + self.tr("All files (*)"))

cursor = self.currentTab.editBox.textCursor()
markupClass = self.currentTab.getActiveMarkupClass()

# if there're more than one file, add break lines '\n'
bl = '\n' if len(fileNames[0]) > 1 else ''
for fileName in fileNames[0]:
imageText = ReTextEdit.getCorrectSyntaxLink(markupClass, fileName)
cursor.insertText(imageText + bl)

self.formattingBox.setCurrentIndex(0)
self.currentTab.editBox.setFocus(Qt.OtherFocusReason)

def openHelp(self):
QDesktopServices.openUrl(QUrl('https://github.com/retext-project/retext/wiki'))

Expand Down