Skip to content

Commit

Permalink
fixed undo on auto indented lines
Browse files Browse the repository at this point in the history
  • Loading branch information
dhamith93 committed Sep 25, 2018
1 parent e3b68b9 commit 6382ddc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/headers/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MainWindow : public QMainWindow
std::string displayMode;

void closeEvent(QCloseEvent *event);
bool eventFilter(QObject *watched, QEvent *event);
void init();
void resetFileList();
void openFile(QString &filePath);
Expand Down
2 changes: 1 addition & 1 deletion src/headers/plaintextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PlainTextEdit : public QPlainTextEdit {
private:
bool checkListItem(QString &line);
bool checkEmptyListItem(QString &line);
QTextCursor getModifiedTextCursor(QTextCursor textCursorIn, QString text);
QTextCursor getModifiedTextCursor(QString text);
protected:
virtual void keyPressEvent(QKeyEvent *event);
virtual void focusOutEvent(QFocusEvent* e);
Expand Down
31 changes: 27 additions & 4 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,38 @@ MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);

QShortcut *shortcut = new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Tab), this);
QObject::connect(shortcut, &QShortcut::activated, this, &MainWindow::reverseTab);

ui->noteText->installEventFilter(this);

init();
}

MainWindow::~MainWindow() {
delete ui;
}


// Overriding `undo (control/command + Z)` event to prevent
// list releted methods from running on empty list item lines
// when undoing changes
bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
if (event->type() == QEvent::ShortcutOverride) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->modifiers().testFlag(Qt::ControlModifier) && keyEvent->key() == 'Z') {
ui->noteText->blockSignals(true);
ui->noteText->undo();
ui->noteText->blockSignals(false);
event->ignore();
return true;
}
}

return QMainWindow::eventFilter(watched, event);
}

void MainWindow::init() {
darkStyles = "QPlainTextEdit { padding: 5% 5% 0 5%; color: white; background-color: #252525; border:none; }";
lightStyles = "QPlainTextEdit { padding: 5% 5% 0 5%; color: #454545; background-color: #FAFAFA; border:none; }";
Expand Down Expand Up @@ -494,11 +517,11 @@ void MainWindow::on_noteText_textChanged() {
ui->openedNotePath->setText(wordCountText);
}

QTextBlock textBlock = ui->noteText->textCursor().block();
QString prevLine = textBlock.previous().text();
QTextBlock textBlock = ui->noteText->textCursor().block();

if (checkListItem(prevLine)) {
if (textBlock.text().length() == 0) {
if (textBlock.text().length() == 0) {
QString prevLine = textBlock.previous().text();
if (checkListItem(prevLine)) {
int spaceCount = getSpaceCount(prevLine);
QString newLine = QString("").leftJustified(spaceCount, ' ');

Expand Down
8 changes: 4 additions & 4 deletions src/plaintextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ bool PlainTextEdit::checkEmptyListItem(QString &line) {
return (regex1.match(line).hasMatch() || regex2.match(line).hasMatch());
}

QTextCursor PlainTextEdit::getModifiedTextCursor(QTextCursor textCursorIn, QString text) {
QTextCursor tempCursor = textCursorIn;
QTextCursor PlainTextEdit::getModifiedTextCursor(QString text) {
QTextCursor tempCursor = textCursor();
tempCursor.movePosition(QTextCursor::StartOfLine);
tempCursor.movePosition(QTextCursor::EndOfLine);
tempCursor.select(QTextCursor::LineUnderCursor);
Expand Down Expand Up @@ -55,7 +55,7 @@ void PlainTextEdit::keyPressEvent(QKeyEvent *event) {
if (checkEmptyListItem(str)) {
event->ignore();
this->blockSignals(true);
this->setTextCursor(getModifiedTextCursor(this->textCursor(), "\n"));
this->setTextCursor(getModifiedTextCursor("\n"));
this->blockSignals(false);
return;
}
Expand All @@ -67,7 +67,7 @@ void PlainTextEdit::keyPressEvent(QKeyEvent *event) {
if (block.text().length() == 1 && checkListItem(str)) {
event->ignore();
this->blockSignals(true);
this->setTextCursor(getModifiedTextCursor(this->textCursor(), ""));
this->setTextCursor(getModifiedTextCursor(""));
this->blockSignals(false);
return;
}
Expand Down

0 comments on commit 6382ddc

Please sign in to comment.