Skip to content

Commit c7c8f51

Browse files
committed
Introduce EdgeModel
1 parent 094cf9d commit c7c8f51

File tree

14 files changed

+104
-70
lines changed

14 files changed

+104
-70
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(LIB_SRC
2828
edge.cpp
2929
edge_context_menu.cpp
3030
edge_dot.cpp
31+
edge_model.hpp
3132
edge_point.hpp
3233
edge_text_edit.cpp
3334
editor_data.cpp

src/alz_file_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static EdgeU readEdge(const QDomElement & element, MindMapData & data)
391391

392392
// Initialize a new edge. QGraphicsScene will take the ownership eventually.
393393
auto edge = std::make_unique<Edge>(data.graph().getNode(index0), data.graph().getNode(index1));
394-
edge->setArrowMode(static_cast<Edge::ArrowMode>(arrowMode));
394+
edge->setArrowMode(static_cast<EdgeModel::ArrowMode>(arrowMode));
395395
edge->setDashedLine(dashedLine);
396396
edge->setReversed(reversed);
397397

src/dialogs/defaults_tab.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ void DefaultsTab::createEdgeWidgets(QVBoxLayout & mainLayout)
7676
const auto edgeGroup = WidgetFactory::buildGroupBoxWithVLayout(tr("Edge Arrow Style"), mainLayout);
7777

7878
m_edgeArrowStyleRadioMap = {
79-
{ Edge::ArrowMode::Hidden, new QRadioButton(tr("No arrow")) },
80-
{ Edge::ArrowMode::Single, new QRadioButton(tr("Single arrow")) },
81-
{ Edge::ArrowMode::Double, new QRadioButton(tr("Double arrow")) }
79+
{ EdgeModel::ArrowMode::Hidden, new QRadioButton(tr("No arrow")) },
80+
{ EdgeModel::ArrowMode::Single, new QRadioButton(tr("Single arrow")) },
81+
{ EdgeModel::ArrowMode::Double, new QRadioButton(tr("Double arrow")) }
8282
};
8383

8484
const auto edgeArrowRadioGroup = new QButtonGroup;

src/dialogs/defaults_tab.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DefaultsTab : public QWidget
4444

4545
void setActiveDefaults();
4646

47-
std::map<Edge::ArrowMode, QRadioButton *> m_edgeArrowStyleRadioMap;
47+
std::map<EdgeModel::ArrowMode, QRadioButton *> m_edgeArrowStyleRadioMap;
4848

4949
QCheckBox * m_edgeDirectionCheckBox = nullptr;
5050

src/edge.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@
4242
#include <cmath>
4343

4444
Edge::Edge(NodeP sourceNode, NodeP targetNode, bool enableAnimations, bool enableLabel)
45-
: m_sourceNode(sourceNode)
45+
: m_edgeModel(std::make_unique<EdgeModel>(SettingsProxy::instance().reversedEdgeDirection(), SettingsProxy::instance().edgeArrowMode()))
46+
, m_sourceNode(sourceNode)
4647
, m_targetNode(targetNode)
47-
, m_reversed(SettingsProxy::instance().reversedEdgeDirection())
48-
, m_arrowMode(SettingsProxy::instance().edgeArrowMode())
4948
, m_enableAnimations(enableAnimations)
5049
, m_enableLabel(enableLabel)
5150
, m_sourceDot(enableAnimations ? new EdgeDot(this) : nullptr)
@@ -72,7 +71,7 @@ Edge::Edge(NodeP sourceNode, NodeP targetNode, bool enableAnimations, bool enabl
7271
m_label->setBackgroundColor(Constants::Edge::LABEL_COLOR);
7372
connect(m_label, &TextEdit::textChanged, this, [=](const QString & text) {
7473
updateLabel();
75-
m_text = text;
74+
m_edgeModel->text = text;
7675
});
7776

7877
connect(m_label, &TextEdit::undoPointRequested, this, &Edge::undoPointRequested);
@@ -137,19 +136,17 @@ QPen Edge::buildPen(bool ignoreDashSetting) const
137136
{
138137
QPen pen { QBrush { QColor { m_color.red(), m_color.green(), m_color.blue() } }, m_edgeWidth };
139138
pen.setCapStyle(Qt::PenCapStyle::RoundCap);
140-
if (!ignoreDashSetting && m_dashedLine) {
139+
if (!ignoreDashSetting && m_edgeModel->dashedLine) {
141140
pen.setDashPattern(Constants::Edge::DASH_PATTERN);
142141
}
143142
return pen;
144143
}
145144

146145
void Edge::copyData(EdgeCR other)
147146
{
148-
m_arrowMode = other.m_arrowMode;
149-
m_dashedLine = other.m_dashedLine;
150-
m_reversed = other.m_reversed;
147+
*m_edgeModel = *other.m_edgeModel;
151148

152-
setText(other.m_text); // Update text to the label component
149+
setText(other.m_edgeModel->text); // Update text to the label component
153150
}
154151

155152
void Edge::changeFont(const QFont & font)
@@ -167,7 +164,7 @@ void Edge::changeFont(const QFont & font)
167164

168165
bool Edge::dashedLine() const
169166
{
170-
return m_dashedLine;
167+
return m_edgeModel->dashedLine;
171168
}
172169

173170
void Edge::initDots()
@@ -254,9 +251,9 @@ void Edge::setEdgeWidth(double edgeWidth)
254251
updateLine();
255252
}
256253

257-
void Edge::setArrowMode(ArrowMode arrowMode)
254+
void Edge::setArrowMode(EdgeModel::ArrowMode arrowMode)
258255
{
259-
m_arrowMode = arrowMode;
256+
m_edgeModel->arrowMode = arrowMode;
260257
if (!TestMode::enabled()) {
261258
updateLine();
262259
} else {
@@ -280,7 +277,7 @@ void Edge::setColor(const QColor & color)
280277

281278
void Edge::setDashedLine(bool enable)
282279
{
283-
m_dashedLine = enable;
280+
m_edgeModel->dashedLine = enable;
284281
if (!TestMode::enabled()) {
285282
updateLine();
286283
} else {
@@ -290,7 +287,7 @@ void Edge::setDashedLine(bool enable)
290287

291288
void Edge::setText(const QString & text)
292289
{
293-
m_text = text;
290+
m_edgeModel->text = text;
294291
if (!TestMode::enabled()) {
295292
if (m_enableLabel) {
296293
m_label->setText(text);
@@ -311,7 +308,7 @@ void Edge::setTextSize(int textSize)
311308

312309
void Edge::setReversed(bool reversed)
313310
{
314-
m_reversed = reversed;
311+
m_edgeModel->reversed = reversed;
315312

316313
updateArrowhead();
317314
}
@@ -349,18 +346,19 @@ void Edge::updateArrowhead()
349346
{
350347
setArrowHeadPen(buildPen(true));
351348

352-
const auto point0 = m_reversed ? this->line().p1() : this->line().p2();
353-
const auto angle0 = m_reversed ? -this->line().angle() + 180 : -this->line().angle();
354-
const auto point1 = m_reversed ? this->line().p2() : this->line().p1();
355-
const auto angle1 = m_reversed ? -this->line().angle() : -this->line().angle() + 180;
349+
const auto reversed = m_edgeModel->reversed;
350+
const auto point0 = reversed ? this->line().p1() : this->line().p2();
351+
const auto angle0 = reversed ? -this->line().angle() + 180 : -this->line().angle();
352+
const auto point1 = reversed ? this->line().p2() : this->line().p1();
353+
const auto angle1 = reversed ? -this->line().angle() : -this->line().angle() + 180;
356354

357355
QLineF lineL0;
358356
QLineF lineR0;
359357
QLineF lineL1;
360358
QLineF lineR1;
361359

362-
switch (m_arrowMode) {
363-
case ArrowMode::Single: {
360+
switch (m_edgeModel->arrowMode) {
361+
case EdgeModel::ArrowMode::Single: {
364362
lineL0.setP1(point0);
365363
const auto angleL = qDegreesToRadians(angle0 + Constants::Edge::ARROW_OPENING);
366364
lineL0.setP2(point0 + QPointF(std::cos(angleL), std::sin(angleL)) * m_arrowSize);
@@ -375,7 +373,7 @@ void Edge::updateArrowhead()
375373
m_arrowheadR1->hide();
376374
break;
377375
}
378-
case ArrowMode::Double: {
376+
case EdgeModel::ArrowMode::Double: {
379377
lineL0.setP1(point0);
380378
const auto angleL0 = qDegreesToRadians(angle0 + Constants::Edge::ARROW_OPENING);
381379
lineL0.setP2(point0 + QPointF(std::cos(angleL0), std::sin(angleL0)) * m_arrowSize);
@@ -398,7 +396,7 @@ void Edge::updateArrowhead()
398396
m_arrowheadR1->show();
399397
break;
400398
}
401-
case ArrowMode::Hidden:
399+
case EdgeModel::ArrowMode::Hidden:
402400
m_arrowheadL0->hide();
403401
m_arrowheadR0->hide();
404402
m_arrowheadL1->hide();
@@ -458,7 +456,7 @@ void Edge::setSourceNode(NodeR sourceNode)
458456

459457
bool Edge::reversed() const
460458
{
461-
return m_reversed;
459+
return m_edgeModel->reversed;
462460
}
463461

464462
void Edge::restoreLabelParent()
@@ -468,14 +466,14 @@ void Edge::restoreLabelParent()
468466
}
469467
}
470468

471-
Edge::ArrowMode Edge::arrowMode() const
469+
EdgeModel::ArrowMode Edge::arrowMode() const
472470
{
473-
return m_arrowMode;
471+
return m_edgeModel->arrowMode;
474472
}
475473

476474
QString Edge::text() const
477475
{
478-
return m_text;
476+
return m_edgeModel->text;
479477
}
480478

481479
void Edge::updateLine()

src/edge.hpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <memory>
2424

2525
#include "constants.hpp"
26+
#include "edge_model.hpp"
2627
#include "edge_point.hpp"
2728
#include "edge_text_edit.hpp"
2829
#include "types.hpp"
@@ -33,6 +34,7 @@ class Node;
3334
class QFont;
3435
class QGraphicsEllipseItem;
3536
class QPropertyAnimation;
37+
3638
struct ShadowEffectParams;
3739

3840
//! A graphic representation of a graph edge between nodes.
@@ -41,12 +43,6 @@ class Edge : public QObject, public QGraphicsLineItem
4143
Q_OBJECT
4244

4345
public:
44-
enum class ArrowMode
45-
{
46-
Single = 0,
47-
Double = 1,
48-
Hidden = 2
49-
};
5046

5147
//! Constructor.
5248
//! Note!!: We are using raw pointers here because the edge only must only refer to the nodes.
@@ -64,7 +60,7 @@ class Edge : public QObject, public QGraphicsLineItem
6460

6561
virtual ~Edge() override;
6662

67-
ArrowMode arrowMode() const;
63+
EdgeModel::ArrowMode arrowMode() const;
6864

6965
bool dashedLine() const;
7066

@@ -92,7 +88,7 @@ public slots:
9288

9389
void updateLine();
9490

95-
void setArrowMode(ArrowMode arrowMode);
91+
void setArrowMode(EdgeModel::ArrowMode arrowMode);
9692

9793
void setArrowSize(double arrowSize);
9894

@@ -139,12 +135,12 @@ public slots:
139135

140136
void updateLabel(LabelUpdateReason lur = LabelUpdateReason::Default);
141137

138+
std::unique_ptr<EdgeModel> m_edgeModel;
139+
142140
NodeP m_sourceNode = nullptr;
143141

144142
NodeP m_targetNode = nullptr;
145143

146-
QString m_text;
147-
148144
double m_arrowSize = Constants::Edge::Defaults::ARROW_SIZE;
149145

150146
double m_edgeWidth = Constants::MindMap::Defaults::EDGE_WIDTH;
@@ -153,14 +149,8 @@ public slots:
153149

154150
QColor m_color;
155151

156-
bool m_reversed;
157-
158152
bool m_selected = false;
159153

160-
ArrowMode m_arrowMode;
161-
162-
bool m_dashedLine = false;
163-
164154
bool m_enableAnimations;
165155

166156
bool m_enableLabel;

src/edge_context_menu.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ EdgeContextMenu::EdgeContextMenu(QWidget * parent, Mediator & mediator)
3333
const auto showEdgeArrowAction(new QAction(tr("Show arrow"), this));
3434
QObject::connect(showEdgeArrowAction, &QAction::triggered, this, [=] {
3535
m_mediator.saveUndoPoint();
36-
m_mediator.selectedEdge()->setArrowMode(showEdgeArrowAction->isChecked() ? Edge::ArrowMode::Single : Edge::ArrowMode::Hidden);
36+
m_mediator.selectedEdge()->setArrowMode(showEdgeArrowAction->isChecked() ? EdgeModel::ArrowMode::Single : EdgeModel::ArrowMode::Hidden);
3737
});
3838
showEdgeArrowAction->setCheckable(true);
3939

4040
const auto doubleArrowAction(new QAction(tr("Double arrow"), this));
4141
QObject::connect(doubleArrowAction, &QAction::triggered, this, [=] {
4242
m_mediator.saveUndoPoint();
43-
m_mediator.selectedEdge()->setArrowMode(doubleArrowAction->isChecked() ? Edge::ArrowMode::Double : Edge::ArrowMode::Single);
43+
m_mediator.selectedEdge()->setArrowMode(doubleArrowAction->isChecked() ? EdgeModel::ArrowMode::Double : EdgeModel::ArrowMode::Single);
4444
});
4545
doubleArrowAction->setCheckable(true);
4646

@@ -76,10 +76,10 @@ EdgeContextMenu::EdgeContextMenu(QWidget * parent, Mediator & mediator)
7676
connect(this, &QMenu::aboutToShow, this, [=] {
7777
m_selectedEdge = m_mediator.selectedEdge();
7878
assert(m_selectedEdge);
79-
changeEdgeDirectionAction->setEnabled(m_selectedEdge->arrowMode() != Edge::ArrowMode::Double && m_selectedEdge->arrowMode() != Edge::ArrowMode::Hidden);
79+
changeEdgeDirectionAction->setEnabled(m_selectedEdge->arrowMode() != EdgeModel::ArrowMode::Double && m_selectedEdge->arrowMode() != EdgeModel::ArrowMode::Hidden);
8080
dashedLineAction->setChecked(m_selectedEdge->dashedLine());
81-
doubleArrowAction->setChecked(m_selectedEdge->arrowMode() == Edge::ArrowMode::Double);
82-
showEdgeArrowAction->setChecked(m_selectedEdge->arrowMode() != Edge::ArrowMode::Hidden);
81+
doubleArrowAction->setChecked(m_selectedEdge->arrowMode() == EdgeModel::ArrowMode::Double);
82+
showEdgeArrowAction->setChecked(m_selectedEdge->arrowMode() != EdgeModel::ArrowMode::Hidden);
8383
});
8484

8585
// Always clear edge selection when the menu closes.

src/edge_model.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// This file is part of Heimer.
2+
// Copyright (C) 2022 Jussi Lind <[email protected]>
3+
//
4+
// Heimer is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
// Heimer is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Heimer. If not, see <http://www.gnu.org/licenses/>.
15+
16+
#ifndef EDGE_MODEL_HPP
17+
#define EDGE_MODEL_HPP
18+
19+
#include <QString>
20+
21+
struct EdgeModel
22+
{
23+
enum class ArrowMode
24+
{
25+
Single = 0,
26+
Double = 1,
27+
Hidden = 2
28+
};
29+
30+
EdgeModel(bool reversed, ArrowMode arrowMode)
31+
: arrowMode(arrowMode)
32+
, reversed(reversed)
33+
{
34+
}
35+
36+
ArrowMode arrowMode;
37+
38+
bool dashedLine = false;
39+
40+
bool reversed;
41+
42+
QString text;
43+
};
44+
45+
#endif // EDGE_MODEL_HPP

src/settings.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ void saveAutoSnapState(int state)
9292
settings.endGroup();
9393
}
9494

95-
Edge::ArrowMode loadEdgeArrowMode(Edge::ArrowMode defaultMode)
95+
EdgeModel::ArrowMode loadEdgeArrowMode(EdgeModel::ArrowMode defaultMode)
9696
{
9797
QSettings settings;
9898
settings.beginGroup(Constants::MindMap::Defaults::SETTINGS_GROUP);
99-
const auto mode = static_cast<Edge::ArrowMode>(settings.value(edgeArrowModeKey, static_cast<int>(defaultMode)).toInt());
99+
const auto mode = static_cast<EdgeModel::ArrowMode>(settings.value(edgeArrowModeKey, static_cast<int>(defaultMode)).toInt());
100100
settings.endGroup();
101101
return mode;
102102
}
103103

104-
void saveEdgeArrowMode(Edge::ArrowMode mode)
104+
void saveEdgeArrowMode(EdgeModel::ArrowMode mode)
105105
{
106106
QSettings settings;
107107
settings.beginGroup(Constants::MindMap::Defaults::SETTINGS_GROUP);

src/settings.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Qt::CheckState loadAutoSnapState();
3232

3333
void saveAutoSnapState(int state);
3434

35-
Edge::ArrowMode loadEdgeArrowMode(Edge::ArrowMode defaultMode);
35+
EdgeModel::ArrowMode loadEdgeArrowMode(EdgeModel::ArrowMode defaultMode);
3636

37-
void saveEdgeArrowMode(Edge::ArrowMode mode);
37+
void saveEdgeArrowMode(EdgeModel::ArrowMode mode);
3838

3939
bool loadReversedEdgeDirection(bool defaultDirection);
4040

0 commit comments

Comments
 (0)