From 1a71fd8efa0350d1e121f6792e8fad67e82b25c1 Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Thu, 5 Apr 2018 13:20:20 +0200 Subject: fixed name conflict, now builds in MSVC --- src/gui/mainmenu.cpp | 6 ++- src/gui/nodeitem.cpp | 2 +- src/gui/tikzscene.cpp | 107 ++++++++++++++++++++++++++++++++++++----------- src/gui/tikzscene.h | 3 ++ src/gui/tikzview.cpp | 33 ++++++++------- src/gui/tikzview.h | 1 + src/gui/undocommands.cpp | 12 +++--- 7 files changed, 117 insertions(+), 47 deletions(-) (limited to 'src/gui') diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp index dfb447f..0166caf 100644 --- a/src/gui/mainmenu.cpp +++ b/src/gui/mainmenu.cpp @@ -80,12 +80,14 @@ void MainMenu::on_actionDelete_triggered() void MainMenu::on_actionSelect_All_triggered() { - // TODO + if (tikzit->activeWindow() != 0) + tikzit->activeWindow()->tikzScene()->selectAllNodes(); } void MainMenu::on_actionDeselect_All_triggered() { - // TODO + if (tikzit->activeWindow() != 0) + tikzit->activeWindow()->tikzScene()->deselectAll(); } diff --git a/src/gui/nodeitem.cpp b/src/gui/nodeitem.cpp index 36d488c..06b46ff 100644 --- a/src/gui/nodeitem.cpp +++ b/src/gui/nodeitem.cpp @@ -94,7 +94,7 @@ void NodeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge QPainterPath NodeItem::shape() const { QPainterPath path; - path.addEllipse(QPointF(0,0), GLOBAL_SCALEF * 0.1, GLOBAL_SCALEF * 0.1); + path.addEllipse(QPointF(0,0), GLOBAL_SCALEF * 0.2, GLOBAL_SCALEF * 0.2); return path; } diff --git a/src/gui/tikzscene.cpp b/src/gui/tikzscene.cpp index 59faa65..24f4ad1 100644 --- a/src/gui/tikzscene.cpp +++ b/src/gui/tikzscene.cpp @@ -8,6 +8,7 @@ #include #include #include +#include TikzScene::TikzScene(TikzDocument *tikzDocument, ToolPalette *tools, QObject *parent) : @@ -16,15 +17,28 @@ TikzScene::TikzScene(TikzDocument *tikzDocument, ToolPalette *tools, QObject *pa _modifyEdgeItem = 0; _edgeStartNodeItem = 0; _drawEdgeItem = new QGraphicsLineItem(); - setSceneRect(-310,-230,620,450); + _rubberBandItem = new QGraphicsRectItem(); + //setSceneRect(-310,-230,620,450); + setSceneRect(-1000,-1000,2000,2000); QPen pen; pen.setColor(QColor::fromRgbF(0.5f, 0.0f, 0.5f)); pen.setWidth(3); + pen.setCosmetic(true); _drawEdgeItem->setPen(pen); _drawEdgeItem->setLine(0,0,0,0); _drawEdgeItem->setVisible(false); addItem(_drawEdgeItem); + + pen.setColor(QColor::fromRgbF(0.6f, 0.6f, 0.8f)); + pen.setWidth(3); + QVector dash; + dash << 4.0 << 4.0; + pen.setDashPattern(dash); + _rubberBandItem->setPen(pen); + + _rubberBandItem->setVisible(false); + addItem(_rubberBandItem); } TikzScene::~TikzScene() { @@ -71,7 +85,7 @@ void TikzScene::mousePressEvent(QGraphicsSceneMouseEvent *event) // disable rubber band drag, which will clear the selection. Only re-enable it // for the SELECT tool, and when no control point has been clicked. - views()[0]->setDragMode(QGraphicsView::NoDrag); + //views()[0]->setDragMode(QGraphicsView::NoDrag); // radius of a control point for bezier edges, in scene coordinates qreal cpR = GLOBAL_SCALEF * (0.05); @@ -113,9 +127,23 @@ void TikzScene::mousePressEvent(QGraphicsSceneMouseEvent *event) _oldWeight = e->weight(); } else { // since we are not dragging a control point, process the click normally - views()[0]->setDragMode(QGraphicsView::RubberBandDrag); + //views()[0]->setDragMode(QGraphicsView::RubberBandDrag); QGraphicsScene::mousePressEvent(event); + if (items(_mouseDownPos).isEmpty()) { + _rubberBandItem->setRect(QRectF(_mouseDownPos,_mouseDownPos)); + _rubberBandItem->setVisible(true); + qDebug() << "starting rubber band drag"; + } + +// foreach (QGraphicsItem *gi, items()) { +// if (EdgeItem *ei = dynamic_cast(gi)) { +// //qDebug() << "got an edge item: " << ei; +// ei->setFlag(QGraphicsItem::ItemIsSelectable, false); +// //ei->setSelected(true); +// } +// } + // save current node positions for undo support _oldNodePositions.clear(); foreach (QGraphicsItem *gi, selectedItems()) { @@ -155,7 +183,7 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) QPointF mousePos = event->scenePos(); //QRectF rb = views()[0]->rubberBandRect(); //invalidate(-800,-800,1600,1600); - invalidate(QRectF(), QGraphicsScene::BackgroundLayer); + //invalidate(QRectF(), QGraphicsScene::BackgroundLayer); switch (_tools->currentTool()) { case ToolPalette::SELECT: @@ -244,8 +272,7 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) // apply the same offset to all nodes, otherwise we get odd rounding behaviour with // multiple selection. QPointF shift = mousePos - _mouseDownPos; - int gridSize = GLOBAL_SCALE / 8; - shift = QPointF(round(shift.x()/gridSize)*gridSize, round(shift.y()/gridSize)*gridSize); + shift = QPointF(round(shift.x()/GRID_SEP)*GRID_SEP, round(shift.y()/GRID_SEP)*GRID_SEP); foreach (Node *n, _oldNodePositions.keys()) { NodeItem *ni = _nodeItems[n]; @@ -257,6 +284,15 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } else { // otherwise, process mouse move normally QGraphicsScene::mouseMoveEvent(event); + + if (_rubberBandItem->isVisible()) { + qreal left = std::min(_mouseDownPos.x(), mousePos.x()); + qreal top = std::min(_mouseDownPos.y(), mousePos.y()); + qreal w = std::abs(_mouseDownPos.x() - mousePos.x()); + qreal h = std::abs(_mouseDownPos.y() - mousePos.y()); + + _rubberBandItem->setRect(QRectF(left, top, w, h)); + } } break; @@ -308,6 +344,19 @@ void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) // otherwise, process mouse move normally QGraphicsScene::mouseReleaseEvent(event); + if (_rubberBandItem->isVisible()) { + QPainterPath sel; + sel.addRect(_rubberBandItem->rect()); + foreach (QGraphicsItem *gi, items()) { + if (NodeItem *ni = dynamic_cast(gi)) { + if (sel.contains(toScreen(ni->node()->point()))) ni->setSelected(true); + } + } + //setSelectionArea(sel); + } + + _rubberBandItem->setVisible(false); + if (!_oldNodePositions.empty()) { QMap newNodePositions; @@ -329,8 +378,7 @@ void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) break; case ToolPalette::VERTEX: { - int gridSize = GLOBAL_SCALE / 8; - QPointF gridPos(round(mousePos.x()/gridSize)*gridSize, round(mousePos.y()/gridSize)*gridSize); + QPointF gridPos(round(mousePos.x()/GRID_SEP)*GRID_SEP, round(mousePos.y()/GRID_SEP)*GRID_SEP); Node *n = new Node(_tikzDocument); n->setName(graph()->freshNodeName()); n->setPoint(fromScreen(gridPos)); @@ -460,7 +508,6 @@ void TikzScene::pasteFromClipboard() QRectF tgtRect = graph()->realBbox(); QPointF shift(tgtRect.right() - srcRect.left(), 0.0f); - // shift g to the right until it is in free space if (shift.x() > 0) { foreach (Node *n, g->nodes()) { n->setPoint(n->point() + shift); @@ -472,6 +519,18 @@ void TikzScene::pasteFromClipboard() } } +void TikzScene::selectAllNodes() +{ + foreach (NodeItem *ni, _nodeItems.values()) { + ni->setSelected(true); + } +} + +void TikzScene::deselectAll() +{ + selectedItems().clear(); +} + void TikzScene::getSelection(QSet &selNodes, QSet &selEdges) { foreach (QGraphicsItem *gi, selectedItems()) { @@ -511,21 +570,21 @@ void TikzScene::reloadStyles() void TikzScene::refreshSceneBounds() { - if (!views().empty()) { - QGraphicsView *v = views().first(); - QRectF viewB = v->mapToScene(v->viewport()->rect()).boundingRect(); - //QPointF tl = v->mapToScene(viewB.topLeft()); - //viewB.setTopLeft(tl); - - QRectF bounds = viewB.united(rectToScreen(graph()->realBbox().adjusted(-1.0f, -1.0f, 1.0f, 1.0f))); - qDebug() << viewB; - - if (bounds != sceneRect()) { - QPointF c = viewB.center(); - setSceneRect(bounds); - v->centerOn(c); - } - } +// if (!views().empty()) { +// QGraphicsView *v = views().first(); +// QRectF viewB = v->mapToScene(v->viewport()->rect()).boundingRect(); +// //QPointF tl = v->mapToScene(viewB.topLeft()); +// //viewB.setTopLeft(tl); + +// QRectF bounds = viewB.united(rectToScreen(graph()->realBbox().adjusted(-1.0f, -1.0f, 1.0f, 1.0f))); +// //qDebug() << viewB; + +// if (bounds != sceneRect()) { +// QPointF c = viewB.center(); +// setSceneRect(bounds); +// v->centerOn(c); +// } +// } //setBounds(graphB); } diff --git a/src/gui/tikzscene.h b/src/gui/tikzscene.h index b551abd..f7735ee 100644 --- a/src/gui/tikzscene.h +++ b/src/gui/tikzscene.h @@ -41,6 +41,8 @@ public: void copyToClipboard(); void cutToClipboard(); void pasteFromClipboard(); + void selectAllNodes(); + void deselectAll(); public slots: void graphReplaced(); @@ -56,6 +58,7 @@ private: QMap _nodeItems; QMap _edgeItems; QGraphicsLineItem *_drawEdgeItem; + QGraphicsRectItem *_rubberBandItem; EdgeItem *_modifyEdgeItem; NodeItem *_edgeStartNodeItem; NodeItem *_edgeEndNodeItem; diff --git a/src/gui/tikzview.cpp b/src/gui/tikzview.cpp index b8ae1c0..60dc2bc 100644 --- a/src/gui/tikzview.cpp +++ b/src/gui/tikzview.cpp @@ -7,7 +7,7 @@ TikzView::TikzView(QWidget *parent) : QGraphicsView(parent) { setRenderHint(QPainter::Antialiasing); - setDragMode(QGraphicsView::RubberBandDrag); + //setDragMode(QGraphicsView::RubberBandDrag); _scale = 1.0f; } @@ -24,10 +24,15 @@ void TikzView::zoomOut() scale(0.625,0.625); } +void TikzView::setScene(QGraphicsScene *scene) +{ + QGraphicsView::setScene(scene); + centerOn(QPointF(0.0f,-230.0f)); +} + void TikzView::drawBackground(QPainter *painter, const QRectF &rect) { // draw the grid - int step = GLOBAL_SCALE / 8; QPen pen1; pen1.setWidth(1); @@ -43,38 +48,38 @@ void TikzView::drawBackground(QPainter *painter, const QRectF &rect) painter->setPen(pen1); if (_scale > 0.2f) { - for (int x = -step; x > rect.left(); x -= step) { - if (x % (step * 8) != 0) painter->drawLine(x, rect.top(), x, rect.bottom()); + for (int x = -GRID_SEP; x > rect.left(); x -= GRID_SEP) { + if (x % (GRID_SEP * GRID_N) != 0) painter->drawLine(x, rect.top(), x, rect.bottom()); } - for (int x = step; x < rect.right(); x += step) { - if (x % (step * 8) != 0) painter->drawLine(x, rect.top(), x, rect.bottom()); + for (int x = GRID_SEP; x < rect.right(); x += GRID_SEP) { + if (x % (GRID_SEP * GRID_N) != 0) painter->drawLine(x, rect.top(), x, rect.bottom()); } - for (int y = -step; y > rect.top(); y -= step) { - if (y % (step * 8) != 0) painter->drawLine(rect.left(), y, rect.right(), y); + for (int y = -GRID_SEP; y > rect.top(); y -= GRID_SEP) { + if (y % (GRID_SEP * GRID_N) != 0) painter->drawLine(rect.left(), y, rect.right(), y); } - for (int y = step; y < rect.bottom(); y += step) { - if (y % (step * 8) != 0) painter->drawLine(rect.left(), y, rect.right(), y); + for (int y = GRID_SEP; y < rect.bottom(); y += GRID_SEP) { + if (y % (GRID_SEP * GRID_N) != 0) painter->drawLine(rect.left(), y, rect.right(), y); } } painter->setPen(pen2); - for (int x = -step*8; x > rect.left(); x -= step*8) { + for (int x = -GRID_SEP*GRID_N; x > rect.left(); x -= GRID_SEP*GRID_N) { painter->drawLine(x, rect.top(), x, rect.bottom()); } - for (int x = step*8; x < rect.right(); x += step*8) { + for (int x = GRID_SEP*GRID_N; x < rect.right(); x += GRID_SEP*GRID_N) { painter->drawLine(x, rect.top(), x, rect.bottom()); } - for (int y = -step*8; y > rect.top(); y -= step*8) { + for (int y = -GRID_SEP*GRID_N; y > rect.top(); y -= GRID_SEP*GRID_N) { painter->drawLine(rect.left(), y, rect.right(), y); } - for (int y = step*8; y < rect.bottom(); y += step*8) { + for (int y = GRID_SEP*GRID_N; y < rect.bottom(); y += GRID_SEP*GRID_N) { painter->drawLine(rect.left(), y, rect.right(), y); } diff --git a/src/gui/tikzview.h b/src/gui/tikzview.h index f89729b..cb41fd4 100644 --- a/src/gui/tikzview.h +++ b/src/gui/tikzview.h @@ -23,6 +23,7 @@ public: public slots: void zoomIn(); void zoomOut(); + void setScene(QGraphicsScene *scene); protected: void drawBackground(QPainter *painter, const QRectF &rect); private: diff --git a/src/gui/undocommands.cpp b/src/gui/undocommands.cpp index c9ca041..f64b1db 100644 --- a/src/gui/undocommands.cpp +++ b/src/gui/undocommands.cpp @@ -312,6 +312,12 @@ void PasteCommand::redo() _scene->clearSelection(); _scene->graph()->insertGraph(_graph); + foreach (Edge *e, _graph->edges()) { + EdgeItem *ei = new EdgeItem(e); + _scene->edgeItems().insert(e, ei); + _scene->addItem(ei); + } + foreach (Node *n, _graph->nodes()) { n->attachStyle(); // in case styles have changed NodeItem *ni = new NodeItem(n); @@ -320,11 +326,5 @@ void PasteCommand::redo() ni->setSelected(true); } - foreach (Edge *e, _graph->edges()) { - EdgeItem *ei = new EdgeItem(e); - _scene->edgeItems().insert(e, ei); - _scene->addItem(ei); - } - GraphUpdateCommand::redo(); } -- cgit v1.2.3