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,
24 : const QString& text,
25 0 : QGraphicsObject* parent)
26 : : QGraphicsObject (parent),
27 0 : m_name(name),
28 0 : m_text(text) {
29 :
30 0 : setAcceptHoverEvents(true);
31 :
32 0 : setFlag ( ItemIsMovable );
33 0 : setFlag ( ItemSendsGeometryChanges, true );
34 0 : setFlag ( ItemSendsScenePositionChanges, true );
35 :
36 0 : }
37 :
38 0 : void SchemaGraphicNote::open_editor() {
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 0 : void SchemaGraphicNote::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent* ) {
60 0 : open_editor();
61 0 : }
62 :
63 :
64 0 : void SchemaGraphicNote::update_note (QString text) {
65 0 : auto this_scene = dynamic_cast<SchemaGraphicsScene*>(scene());
66 0 : this_scene->remove_note_object(this);
67 0 : m_text = text;
68 0 : this_scene->addItem(this);
69 0 : emit updated();
70 0 : }
71 :
72 0 : QRectF SchemaGraphicNote::boundingRect() const
73 : {
74 0 : QFontMetrics font_metrics(SchemaStyle::get_font("note"));
75 0 : double height = font_metrics.height()/2;
76 0 : double width = 0;
77 0 : for (auto line: m_text.split("\n")) {
78 0 : height += font_metrics.height();
79 0 : QRectF font_rect = font_metrics.boundingRect(line);
80 0 : if (font_rect.width() > width) {
81 0 : width = font_rect.width();
82 : }
83 0 : }
84 0 : width += font_metrics.averageCharWidth()*2;
85 :
86 0 : return QRectF(0, 0, width, height);
87 : //return font_metrics.boundingRect(m_text);
88 0 : }
89 :
90 0 : void SchemaGraphicNote::paint (QPainter* painter,
91 : const QStyleOptionGraphicsItem* /*option*/,
92 : QWidget* /*widget*/ ) {
93 :
94 0 : const QPen pen(SchemaStyle::get_color("foreground", "note"), 0.5);
95 0 : const QBrush brush(SchemaStyle::get_color("background", "note"));
96 0 : painter->setFont (SchemaStyle::get_font("note"));
97 0 : painter->setBrush (brush);
98 : // painter->setBackgroundMode(Qt::OpaqueMode);
99 0 : painter->setPen ( pen );
100 0 : const auto rect = boundingRect();
101 0 : painter->drawRect ( rect );
102 0 : painter->drawText(rect.adjusted(5,5,-2,-2), Qt::AlignJustify, m_text);
103 0 : }
104 :
105 0 : QPainterPath SchemaGraphicNote::shape() const {
106 0 : QPainterPath path;
107 0 : path.addRect ( boundingRect() );
108 0 : return path;
109 0 : }
110 : } //namespace dbse
|