From ff82a575f6dc02838b3a30f4a57ec2d1fb783517 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Thu, 28 Jun 2018 04:48:14 -0400 Subject: [PATCH] [Companion][AutoLineEdit] Add ability to work with QString data type w/out default validation. --- companion/src/shared/autolineedit.h | 46 ++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/companion/src/shared/autolineedit.h b/companion/src/shared/autolineedit.h index d063bc2e8..36d848972 100644 --- a/companion/src/shared/autolineedit.h +++ b/companion/src/shared/autolineedit.h @@ -25,54 +25,72 @@ #include #include "genericpanel.h" -#define CHAR_FOR_NAMES_REGEX "[ A-Za-z0-9_.-,]*" - class AutoLineEdit: public QLineEdit { Q_OBJECT public: - explicit AutoLineEdit(QWidget *parent = 0): + explicit AutoLineEdit(QWidget *parent = nullptr, bool updateOnChange = false): QLineEdit(parent), field(NULL), + strField(NULL), panel(NULL), lock(false) { - QRegExp rx(CHAR_FOR_NAMES_REGEX); - setValidator(new QRegExpValidator(rx, this)); - connect(this, SIGNAL(editingFinished()), this, SLOT(onEdited())); + if (updateOnChange) + connect(this, &AutoLineEdit::textChanged, this, &AutoLineEdit::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->panel = panel; + setValidator(new QRegExpValidator(QRegExp("[ A-Za-z0-9_.-,]*"), this)); setMaxLength(len); updateValue(); } + void setField(QString & field, int len = 0, GenericPanel * panel = nullptr) + { + strField = &field; + this->panel = panel; + if (len) + setMaxLength(len); + updateValue(); + } + void updateValue() { lock = true; - if (field) { + if (field) setText(field); - } + else if (strField) + setText(*strField); lock = false; } protected slots: void onEdited() { - if (field && !lock) { + if (lock) + return; + + if (field) strcpy(field, text().toLatin1()); - if (panel) { - emit panel->modified(); - } - } + else if (strField) + *strField = text(); + else + return; + + if (panel) + emit panel->modified(); } protected: char * field; + QString * strField; GenericPanel * panel; bool lock; };