File indexing completed on 2024-04-21 03:50:56
0001 #include "text/sentence.h" 0002 #include "text/textpainter.h" 0003 0004 #include <QFile> 0005 #include <QScrollBar> 0006 #include <QString> 0007 #include <QTextEdit> 0008 #include <QTextStream> 0009 0010 TextPainter::TextPainter(Container* parent) : 0011 Painter(parent), 0012 m_textEdit(new QTextEdit(parent)) 0013 { 0014 m_textEdit->resize(parent->size()); 0015 m_textEdit->viewport()->installEventFilter(m_parent); 0016 m_textEdit->viewport()->setMouseTracking(false); 0017 m_textEdit->setAttribute(Qt::WA_DeleteOnClose); 0018 m_textEdit->setReadOnly(true); 0019 m_textEdit->setVisible(true); 0020 m_parent->setCurrentObject(new Sentence(m_parent->currentObject()->objClass())); 0021 } 0022 0023 TextPainter::~TextPainter() 0024 { 0025 m_textEdit->close(); 0026 } 0027 0028 void TextPainter::changeItem(const QString& filepath) 0029 { 0030 QFile textFile(filepath); 0031 if (!textFile.open(QIODevice::ReadOnly|QIODevice::Text)) 0032 return; 0033 0034 QTextStream textStream (&textFile); 0035 0036 m_textEdit->setText(textStream.readAll()); 0037 } 0038 0039 void TextPainter::paint(QPoint point, bool isDragging) 0040 { 0041 QTextCursor textCursor = m_textEdit->cursorForPosition(point); 0042 0043 Sentence *currentSentence = static_cast<Sentence*>(m_parent->currentObject()); 0044 0045 bool toSave = point.isNull() && !isDragging && currentSentence->isValid(); 0046 if (!toSave) { 0047 if (!isDragging) 0048 m_anchor = textCursor.position() - 1; 0049 double end = textCursor.position(); 0050 0051 int idxNearbySentence = -1; 0052 int idxDoubleClick = -1; 0053 for (int i = 0; i < m_parent->savedObjects().size(); i++) { 0054 Sentence *obj = static_cast<Sentence*>(m_parent->savedObjects()[i]); 0055 if (obj->hasBetween(m_anchor) || obj->hasBetween(end)) 0056 return; 0057 else if (m_anchor == obj->end() || end == obj->begin()) { 0058 idxNearbySentence = i; 0059 break; 0060 } 0061 else if (obj->end() - obj->begin() == 1 && end == obj->end()) { 0062 idxDoubleClick = i; 0063 break; 0064 } 0065 } 0066 0067 if (idxDoubleClick != -1) { 0068 toSave = true; 0069 textCursor.select(QTextCursor::WordUnderCursor); 0070 m_anchor = textCursor.anchor(); 0071 end = textCursor.position(); 0072 m_parent->savedObjects().remove(idxDoubleClick); 0073 } 0074 0075 else if (idxNearbySentence != -1) { 0076 Sentence* obj = static_cast<Sentence*>(m_parent->savedObjects()[idxNearbySentence]); 0077 if (currentSentence->objClass() == obj->objClass()) { 0078 if (end == obj->begin()) 0079 toSave = true; 0080 m_anchor = qMin(m_anchor, obj->begin()); 0081 end = qMax(end, obj->end()); 0082 m_parent->savedObjects().remove(idxNearbySentence); 0083 } 0084 } 0085 0086 currentSentence->move(std::min(m_anchor, end), std::max(end, m_anchor)); 0087 0088 if (currentSentence->isValid()) { 0089 if (isDragging) { 0090 int scrollBarPreviousPos = m_textEdit->verticalScrollBar()->value(); 0091 m_textEdit->undo(); 0092 m_textEdit->verticalScrollBar()->setValue(scrollBarPreviousPos); 0093 } 0094 0095 paintObject(currentSentence); 0096 } 0097 else 0098 currentSentence->move(m_anchor, end + 1); 0099 } 0100 if (toSave) { 0101 m_parent->appendObject(currentSentence); 0102 repaint(); 0103 m_parent->setCurrentObject(new Sentence(currentSentence->objClass())); 0104 } 0105 } 0106 0107 void TextPainter::repaint() 0108 { 0109 QTextCharFormat fmt; 0110 fmt.setBackground(QBrush(QColor(), Qt::NoBrush)); 0111 m_textEdit->setCurrentCharFormat(fmt); 0112 0113 int scrollPreviousPos = m_textEdit->verticalScrollBar()->value(); 0114 QString plainText = m_textEdit->toPlainText(); 0115 m_textEdit->setPlainText(plainText); 0116 m_textEdit->verticalScrollBar()->setValue(scrollPreviousPos); 0117 0118 for (MarkedObject* obj : m_parent->savedObjects()) 0119 paintObject(obj); 0120 } 0121 0122 void TextPainter::undo() 0123 { 0124 if (m_parent->savedObjects().isEmpty()) 0125 return; 0126 0127 m_parent->savedObjects().pop_back(); 0128 repaint(); 0129 } 0130 0131 void TextPainter::deleteCurrentObject() 0132 { 0133 if (m_parent->savedObjects().isEmpty()) 0134 return; 0135 0136 m_parent->savedObjects().pop_back(); 0137 repaint(); 0138 } 0139 0140 void TextPainter::paintObject(MarkedObject *object) 0141 { 0142 Sentence *sentence = static_cast<Sentence*>(object); 0143 0144 QTextCursor textCursor = m_textEdit->textCursor(); 0145 textCursor.setPosition(sentence->begin(), QTextCursor::MoveAnchor); 0146 textCursor.setPosition(sentence->end(), QTextCursor::KeepAnchor); 0147 0148 QTextCharFormat fmt; 0149 QColor color (object->objClass()->color()); 0150 color.setAlpha(85); 0151 fmt.setBackground(color); 0152 textCursor.setCharFormat(fmt); 0153 } 0154 0155 bool TextPainter::importObjects(QVector<MarkedObject*> objects) 0156 { 0157 if (objects.isEmpty() || objects.first()->type() != MarkedObject::Type::Sentence) 0158 return false; 0159 0160 m_parent->savedObjects() = objects; 0161 repaint(); 0162 return true; 0163 }