allow changing image alignment for floating images as well

This commit is contained in:
Kovid Goyal 2023-09-06 11:02:13 +05:30
parent 0be966db4a
commit 9240af7d40
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -895,6 +895,7 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
def align_image_at(self, cursor_pos, alignment): def align_image_at(self, cursor_pos, alignment):
c = self.textCursor() c = self.textCursor()
c.clearSelection()
c.setPosition(cursor_pos) c.setPosition(cursor_pos)
fmt = c.charFormat() fmt = c.charFormat()
if fmt.isImageFormat() and c.currentFrame(): if fmt.isImageFormat() and c.currentFrame():
@ -904,25 +905,31 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
ff.setPosition(alignment) ff.setPosition(alignment)
cf[0].setFrameFormat(ff) cf[0].setFrameFormat(ff)
self.document().markContentsDirty(cursor_pos-2, 5) self.document().markContentsDirty(cursor_pos-2, 5)
else:
c.deleteChar()
c.insertImage(fmt.toImageFormat(), alignment)
def contextMenuEvent(self, ev): def contextMenuEvent(self, ev):
menu = QMenu(self) menu = QMenu(self)
# unfortunately there appears to be no way to get the image under the
# mouse for floating images.
c = self.cursorForPosition(ev.pos()) c = self.cursorForPosition(ev.pos())
fmt = c.charFormat() fmt = c.charFormat()
if fmt.isImageFormat() and c.currentFrame(): if fmt.isImageFormat() and c.currentFrame():
cf = c.currentFrame().childFrames() cf = c.currentFrame().childFrames()
pos = QTextFrameFormat.Position.InFlow
if len(cf) == 1: if len(cf) == 1:
pos = cf[0].frameFormat().position() pos = cf[0].frameFormat().position()
align_menu = menu.addMenu(QIcon.ic('view-image.png'), _('Change image alignment...')) align_menu = menu.addMenu(QIcon.ic('view-image.png'), _('Change image alignment...'))
def a(text, epos): def a(text, epos):
ac = align_menu.addAction(text) ac = align_menu.addAction(text)
ac.setCheckable(True) ac.setCheckable(True)
ac.triggered.connect(partial(self.align_image_at, c.position(), epos)) ac.triggered.connect(partial(self.align_image_at, c.position(), epos))
if pos == epos: if pos == epos:
ac.setChecked(True) ac.setChecked(True)
a(_('Float to the left'), QTextFrameFormat.Position.FloatLeft) a(_('Float to the left'), QTextFrameFormat.Position.FloatLeft)
a(_('Inline with text'), QTextFrameFormat.Position.InFlow) a(_('Inline with text'), QTextFrameFormat.Position.InFlow)
a(_('Float to the right'), QTextFrameFormat.Position.FloatRight) a(_('Float to the right'), QTextFrameFormat.Position.FloatRight)
for ac in 'undo redo -- cut copy paste paste_and_match_style -- select_all'.split(): for ac in 'undo redo -- cut copy paste paste_and_match_style -- select_all'.split():
if ac == '--': if ac == '--':
menu.addSeparator() menu.addSeparator()
@ -1353,5 +1360,6 @@ if __name__ == '__main__':
set <u>out</u> to have an <em>affair</em>, <span style="font-style:italic; background-color:red"> set <u>out</u> to have an <em>affair</em>, <span style="font-style:italic; background-color:red">
much</span> less a <s>long-term</s>, <b>devoted</b> one.</span><p>hello''' much</span> less a <s>long-term</s>, <b>devoted</b> one.</span><p>hello'''
w.html = '<div><p id="moo" align="justify">Testing <em>a</em> link.</p><p align="justify">\xa0</p><p align="justify">ss</p></div>' w.html = '<div><p id="moo" align="justify">Testing <em>a</em> link.</p><p align="justify">\xa0</p><p align="justify">ss</p></div>'
w.html = '<p>Testing <img src="file:///home/kovid/work/calibre/resources/images/donate.png"> img</p>'
app.exec() app.exec()
# print w.html # print w.html