-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexpdbeditor.cpp
176 lines (154 loc) · 5.49 KB
/
regexpdbeditor.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "regexpdbeditor.h"
RegExpDbEditor::RegExpDbEditor(QWidget *parent) :
QWidget(parent)
{
db = new RegExpDb();
/* Create toolbar */
QToolBar *toolBar1 = new QToolBar(this);
addAction = new QAction("Add", this);
delAction = new QAction("Del", this);
editAction = new QAction("Edit", this);
insertAction = new QAction("Insert", this);
toolBar1->addAction(addAction);
toolBar1->addAction(editAction);
toolBar1->addAction(delAction);
QToolBar *toolBar2 = new QToolBar(this);
QLabel *label = new QLabel("Search:", this);
searchLineEdit = new QLineEdit(this);
toolBar2->addWidget(label);
toolBar2->addWidget(searchLineEdit);
QSplitter *splitter = new QSplitter(Qt::Vertical, this);
regExpListView = new QListView(this);
regExpListView->setModel(db->model);
regExpListView->setModelColumn(0);
regExpEdit = new QTextEdit(this);
regExpEdit->setReadOnly(true);
splitter->addWidget(regExpListView);
splitter->addWidget(regExpEdit);
splitter->setStretchFactor(0, 3);
splitter->setStretchFactor(1, 1);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(toolBar1);
mainLayout->addWidget(toolBar2);
mainLayout->addWidget(splitter);
mainLayout->setContentsMargins(0,0,0,0);
mainLayout->setSpacing(0);
setLayout(mainLayout);
connect(addAction, SIGNAL(triggered()), this, SLOT(addRegExp()));
connect(delAction, SIGNAL(triggered()), this, SLOT(delRegExp()));
connect(editAction, SIGNAL(triggered()), this, SLOT(editRegExp()));
connect(insertAction, SIGNAL(triggered()), this, SLOT(insertRegExp()));
connect(regExpListView, SIGNAL(clicked(QModelIndex)), this, SLOT(currentItemChanged(QModelIndex)));
connect(searchLineEdit, SIGNAL(textChanged(QString)), this, SLOT(findItem(QString)));
regExpListView->viewport()->installEventFilter(this);
}
void RegExpDbEditor::addRegExp()
{
AddEditDialog dialog(0, this);
connect(&dialog, SIGNAL(okPressed()), this, SLOT(addEditRegExpCheck()));
dialog.exec();
disconnect(&dialog);
}
void RegExpDbEditor::editRegExp()
{
if(regExpListView->currentIndex().isValid())
{
QString name = regExpListView->currentIndex().data().toString();
QString value = db->getValue(name);
AddEditDialog dialog(1, this);
dialog.setName(name);
dialog.setValue(value);
connect(&dialog, SIGNAL(okPressed()), this, SLOT(addEditRegExpCheck()));
dialog.exec();
disconnect(&dialog);
}
else
QMessageBox::warning(this, "Error", "Select row", QMessageBox::Ok);
}
void RegExpDbEditor::addEditRegExpCheck()
{
AddEditDialog *dialog = (AddEditDialog*) sender();
if(dialog->isEdit)
{
if(db->updateRow(dialog->getPrevName(), dialog->getName(), dialog->getValue()))
dialog->accept();
else
{
QString errorStr = db->lastError();
if(errorStr.contains("column name is not unique"))
{
errorStr = "Record with name \"%1\" already exist";
errorStr = errorStr.arg(dialog->getName());
}
QMessageBox::warning(this, "Error", errorStr, QMessageBox::Ok);
}
}
else
{
if(db->insertRow(dialog->getName(), dialog->getValue()))
dialog->accept();
else
{
QString errorStr = db->lastError();
if(errorStr.contains("column name is not unique"))
{
errorStr = "Record with name \"%1\" already exist";
errorStr = errorStr.arg(dialog->getName());
}
QMessageBox::warning(this, "Error", errorStr, QMessageBox::Ok);
}
}
}
void RegExpDbEditor::delRegExp()
{
if(regExpListView->currentIndex().isValid())
{
QString str = regExpListView->currentIndex().data().toString();
if(QMessageBox::question(this, "Confirm",
"Are you sure to delete record?",
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok)
{
db->deleteRow(str);
db->model->select();
}
}
else
QMessageBox::warning(this, "Error", "Select row", QMessageBox::Ok);
}
void RegExpDbEditor::insertRegExp()
{
if(regExpListView->currentIndex().isValid())
{
QString name = regExpListView->currentIndex().data().toString();
emit insertRegExp(db->getValue(name));
}
else
QMessageBox::warning(this, "Error", "Select row", QMessageBox::Ok);
}
void RegExpDbEditor::currentItemChanged(QModelIndex index)
{
QModelIndex temp = db->model->index(index.row(), 1);
regExpEdit->setText(temp.data().toString());
}
void RegExpDbEditor::findItem(QString pattern)
{
db->model->setFilter(QString("name LIKE \'").append(pattern).append("%\'"));
}
bool RegExpDbEditor::eventFilter(QObject *watched, QEvent* e)
{
if (watched == regExpListView->viewport() && e->type() == QEvent::MouseButtonDblClick)
{
return true;
}
return QWidget::eventFilter(watched, e);
}
void RegExpDbEditor::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu;
menu.addAction(addAction);
menu.addAction(editAction);
menu.addAction(delAction);
menu.addSeparator();
menu.addAction(insertAction);
menu.exec(event->globalPos());
}