mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Allow right click to adjust image alignment
This commit is contained in:
parent
6a3d7388d7
commit
e26df3b529
@ -7,6 +7,7 @@ import re
|
|||||||
import weakref
|
import weakref
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
from functools import partial
|
||||||
from html5_parser import parse
|
from html5_parser import parse
|
||||||
from lxml import html
|
from lxml import html
|
||||||
from qt.core import (
|
from qt.core import (
|
||||||
@ -14,8 +15,8 @@ from qt.core import (
|
|||||||
QDialogButtonBox, QFont, QFontInfo, QFontMetrics, QFormLayout, QHBoxLayout, QIcon,
|
QDialogButtonBox, QFont, QFontInfo, QFontMetrics, QFormLayout, QHBoxLayout, QIcon,
|
||||||
QKeySequence, QLabel, QLineEdit, QMenu, QPalette, QPlainTextEdit, QPushButton,
|
QKeySequence, QLabel, QLineEdit, QMenu, QPalette, QPlainTextEdit, QPushButton,
|
||||||
QSize, QSyntaxHighlighter, Qt, QTabWidget, QTextBlockFormat, QTextCharFormat,
|
QSize, QSyntaxHighlighter, Qt, QTabWidget, QTextBlockFormat, QTextCharFormat,
|
||||||
QTextCursor, QTextEdit, QTextFormat, QTextListFormat, QTimer, QToolButton, QUrl,
|
QTextCursor, QTextEdit, QTextFormat, QTextFrameFormat, QTextListFormat, QTimer,
|
||||||
QVBoxLayout, QWidget, pyqtSignal, pyqtSlot,
|
QToolButton, QUrl, QVBoxLayout, QWidget, pyqtSignal, pyqtSlot,
|
||||||
)
|
)
|
||||||
|
|
||||||
from calibre import xml_replace_entities
|
from calibre import xml_replace_entities
|
||||||
@ -892,8 +893,36 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
|
|||||||
ans.setHtml(html)
|
ans.setHtml(html)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
def align_image_at(self, cursor_pos, alignment):
|
||||||
|
c = self.textCursor()
|
||||||
|
c.setPosition(cursor_pos)
|
||||||
|
fmt = c.charFormat()
|
||||||
|
if fmt.isImageFormat() and c.currentFrame():
|
||||||
|
cf = c.currentFrame().childFrames()
|
||||||
|
if len(cf) == 1:
|
||||||
|
ff = cf[0].frameFormat()
|
||||||
|
ff.setPosition(alignment)
|
||||||
|
cf[0].setFrameFormat(ff)
|
||||||
|
self.document().markContentsDirty(cursor_pos-2, 5)
|
||||||
|
|
||||||
def contextMenuEvent(self, ev):
|
def contextMenuEvent(self, ev):
|
||||||
menu = QMenu(self)
|
menu = QMenu(self)
|
||||||
|
c = self.cursorForPosition(ev.pos())
|
||||||
|
fmt = c.charFormat()
|
||||||
|
if fmt.isImageFormat() and c.currentFrame():
|
||||||
|
cf = c.currentFrame().childFrames()
|
||||||
|
if len(cf) == 1:
|
||||||
|
pos = cf[0].frameFormat().position()
|
||||||
|
align_menu = menu.addMenu(QIcon.ic('view-image.png'), _('Change image alignment...'))
|
||||||
|
def a(text, epos):
|
||||||
|
ac = align_menu.addAction(text)
|
||||||
|
ac.setCheckable(True)
|
||||||
|
ac.triggered.connect(partial(self.align_image_at, c.position(), epos))
|
||||||
|
if pos == epos:
|
||||||
|
ac.setChecked(True)
|
||||||
|
a(_('Float to the left'), QTextFrameFormat.Position.FloatLeft)
|
||||||
|
a(_('Inline with text'), QTextFrameFormat.Position.InFlow)
|
||||||
|
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()
|
||||||
|
@ -118,6 +118,7 @@ class AskImage(Dialog):
|
|||||||
hb.addWidget(b)
|
hb.addWidget(b)
|
||||||
|
|
||||||
vr.addStretch(10)
|
vr.addStretch(10)
|
||||||
|
self.add_file_button.setFocus(Qt.FocusReason.OtherFocusReason)
|
||||||
|
|
||||||
def image_pasted_or_dropped(self, cover_data):
|
def image_pasted_or_dropped(self, cover_data):
|
||||||
digest = hash_data(cover_data)
|
digest = hash_data(cover_data)
|
||||||
@ -146,6 +147,7 @@ class AskImage(Dialog):
|
|||||||
self.image_preview.set_pixmap(p)
|
self.image_preview.set_pixmap(p)
|
||||||
self.name_edit.setText(ir.name)
|
self.name_edit.setText(ir.name)
|
||||||
self.current_digest = digest
|
self.current_digest = digest
|
||||||
|
self.bb.setFocus(Qt.FocusReason.OtherFocusReason)
|
||||||
|
|
||||||
def paste_image(self):
|
def paste_image(self):
|
||||||
if not self.image_preview.paste_from_clipboard():
|
if not self.image_preview.paste_from_clipboard():
|
||||||
@ -202,10 +204,10 @@ class NoteEditorWidget(EditorWidget):
|
|||||||
if d.exec() == QDialog.DialogCode.Accepted and d.current_digest:
|
if d.exec() == QDialog.DialogCode.Accepted and d.current_digest:
|
||||||
ir = self.images[d.current_digest]
|
ir = self.images[d.current_digest]
|
||||||
self.focus_self()
|
self.focus_self()
|
||||||
with self.editing_cursor() as c:
|
c = self.textCursor()
|
||||||
fmt = QTextImageFormat()
|
fmt = QTextImageFormat()
|
||||||
fmt.setName(RESOURCE_URL_SCHEME + ':///' + ir.digest)
|
fmt.setName(RESOURCE_URL_SCHEME + ':///' + ir.digest)
|
||||||
c.insertImage(fmt, d.image_layout)
|
c.insertImage(fmt, d.image_layout)
|
||||||
|
|
||||||
|
|
||||||
class NoteEditor(Editor):
|
class NoteEditor(Editor):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user