diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ add_executable(${PROGRAM_NAME} WIN32 plotsnapshotoverlay.cpp commandpanel.cpp commandwidget.cpp + commandedit.cpp ${UI_FILES} ${RES_FILES} misc/windows_icon.rc diff --git a/commandedit.cpp b/commandedit.cpp new file mode 100644 --- /dev/null +++ b/commandedit.cpp @@ -0,0 +1,102 @@ +/* + Copyright © 2015 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#include + +#include "commandedit.h" + +class HexCommandValidator : public QRegExpValidator +{ +public: + explicit HexCommandValidator(QObject* parent = 0); + QValidator::State validate(QString & input, int & pos) const; +}; + +HexCommandValidator::HexCommandValidator(QObject* parent) : + QRegExpValidator(parent) +{ + QRegExp regExp("^(?:(?:[0-9A-F]{2}[ ])+(?:[0-9A-F]{2}))|(?:[0-9A-F]{2})$"); + setRegExp(regExp); +} + +QValidator::State HexCommandValidator::validate(QString & input, int & pos) const +{ + input = input.toUpper(); + + // don't let pos to be altered at this stage + int orgPos = pos; + auto r = QRegExpValidator::validate(input, pos); + pos = orgPos; + + // try fixing up spaces + if (r != QValidator::Acceptable) + { + input = input.replace(" ", ""); + input.replace(QRegExp("([0-9A-F]{2}(?!$))"), "\\1 "); + if (pos == input.size()-1) pos = input.size(); + r = QRegExpValidator::validate(input, pos); + } + + return r; +} + +CommandEdit::CommandEdit(QWidget *parent) : + QLineEdit(parent) +{ + hexValidator = new HexCommandValidator(this); + ascii_mode = true; +} + +CommandEdit::~CommandEdit() +{ + delete hexValidator; +} + +void CommandEdit::setMode(bool ascii) +{ + ascii_mode = ascii; + if (ascii) + { + setValidator(0); + } + else + { + setValidator(hexValidator); + } +} + +void CommandEdit::keyPressEvent(QKeyEvent * event) +{ + if (ascii_mode) + { + QLineEdit::keyPressEvent(event); + return; + } + + if (event->key() == Qt::Key_Backspace && !hasSelectedText()) + { + int cursor = cursorPosition(); + if (cursor != 0 && text()[cursor-1] == ' ') + { + setCursorPosition(cursor-1); + } + } + + QLineEdit::keyPressEvent(event); +} diff --git a/commandedit.h b/commandedit.h new file mode 100644 --- /dev/null +++ b/commandedit.h @@ -0,0 +1,43 @@ +/* + Copyright © 2015 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#ifndef COMMANDEDIT_H +#define COMMANDEDIT_H + +#include +#include + +class CommandEdit : public QLineEdit +{ + Q_OBJECT + +public: + explicit CommandEdit(QWidget *parent = 0); + ~CommandEdit(); + void setMode(bool ascii); // true = ascii, false = hex + +private: + bool ascii_mode; + QValidator* hexValidator; + +protected: + void keyPressEvent(QKeyEvent * event) Q_DECL_OVERRIDE; +}; + +#endif // COMMANDEDIT_H diff --git a/commandwidget.cpp b/commandwidget.cpp --- a/commandwidget.cpp +++ b/commandwidget.cpp @@ -23,40 +23,7 @@ #include #include -class HexCommandValidator : public QRegExpValidator -{ -public: - explicit HexCommandValidator(QObject* parent = 0); - QValidator::State validate(QString & input, int & pos) const; -}; - -HexCommandValidator::HexCommandValidator(QObject* parent) : - QRegExpValidator(parent) -{ - QRegExp regExp("^(?:(?:[0-9A-F]{2}[ ])+(?:[0-9A-F]{2}))|(?:[0-9A-F]{2})$"); - setRegExp(regExp); -} - -QValidator::State HexCommandValidator::validate(QString & input, int & pos) const -{ - input = input.toUpper(); - - // don't let pos to be altered at this stage - int orgPos = pos; - auto r = QRegExpValidator::validate(input, pos); - pos = orgPos; - - // try fixing up spaces - if (r != QValidator::Acceptable) - { - input = input.replace(" ", ""); - input.replace(QRegExp("([0-9A-F]{2}(?!$))"), "\\1 "); - if (pos == input.size()-1) pos = input.size(); - r = QRegExpValidator::validate(input, pos); - } - - return r; -} +#include CommandWidget::CommandWidget(QWidget *parent) : QWidget(parent), @@ -67,14 +34,11 @@ CommandWidget::CommandWidget(QWidget *pa connect(ui->pbDelete, &QPushButton::clicked, this, &CommandWidget::onDeleteClicked); connect(ui->pbSend, &QPushButton::clicked, this, &CommandWidget::onSendClicked); connect(ui->pbASCII, &QPushButton::toggled, this, &CommandWidget::onASCIIToggled); - - hexValidator = new HexCommandValidator(this); } CommandWidget::~CommandWidget() { delete ui; - delete hexValidator; } void CommandWidget::onDeleteClicked() @@ -89,12 +53,5 @@ void CommandWidget::onSendClicked() void CommandWidget::onASCIIToggled(bool checked) { - if (checked) - { - ui->leCommand->setValidator(0); - } - else - { - ui->leCommand->setValidator(hexValidator); - } + ui->leCommand->setMode(checked); } diff --git a/commandwidget.h b/commandwidget.h --- a/commandwidget.h +++ b/commandwidget.h @@ -22,7 +22,6 @@ #include #include -#include namespace Ui { class CommandWidget; @@ -42,7 +41,6 @@ signals: private: Ui::CommandWidget *ui; - QValidator* hexValidator; private slots: void onDeleteClicked(); diff --git a/commandwidget.ui b/commandwidget.ui --- a/commandwidget.ui +++ b/commandwidget.ui @@ -48,7 +48,7 @@ - + 0 @@ -155,6 +155,13 @@ + + + CommandEdit + QLineEdit +
commandedit.h
+
+
diff --git a/serialplot.pro b/serialplot.pro --- a/serialplot.pro +++ b/serialplot.pro @@ -48,7 +48,8 @@ SOURCES += main.cpp\ snapshot.cpp \ plotsnapshotoverlay.cpp \ commandpanel.cpp \ - commandwidget.cpp + commandwidget.cpp \ + commandedit.cpp HEADERS += mainwindow.h \ utils.h \ @@ -66,7 +67,8 @@ HEADERS += mainwindow.h \ snapshot.h \ plotsnapshotoverlay.h \ commandpanel.h \ - commandwidget.h + commandwidget.h \ + commandedit.h FORMS += mainwindow.ui \ about_dialog.ui \