CTextItem.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "CTextItem.h"
  2. #include <QKeyEvent>
  3. #include <QGraphicsSceneMouseEvent>
  4. #include <QTextDocument>
  5. #include <QPainter>
  6. #include <QTextCursor>
  7. #include <QCursor>
  8. #include <QDebug>
  9. CTextItem::CTextItem(QGraphicsItem *parent) : CTextItem(QString(), parent) { }
  10. CTextItem::CTextItem(const QString &text, QGraphicsItem *parent) : QGraphicsTextItem(text, parent)
  11. {
  12. setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
  13. setTextInteractionFlags(Qt::TextEditorInteraction);
  14. connect(document(), &QTextDocument::contentsChanged, this, &CTextItem::slotTextChanged);
  15. connect(document(), &QTextDocument::contentsChange, this, &CTextItem::slotTextChange);
  16. }
  17. void CTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
  18. {
  19. if (event->button() == Qt::LeftButton) { //左键双击进入可编辑状态并打开焦点
  20. if (allowEdit()) {
  21. emit sigWillBeginEditing();
  22. beginEditing();
  23. }
  24. QGraphicsTextItem::mouseDoubleClickEvent(event);
  25. }
  26. }
  27. void CTextItem::keyPressEvent(QKeyEvent *event)
  28. {
  29. switch (event->key()) {
  30. case Qt::Key_Return: {
  31. endEditing();
  32. }
  33. }
  34. QGraphicsTextItem::keyPressEvent(event);
  35. }
  36. void CTextItem::beginEditing()
  37. {
  38. setTextInteractionFlags(Qt::TextEditorInteraction);
  39. setFocus();
  40. parentItem()->setZValue(-1);
  41. }
  42. void CTextItem::endEditing()
  43. {
  44. setZValue(0);
  45. setTextInteractionFlags(Qt::NoTextInteraction);
  46. setSelected(false);
  47. clearFocus();
  48. if (m_textChanged) {
  49. emit sigTextChanged();
  50. m_textChanged = false;
  51. }
  52. // 没找到取消文字选中状态的方法, 暂且用这个吧
  53. // 因为这相当于编辑了文字, 会触发数据库保存操作
  54. setPlainText(toPlainText());
  55. }
  56. bool CTextItem::isEditing() const
  57. {
  58. return (textInteractionFlags() & Qt::TextEditorInteraction) == Qt::TextEditorInteraction;
  59. }
  60. bool CTextItem::allowEdit() const
  61. {
  62. return m_allowEdit;
  63. }
  64. void CTextItem::setAllowEdit(bool a)
  65. {
  66. m_allowEdit = a;
  67. if (a == false) {
  68. endEditing();
  69. }
  70. }
  71. QSizeF CTextItem::textSize(const QString t, qreal width)
  72. {
  73. static CTextItem *i = nullptr;
  74. if (i == nullptr) {
  75. i = new CTextItem(nullptr);
  76. }
  77. i->document()->setPlainText(t);
  78. i->setTextWidth(width);
  79. return i->document()->size();
  80. }
  81. void CTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  82. {
  83. QGraphicsTextItem::paint(painter, option, widget);
  84. }
  85. void CTextItem::slotTextChanged()
  86. {
  87. m_textChanged = true;
  88. }
  89. void CTextItem::slotTextChange(int from, int charsRemoved, int charsAdded)
  90. {
  91. Q_UNUSED(from)
  92. Q_UNUSED(charsRemoved)
  93. Q_UNUSED(charsAdded)
  94. QTextCursor c = textCursor();
  95. if (toPlainText().contains(" ")) {
  96. setPlainText(toPlainText().remove(QRegExp("\\s")));
  97. setTextCursor(c);
  98. }
  99. }