graph_analysis
EdgeLabel.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_GUI_ITEMS_EDGELABEL_HPP
2 #define GRAPH_ANALYSIS_GUI_ITEMS_EDGELABEL_HPP
3 
4 #include <QGraphicsTextItem>
5 #include <QTextCursor>
6 #include <base-logging/Logging.hpp>
7 
8 namespace graph_analysis {
9 namespace gui {
10 namespace items {
11 
18 class EdgeLabel : public QGraphicsTextItem
19 {
20 public:
26  EdgeLabel(const std::string& label, QGraphicsItem* item)
27  : QGraphicsTextItem(QString(label.c_str()), item)
28  {
29  setFlags(QGraphicsTextItem::ItemIsSelectable | ItemIsFocusable);
30  setTextInteractionFlags(Qt::NoTextInteraction);
31  setAcceptDrops(false);
32  }
33 
39  void setTextInteraction(bool on, bool selectAll = false)
40  {
41  if(on && textInteractionFlags() == Qt::NoTextInteraction)
42  {
43  setTextInteractionFlags(Qt::TextEditorInteraction);
44  setFocus(Qt::MouseFocusReason);
45  setSelected(true);
46  if(selectAll)
47  {
48  QTextCursor c = textCursor();
49  c.select(QTextCursor::Document);
50  setTextCursor(c);
51  }
52 
53  } else if(!on && textInteractionFlags() == Qt::TextEditorInteraction)
54  {
55  setTextInteractionFlags(Qt::NoTextInteraction);
56  QTextCursor c = this->textCursor();
57  c.clearSelection();
58  this->setTextCursor(c);
59  clearFocus();
60  }
61  }
62 
64  void setText(const QString& label)
65  {
66  setPlainText(label);
67  }
68 
69 protected:
70 
72  QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
73  {
74  if(change == QGraphicsItem::ItemSelectedChange)
75  {
76  LOG_DEBUG_S << "itemChange ' " << this->toPlainText().toStdString()
77  << "', selected: '" << value.toString().toStdString() << "'";
78  }
79  if(change == QGraphicsItem::ItemSelectedChange && textInteractionFlags() != Qt::NoTextInteraction && !value.toBool())
80  {
81  // item received SelectedChange event AND is in editor mode AND is about to be deselected:
82  setTextInteraction(false); // leave editor mode
83  }
84  return QGraphicsTextItem::itemChange(change, value);
85  }
86 };
87 
88 } // end namespace items
89 } // end namespace gui
90 } // end namespace graph_analysis
91 #endif // GRAPH_ANALYSIS_GUI_ITEMS_EDGELABEL_HPP
EdgeLabel(const std::string &label, QGraphicsItem *item)
constructor
Definition: EdgeLabel.hpp:26
void setTextInteraction(bool on, bool selectAll=false)
toggles direct editing options of the displayed text
Definition: EdgeLabel.hpp:39
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
qt item change callback
Definition: EdgeLabel.hpp:72
graphical edge text label implementation
Definition: EdgeLabel.hpp:18
void setText(const QString &label)
updates displayed text string to &#39;label&#39;
Definition: EdgeLabel.hpp:64