From 6a484c615ae3f04873fe41a415277ef6c2c37573 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 17 Dec 2020 15:43:37 +0100 Subject: WIP --- Icons.qrc | 9 +++++++ edit_tool.png | Bin 0 -> 448 bytes move_tool.png | Bin 0 -> 519 bytes numberator.cpp | 30 +++++++++++++++++++++++ numberator.h | 12 ++++++++++ numberator.pro | 3 +++ open-iconic-master/png/tag-3x.png | Bin 0 -> 267 bytes selection_tool.png | Bin 0 -> 471 bytes sqlitebackend.h | 3 ++- tag.png | Bin 0 -> 322 bytes tagitem.cpp | 9 ++++++- taglistmodel.cpp | 20 ++++++++++++++-- tags.png | Bin 0 -> 280 bytes tagscene.cpp | 49 ++++++++++++++++++++++++++++++++++++++ tagscene.h | 16 +++++++++++-- tagview.cpp | 28 +++++++++++++++++++++- tagview.h | 3 +++ 17 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 Icons.qrc create mode 100644 edit_tool.png create mode 100644 move_tool.png create mode 100644 open-iconic-master/png/tag-3x.png create mode 100644 selection_tool.png create mode 100644 tag.png create mode 100644 tags.png diff --git a/Icons.qrc b/Icons.qrc new file mode 100644 index 0000000..5c8ced5 --- /dev/null +++ b/Icons.qrc @@ -0,0 +1,9 @@ + + + tag.png + tags.png + selection_tool.png + move_tool.png + edit_tool.png + + diff --git a/edit_tool.png b/edit_tool.png new file mode 100644 index 0000000..82a859c Binary files /dev/null and b/edit_tool.png differ diff --git a/move_tool.png b/move_tool.png new file mode 100644 index 0000000..263c216 Binary files /dev/null and b/move_tool.png differ diff --git a/numberator.cpp b/numberator.cpp index 3161ee8..3fe6497 100644 --- a/numberator.cpp +++ b/numberator.cpp @@ -3,6 +3,7 @@ #include "ui_TagListDock.h" #include +#include Numberator::Numberator(QWidget *parent) : QMainWindow(parent) @@ -88,6 +89,30 @@ Numberator::Numberator(QWidget *parent) } }); + QToolBar *tools_tb = new QToolBar("Tools", this); + struct tool_def { + ToolType type; + QString filename; + QString name; + }; + + std::initializer_list tool_defs = { + {SELECTION_TOOL, ":/icons/selection_tool.png", "Select"}, + {TAG_TOOL, ":/icons/tag.png", "Add Tag"}, + {MOVE_TOOL, ":/icons/move_tool.png", "Move Tag"}, + {EDIT_TOOL, ":/icons/edit_tool.png", "Edit Tag"}, + }; + QActionGroup toolsActionGroup(this); + for (auto tool : tool_defs) { + auto action = tools_tb->addAction(QIcon(tool.filename), tool.name, [=](){ + setTool(tool.type); + }); + action->setCheckable(true); + toolsActionGroup.addAction(action); + } + toolsActionGroup.actions().first()->setChecked(true); + this->addToolBar(Qt::TopToolBarArea, tools_tb); + ui->menuView->addAction(dock->toggleViewAction()); connect(ui->actionReload_Image, &QAction::triggered, &proj, &SQLiteSaveFile::reloadImageFromDisk); @@ -151,6 +176,11 @@ Numberator::~Numberator() delete ui; } +void Numberator::setTool(Numberator::ToolType tool) +{ + ui->graphicsView->scene()->setTool(tool); +} + bool Numberator::showConfirmDiscardDialog() { if (!proj.isMemory() || !proj.isDirty()) diff --git a/numberator.h b/numberator.h index f81c44b..930a171 100644 --- a/numberator.h +++ b/numberator.h @@ -18,6 +18,14 @@ namespace Ui { } QT_END_NAMESPACE +enum ToolType { + SELECTION_TOOL, + TAG_TOOL, + MOVE_TOOL, + EDIT_TOOL, + NUM_TOOLS +}; + class Numberator : public QMainWindow { Q_OBJECT @@ -26,6 +34,8 @@ public: Numberator(QWidget *parent = nullptr); ~Numberator(); + void setTool(ToolType tool); + bool showConfirmDiscardDialog(); public slots: @@ -45,5 +55,7 @@ private: TagListModel tagListModel; TagPropTableModel tagPropTableModel, dialogTagPropTableModel; Tag m_tagBeingEdited; + + ToolType m_activeTool = SELECTION_TOOL; }; #endif // NUMBERATOR_H diff --git a/numberator.pro b/numberator.pro index 71b9fa3..aab09b7 100644 --- a/numberator.pro +++ b/numberator.pro @@ -47,3 +47,6 @@ FORMS += \ qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + Icons.qrc diff --git a/open-iconic-master/png/tag-3x.png b/open-iconic-master/png/tag-3x.png new file mode 100644 index 0000000..de1e4c8 Binary files /dev/null and b/open-iconic-master/png/tag-3x.png differ diff --git a/selection_tool.png b/selection_tool.png new file mode 100644 index 0000000..d3895fa Binary files /dev/null and b/selection_tool.png differ diff --git a/sqlitebackend.h b/sqlitebackend.h index baa3b19..a1e8026 100644 --- a/sqlitebackend.h +++ b/sqlitebackend.h @@ -19,7 +19,7 @@ public: Tag() : valid(false) {} Tag(long long int id, QString name, qreal anchor_x, qreal anchor_y, QByteArray metadata); Tag(long long int id, const Tag &other); - Tag(QString name, const QPointF &anchor, const QVariantMap metadata=QVariantMap()); + Tag(QString name, const QPointF &anchor=QPointF(), const QVariantMap metadata=QVariantMap()); bool operator==(const Tag &t2) const { return id == t2.id; } @@ -29,6 +29,7 @@ public: long long int id; QString name; QPointF anchor; + QPointF labelPos; QVariantMap metadata; private: diff --git a/tag.png b/tag.png new file mode 100644 index 0000000..5a446f5 Binary files /dev/null and b/tag.png differ diff --git a/tagitem.cpp b/tagitem.cpp index 01b26fb..8c17ba6 100644 --- a/tagitem.cpp +++ b/tagitem.cpp @@ -11,7 +11,6 @@ TagItem::TagItem(const Tag &tag) , m_margins(2, 2, 2, 2) { setFlags(QGraphicsItem::ItemIsSelectable - | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIgnoresTransformations | QGraphicsItem::ItemSendsGeometryChanges); @@ -97,6 +96,9 @@ QVariant TagItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVa * */ /* FIXME */ + } else if (change == ItemSelectedChange) { + if (!value.toBool()) + setFlag(QGraphicsItem::ItemIsMovable, false); } return QGraphicsItem::itemChange(change, value); } @@ -123,5 +125,10 @@ void TagItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { Q_UNUSED(event); dragAboveThreshold = false; + if (isSelected()) + setFlag(QGraphicsItem::ItemIsMovable, true); + else + setFlag(QGraphicsItem::ItemIsMovable, false); + QGraphicsItem::mouseReleaseEvent(event); } diff --git a/taglistmodel.cpp b/taglistmodel.cpp index 435f5f1..9cf4fb7 100644 --- a/taglistmodel.cpp +++ b/taglistmodel.cpp @@ -24,9 +24,25 @@ bool collateTagNames(QString a, QString b) { if (res_a.captured(1).isEmpty() && !res_b.captured(1).isEmpty()) return true; - if (res_a.captured().toInt() < res_b.captured().toInt()) + if (res_a.captured(1).toInt() < res_b.captured(1).toInt()) return true; - /* FIXME TODO */ + + if (res_a.captured(1) != res_b.captured(1)) + return false; + + if (res_a.captured(2) < res_b.captured(2)) + return true; + + if (res_a.captured(2) != res_b.captured(2)) + return false; + + if (res_a.captured(3).isEmpty() && !res_b.captured(3).isEmpty()) + return true; + + if (res_a.captured(3).toInt() < res_b.captured(3).toInt()) + return true; + + return false; } void TagListModel::reloadTags() diff --git a/tags.png b/tags.png new file mode 100644 index 0000000..4e7a713 Binary files /dev/null and b/tags.png differ diff --git a/tagscene.cpp b/tagscene.cpp index c5777e0..a10bd2d 100644 --- a/tagscene.cpp +++ b/tagscene.cpp @@ -90,6 +90,29 @@ void TagScene::selectTag(const Tag &tag) it->setSelected(true); } +void TagScene::setTool(ToolType tool) +{ + abortTool(); + m_tool = tool; + + if (tool == ToolType::TAG_TOOL) { + m_tagToolState = TAG_TOOL_ANCHOR; + + } else if (tool == ToolType::SELECTION_TOOL) { + + } +} + +void TagScene::abortTool() +{ + if (m_tool == ToolType::TAG_TOOL) { + removeItem(m_previewTag); + + } else if (m_tool == ToolType::SELECTION_TOOL) { + + } +} + void TagScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { QGraphicsItem *it = itemAt(event->scenePos(), QTransform()); @@ -111,3 +134,29 @@ void TagScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) tagDoubleClicked(tagitem->tag()); } + +void TagScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if (m_tool == ToolType::TAG_TOOL) { + if (m_tagToolState == TAG_TOOL_LABEL) { + Tag t = m_previewTag->tag(); + t.labelPos = event->scenePos(); + m_previewTag->tagUpdated(t); + } + } +} + +void TagScene::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (m_tool == ToolType::TAG_TOOL) { + if (m_tagToolState == TAG_TOOL_ANCHOR) { + m_previewTag->tagUpdated(Tag(m_proj->getNextAutoTagName(), event->scenePos())); + m_tagToolState = TAG_TOOL_LABEL; + + } else { /* TAG_TOOL_LABEL */ + removeItem(m_previewTag); + m_proj->createTag(m_previewTag->tag()); + m_tagToolState = TAG_TOOL_ANCHOR; + } + } +} diff --git a/tagscene.h b/tagscene.h index 18f9f54..bb83da4 100644 --- a/tagscene.h +++ b/tagscene.h @@ -3,6 +3,7 @@ #include "sqlitebackend.h" #include "tagitem.h" +#include "numberator.h" #include #include @@ -14,8 +15,8 @@ class TagScene : public QGraphicsScene Q_OBJECT public: - TagScene() {} - ~TagScene() { this->blockSignals(true); } + TagScene() : m_previewTag(new TagItem()) {} + ~TagScene() { this->blockSignals(true); delete m_previewTag; } const QGraphicsPixmapItem *backgroundPixmapItem() const { return pix_it; } public slots: @@ -23,6 +24,9 @@ public slots: void reloadScene(); void selectTag(const Tag &tag); + void setTool(ToolType tool); + void abortTool(); + void setProject(SQLiteSaveFile *proj); signals: @@ -31,6 +35,8 @@ signals: protected: void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override; + void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; + void mousePressEvent(QGraphicsSceneMouseEvent *event) override; private slots: void tagChanged(TagChange change, const Tag &tag); @@ -43,6 +49,12 @@ private: QGraphicsPixmapItem *pix_it = nullptr; QPixmap pix; QMap tags; + ToolType m_tool; + enum TagToolState { + TAG_TOOL_ANCHOR, + TAG_TOOL_LABEL + } m_tagToolState = TAG_TOOL_ANCHOR; + TagItem *m_previewTag; }; #endif // TAGSCENE_H diff --git a/tagview.cpp b/tagview.cpp index 9a9c9a0..6bc3934 100644 --- a/tagview.cpp +++ b/tagview.cpp @@ -3,12 +3,13 @@ #include #include #include +#include TagView::TagView(QWidget *parent) : QGraphicsView(parent) , saveCenterTimer(this) { - setDragMode(QGraphicsView::ScrollHandDrag); + setDragMode(QGraphicsView::RubberBandDrag); setScene(&m_scene); connect(&m_scene, &TagScene::tagDoubleClicked, this, &TagView::tagDoubleClicked); connect(&m_scene, &TagScene::imageLoaded, this, &TagView::zoomToFit); @@ -86,6 +87,31 @@ void TagView::scrollContentsBy(int dx, int dy) saveCenterTimer.start(); } +void TagView::keyPressEvent(QKeyEvent *event) +{ + if (event->modifiers() & Qt::ControlModifier) + setDragMode(QGraphicsView::ScrollHandDrag); + else + setDragMode(QGraphicsView::RubberBandDrag); +} + +void TagView::keyReleaseEvent(QKeyEvent *event) +{ + if (event->modifiers() & Qt::ControlModifier) + setDragMode(QGraphicsView::ScrollHandDrag); + else + setDragMode(QGraphicsView::RubberBandDrag); +} + +void TagView::focusInEvent(QFocusEvent *event) +{ + Q_UNUSED(event); + if (QApplication::keyboardModifiers() & Qt::ControlModifier) + setDragMode(QGraphicsView::ScrollHandDrag); + else + setDragMode(QGraphicsView::RubberBandDrag); +} + void TagView::saveCenter() { QPointF p = mapToScene(viewport()->rect().center()); diff --git a/tagview.h b/tagview.h index 12d1088..808b100 100644 --- a/tagview.h +++ b/tagview.h @@ -33,6 +33,9 @@ signals: protected: void wheelEvent(QWheelEvent *evt) override; void scrollContentsBy(int dx, int dy) override; + void keyPressEvent(QKeyEvent *event) override; + void keyReleaseEvent(QKeyEvent *event) override; + void focusInEvent(QFocusEvent *event) override; private slots: void saveCenter(); -- cgit