Make uncheckable menu items invisible

master
Jeremy Hu 2018-04-09 22:37:20 +08:00
parent 62fdcbdced
commit 2a7554b852
2 changed files with 45 additions and 34 deletions

View File

@ -83,56 +83,59 @@ void SkeletonGraphicsWidget::showContextMenu(const QPoint &pos)
}); });
contextMenu.addAction(&addAction); contextMenu.addAction(&addAction);
contextMenu.addSeparator();
QAction deleteAction("Delete", this); QAction deleteAction("Delete", this);
connect(&deleteAction, &QAction::triggered, this, &SkeletonGraphicsWidget::deleteSelected); if (!m_rangeSelectionSet.empty()) {
deleteAction.setEnabled(!m_rangeSelectionSet.empty()); connect(&deleteAction, &QAction::triggered, this, &SkeletonGraphicsWidget::deleteSelected);
contextMenu.addAction(&deleteAction); contextMenu.addAction(&deleteAction);
}
QAction cutAction("Cut", this); QAction cutAction("Cut", this);
connect(&cutAction, &QAction::triggered, this, &SkeletonGraphicsWidget::cut); if (!m_rangeSelectionSet.empty()) {
cutAction.setEnabled(!m_rangeSelectionSet.empty()); connect(&cutAction, &QAction::triggered, this, &SkeletonGraphicsWidget::cut);
contextMenu.addAction(&cutAction); contextMenu.addAction(&cutAction);
}
QAction copyAction("Copy", this); QAction copyAction("Copy", this);
connect(&copyAction, &QAction::triggered, this, &SkeletonGraphicsWidget::copy); if (!m_rangeSelectionSet.empty()) {
copyAction.setEnabled(!m_rangeSelectionSet.empty()); connect(&copyAction, &QAction::triggered, this, &SkeletonGraphicsWidget::copy);
contextMenu.addAction(&copyAction); contextMenu.addAction(&copyAction);
}
QAction pasteAction("Paste", this); QAction pasteAction("Paste", this);
connect(&pasteAction, &QAction::triggered, m_document, &SkeletonDocument::paste); if (m_document->hasPastableContentInClipboard()) {
pasteAction.setEnabled(m_document->hasPastableContentInClipboard()); connect(&pasteAction, &QAction::triggered, m_document, &SkeletonDocument::paste);
contextMenu.addAction(&pasteAction); contextMenu.addAction(&pasteAction);
}
contextMenu.addSeparator();
QAction flipHorizontallyAction("H Flip", this); QAction flipHorizontallyAction("H Flip", this);
connect(&flipHorizontallyAction, &QAction::triggered, this, &SkeletonGraphicsWidget::flipHorizontally); if (!m_rangeSelectionSet.empty() && !isSingleNodeSelected()) {
flipHorizontallyAction.setEnabled(!m_rangeSelectionSet.empty()); connect(&flipHorizontallyAction, &QAction::triggered, this, &SkeletonGraphicsWidget::flipHorizontally);
contextMenu.addAction(&flipHorizontallyAction); contextMenu.addAction(&flipHorizontallyAction);
}
QAction flipVerticallyAction("V Flip", this); QAction flipVerticallyAction("V Flip", this);
connect(&flipVerticallyAction, &QAction::triggered, this, &SkeletonGraphicsWidget::flipVertically); if (!m_rangeSelectionSet.empty() && !isSingleNodeSelected()) {
flipVerticallyAction.setEnabled(!m_rangeSelectionSet.empty()); connect(&flipVerticallyAction, &QAction::triggered, this, &SkeletonGraphicsWidget::flipVertically);
contextMenu.addAction(&flipVerticallyAction); contextMenu.addAction(&flipVerticallyAction);
}
contextMenu.addSeparator();
QAction selectAllAction("Select All", this); QAction selectAllAction("Select All", this);
connect(&selectAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::selectAll); if (!nodeItemMap.empty()) {
selectAllAction.setEnabled(!nodeItemMap.empty()); connect(&selectAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::selectAll);
contextMenu.addAction(&selectAllAction); contextMenu.addAction(&selectAllAction);
}
QAction selectPartAllAction("Select Part", this); QAction selectPartAllAction("Select Part", this);
connect(&selectPartAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::selectPartAll); if (!nodeItemMap.empty()) {
selectPartAllAction.setEnabled(!nodeItemMap.empty()); connect(&selectPartAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::selectPartAll);
contextMenu.addAction(&selectPartAllAction); contextMenu.addAction(&selectPartAllAction);
}
QAction unselectAllAction("Unselect All", this); QAction unselectAllAction("Unselect All", this);
connect(&unselectAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::unselectAll); if (!m_rangeSelectionSet.empty()) {
unselectAllAction.setEnabled(!m_rangeSelectionSet.empty()); connect(&unselectAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::unselectAll);
contextMenu.addAction(&unselectAllAction); contextMenu.addAction(&unselectAllAction);
}
contextMenu.addSeparator(); contextMenu.addSeparator();
@ -1138,5 +1141,12 @@ void SkeletonGraphicsWidget::copy()
clipboard->setText(snapshotXml); clipboard->setText(snapshotXml);
} }
bool SkeletonGraphicsWidget::isSingleNodeSelected()
{
if (m_rangeSelectionSet.size() != 1)
return false;
const auto &it = m_rangeSelectionSet.begin();
QGraphicsItem *item = *it;
return item->data(0) == "node";
}

View File

@ -324,6 +324,7 @@ private:
void clearRangeSelection(); void clearRangeSelection();
void removeItem(QGraphicsItem *item); void removeItem(QGraphicsItem *item);
QVector2D centerOfNodeItemSet(const std::set<SkeletonGraphicsNodeItem *> &set); QVector2D centerOfNodeItemSet(const std::set<SkeletonGraphicsNodeItem *> &set);
bool isSingleNodeSelected();
private: private:
QGraphicsPixmapItem *m_backgroundItem; QGraphicsPixmapItem *m_backgroundItem;
const SkeletonDocument *m_document; const SkeletonDocument *m_document;