Changeset - fc3cbb071069
[Not reviewed]
Hasan Yavuz ÖZDERYA - 10 years ago 2015-10-07 16:53:10
hy@ozderya.net
improved deleting behaviour of hex command editor, spaces are skipped when backspace is pressed
7 files changed with 160 insertions and 50 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -70,24 +70,25 @@ add_executable(${PROGRAM_NAME} WIN32
 
  zoomer.cpp
 
  hidabletabwidget.cpp
 
  framebuffer.cpp
 
  scalepicker.cpp
 
  scalezoomer.cpp
 
  portlist.cpp
 
  snapshot.cpp
 
  snapshotview.cpp
 
  snapshotmanager.cpp
 
  plotsnapshotoverlay.cpp
 
  commandpanel.cpp
 
  commandwidget.cpp
 
  commandedit.cpp
 
  ${UI_FILES}
 
  ${RES_FILES}
 
  misc/windows_icon.rc
 
  )
 

	
 
# Use the Widgets module from Qt 5.
 
target_link_libraries(${PROGRAM_NAME} ${QWT_LIBRARY})
 
qt5_use_modules(${PROGRAM_NAME} Widgets SerialPort Svg)
 

	
 
# set compiler flags
 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
 

	
commandedit.cpp
Show inline comments
 
new file 100644
 
/*
 
  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 <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QKeyEvent>
 

	
 
#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);
 
}
commandedit.h
Show inline comments
 
new file 100644
 
/*
 
  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 <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef COMMANDEDIT_H
 
#define COMMANDEDIT_H
 

	
 
#include <QLineEdit>
 
#include <QValidator>
 

	
 
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
commandwidget.cpp
Show inline comments
 
@@ -14,87 +14,44 @@
 
  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 <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "commandwidget.h"
 
#include "ui_commandwidget.h"
 

	
 
#include <QRegExp>
 
#include <QRegExpValidator>
 

	
 
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 <QtDebug>
 

	
 
CommandWidget::CommandWidget(QWidget *parent) :
 
    QWidget(parent),
 
    ui(new Ui::CommandWidget)
 
{
 
    ui->setupUi(this);
 

	
 
    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()
 
{
 
    this->deleteLater();
 
}
 

	
 
void CommandWidget::onSendClicked()
 
{
 
    emit sendCommand(ui->leCommand->text(), ui->pbASCII->isChecked());
 
}
 

	
 
void CommandWidget::onASCIIToggled(bool checked)
 
{
 
    if (checked)
 
    {
 
        ui->leCommand->setValidator(0);
 
    }
 
    else
 
    {
 
        ui->leCommand->setValidator(hexValidator);
 
    }
 
    ui->leCommand->setMode(checked);
 
}
commandwidget.h
Show inline comments
 
@@ -13,41 +13,39 @@
 
  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 <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef COMMANDWIDGET_H
 
#define COMMANDWIDGET_H
 

	
 
#include <QWidget>
 
#include <QString>
 
#include <QValidator>
 

	
 
namespace Ui {
 
class CommandWidget;
 
}
 

	
 
class CommandWidget : public QWidget
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit CommandWidget(QWidget *parent = 0);
 
    ~CommandWidget();
 

	
 
signals:
 
    void deleteRequested(CommandWidget* thisWidget); // emitted when delete button is clicked
 
    void sendCommand(QString command, bool ascii);   // emitted when send button clicked
 

	
 
private:
 
    Ui::CommandWidget *ui;
 
    QValidator* hexValidator;
 

	
 
private slots:
 
    void onDeleteClicked();
 
    void onSendClicked();
 
    void onASCIIToggled(bool checked);
 
};
 

	
 
#endif // COMMANDWIDGET_H
commandwidget.ui
Show inline comments
 
@@ -39,25 +39,25 @@
 
     </property>
 
     <property name="icon">
 
      <iconset theme="list-remove">
 
       <normaloff/>
 
      </iconset>
 
     </property>
 
     <property name="autoRaise">
 
      <bool>true</bool>
 
     </property>
 
    </widget>
 
   </item>
 
   <item>
 
    <widget class="QLineEdit" name="leCommand">
 
    <widget class="CommandEdit" name="leCommand">
 
     <property name="sizePolicy">
 
      <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
 
       <horstretch>0</horstretch>
 
       <verstretch>0</verstretch>
 
      </sizepolicy>
 
     </property>
 
    </widget>
 
   </item>
 
   <item>
 
    <widget class="QFrame" name="frame">
 
     <property name="sizePolicy">
 
      <sizepolicy hsizetype="Maximum" vsizetype="Preferred">
 
@@ -146,15 +146,22 @@
 
      <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
 
       <horstretch>0</horstretch>
 
       <verstretch>0</verstretch>
 
      </sizepolicy>
 
     </property>
 
     <property name="text">
 
      <string>Send</string>
 
     </property>
 
    </widget>
 
   </item>
 
  </layout>
 
 </widget>
 
 <customwidgets>
 
  <customwidget>
 
   <class>CommandEdit</class>
 
   <extends>QLineEdit</extends>
 
   <header>commandedit.h</header>
 
  </customwidget>
 
 </customwidgets>
 
 <resources/>
 
 <connections/>
 
</ui>
serialplot.pro
Show inline comments
 
@@ -39,43 +39,45 @@ SOURCES += main.cpp\
 
    plot.cpp \
 
    zoomer.cpp \
 
    hidabletabwidget.cpp \
 
    framebuffer.cpp \
 
    scalepicker.cpp \
 
    scalezoomer.cpp \
 
    portlist.cpp \
 
    snapshotview.cpp \
 
    snapshotmanager.cpp \
 
    snapshot.cpp \
 
    plotsnapshotoverlay.cpp \
 
    commandpanel.cpp \
 
    commandwidget.cpp
 
    commandwidget.cpp \
 
    commandedit.cpp
 

	
 
HEADERS  += mainwindow.h \
 
    utils.h \
 
    portcontrol.h \
 
    floatswap.h \
 
    plot.h \
 
    zoomer.h \
 
    hidabletabwidget.h \
 
    framebuffer.h \
 
    scalepicker.h \
 
    scalezoomer.h \
 
    portlist.h \
 
    snapshotview.h \
 
    snapshotmanager.h \
 
    snapshot.h \
 
    plotsnapshotoverlay.h \
 
    commandpanel.h \
 
    commandwidget.h
 
    commandwidget.h \
 
    commandedit.h
 

	
 
FORMS    += mainwindow.ui \
 
    about_dialog.ui \
 
    portcontrol.ui \
 
    snapshotview.ui \
 
    commandpanel.ui \
 
    commandwidget.ui
 

	
 
INCLUDEPATH += qmake/
 

	
 
CONFIG += c++11
 

	
0 comments (0 inline, 0 general)