~charon-developers/tuchulcha/supernodes

« back to all changes in this revision

Viewing changes to src/GraphModel.cpp

  • Committer: Jens Malte Gottfried
  • Date: 2013-07-04 16:29:23 UTC
  • Revision ID: jmgottfried@web.de-20130704162923-htru5p4jlfp5jxkl
added input validation to add/rename node

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <sstream>
32
32
#include <set>
33
33
#include <algorithm>
34
 
#include <QInputDialog>
 
34
#include "QTextInputDialog.h"
35
35
#include <stdexcept>
 
36
#include <QRegExpValidator>
36
37
 
37
38
/// transform std::string into lowercase
38
39
/** \param[in] input  string to transform
178
179
                return;
179
180
 
180
181
        // identify input and output slot
181
 
    if(!isInputSlot(source)) {
 
182
        if(!isInputSlot(source)) {
182
183
                // swap source and target
183
184
                qSwap(source,target);
184
185
        }
185
186
 
186
187
        // disconnect input slot, if assigned and not multi slot
187
 
    if (!isMultiSlot(source)) {
 
188
        if (!isMultiSlot(source)) {
188
189
                QString val = getValue(source);
189
190
                if (!val.isEmpty())
190
191
                        disconnectSlot(source, val, false);
260
261
        emit statusMessage(tr("disconnected all slots of node %1").arg(node));
261
262
}
262
263
 
 
264
const QRegExp GraphModel::instanceNameCheck("[a-z][a-zA-Z0-9_\\-]*");
 
265
 
263
266
void GraphModel::renameNode(QString nodename, bool draw) {
264
267
        nodename = nodename.section(".",0,0).toLower();
265
268
        bool ok;
266
 
        QString newName = QInputDialog::getText(
 
269
        QRegExpValidator validator(instanceNameCheck);
 
270
        QString newName = QTextInputDialog::getText(
267
271
                        0, tr("rename node"),
268
272
                        tr("Enter new name for node \"%1\":").arg(nodename),
269
 
                        QLineEdit::Normal, nodename, &ok);
 
273
                        QLineEdit::Normal, nodename, &ok,0,Qt::ImhNone,
 
274
                        &validator);
270
275
        newName = newName.toLower() ;
271
276
        if (ok) {
272
277
                if(nodeValid(newName)) {
408
413
 
409
414
QString GraphModel::addNode(QString className, bool draw) {
410
415
        QString info, newName, baseName = className.toLower();
 
416
        // cNameCheck matches input with dots and/or spaces (invalid)
 
417
        // the captured part (cap(1)) is the valid part before the spaces/dots
411
418
        QRegExp cNameCheck("([\\w]+)[\\s\\.]+.*");
412
419
        if (cNameCheck.exactMatch(baseName)) {
413
420
                baseName = cNameCheck.cap(1).toLower();
414
421
                Q_ASSERT(!baseName.isEmpty());
415
422
        }
416
423
        bool retry = false;
 
424
        QRegExpValidator validator(instanceNameCheck);
417
425
 
418
426
        // loop until a valid name is found or add node canceled
419
427
        do {
425
433
                } while (nodeValid(newName));
426
434
 
427
435
                // ask user
428
 
                newName = QInputDialog::getText(
 
436
                newName = QTextInputDialog::getText(
429
437
                                0, tr("add new node"),
430
438
                                info + tr("Enter a name for the new node:"),
431
 
                                QLineEdit::Normal, newName).trimmed();
 
439
                                QLineEdit::Normal, newName,
 
440
                                0,0,Qt::ImhNone,&validator).trimmed();
432
441
 
433
442
                if (newName.isEmpty()) {
434
443
                        return QString();  // cancel adding node