CNodeItem.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "CNodeItem.h"
  2. #include "CRectItem.h"
  3. #include "CTextItem.h"
  4. #include "CLineItem.h"
  5. #include <QAction>
  6. #include <QTextDocument>
  7. #include <QDebug>
  8. CNodeItem::CNodeItem(const CNodeData data, CNodeItem *parent) : QObject(parent), m_data(data)
  9. {
  10. m_rectItem = new CRectItem();
  11. m_rectItem->setToolTip(data.remark);
  12. m_textItem = new CTextItem(data.name, m_rectItem);
  13. m_lineItem = new CLineItem(m_rectItem);
  14. updateItemsGeometry();
  15. connectSignalsAndSlots();
  16. // m_textItem->setDefaultTextColor(Qt::white);
  17. m_rectItem->setNormalFillColor(QColor("#ddddff"));
  18. m_lineItem->setNormalLineColor(QColor("#ddddff"));
  19. m_lineItem->setLineWidth(2);
  20. m_lineItem->setBrush(Qt::NoBrush);
  21. }
  22. CNodeData CNodeItem::data() const
  23. {
  24. return m_data;
  25. }
  26. void CNodeItem::setData(CNodeData data)
  27. {
  28. m_data = data;
  29. m_textItem->setPlainText(data.name);
  30. m_rectItem->setToolTip(data.remark);
  31. }
  32. void CNodeItem::addSubNode(CNodeItem *n)
  33. {
  34. if (n == nullptr) {
  35. return;
  36. }
  37. if (n->data().pNumber == data().number) {
  38. n->setParent(this);
  39. } else {
  40. for (QObject *obj : children()) {
  41. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  42. item->addSubNode(n);
  43. }
  44. }
  45. }
  46. void CNodeItem::removeNode(int number)
  47. {
  48. for (QObject *obj : children()) {
  49. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  50. if (item->data().number == number) {
  51. item->setParent(nullptr);
  52. delete item;
  53. } else {
  54. item->removeNode(number);
  55. }
  56. }
  57. }
  58. CNodeItem *CNodeItem::subNode(int number)
  59. {
  60. if (data().number == number) {
  61. return this;
  62. }
  63. for (QObject *obj : children()) {
  64. CNodeItem *t = dynamic_cast<CNodeItem *>(obj);
  65. CNodeItem *sub = t->subNode(number);
  66. if (sub != nullptr) {
  67. return sub;
  68. }
  69. }
  70. return nullptr;
  71. }
  72. CRectItem *CNodeItem::rectItem() const
  73. {
  74. return m_rectItem;
  75. }
  76. CTextItem *CNodeItem::textItem() const
  77. {
  78. return m_textItem;
  79. }
  80. CLineItem *CNodeItem::lineItem() const
  81. {
  82. return m_lineItem;
  83. }
  84. QPointF CNodeItem::pos() const
  85. {
  86. return m_pos;
  87. }
  88. void CNodeItem::setPos(const QPointF pos)
  89. {
  90. m_pos = pos;
  91. updateItemsGeometry();
  92. }
  93. qreal CNodeItem::xMargin() const
  94. {
  95. return m_xMargin;
  96. }
  97. void CNodeItem::setXMargin(qreal x)
  98. {
  99. m_xMargin = x;
  100. updateItemsGeometry();
  101. }
  102. qreal CNodeItem::yMargin() const
  103. {
  104. return m_yMargin;
  105. }
  106. void CNodeItem::setYMargin(qreal y)
  107. {
  108. m_yMargin = y;
  109. updateItemsGeometry();
  110. }
  111. qreal CNodeItem::minWidth() const
  112. {
  113. return m_minWidth;
  114. }
  115. void CNodeItem::setMinWidth(qreal w)
  116. {
  117. m_minWidth = w;
  118. updateItemsGeometry();
  119. }
  120. qreal CNodeItem::maxWidth() const
  121. {
  122. return m_maxWidth;
  123. }
  124. void CNodeItem::setMaxWidth(qreal w)
  125. {
  126. m_maxWidth = w;
  127. }
  128. qreal CNodeItem::minHeight() const
  129. {
  130. return m_minHeight;
  131. }
  132. void CNodeItem::setMinHeight(qreal h)
  133. {
  134. m_minHeight = h;
  135. updateItemsGeometry();
  136. }
  137. void CNodeItem::connectSignalsAndSlots()
  138. {
  139. connect(m_rectItem->editAction(), &QAction::triggered, this, &CNodeItem::slotEditNode);
  140. connect(m_rectItem->selectAction(), &QAction::triggered, this, &CNodeItem::slotSelect);
  141. connect(m_rectItem->subNodeAction(), &QAction::triggered, this, &CNodeItem::slotSubNode);
  142. connect(m_rectItem->removeAction(), &QAction::triggered, this, &CNodeItem::slotRemove);
  143. connect(m_textItem, &CTextItem::sigWillBeginEditing, this, &CNodeItem::slotWillBeginEditing);
  144. connect(m_textItem, &CTextItem::sigTextChanged, this, &CNodeItem::slotTextChanged);
  145. }
  146. void CNodeItem::updateItemsGeometry()
  147. {
  148. QRectF tr = m_textItem->boundingRect();
  149. qreal w = std::max(tr.width() + m_xMargin * 2, m_minWidth);
  150. qreal h = std::max(tr.height() + m_yMargin * 2, m_minHeight);
  151. m_rectItem->setRect(QRectF(m_pos, QSizeF(w, h)));
  152. m_textItem->setPos(m_pos + QPointF(m_xMargin, (h - tr.height()) / 2));
  153. }
  154. int CNodeItem::height() const
  155. {
  156. int h = 1;
  157. for (QObject *obj : children()) {
  158. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  159. h = std::max(h, item->height() + 1);
  160. }
  161. return h;
  162. }
  163. int CNodeItem::depth() const
  164. {
  165. int d = 1;
  166. const CNodeItem *node = this;
  167. while (node->parent() != nullptr) {
  168. d++;
  169. node = dynamic_cast<CNodeItem *>(node->parent());
  170. }
  171. return d;
  172. }
  173. int CNodeItem::leafs() const
  174. {
  175. int d = 1;
  176. if (children().count() > 0) {
  177. int d = 0;
  178. for (QObject *obj : children()) {
  179. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  180. d += item->leafs();
  181. }
  182. }
  183. return d;
  184. }
  185. int CNodeItem::sizeOfLevel(int lev) const
  186. {
  187. if (lev < 1) {
  188. return 0;
  189. }
  190. if (lev == 1) {
  191. return 1;
  192. }
  193. int size = 0;
  194. for (QObject *obj : children()) {
  195. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  196. size += item->sizeOfLevel(lev - 1);
  197. }
  198. return size;
  199. }
  200. qreal CNodeItem::textWidth() const
  201. {
  202. return m_textItem->boundingRect().width();
  203. }
  204. qreal CNodeItem::textHeight() const
  205. {
  206. return m_textItem->boundingRect().height();
  207. }
  208. qreal CNodeItem::borderWidth() const
  209. {
  210. return m_minWidth;
  211. QFont font = m_textItem->font();
  212. QFontMetrics fm(font);
  213. int w = fm.width(m_textItem->toPlainText());
  214. return std::max(w + m_xMargin * 2, m_minWidth);
  215. }
  216. qreal CNodeItem::borderHeight() const
  217. {
  218. return std::max(textHeight() + m_yMargin * 2, m_minHeight);
  219. }
  220. qreal CNodeItem::hNodeSpace() const
  221. {
  222. return m_hNodeSpace;
  223. }
  224. void CNodeItem::setHNodeSpace(qreal h)
  225. {
  226. m_hNodeSpace = h;
  227. }
  228. qreal CNodeItem::vNodeSpace() const
  229. {
  230. return m_vNodeSpace;
  231. }
  232. void CNodeItem::setVNodeSpace(qreal v)
  233. {
  234. m_vNodeSpace = v;
  235. }
  236. qreal CNodeItem::childrenHeight() const
  237. {
  238. qreal h = vNodeSpace() * (children().count() - 1); // 子节点竖直方向上总间隔
  239. for (QObject *obj : children()) {
  240. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  241. h += item->treeHeight(); // 加上各子节点高度
  242. }
  243. return std::max(h, h - h);
  244. }
  245. qreal CNodeItem::treeHeight() const
  246. {
  247. return std::max(borderHeight(), childrenHeight());
  248. }
  249. qreal CNodeItem::maxBorderWidthOfLevel(int lev) const
  250. {
  251. if (lev < 1) {
  252. return 0;
  253. }
  254. if (lev == 1) {
  255. return borderWidth();
  256. }
  257. qreal w = 0;
  258. for (QObject *obj : children()) {
  259. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  260. w = std::max(w, item->maxBorderWidthOfLevel(lev - 1));
  261. }
  262. return w;
  263. }
  264. void CNodeItem::beginEditing()
  265. {
  266. m_textItem->beginEditing();
  267. }
  268. void CNodeItem::endEditing()
  269. {
  270. CNodeItem *item = editingNode();
  271. if (item != nullptr) {
  272. item->textItem()->endEditing();
  273. }
  274. }
  275. CNodeItem *CNodeItem::editingNode()
  276. {
  277. CNodeItem *node = nullptr;
  278. if (m_textItem->isEditing()) {
  279. return this;
  280. }
  281. for (QObject *obj : children()) {
  282. CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
  283. node = item->editingNode();
  284. if (node != nullptr) {
  285. return node;
  286. }
  287. }
  288. return node;
  289. }
  290. void CNodeItem::setHighlighted(bool h)
  291. {
  292. m_rectItem->setHighlighted(h);
  293. m_lineItem->setHighlighted(h);
  294. m_lineItem->setBrush(Qt::NoBrush);
  295. m_textItem->setDefaultTextColor(h ? QColor("#cccccc") : Qt::black);
  296. }
  297. void CNodeItem::slotEditNode()
  298. {
  299. emit sigEditNode(data());
  300. }
  301. void CNodeItem::slotSelect()
  302. {
  303. m_rectItem->setHighlighted(!m_rectItem->highlighted());
  304. }
  305. void CNodeItem::slotSubNode()
  306. {
  307. emit sigAddSubItem(data().number);
  308. }
  309. void CNodeItem::slotRemove()
  310. {
  311. emit sigRemoveItem(data().number);
  312. }
  313. void CNodeItem::slotTextChanged()
  314. {
  315. m_data.name = m_textItem->toPlainText();
  316. emit sigTextChanged();
  317. }
  318. void CNodeItem::slotWillBeginEditing()
  319. {
  320. emit sigWillBeginEditing();
  321. }