summaryrefslogtreecommitdiffstats
path: root/src/gui/numberconversionform.cpp
blob: 8481a9ba7366bf1a360da1c1099d7e9e7a89bb7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "numberconversionform.h"

#include <QRegExpValidator>
#include "gui.h"

/*
 *  Constructs a NumberConversionForm which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  true to construct a modal dialog.
 */
NumberConversionForm::NumberConversionForm(QWidget* parent)
    : QDialog(parent)
{
	setupUi(this);
}

/*
 *  Destroys the object and frees any allocated resources
 */
NumberConversionForm::~NumberConversionForm()
{
    // no need to delete child widgets, Qt does it all for us
}

void NumberConversionForm::init()
{
	QRegExp rxNoAtSign("[^@]*");

	exprLineEdit->setValidator(new QRegExpValidator(rxNoAtSign, this));
	replaceLineEdit->setValidator(new QRegExpValidator(rxNoAtSign, this));
}

int NumberConversionForm::exec(QString &expr, QString &replace)
{
	exprLineEdit->setText(expr);
	replaceLineEdit->setText(replace);
	int retval = QDialog::exec();

	if (retval == QDialog::Accepted) {
		expr = exprLineEdit->text();
		replace = replaceLineEdit->text();
	}

	return retval;
}

void NumberConversionForm::validate()
{
	QString expr = exprLineEdit->text();
	QString replace = replaceLineEdit->text();

	if (expr.isEmpty()) {
		((t_gui *)ui)->cb_show_msg(this,
            tr("Match expression may not be empty.").toStdString(), MSG_CRITICAL);
		exprLineEdit->setFocus();
		exprLineEdit->selectAll();
		return;
	}

	if (replace.isEmpty()) {
		((t_gui *)ui)->cb_show_msg(this,
            tr("Replace value may not be empty.").toStdString(), MSG_CRITICAL);
		replaceLineEdit->setFocus();
		replaceLineEdit->selectAll();
		return;
	}

	try {
        std::regex re(expr.toStdString());
    } catch (std::regex_error) {
		((t_gui *)ui)->cb_show_msg(this,
            tr("Invalid regular expression.").toStdString(), MSG_CRITICAL);
		exprLineEdit->setFocus();
		return;
	}

	accept();
}