Fixed bugs with the editing of the text area based on cursor movement

This commit is contained in:
Jordan Sherer 2019-09-26 20:46:33 -04:00
parent 7aff060ec7
commit 81e5aa00f0
3 changed files with 13 additions and 44 deletions

View File

@ -86,7 +86,6 @@ void highlightBlock(QTextBlock block, QFont font, QColor foreground, QColor back
TransmitTextEdit::TransmitTextEdit(QWidget *parent): TransmitTextEdit::TransmitTextEdit(QWidget *parent):
QTextEdit(parent), QTextEdit(parent),
m_sent { 0 }, m_sent { 0 },
m_textSent { "" },
m_protected { false } m_protected { false }
{ {
connect(this, &QTextEdit::selectionChanged, this, &TransmitTextEdit::on_selectionChanged); connect(this, &QTextEdit::selectionChanged, this, &TransmitTextEdit::on_selectionChanged);
@ -174,26 +173,25 @@ bool TransmitTextEdit::cursorShouldBeProtected(QTextCursor c){
void TransmitTextEdit::on_selectionChanged(){ void TransmitTextEdit::on_selectionChanged(){
auto c = textCursor(); auto c = textCursor();
auto protect = cursorShouldBeProtected(c); auto shouldProtect = cursorShouldBeProtected(c);
if(protect){ if(shouldProtect){
blockSignals(true); blockSignals(true);
{ {
int end = c.selectionEnd(); int end = c.selectionEnd();
c.setPosition(m_sent); c.movePosition(QTextCursor::Start);
c.setPosition(end, QTextCursor::KeepAnchor); c.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_sent);
c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, qMax(0, end-m_sent));
setTextCursor(c); setTextCursor(c);
} }
blockSignals(false); blockSignals(false);
} }
setProtected(protect); setProtected(shouldProtect);
// TODO: when protected and text is selected, remove protected region from selection
} }
// slot // slot
void TransmitTextEdit::on_textContentsChanged(int pos, int rem, int add){ void TransmitTextEdit::on_textContentsChanged(int /*pos*/, int rem, int add){
if(rem == 0 && add == 0){ if(rem == 0 && add == 0){
return; return;
} }
@ -252,37 +250,6 @@ void TransmitTextEdit::highlight(){
highlightCharsSent(); highlightCharsSent();
} }
bool isMovementKeyEvent(QKeyEvent * k){
return (
k == QKeySequence::MoveToNextChar ||
k == QKeySequence::MoveToPreviousChar ||
k == QKeySequence::SelectNextChar ||
k == QKeySequence::SelectPreviousChar ||
k == QKeySequence::SelectNextWord ||
k == QKeySequence::SelectPreviousWord ||
k == QKeySequence::SelectStartOfLine ||
k == QKeySequence::SelectEndOfLine ||
k == QKeySequence::SelectStartOfBlock ||
k == QKeySequence::SelectEndOfBlock ||
k == QKeySequence::SelectStartOfDocument ||
k == QKeySequence::SelectEndOfDocument ||
k == QKeySequence::SelectPreviousLine ||
k == QKeySequence::SelectNextLine ||
k == QKeySequence::MoveToNextWord ||
k == QKeySequence::MoveToPreviousWord ||
k == QKeySequence::MoveToEndOfBlock ||
k == QKeySequence::MoveToStartOfBlock ||
k == QKeySequence::MoveToNextLine ||
k == QKeySequence::MoveToPreviousLine ||
k == QKeySequence::MoveToPreviousLine ||
k == QKeySequence::MoveToStartOfLine ||
k == QKeySequence::MoveToEndOfLine ||
k == QKeySequence::MoveToStartOfDocument ||
k == QKeySequence::MoveToEndOfDocument
);
}
QTextCursor::MoveOperation movementKeyEventToMoveOperation(QKeyEvent *e){ QTextCursor::MoveOperation movementKeyEventToMoveOperation(QKeyEvent *e){
QTextCursor::MoveOperation op = QTextCursor::NoMove; QTextCursor::MoveOperation op = QTextCursor::NoMove;
@ -365,6 +332,9 @@ QTextCursor::MoveOperation movementKeyEventToMoveOperation(QKeyEvent *e){
return op; return op;
} }
bool isMovementKeyEvent(QKeyEvent * k){
return movementKeyEventToMoveOperation(k) != QTextCursor::NoMove;
}
bool TransmitTextEdit::eventFilter(QObject */*o*/, QEvent *e){ bool TransmitTextEdit::eventFilter(QObject */*o*/, QEvent *e){
if(e->type() != QEvent::KeyPress){ if(e->type() != QEvent::KeyPress){
@ -393,16 +363,15 @@ bool TransmitTextEdit::eventFilter(QObject */*o*/, QEvent *e){
// 2. if on the edge, do not filter if not a backspace // 2. if on the edge, do not filter if not a backspace
int start = qMin(c.selectionStart(), c.selectionEnd()); int start = qMin(c.selectionStart(), c.selectionEnd());
int end = qMax(c.selectionStart(), c.selectionEnd());
if(start == m_sent && k->key() != Qt::Key_Backspace){ if(start == m_sent && k->key() != Qt::Key_Backspace){
return false; return false;
} }
// 3. if on the edge, do not filter if a backspace and there is text selected // 3. if on the edge, do not filter if a backspace and there is text selected
int end = qMax(c.selectionStart(), c.selectionEnd());
if(start == m_sent && start != end && k->key() == Qt::Key_Backspace){ if(start == m_sent && start != end && k->key() == Qt::Key_Backspace){
return false; return false;
} }
return true; return true;
} }

View File

@ -55,8 +55,8 @@ public slots:
private: private:
QString m_lastText; QString m_lastText;
QString m_textSent;
int m_sent; int m_sent;
QString m_textSent;
bool m_protected; bool m_protected;
QFont m_font; QFont m_font;
QColor m_fg; QColor m_fg;

View File

@ -1095,7 +1095,7 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple,
toggleTx(true); toggleTx(true);
}); });
//ui->extFreeTextMsgEdit->installEventFilter(enterFilter); ui->extFreeTextMsgEdit->installEventFilter(enterFilter);
auto doubleClickFilter = new MouseDoubleClickEater(); auto doubleClickFilter = new MouseDoubleClickEater();
connect(doubleClickFilter, &MouseDoubleClickEater::mouseDoubleClicked, this, [this](QObject *, QMouseEvent *, bool *){ connect(doubleClickFilter, &MouseDoubleClickEater::mouseDoubleClicked, this, [this](QObject *, QMouseEvent *, bool *){