/
expressiongraphicsitem.cpp
280 lines (255 loc) · 8.45 KB
/
expressiongraphicsitem.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*!
* \file
*
* Copyright (c) 2011,2012 Alex Elliott <alex@alex-elliott.co.uk>
*
* \section LICENSE
* This file is part of Expression editor
*
* Expression editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Expression editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Expression editor. If not, see <http://www.gnu.org/licenses/>.
*/
#include "expressiongraphicsitem.hpp"
ExpressionGraphicsItem::ExpressionGraphicsItem(QString expression, RegexFactory::RegexFormat format, QGraphicsItem *parent)
: QGraphicsWidget(parent)
, _expression(expression)
, _pos(0)
{
QSettings settings;
_itemSpacing = settings.value("Visualisation/HorizontalSpacing", 8.0).toDouble();
_factory = new RegexFactory(format);
Parser *parser = _factory->regexpParser();
parser->parse(_expression);
_tokens = parser->tokens();
_root = new QGraphicsLinearLayout(Qt::Horizontal);
_root->setSpacing(_itemSpacing);
for(_iter = _tokens.begin(); _iter != _tokens.end();)
{
_incremented = false;
_root = handleToken(**_iter, _root);
// The iterator can be incremented from within handleToken, so we must
// ensure we do not run over the end
if(_iter != _tokens.end() && !_incremented)
{
++_iter;
++_pos;
}
}
_root->setContentsMargins(10, 10, 10, 10);
setLayout(_root);
delete parser;
}
ExpressionGraphicsItem::~ExpressionGraphicsItem()
{
delete _factory;
}
QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphicsLinearLayout *currentLayout)
{
RegexGraphicsItem *newItem;
RepeatGraphicsItem *repeat;
QGraphicsLinearLayout *newLayout;
bool defaultCapturing = true;
bool named = false;
_incremented = false;
switch(token.type())
{
case T_LITERAL:
newItem = new LiteralGraphicsItem(*_iter, _pos);
currentLayout->addItem(newItem);
//currentLayout->setAlignment(newItem, Qt::AlignVCenter | Qt::AlignLeft);
_lastItem = newItem;
break;
case T_ALTERNATION:
{
// This is a bit nasty, but it works
int start = static_cast<RegexGraphicsItem *>(currentLayout->itemAt(0))->tokenPos();
AlternationGraphicsItem *alternation = new AlternationGraphicsItem(currentLayout, *_iter, start);
currentLayout = new QGraphicsLinearLayout(Qt::Horizontal);
currentLayout->setSpacing(_itemSpacing);
currentLayout->addItem(alternation);
++_iter;
++_pos;
_incremented = true;
while(_iter != _tokens.end() && (*_iter)->type() != T_GROUPING_CLOSE)
{
_incremented = false;
if((*_iter)->type() == T_ALTERNATION)
{
alternation->newBranch();
alternation->linearLayout()->setSpacing(_itemSpacing);
}
else
handleToken(**_iter, alternation->linearLayout());
if(_iter != _tokens.end() && !_incremented)
{
++_iter;
++_pos;
_incremented = true;
}
}
alternation->setEndPos(_pos-1);
_lastItem = alternation;
}
break;
case T_NAMED_GROUPING_OPEN:
case T_GROUPING_OPEN:
case T_REVERSED_CAPTURING_GROUPING_OPEN:
{
GroupingGraphicsItem *grouping = new GroupingGraphicsItem(*_iter, _pos, defaultCapturing);
currentLayout->addItem(grouping);
//currentLayout->setAlignment(grouping, Qt::AlignVCenter | Qt::AlignLeft);
++_iter;
++_pos;
_incremented = true;
newLayout = new QGraphicsLinearLayout(Qt::Horizontal);
newLayout->setSpacing(_itemSpacing);
while(_iter != _tokens.end() && (*_iter)->type() != T_GROUPING_CLOSE)
{
_incremented = false;
newLayout = handleToken(**_iter, newLayout);
if(_iter != _tokens.end() && !_incremented)
{
++_iter;
++_pos;
_incremented = true;
}
}
if((*_iter)->type() == T_GROUPING_CLOSE)
{
++_iter;
++_pos;
_incremented = true;
}
grouping->setLinearLayout(newLayout);
grouping->setEndPos(_pos-1);
_lastItem = grouping;
}
break;
case T_BRACKET_EXPRESSION_OPEN:
case T_NEGATED_BRACKET_EXPRESSION_OPEN:
{
BracketExpressionGraphicsItem *bracketExpression = new BracketExpressionGraphicsItem(*_iter, _pos);
currentLayout->addItem(bracketExpression);
++_iter;
++_pos;
_incremented = true;
while(_iter != _tokens.end() && (*_iter)->type() != T_BRACKET_EXPRESSION_CLOSE)
{
bracketExpression->addToken(*_iter);
++_iter;
++_pos;
}
if((*_iter)->type() == T_BRACKET_EXPRESSION_CLOSE)
{
++_iter;
++_pos;
}
bracketExpression->setEndPos(_pos-1);
_lastItem = bracketExpression;
}
break;
case T_STARTING_POSITION:
case T_ENDING_POSITION:
newItem = new AnchorGraphicsItem(*_iter, _pos);
currentLayout->addItem(newItem);
_lastItem = newItem;
break;
case T_WORD:
case T_NOT_WORD:
case T_DIGIT:
case T_NOT_DIGIT:
case T_SPACE:
case T_NOT_SPACE:
newItem = new CharacterClassItem(*_iter, _pos);
currentLayout->addItem(newItem);
_lastItem = newItem;
break;
case T_ANY_CHARACTER:
case T_BELL:
case T_BACKSPACE:
case T_ESCAPE:
case T_FORM_FEED:
case T_LINE_FEED:
case T_CARRIAGE_RETURN:
case T_HORIZONTAL_TAB:
case T_VERTICAL_TAB:
case T_ASCII_CONTROL_CHAR:
case T_UNICODE_NEWLINE:
case T_BYTE:
case T_OCTAL_CHAR:
case T_HEXADECIMAL_CHAR:
case T_UNICODE_CHAR:
case T_UNICODE_NAMED_CHAR:
case T_GRAPHEME_CLUSTER:
newItem = new SpecialCharGraphicsItem(*_iter, _pos);
currentLayout->addItem(newItem);
_lastItem = newItem;
break;
case T_REPEAT_ZERO_OR_ONE:
case T_REPEAT_ANY_NUMBER:
case T_REPEAT_ONE_OR_MORE:
case T_REPEAT_SPECIFIED:
currentLayout->removeItem(_lastItem);
repeat = new RepeatGraphicsItem(*_iter, _lastItem->tokenPos(), _lastItem);
repeat->setEndPos(_pos);
currentLayout->addItem(repeat);
_lastItem = repeat;
break;
case T_COMMENT_OPEN:
newItem = new CommentGraphicsItem(*_iter, _pos);
++_iter;
++_pos;
_incremented = true;
if(_iter != _tokens.end() && (*_iter)->type() == T_LITERAL)
{
currentLayout->addItem(newItem);
++_iter;
++_pos;
if(_iter != _tokens.end() && (*_iter)->type() == T_COMMENT_CLOSE)
{
++_iter;
++_pos;
}
}
else if(_iter != _tokens.end() && (*_iter)->type() == T_COMMENT_CLOSE)
{
currentLayout->addItem(newItem);
++_iter;
++_pos;
}
newItem->setEndPos(_pos-1);
_lastItem = newItem;
break;
default:
newItem = new ErrorGraphicsItem(*_iter, _pos);
currentLayout->addItem(newItem);
//currentLayout->setAlignment(newItem, Qt::AlignVCenter | Qt::AlignLeft);
_lastItem = newItem;
}
_lastItem->setGeometry(_lastItem->boundingRect());
currentLayout->setAlignment(_lastItem, Qt::AlignVCenter | Qt::AlignLeft);
currentLayout->setGeometry(QRectF(0, 0, currentLayout->sizeHint(Qt::PreferredSize).width(),
currentLayout->sizeHint(Qt::PreferredSize).height()));
return currentLayout;
}
QRectF ExpressionGraphicsItem::boundingRect() const
{
return QRectF(0, 0, _root->geometry().width()+20, _root->geometry().height() + 20 );
}
QSizeF ExpressionGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(which)
Q_UNUSED(constraint)
QRectF rect = boundingRect();
return QSizeF(rect.width(), rect.height());
}