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 "browser.h"
|
---|
28 | #include "editor.h"
|
---|
29 | #include <private/qrichtext_p.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | EditorBrowser::EditorBrowser( Editor *e )
|
---|
33 | : curEditor( e ), oldHighlightedParag( 0 )
|
---|
34 | {
|
---|
35 | curEditor = e;
|
---|
36 |
|
---|
37 | // ### disabled for now
|
---|
38 | // curEditor->viewport()->installEventFilter( this );
|
---|
39 | // curEditor->installEventFilter( this );
|
---|
40 |
|
---|
41 | QFont fn( curEditor->font() );
|
---|
42 | fn.setUnderline( TRUE );
|
---|
43 | highlightedFormat = new QTextFormat( fn, blue );
|
---|
44 | }
|
---|
45 |
|
---|
46 | EditorBrowser::~EditorBrowser()
|
---|
47 | {
|
---|
48 | delete highlightedFormat;
|
---|
49 | }
|
---|
50 |
|
---|
51 | bool EditorBrowser::eventFilter( QObject *o, QEvent *e )
|
---|
52 | {
|
---|
53 | if ( ::qt_cast<Editor*>(o->parent()) || ::qt_cast<Editor*>(o) ) {
|
---|
54 | QMouseEvent *me;
|
---|
55 | QKeyEvent *ke;
|
---|
56 | switch ( e->type() ) {
|
---|
57 | case QEvent::MouseMove:
|
---|
58 | me = (QMouseEvent*)e;
|
---|
59 | if ( ( me->state() & ControlButton ) == ControlButton ) {
|
---|
60 | curEditor->viewport()->setCursor( pointingHandCursor );
|
---|
61 | QTextCursor c( curEditor->document() );
|
---|
62 | curEditor->placeCursor( curEditor->viewportToContents( me->pos() ), &c );
|
---|
63 | QTextCursor from, to;
|
---|
64 | if ( oldHighlightedParag ) {
|
---|
65 | oldHighlightedParag->setEndState( -1 );
|
---|
66 | oldHighlightedParag->format();
|
---|
67 | oldHighlightedParag = 0;
|
---|
68 | }
|
---|
69 | if ( findCursor( c, from, to ) && from.paragraph() == to.paragraph() ) {
|
---|
70 | // avoid collision with other selections
|
---|
71 | for ( int i = 0; i < curEditor->document()->numSelections(); ++i )
|
---|
72 | curEditor->document()->removeSelection( i );
|
---|
73 | from.paragraph()->setFormat( from.index(), to.index() - from.index() + 1, highlightedFormat, FALSE );
|
---|
74 | lastWord = from.paragraph()->string()->toString().mid( from.index(), to.index() - from.index() + 1 );
|
---|
75 | oldHighlightedParag = from.paragraph();
|
---|
76 | } else {
|
---|
77 | lastWord = "";
|
---|
78 | }
|
---|
79 | curEditor->repaintChanged();
|
---|
80 | return TRUE;
|
---|
81 | }
|
---|
82 | break;
|
---|
83 | case QEvent::MouseButtonPress: {
|
---|
84 | bool killEvent = !lastWord.isEmpty();
|
---|
85 | if ( !lastWord.isEmpty() )
|
---|
86 | showHelp( lastWord );
|
---|
87 | lastWord = "";
|
---|
88 | curEditor->viewport()->setCursor( ibeamCursor );
|
---|
89 | if ( oldHighlightedParag ) {
|
---|
90 | oldHighlightedParag->setEndState( -1 );
|
---|
91 | oldHighlightedParag->format();
|
---|
92 | curEditor->repaintChanged();
|
---|
93 | oldHighlightedParag = 0;
|
---|
94 | }
|
---|
95 | if ( killEvent )
|
---|
96 | return TRUE;
|
---|
97 | } break;
|
---|
98 | case QEvent::KeyRelease:
|
---|
99 | lastWord = "";
|
---|
100 | ke = (QKeyEvent*)e;
|
---|
101 | if ( ke->key() == Key_Control ) {
|
---|
102 | curEditor->viewport()->setCursor( ibeamCursor );
|
---|
103 | if ( oldHighlightedParag ) {
|
---|
104 | oldHighlightedParag->setEndState( -1 );
|
---|
105 | oldHighlightedParag->format();
|
---|
106 | curEditor->repaintChanged();
|
---|
107 | oldHighlightedParag = 0;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | default:
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return FALSE;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void EditorBrowser::setCurrentEdior( Editor *e )
|
---|
118 | {
|
---|
119 | curEditor = e;
|
---|
120 | curEditor->installEventFilter( this );
|
---|
121 | }
|
---|
122 |
|
---|
123 | void EditorBrowser::addEditor( Editor *e )
|
---|
124 | {
|
---|
125 | e->installEventFilter( this );
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool EditorBrowser::findCursor( const QTextCursor &c, QTextCursor &from, QTextCursor &to )
|
---|
129 | {
|
---|
130 | from = c;
|
---|
131 | while ( from.paragraph()->at( from.index() )->c != ' ' && from.paragraph()->at( from.index() )->c != '\t' && from.index() > 0 )
|
---|
132 | from.gotoLeft();
|
---|
133 | if ( from.paragraph()->at( from.index() )->c == ' ' || from.paragraph()->at( from.index() )->c == '\t' )
|
---|
134 | from.gotoRight();
|
---|
135 | to = c;
|
---|
136 | while ( to.paragraph()->at( to.index() )->c != ' ' && to.paragraph()->at( to.index() )->c != '\t' &&
|
---|
137 | to.index() < to.paragraph()->length() - 1 )
|
---|
138 | to.gotoRight();
|
---|
139 | if ( to.paragraph()->at( to.index() )->c == ' ' || to.paragraph()->at( to.index() )->c == '\t' )
|
---|
140 | to.gotoLeft();
|
---|
141 | return TRUE;
|
---|
142 | }
|
---|