Line data Source code
1 : /// Including QT Headers
2 : #include <QApplication>
3 : #include <QCursor>
4 : #include <QEvent>
5 : #include <QGraphicsScene>
6 : #include <QGraphicsSceneHoverEvent>
7 : #include <QGraphicsView>
8 : #include <QPainter>
9 : #include <QPainterPath>
10 : #include <QPen>
11 :
12 : #include <QToolTip>
13 :
14 : #include "dbe/SchemaGraphicNote.hpp"
15 : #include "dbe/SchemaGraphicsScene.hpp"
16 : #include "dbe/SchemaNoteEditor.hpp"
17 : #include "dbe/SchemaStyle.hpp"
18 :
19 : #include <iostream>
20 :
21 : namespace dbse {
22 :
23 0 : SchemaGraphicNote::SchemaGraphicNote(const QString& name, const QString& text, QGraphicsObject* parent)
24 : : QGraphicsObject(parent)
25 0 : , m_name(name)
26 0 : , m_text(text)
27 : {
28 :
29 0 : setAcceptHoverEvents(true);
30 :
31 0 : setFlag(ItemIsMovable);
32 0 : setFlag(ItemSendsGeometryChanges, true);
33 0 : setFlag(ItemSendsScenePositionChanges, true);
34 0 : }
35 :
36 : void
37 0 : SchemaGraphicNote::open_editor()
38 : {
39 0 : bool editorFound = false;
40 :
41 0 : for (auto widget : QApplication::allWidgets()) {
42 0 : auto editor = dynamic_cast<SchemaNoteEditor*>(widget);
43 0 : if (editor != nullptr) {
44 0 : if ((editor->objectName()).compare(m_name) == 0) {
45 0 : editor->raise();
46 0 : editor->setVisible(true);
47 0 : editor->activateWindow();
48 : editorFound = true;
49 : }
50 : }
51 0 : }
52 :
53 0 : if (!editorFound) {
54 0 : auto editor = new SchemaNoteEditor(this);
55 0 : editor->show();
56 : }
57 0 : }
58 :
59 : void
60 0 : SchemaGraphicNote::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*)
61 : {
62 0 : open_editor();
63 0 : }
64 :
65 : void
66 0 : SchemaGraphicNote::update_note(QString text)
67 : {
68 0 : auto this_scene = dynamic_cast<SchemaGraphicsScene*>(scene());
69 0 : this_scene->remove_note_object(this);
70 0 : m_text = text;
71 0 : this_scene->addItem(this);
72 0 : emit updated();
73 0 : }
74 :
75 : QRectF
76 0 : SchemaGraphicNote::boundingRect() const
77 : {
78 0 : QFontMetrics font_metrics(SchemaStyle::get_font("note"));
79 0 : double height = font_metrics.height() / 2;
80 0 : double width = 0;
81 0 : for (auto line : m_text.split("\n")) {
82 0 : height += font_metrics.height();
83 0 : QRectF font_rect = font_metrics.boundingRect(line);
84 0 : if (font_rect.width() > width) {
85 : width = font_rect.width();
86 : }
87 0 : }
88 0 : width += font_metrics.averageCharWidth() * 2;
89 :
90 0 : return QRectF(0, 0, width, height);
91 : // return font_metrics.boundingRect(m_text);
92 0 : }
93 :
94 : void
95 0 : SchemaGraphicNote::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
96 : {
97 :
98 0 : const QPen pen(SchemaStyle::get_color("foreground", "note"), 0.5);
99 0 : const QBrush brush(SchemaStyle::get_color("background", "note"));
100 0 : painter->setFont(SchemaStyle::get_font("note"));
101 0 : painter->setBrush(brush);
102 : // painter->setBackgroundMode(Qt::OpaqueMode);
103 0 : painter->setPen(pen);
104 0 : const auto rect = boundingRect();
105 0 : painter->drawRect(rect);
106 0 : painter->drawText(rect.adjusted(5, 5, -2, -2), Qt::AlignJustify, m_text);
107 0 : }
108 :
109 : QPainterPath
110 0 : SchemaGraphicNote::shape() const
111 : {
112 0 : QPainterPath path;
113 0 : path.addRect(boundingRect());
114 0 : return path;
115 0 : }
116 : } // namespace dbse
|