|
8 | 8 |
|
9 | 9 | from AnyQt.QtWidgets import ( |
10 | 10 | QGraphicsItem, QGraphicsPathItem, QGraphicsWidget, QGraphicsTextItem, |
11 | | - QGraphicsDropShadowEffect, QMenu |
| 11 | + QGraphicsDropShadowEffect, QMenu, QAction, QActionGroup |
12 | 12 | ) |
13 | 13 | from AnyQt.QtGui import ( |
14 | 14 | QPainterPath, QPainterPathStroker, QPolygonF, QColor, QPen |
@@ -496,20 +496,41 @@ def contextMenuEvent(self, event): |
496 | 496 | if event.modifiers() & Qt.AltModifier: |
497 | 497 | menu = QMenu(event.widget()) |
498 | 498 | menu.setAttribute(Qt.WA_DeleteOnClose) |
| 499 | + formatmenu = menu.addMenu("Render as") |
| 500 | + group = QActionGroup(self, exclusive=True) |
| 501 | + |
| 502 | + def makeaction(text, parent, data=None, **kwargs): |
| 503 | + action = QAction(text, parent, **kwargs) |
| 504 | + if data is not None: |
| 505 | + action.setData(data) |
| 506 | + return action |
| 507 | + |
| 508 | + formatactions = [ |
| 509 | + makeaction("Plain Text", group, checkable=True, |
| 510 | + toolTip=self.tr("Render contents as plain text"), |
| 511 | + data="text/plain"), |
| 512 | + makeaction("HTML", group, checkable=True, |
| 513 | + toolTip=self.tr("Render contents as HTML"), |
| 514 | + data="text/html"), |
| 515 | + makeaction("RST", group, checkable=True, |
| 516 | + toolTip=self.tr("Render contents as RST " |
| 517 | + "(reStructuredText)"), |
| 518 | + data="text/rst"), |
| 519 | + makeaction("Markdown", group, checkable=True, |
| 520 | + toolTip=self.tr("Render contents as Markdown"), |
| 521 | + data="text/markdown") |
| 522 | + ] |
| 523 | + for action in formatactions: |
| 524 | + action.setChecked(action.data() == self.__contentType.lower()) |
| 525 | + formatmenu.addAction(action) |
499 | 526 |
|
500 | | - menu.addAction("text/plain") |
501 | | - menu.addAction("text/markdown") |
502 | | - menu.addAction("text/rst") |
503 | | - menu.addAction("text/html") |
504 | | - |
505 | | - for action in menu.actions(): |
506 | | - action.setCheckable(True) |
507 | | - action.setChecked(action.text() == self.__contentType.lower()) |
508 | | - |
509 | | - @menu.triggered.connect |
510 | 527 | def ontriggered(action): |
511 | | - self.setContent(self.content(), action.text()) |
| 528 | + mimetype = action.data() |
| 529 | + content = self.content() |
| 530 | + self.setContent(content, mimetype) |
| 531 | + self.editingFinished.emit() |
512 | 532 |
|
| 533 | + menu.triggered.connect(ontriggered) |
513 | 534 | menu.popup(event.screenPos()) |
514 | 535 | event.accept() |
515 | 536 | else: |
|
0 commit comments