Skip to content

Commit

Permalink
#146 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ArsMasiuk committed Apr 24, 2021
1 parent 2822700 commit dcef730
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ New features/improvements:
- added actions to toggle active selection, select only nodes or only edges (#134)
- added font combo box (#135)
- added action to clear nodes & edges appearance to defaults
- added default context menu to the label editor (#146)

Bugfixes:
- fixed crash if DOT file has HTML labels (#131)
Expand Down
46 changes: 39 additions & 7 deletions src/qvgelib/CTextLabelEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ It can be used freely, maintaining the information above.
#include <QMenu>
#include <QKeyEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QTimer>
#include <QApplication>


CTextLabelEdit::CTextLabelEdit()
Expand Down Expand Up @@ -80,15 +82,45 @@ void CTextLabelEdit::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
m_menuActive = true;

QMenu menu;
menu.addAction("Cut");
menu.addAction("Copy");
menu.addAction("Paste");
bool hasSelection = textCursor().hasSelection();

auto cutAction = menu.addAction(QIcon(":/Icons/Cut"), tr("Cut"), this, [&]
{
static QKeyEvent ke(QEvent::KeyPress, Qt::Key_X, Qt::ControlModifier);
QApplication::sendEvent(scene(), &ke);
});
cutAction->setEnabled(hasSelection);
cutAction->setShortcut(QKeySequence::Cut);

auto copyAction = menu.addAction(QIcon(":/Icons/Copy"), tr("Copy"), this, [&]
{
static QKeyEvent ke(QEvent::KeyPress, Qt::Key_C, Qt::ControlModifier);
QApplication::sendEvent(scene(), &ke);
});
copyAction->setEnabled(hasSelection);
copyAction->setShortcut(QKeySequence::Copy);

auto pasteAction = menu.addAction(QIcon(":/Icons/Paste"), tr("Paste"), this, [&]
{
static QKeyEvent ke(QEvent::KeyPress, Qt::Key_V, Qt::ControlModifier);
QApplication::sendEvent(scene(), &ke);
});
pasteAction->setShortcut(QKeySequence::Paste);

menu.addSeparator();
menu.addAction("Select all");

QAction *selectedAction = menu.exec(event->screenPos());
auto selectAllAction = menu.addAction(tr("Select all"), this, [&]
{
QTextCursor c(this->document());
c.select(QTextCursor::Document);
this->setTextCursor(c);
});
selectAllAction->setShortcut(QKeySequence::SelectAll);

menu.exec(event->screenPos());

m_menuActive = false;
// delayed call to "m_menuActive = false" to get the event processed before
QTimer::singleShot(100, this, [&] { m_menuActive = false; });
}


Expand All @@ -114,7 +146,7 @@ void CTextLabelEdit::startEdit(CItem *item)

updateGeometry();

QTextCursor c = textCursor();
QTextCursor c(document());
c.select(QTextCursor::Document);
setTextCursor(c);

Expand Down

0 comments on commit dcef730

Please sign in to comment.