source: trunk/tools/designer/editor/parenmatcher.cpp

Last change on this file was 197, checked in by rudi, 14 years ago

Added QtDesigner

File size: 5.6 KB
Line 
1/**********************************************************************
2** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12** licenses may use this file in accordance with the Qt Commercial License
13** Agreement provided with the Software.
14**
15** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17**
18** See http://www.trolltech.com/gpl/ for GPL licensing information.
19** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20** information about Qt Commercial License Agreements.
21**
22** Contact info@trolltech.com if any conditions of this licensing are
23** not clear to you.
24**
25**********************************************************************/
26
27#include "parenmatcher.h"
28#include "paragdata.h"
29
30#include "qtextedit.h"
31#include <private/qrichtext_p.h>
32#include <qapplication.h>
33
34ParenMatcher::ParenMatcher()
35{
36 enabled = TRUE;
37}
38
39bool ParenMatcher::match( QTextCursor *cursor )
40{
41 if ( !enabled )
42 return FALSE;
43 bool ret = FALSE;
44
45 QChar c( cursor->paragraph()->at( cursor->index() )->c );
46 bool ok1 = FALSE;
47 bool ok2 = FALSE;
48 if ( c == '{' || c == '(' || c == '[' ) {
49 ok1 = checkOpenParen( cursor );
50 ret = ok1 || ret;
51 } else if ( cursor->index() > 0 ) {
52 c = cursor->paragraph()->at( cursor->index() - 1 )->c;
53 if ( c == '}' || c == ')' || c == ']' ) {
54 ok2 = checkClosedParen( cursor );
55 ret = ok2 || ret;
56 }
57 }
58
59 return ret;
60}
61
62bool ParenMatcher::checkOpenParen( QTextCursor *cursor )
63{
64 if ( !cursor->paragraph()->extraData() )
65 return FALSE;
66 ParenList parenList = ( (ParagData*)cursor->paragraph()->extraData() )->parenList;
67
68 Paren openParen, closedParen;
69 QTextParagraph *closedParenParag = cursor->paragraph();
70
71 int i = 0;
72 int ignore = 0;
73 bool foundOpen = FALSE;
74 QChar c = cursor->paragraph()->at( cursor->index() )->c;
75 for (;;) {
76 if ( !foundOpen ) {
77 if ( i >= (int)parenList.count() )
78 goto bye;
79 openParen = parenList[ i ];
80 if ( openParen.pos != cursor->index() ) {
81 ++i;
82 continue;
83 } else {
84 foundOpen = TRUE;
85 ++i;
86 }
87 }
88
89 if ( i >= (int)parenList.count() ) {
90 for (;;) {
91 closedParenParag = closedParenParag->next();
92 if ( !closedParenParag )
93 goto bye;
94 if ( closedParenParag->extraData() &&
95 ( (ParagData*)closedParenParag->extraData() )->parenList.count() > 0 ) {
96 parenList = ( (ParagData*)closedParenParag->extraData() )->parenList;
97 break;
98 }
99 }
100 i = 0;
101 }
102
103 closedParen = parenList[ i ];
104 if ( closedParen.type == Paren::Open ) {
105 ignore++;
106 ++i;
107 continue;
108 } else {
109 if ( ignore > 0 ) {
110 ignore--;
111 ++i;
112 continue;
113 }
114
115 int id = Match;
116 if ( c == '{' && closedParen.chr != '}' ||
117 c == '(' && closedParen.chr != ')' ||
118 c == '[' && closedParen.chr != ']' )
119 id = Mismatch;
120 cursor->document()->setSelectionStart( id, *cursor );
121 int tidx = cursor->index();
122 QTextParagraph *tstring = cursor->paragraph();
123 cursor->setParagraph( closedParenParag );
124 cursor->setIndex( closedParen.pos + 1 );
125 cursor->document()->setSelectionEnd( id, *cursor );
126 cursor->setParagraph( tstring );
127 cursor->setIndex( tidx );
128 return TRUE;
129 }
130 }
131
132 bye:
133 return FALSE;
134}
135
136bool ParenMatcher::checkClosedParen( QTextCursor *cursor )
137{
138 if ( !cursor->paragraph()->extraData() )
139 return FALSE;
140 ParenList parenList = ( (ParagData*)cursor->paragraph()->extraData() )->parenList;
141
142 Paren openParen, closedParen;
143 QTextParagraph *openParenParag = cursor->paragraph();
144
145 int i = (int)parenList.count() - 1;
146 int ignore = 0;
147 bool foundClosed = FALSE;
148 QChar c = cursor->paragraph()->at( cursor->index() - 1 )->c;
149 for (;;) {
150 if ( !foundClosed ) {
151 if ( i < 0 )
152 goto bye;
153 closedParen = parenList[ i ];
154 if ( closedParen.pos != cursor->index() - 1 ) {
155 --i;
156 continue;
157 } else {
158 foundClosed = TRUE;
159 --i;
160 }
161 }
162
163 if ( i < 0 ) {
164 for (;;) {
165 openParenParag = openParenParag->prev();
166 if ( !openParenParag )
167 goto bye;
168 if ( openParenParag->extraData() &&
169 ( (ParagData*)openParenParag->extraData() )->parenList.count() > 0 ) {
170 parenList = ( (ParagData*)openParenParag->extraData() )->parenList;
171 break;
172 }
173 }
174 i = (int)parenList.count() - 1;
175 }
176
177 openParen = parenList[ i ];
178 if ( openParen.type == Paren::Closed ) {
179 ignore++;
180 --i;
181 continue;
182 } else {
183 if ( ignore > 0 ) {
184 ignore--;
185 --i;
186 continue;
187 }
188
189 int id = Match;
190 if ( c == '}' && openParen.chr != '{' ||
191 c == ')' && openParen.chr != '(' ||
192 c == ']' && openParen.chr != '[' )
193 id = Mismatch;
194 cursor->document()->setSelectionStart( id, *cursor );
195 int tidx = cursor->index();
196 QTextParagraph *tstring = cursor->paragraph();
197 cursor->setParagraph( openParenParag );
198 cursor->setIndex( openParen.pos );
199 cursor->document()->setSelectionEnd( id, *cursor );
200 cursor->setParagraph( tstring );
201 cursor->setIndex( tidx );
202 return TRUE;
203 }
204 }
205
206 bye:
207 return FALSE;
208}
Note: See TracBrowser for help on using the repository browser.