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 "editor.h"
|
---|
28 | #include "parenmatcher.h"
|
---|
29 | #include <qfile.h>
|
---|
30 | #include <private/qrichtext_p.h>
|
---|
31 | #include "conf.h"
|
---|
32 | #include <qapplication.h>
|
---|
33 | #include <qpopupmenu.h>
|
---|
34 | #include <qaccel.h>
|
---|
35 |
|
---|
36 | Editor::Editor( const QString &fn, QWidget *parent, const char *name )
|
---|
37 | : QTextEdit( parent, name ), hasError( FALSE )
|
---|
38 | {
|
---|
39 | document()->setFormatter( new QTextFormatterBreakInWords );
|
---|
40 | if ( !fn.isEmpty() )
|
---|
41 | load( fn );
|
---|
42 | setHScrollBarMode( QScrollView::AlwaysOff );
|
---|
43 | setVScrollBarMode( QScrollView::AlwaysOn );
|
---|
44 | document()->setUseFormatCollection( FALSE );
|
---|
45 | parenMatcher = new ParenMatcher;
|
---|
46 | connect( this, SIGNAL( cursorPositionChanged( QTextCursor * ) ),
|
---|
47 | this, SLOT( cursorPosChanged( QTextCursor * ) ) );
|
---|
48 | cfg = new Config;
|
---|
49 | document()->addSelection( Error );
|
---|
50 | document()->addSelection( Step );
|
---|
51 | document()->setSelectionColor( Error, red );
|
---|
52 | document()->setSelectionColor( Step, yellow );
|
---|
53 | document()->setInvertSelectionText( Error, FALSE );
|
---|
54 | document()->setInvertSelectionText( Step, FALSE );
|
---|
55 | document()->addSelection( ParenMatcher::Match );
|
---|
56 | document()->addSelection( ParenMatcher::Mismatch );
|
---|
57 | document()->setSelectionColor( ParenMatcher::Match, QColor( 204, 232, 195 ) );
|
---|
58 | document()->setSelectionColor( ParenMatcher::Mismatch, Qt::magenta );
|
---|
59 | document()->setInvertSelectionText( ParenMatcher::Match, FALSE );
|
---|
60 | document()->setInvertSelectionText( ParenMatcher::Mismatch, FALSE );
|
---|
61 |
|
---|
62 | accelComment = new QAccel( this );
|
---|
63 | accelComment->connectItem( accelComment->insertItem( ALT + Key_C ),
|
---|
64 | this, SLOT( commentSelection() ) );
|
---|
65 | accelUncomment = new QAccel( this );
|
---|
66 | accelUncomment->connectItem( accelUncomment->insertItem( ALT + Key_U ),
|
---|
67 | this, SLOT( uncommentSelection() ) );
|
---|
68 | editable = TRUE;
|
---|
69 | }
|
---|
70 |
|
---|
71 | Editor::~Editor()
|
---|
72 | {
|
---|
73 | delete cfg;
|
---|
74 | delete parenMatcher;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void Editor::cursorPosChanged( QTextCursor *c )
|
---|
78 | {
|
---|
79 | if ( parenMatcher->match( c ) )
|
---|
80 | repaintChanged();
|
---|
81 | if ( hasError ) {
|
---|
82 | emit clearErrorMarker();
|
---|
83 | hasError = FALSE;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | void Editor::load( const QString &fn )
|
---|
88 | {
|
---|
89 | filename = fn;
|
---|
90 | QFile f( filename );
|
---|
91 | if ( !f.open( IO_ReadOnly ) )
|
---|
92 | return;
|
---|
93 | QCString txt;
|
---|
94 | txt.resize( f.size() );
|
---|
95 | f.readBlock( txt.data(), f.size() );
|
---|
96 | QString s( QString::fromLatin1( txt ) );
|
---|
97 | setText( s );
|
---|
98 | }
|
---|
99 |
|
---|
100 | void Editor::save( const QString &fn )
|
---|
101 | {
|
---|
102 | if ( !filename.isEmpty() )
|
---|
103 | filename = fn;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void Editor::configChanged()
|
---|
107 | {
|
---|
108 | document()->invalidate();
|
---|
109 | viewport()->repaint( FALSE );
|
---|
110 | }
|
---|
111 |
|
---|
112 | void Editor::setErrorSelection( int line )
|
---|
113 | {
|
---|
114 | QTextParagraph *p = document()->paragAt( line );
|
---|
115 | if ( !p )
|
---|
116 | return;
|
---|
117 | QTextCursor c( document() );
|
---|
118 | c.setParagraph( p );
|
---|
119 | c.setIndex( 0 );
|
---|
120 | document()->removeSelection( Error );
|
---|
121 | document()->setSelectionStart( Error, c );
|
---|
122 | c.gotoLineEnd();
|
---|
123 | document()->setSelectionEnd( Error, c );
|
---|
124 | hasError = TRUE;
|
---|
125 | viewport()->repaint( FALSE );
|
---|
126 | }
|
---|
127 |
|
---|
128 | void Editor::setStepSelection( int line )
|
---|
129 | {
|
---|
130 | QTextParagraph *p = document()->paragAt( line );
|
---|
131 | if ( !p )
|
---|
132 | return;
|
---|
133 | QTextCursor c( document() );
|
---|
134 | c.setParagraph( p );
|
---|
135 | c.setIndex( 0 );
|
---|
136 | document()->removeSelection( Step );
|
---|
137 | document()->setSelectionStart( Step, c );
|
---|
138 | c.gotoLineEnd();
|
---|
139 | document()->setSelectionEnd( Step, c );
|
---|
140 | viewport()->repaint( FALSE );
|
---|
141 | }
|
---|
142 |
|
---|
143 | void Editor::clearStepSelection()
|
---|
144 | {
|
---|
145 | document()->removeSelection( Step );
|
---|
146 | viewport()->repaint( FALSE );
|
---|
147 | }
|
---|
148 |
|
---|
149 | void Editor::doChangeInterval()
|
---|
150 | {
|
---|
151 | emit intervalChanged();
|
---|
152 | QTextEdit::doChangeInterval();
|
---|
153 | }
|
---|
154 |
|
---|
155 | void Editor::commentSelection()
|
---|
156 | {
|
---|
157 | QTextParagraph *start = document()->selectionStartCursor( QTextDocument::Standard ).paragraph();
|
---|
158 | QTextParagraph *end = document()->selectionEndCursor( QTextDocument::Standard ).paragraph();
|
---|
159 | if ( !start || !end )
|
---|
160 | start = end = textCursor()->paragraph();
|
---|
161 | while ( start ) {
|
---|
162 | if ( start == end && textCursor()->index() == 0 )
|
---|
163 | break;
|
---|
164 | start->insert( 0, "//" );
|
---|
165 | if ( start == end )
|
---|
166 | break;
|
---|
167 | start = start->next();
|
---|
168 | }
|
---|
169 | document()->removeSelection( QTextDocument::Standard );
|
---|
170 | repaintChanged();
|
---|
171 | setModified( TRUE );
|
---|
172 | }
|
---|
173 |
|
---|
174 | void Editor::uncommentSelection()
|
---|
175 | {
|
---|
176 | QTextParagraph *start = document()->selectionStartCursor( QTextDocument::Standard ).paragraph();
|
---|
177 | QTextParagraph *end = document()->selectionEndCursor( QTextDocument::Standard ).paragraph();
|
---|
178 | if ( !start || !end )
|
---|
179 | start = end = textCursor()->paragraph();
|
---|
180 | while ( start ) {
|
---|
181 | if ( start == end && textCursor()->index() == 0 )
|
---|
182 | break;
|
---|
183 | while ( start->at( 0 )->c == '/' )
|
---|
184 | start->remove( 0, 1 );
|
---|
185 | if ( start == end )
|
---|
186 | break;
|
---|
187 | start = start->next();
|
---|
188 | }
|
---|
189 | document()->removeSelection( QTextDocument::Standard );
|
---|
190 | repaintChanged();
|
---|
191 | setModified( TRUE );
|
---|
192 | }
|
---|
193 |
|
---|
194 | QPopupMenu *Editor::createPopupMenu( const QPoint &p )
|
---|
195 | {
|
---|
196 | QPopupMenu *menu = QTextEdit::createPopupMenu( p );
|
---|
197 | menu->insertSeparator();
|
---|
198 | menu->insertItem( tr( "C&omment Code\tAlt+C" ), this, SLOT( commentSelection() ) );
|
---|
199 | menu->insertItem( tr( "Unco&mment Code\tAlt+U" ), this, SLOT( uncommentSelection() ) );
|
---|
200 | return menu;
|
---|
201 | }
|
---|
202 |
|
---|
203 | bool Editor::eventFilter( QObject *o, QEvent *e )
|
---|
204 | {
|
---|
205 | if ( ( e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut ) &&
|
---|
206 | ( o == this || o == viewport() ) ) {
|
---|
207 | accelUncomment->setEnabled( e->type() == QEvent::FocusIn );
|
---|
208 | accelComment->setEnabled( e->type() == QEvent::FocusIn );
|
---|
209 | }
|
---|
210 | return QTextEdit::eventFilter( o, e );
|
---|
211 | }
|
---|
212 |
|
---|
213 | void Editor::doKeyboardAction( KeyboardAction action )
|
---|
214 | {
|
---|
215 | if ( !editable )
|
---|
216 | return;
|
---|
217 | QTextEdit::doKeyboardAction( action );
|
---|
218 | }
|
---|
219 |
|
---|
220 | void Editor::keyPressEvent( QKeyEvent *e )
|
---|
221 | {
|
---|
222 | if ( editable ) {
|
---|
223 | QTextEdit::keyPressEvent( e );
|
---|
224 | return;
|
---|
225 | }
|
---|
226 |
|
---|
227 | switch ( e->key() ) {
|
---|
228 | case Key_Left:
|
---|
229 | case Key_Right:
|
---|
230 | case Key_Up:
|
---|
231 | case Key_Down:
|
---|
232 | case Key_Home:
|
---|
233 | case Key_End:
|
---|
234 | case Key_Prior:
|
---|
235 | case Key_Next:
|
---|
236 | case Key_Direction_L:
|
---|
237 | case Key_Direction_R:
|
---|
238 | QTextEdit::keyPressEvent( e );
|
---|
239 | break;
|
---|
240 | default:
|
---|
241 | e->accept();
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|