Skip to content

Commit

Permalink
#2074 note-relations: draw linked notes
Browse files Browse the repository at this point in the history
Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Feb 25, 2025
1 parent bcc7fca commit 6683851
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,10 @@ void MainWindow::togglePanelVisibility(const QString &objectName) {
newVisibility =
NoteFolder::isCurrentShowSubfolders() && !Utils::Misc::isEnableNoteTree();
}
} else if (objectName == QStringLiteral("noteGraphicsViewDockWidget")) {
if (newVisibility) {
_noteRelationScene->drawForNote(currentNote);
}
}

dockWidget->setVisible(newVisibility);
Expand Down
35 changes: 23 additions & 12 deletions src/widgets/noterelationscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
#include <QDebug>

// NoteItem Implementation
NoteItem::NoteItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent)
NoteItem::NoteItem(const QString &noteName, qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent)
: QGraphicsRectItem(x, y, width, height, parent) {
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);

setBrush(QBrush(Qt::white));
setPen(QPen(Qt::black, 2));
_noteName = noteName;
}

QVariant NoteItem::itemChange(GraphicsItemChange change, const QVariant &value) {
Expand All @@ -35,7 +36,7 @@ QVariant NoteItem::itemChange(GraphicsItemChange change, const QVariant &value)

void NoteItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
QGraphicsRectItem::paint(painter, option, widget);
painter->drawText(rect().adjusted(5, 5, -5, -5), Qt::AlignCenter, "Note");
painter->drawText(rect().adjusted(5, 5, -5, -5), Qt::AlignCenter, _noteName);
}

void NoteItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
Expand All @@ -61,6 +62,7 @@ ConnectionLine::ConnectionLine(NoteItem *startItem, NoteItem *endItem, QGraphics
void ConnectionLine::updatePosition() {
if (!m_startItem || !m_endItem) return;

// TODO: Fix crash after scene was cleared and new items were created
QPointF startCenter = m_startItem->rect().center() + m_startItem->pos();
// TODO: Fix crash after scene was cleared and new items were created
QPointF endCenter = m_endItem->rect().center() + m_endItem->pos();
Expand Down Expand Up @@ -132,8 +134,9 @@ void NoteRelationScene::createNote(const QPointF &pos, const QString &noteName)
Q_UNUSED(noteName)

// TODO: Handle memory leak
auto *note = new NoteItem(0, 0, 100, 60);
auto *note = new NoteItem(noteName, 0, 0, 100, 60);
note->setPos(pos - QPointF(50, 30));
// The scene is taking ownership over the note item
addItem(note);
}

Expand All @@ -144,17 +147,25 @@ void NoteRelationScene::createConnection(NoteItem *startItem, NoteItem *endItem)
m_connections.push_back(connection);
}

void NoteRelationScene::drawForNote(const Note &note) {
void NoteRelationScene::drawForNote(Note &note) {
clear();
qDebug() << __func__ << " - 'note': " << note;

// TODO: Draw note item from note data
// TODO: Write note name on note item
createNote(QPointF(100, 100));
createNote(QPointF(300, 100));
createNote(QPointF(200, 200));
createConnection(dynamic_cast<NoteItem *>(items().at(0)),
dynamic_cast<NoteItem *>(items().at(1)));
createConnection(dynamic_cast<NoteItem *>(items().at(1)),
dynamic_cast<NoteItem *>(items().at(2)));
createNote(QPointF(100, 100), note.getName());
auto linkedNotes = note.findLinkedNotes();

for (const auto& linkedNote : linkedNotes.keys()) {
// TODO: Spread note around the root note
createNote(QPointF(200, 200), linkedNote.getName());
createConnection(dynamic_cast<NoteItem *>(items().at(0)),
dynamic_cast<NoteItem *>(items().at(1)));
}

// createNote(QPointF(300, 100));
// createNote(QPointF(200, 200));
// createConnection(dynamic_cast<NoteItem *>(items().at(0)),
// dynamic_cast<NoteItem *>(items().at(1)));
// createConnection(dynamic_cast<NoteItem *>(items().at(1)),
// dynamic_cast<NoteItem *>(items().at(2)));
}
5 changes: 3 additions & 2 deletions src/widgets/noterelationscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ class ConnectionLine;
// Note rectangle item representing a note
class NoteItem : public QGraphicsRectItem {
public:
NoteItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr);
NoteItem(const QString &noteName, qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr);

protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
QString _noteName;

private:
void emit_position_changed();
Expand All @@ -56,7 +57,7 @@ class NoteRelationScene : public QGraphicsScene {

public:
explicit NoteRelationScene(QObject *parent = nullptr);
void drawForNote(const Note &note);
void drawForNote(Note &note);

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
Expand Down

0 comments on commit 6683851

Please sign in to comment.