Add context menu

master
Jeremy Hu 2018-04-09 11:39:04 +08:00
parent cd6a973fe7
commit b8e5d31169
5 changed files with 102 additions and 19 deletions

View File

@ -25,19 +25,19 @@ int main(int argc, char ** argv)
// https://gist.github.com/QuantumCD/6245215 // https://gist.github.com/QuantumCD/6245215
qApp->setStyle(QStyleFactory::create("Fusion")); qApp->setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette; QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(0x25,0x25,0x25)); darkPalette.setColor(QPalette::Window, Theme::black);
darkPalette.setColor(QPalette::WindowText, QColor(239,239,239)); darkPalette.setColor(QPalette::WindowText, Theme::white);
darkPalette.setColor(QPalette::Base, QColor(25,25,25)); darkPalette.setColor(QPalette::Base, QColor(25,25,25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53,53,53)); darkPalette.setColor(QPalette::AlternateBase, QColor(53,53,53));
darkPalette.setColor(QPalette::ToolTipBase, Qt::white); darkPalette.setColor(QPalette::ToolTipBase, Theme::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white); darkPalette.setColor(QPalette::ToolTipText, Theme::white);
darkPalette.setColor(QPalette::Text, Qt::white); darkPalette.setColor(QPalette::Text, Theme::white);
darkPalette.setColor(QPalette::Button, QColor(53,53,53)); darkPalette.setColor(QPalette::Button, QColor(53,53,53));
darkPalette.setColor(QPalette::ButtonText, QColor(239,239,239)); darkPalette.setColor(QPalette::ButtonText, Theme::white);
darkPalette.setColor(QPalette::BrightText, Qt::red); darkPalette.setColor(QPalette::BrightText, Theme::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218)); darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, Theme::white); darkPalette.setColor(QPalette::Highlight, Theme::white);
darkPalette.setColor(QPalette::HighlightedText, Qt::white); darkPalette.setColor(QPalette::HighlightedText, Theme::black);
qApp->setPalette(darkPalette); qApp->setPalette(darkPalette);
qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: #fc6621; border: 1px solid white; }"); qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: #fc6621; border: 1px solid white; }");

View File

@ -5,6 +5,7 @@
#include <QtGlobal> #include <QtGlobal>
#include <algorithm> #include <algorithm>
#include <QVector2D> #include <QVector2D>
#include <QMenu>
#include "skeletongraphicswidget.h" #include "skeletongraphicswidget.h"
#include "theme.h" #include "theme.h"
#include "util.h" #include "util.h"
@ -55,6 +56,48 @@ SkeletonGraphicsWidget::SkeletonGraphicsWidget(const SkeletonDocument *document)
scene()->setSceneRect(rect()); scene()->setSceneRect(rect());
setMouseTracking(true); setMouseTracking(true);
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &SkeletonGraphicsWidget::customContextMenuRequested, this, &SkeletonGraphicsWidget::showContextMenu);
}
void SkeletonGraphicsWidget::showContextMenu(const QPoint &pos)
{
if (SkeletonDocumentEditMode::Add == m_document->editMode) {
emit setEditMode(SkeletonDocumentEditMode::Select);
return;
}
if (SkeletonDocumentEditMode::Select != m_document->editMode) {
return;
}
QMenu contextMenu(this);
QAction addAction("Add..", this);
connect(&addAction, &QAction::triggered, [=]() {
emit setEditMode(SkeletonDocumentEditMode::Add);
});
contextMenu.addAction(&addAction);
contextMenu.addSeparator();
QAction deleteAction("Delete", this);
connect(&deleteAction, &QAction::triggered, this, &SkeletonGraphicsWidget::deleteSelected);
deleteAction.setEnabled(!m_rangeSelectionSet.empty());
contextMenu.addAction(&deleteAction);
contextMenu.addSeparator();
QAction selectAllAction("Select All", this);
connect(&selectAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::selectAll);
selectAllAction.setEnabled(!nodeItemMap.empty());
contextMenu.addAction(&selectAllAction);
QAction unselectAllAction("Unselect All", this);
connect(&unselectAllAction, &QAction::triggered, this, &SkeletonGraphicsWidget::unselectAll);
unselectAllAction.setEnabled(!m_rangeSelectionSet.empty());
contextMenu.addAction(&unselectAllAction);
contextMenu.exec(mapToGlobal(pos));
} }
void SkeletonGraphicsWidget::updateItems() void SkeletonGraphicsWidget::updateItems()
@ -525,23 +568,29 @@ bool SkeletonGraphicsWidget::mouseDoubleClick(QMouseEvent *event)
return false; return false;
} }
bool SkeletonGraphicsWidget::keyPress(QKeyEvent *event) void SkeletonGraphicsWidget::deleteSelected()
{ {
if (event->key() == Qt::Key_Delete || event->key() ==Qt::Key_Backspace) {
bool processed = false;
if (!m_rangeSelectionSet.empty()) { if (!m_rangeSelectionSet.empty()) {
std::set<QUuid> nodeIdSet; std::set<QUuid> nodeIdSet;
std::set<QUuid> edgeIdSet; std::set<QUuid> edgeIdSet;
readSkeletonNodeAndEdgeIdSetFromRangeSelection(&nodeIdSet, &edgeIdSet); readSkeletonNodeAndEdgeIdSetFromRangeSelection(&nodeIdSet, &edgeIdSet);
for (const auto &id: edgeIdSet) { for (const auto &id: edgeIdSet) {
emit removeEdge(id); emit removeEdge(id);
processed = true;
} }
for (const auto &id: nodeIdSet) { for (const auto &id: nodeIdSet) {
emit removeNode(id); emit removeNode(id);
processed = true;
} }
} }
}
bool SkeletonGraphicsWidget::keyPress(QKeyEvent *event)
{
if (event->key() == Qt::Key_Delete || event->key() ==Qt::Key_Backspace) {
bool processed = false;
if (!m_rangeSelectionSet.empty()) {
deleteSelected();
processed = true;
}
if (processed) { if (processed) {
emit groupOperationAdded(); emit groupOperationAdded();
return true; return true;
@ -854,3 +903,31 @@ void SkeletonGraphicsWidget::readSkeletonNodeAndEdgeIdSetFromRangeSelection(std:
} }
} }
void SkeletonGraphicsWidget::selectAll()
{
unselectAll();
SkeletonProfile choosenProfile = SkeletonProfile::Main;
if (m_hoveredNodeItem) {
choosenProfile = m_hoveredNodeItem->profile();
} else if (m_hoveredEdgeItem) {
choosenProfile = m_hoveredEdgeItem->profile();
}
for (const auto &it: nodeItemMap) {
SkeletonGraphicsNodeItem *item = SkeletonProfile::Main == choosenProfile ? it.second.first : it.second.second;
checkSkeletonItem(item, true);
m_rangeSelectionSet.insert(item);
}
for (const auto &it: edgeItemMap) {
SkeletonGraphicsEdgeItem *item = SkeletonProfile::Main == choosenProfile ? it.second.first : it.second.second;
checkSkeletonItem(item, true);
m_rangeSelectionSet.insert(item);
}
}
void SkeletonGraphicsWidget::unselectAll()
{
for (const auto &item: m_rangeSelectionSet) {
checkSkeletonItem(item, false);
}
m_rangeSelectionSet.clear();
}

View File

@ -296,6 +296,10 @@ public slots:
void editModeChanged(); void editModeChanged();
void updateCursor(); void updateCursor();
void partVisibleStateChanged(QUuid partId); void partVisibleStateChanged(QUuid partId);
void showContextMenu(const QPoint &pos);
void deleteSelected();
void selectAll();
void unselectAll();
private slots: private slots:
void turnaroundImageReady(); void turnaroundImageReady();
private: private:

View File

@ -18,6 +18,7 @@ QColor Theme::red = QColor(0xfc, 0x66, 0x21);
QColor Theme::green = QColor(0xaa, 0xeb, 0xc4); QColor Theme::green = QColor(0xaa, 0xeb, 0xc4);
QColor Theme::blue = QColor(0x2a, 0x5a, 0xac); QColor Theme::blue = QColor(0x2a, 0x5a, 0xac);
QColor Theme::white = QColor(0xf7, 0xd9, 0xc8); QColor Theme::white = QColor(0xf7, 0xd9, 0xc8);
QColor Theme::black = QColor(0x25,0x25,0x25);
float Theme::normalAlpha = 96.0 / 255; float Theme::normalAlpha = 96.0 / 255;
float Theme::branchAlpha = 64.0 / 255; float Theme::branchAlpha = 64.0 / 255;
float Theme::checkedAlpha = 1.0; float Theme::checkedAlpha = 1.0;

View File

@ -12,6 +12,7 @@ public:
static QColor green; static QColor green;
static QColor blue; static QColor blue;
static QColor white; static QColor white;
static QColor black;
static float normalAlpha; static float normalAlpha;
static float checkedAlpha; static float checkedAlpha;
static float branchAlpha; static float branchAlpha;