[Companion][AutoLineEdit] Add ability to work with QString data type w/out default validation.

This commit is contained in:
Max Paperno
2018-06-30 10:24:08 -04:00
parent 86fb88cdee
commit ff82a575f6
+32 -14
View File
@@ -25,54 +25,72 @@
#include <QRegExpValidator> #include <QRegExpValidator>
#include "genericpanel.h" #include "genericpanel.h"
#define CHAR_FOR_NAMES_REGEX "[ A-Za-z0-9_.-,]*"
class AutoLineEdit: public QLineEdit class AutoLineEdit: public QLineEdit
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit AutoLineEdit(QWidget *parent = 0): explicit AutoLineEdit(QWidget *parent = nullptr, bool updateOnChange = false):
QLineEdit(parent), QLineEdit(parent),
field(NULL), field(NULL),
strField(NULL),
panel(NULL), panel(NULL),
lock(false) lock(false)
{ {
QRegExp rx(CHAR_FOR_NAMES_REGEX); if (updateOnChange)
setValidator(new QRegExpValidator(rx, this)); connect(this, &AutoLineEdit::textChanged, this, &AutoLineEdit::onEdited);
connect(this, SIGNAL(editingFinished()), this, SLOT(onEdited())); else
connect(this, &AutoLineEdit::editingFinished, this, &AutoLineEdit::onEdited);
} }
void setField(char * field, int len, GenericPanel * panel=NULL) void setField(char * field, int len, GenericPanel * panel = nullptr)
{ {
this->field = field; this->field = field;
this->panel = panel; this->panel = panel;
setValidator(new QRegExpValidator(QRegExp("[ A-Za-z0-9_.-,]*"), this));
setMaxLength(len); setMaxLength(len);
updateValue(); updateValue();
} }
void setField(QString & field, int len = 0, GenericPanel * panel = nullptr)
{
strField = &field;
this->panel = panel;
if (len)
setMaxLength(len);
updateValue();
}
void updateValue() void updateValue()
{ {
lock = true; lock = true;
if (field) { if (field)
setText(field); setText(field);
} else if (strField)
setText(*strField);
lock = false; lock = false;
} }
protected slots: protected slots:
void onEdited() void onEdited()
{ {
if (field && !lock) { if (lock)
return;
if (field)
strcpy(field, text().toLatin1()); strcpy(field, text().toLatin1());
if (panel) { else if (strField)
emit panel->modified(); *strField = text();
} else
} return;
if (panel)
emit panel->modified();
} }
protected: protected:
char * field; char * field;
QString * strField;
GenericPanel * panel; GenericPanel * panel;
bool lock; bool lock;
}; };