source: vendor/trolltech/current/src/widgets/qmultilineedit.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 13.2 KB
Line 
1/**********************************************************************
2** $Id: qmultilineedit.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QMultiLineEdit widget class
5**
6** Created : 961005
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the widgets module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qmultilineedit.h"
39#ifndef QT_NO_MULTILINEEDIT
40#include "qpainter.h"
41#include "qscrollbar.h"
42#include "qcursor.h"
43#include "qclipboard.h"
44#include "qpixmap.h"
45#include "qregexp.h"
46#include "qapplication.h"
47#include "qdragobject.h"
48#include "qpopupmenu.h"
49#include "qtimer.h"
50#include "qdict.h"
51#include "../kernel/qrichtext_p.h"
52
53
54/*!
55 \class QMultiLineEdit qmultilineedit.h
56 \obsolete
57
58 \brief The QMultiLineEdit widget is a simple editor for inputting text.
59
60 \ingroup advanced
61
62 The QMultiLineEdit was a simple editor widget in former Qt versions. Qt
63 3.0 includes a new richtext engine which obsoletes QMultiLineEdit. It is
64 still included for compatibility reasons. It is now a subclass of
65 \l QTextEdit, and provides enough of the old QMultiLineEdit API to keep old
66 applications working.
67
68 If you implement something new with QMultiLineEdit, we suggest using
69 \l QTextEdit instead and call QTextEdit::setTextFormat(Qt::PlainText).
70
71 Although most of the old QMultiLineEdit API is still available, there is
72 a few difference. The old QMultiLineEdit operated on lines, not on
73 paragraphs. As lines change all the time during wordwrap, the new
74 richtext engine uses paragraphs as basic elements in the data structure.
75 All functions (numLines(), textLine(), etc.) that operated on lines, now
76 operate on paragraphs. Further, getString() has been removed completely.
77 It revealed too much of the internal data structure.
78
79 Applications which made normal and reasonable use of QMultiLineEdit
80 should still work without problems. Some odd usage will require some
81 porting. In these cases, it may be better to use \l QTextEdit now.
82
83 <img src=qmlined-m.png> <img src=qmlined-w.png>
84
85 \sa QTextEdit
86*/
87
88/*!
89 \fn bool QMultiLineEdit::autoUpdate() const
90 \obsolete
91*/
92
93/*!
94 \fn virtual void QMultiLineEdit::setAutoUpdate( bool )
95 \obsolete
96*/
97
98/*!
99 \fn int QMultiLineEdit::totalWidth() const
100 \obsolete
101*/
102
103/*!
104 \fn int QMultiLineEdit::totalHeight() const
105 \obsolete
106*/
107
108/*!
109 \fn int QMultiLineEdit::maxLines() const
110 \obsolete
111*/
112
113/*!
114 \fn void QMultiLineEdit::setMaxLines( int )
115 \obsolete
116*/
117
118/*!
119 \fn void QMultiLineEdit::deselect()
120 \obsolete
121*/
122
123
124class QMultiLineEditData
125{
126};
127
128
129/*!
130 Constructs a new, empty, QMultiLineEdit with parent \a parent called
131 \a name.
132*/
133
134QMultiLineEdit::QMultiLineEdit( QWidget *parent , const char *name )
135 : QTextEdit( parent, name )
136{
137 d = new QMultiLineEditData;
138 setTextFormat( Qt::PlainText );
139}
140
141/*! \property QMultiLineEdit::numLines
142 \brief the number of paragraphs in the editor
143
144 The count includes any empty paragraph at top and bottom, so for an
145 empty editor this method returns 1.
146*/
147
148int QMultiLineEdit::numLines() const
149{
150 return document()->lastParagraph()->paragId() + 1;
151}
152
153/*! \property QMultiLineEdit::atEnd
154 \brief whether the cursor is placed at the end of the text
155
156 \sa atBeginning
157*/
158
159bool QMultiLineEdit::atEnd() const
160{
161 return textCursor()->paragraph() == document()->lastParagraph() && textCursor()->atParagEnd();
162}
163
164
165/*! \property QMultiLineEdit::atBeginning
166 \brief whether the cursor is placed at the beginning of the text
167
168 \sa atEnd
169*/
170
171bool QMultiLineEdit::atBeginning() const
172{
173 return textCursor()->paragraph() == document()->firstParagraph() && textCursor()->atParagStart();
174}
175
176/*! Returns the number of characters at paragraph number \a row. If
177 \a row is out of range, -1 is returned.
178*/
179
180int QMultiLineEdit::lineLength( int row ) const
181{
182 if ( row < 0 || row > numLines() )
183 return -1;
184 return document()->paragAt( row )->length() - 1;
185}
186
187
188/*! \reimp */
189
190QMultiLineEdit::~QMultiLineEdit()
191{
192 delete d;
193}
194
195/*!
196 If there is selected text, sets \a line1, \a col1, \a line2 and \a col2
197 to the start and end of the selected region and returns TRUE. Returns
198 FALSE if there is no selected text.
199 */
200bool QMultiLineEdit::getMarkedRegion( int *line1, int *col1,
201 int *line2, int *col2 ) const
202{
203 int p1,c1, p2, c2;
204 getSelection( &p1, &c1, &p2, &c2 );
205 if ( p1 == -1 && c1 == -1 && p2 == -1 && c2 == -1 )
206 return FALSE;
207 if ( line1 )
208 *line1 = p1;
209 if ( col1 )
210 *col1 = c1;
211 if ( line2 )
212 *line2 = p2;
213 if ( col2 )
214 *col2 = c2;
215 return TRUE;
216}
217
218
219/*!
220 Returns TRUE if there is selected text.
221*/
222
223bool QMultiLineEdit::hasMarkedText() const
224{
225 return hasSelectedText();
226}
227
228
229/*!
230 Returns a copy of the selected text.
231*/
232
233QString QMultiLineEdit::markedText() const
234{
235 return selectedText();
236}
237
238/*!
239 Moves the cursor one page down. If \a mark is TRUE, the text
240 is selected.
241*/
242
243void QMultiLineEdit::pageDown( bool mark )
244{
245 moveCursor( MoveDown, mark );
246}
247
248
249/*!
250 Moves the cursor one page up. If \a mark is TRUE, the text
251 is selected.
252*/
253
254void QMultiLineEdit::pageUp( bool mark )
255{
256 moveCursor( MovePgUp, mark );
257}
258
259
260/*! Inserts \a txt at paragraph number \a line. If \a line is less
261 than zero, or larger than the number of paragraphs, the new text is
262 put at the end. If \a txt contains newline characters, several
263 paragraphs are inserted.
264
265 The cursor position is not changed.
266*/
267
268void QMultiLineEdit::insertLine( const QString &txt, int line )
269{
270 insertParagraph( txt, line );
271}
272
273/*! Deletes the paragraph at paragraph number \a paragraph. If \a
274 paragraph is less than zero or larger than the number of paragraphs,
275 nothing is deleted.
276*/
277
278void QMultiLineEdit::removeLine( int paragraph )
279{
280 removeParagraph( paragraph );
281}
282
283/*! Inserts \a str at the current cursor position and selects the
284 text if \a mark is TRUE.
285*/
286
287void QMultiLineEdit::insertAndMark( const QString& str, bool mark )
288{
289 insert( str );
290 if ( mark )
291 document()->setSelectionEnd( QTextDocument::Standard, *textCursor() );
292}
293
294/*! Splits the paragraph at the current cursor position.
295*/
296
297void QMultiLineEdit::newLine()
298{
299 insert( "\n" );
300}
301
302
303/*! Deletes the character on the left side of the text cursor and
304 moves the cursor one position to the left. If a text has been selected
305 by the user (e.g. by clicking and dragging) the cursor is put at the
306 beginning of the selected text and the selected text is removed. \sa
307 del()
308*/
309
310void QMultiLineEdit::backspace()
311{
312 if ( document()->hasSelection( QTextDocument::Standard ) ) {
313 removeSelectedText();
314 return;
315 }
316
317 if ( !textCursor()->paragraph()->prev() &&
318 textCursor()->atParagStart() )
319 return;
320
321 doKeyboardAction( ActionBackspace );
322}
323
324
325/*! Moves the text cursor to the left end of the line. If \a mark is
326 TRUE, text is selected toward the first position. If it is FALSE and the
327 cursor is moved, all selected text is unselected.
328
329 \sa end()
330*/
331
332void QMultiLineEdit::home( bool mark )
333{
334 moveCursor( MoveLineStart, mark );
335}
336
337/*! Moves the text cursor to the right end of the line. If \a mark is
338 TRUE, text is selected toward the last position. If it is FALSE and the
339 cursor is moved, all selected text is unselected.
340
341 \sa home()
342*/
343
344void QMultiLineEdit::end( bool mark )
345{
346 moveCursor( MoveLineEnd, mark );
347}
348
349
350/*!
351 \fn void QMultiLineEdit::setCursorPosition( int line, int col )
352 \reimp
353*/
354
355/*! Sets the cursor position to character number \a col in paragraph
356 number \a line. The parameters are adjusted to lie within the legal
357 range.
358
359 If \a mark is FALSE, the selection is cleared. otherwise it is extended.
360
361*/
362
363void QMultiLineEdit::setCursorPosition( int line, int col, bool mark )
364{
365 if ( !mark )
366 selectAll( FALSE );
367 QTextEdit::setCursorPosition( line, col );
368 if ( mark )
369 document()->setSelectionEnd( QTextDocument::Standard, *textCursor() );
370}
371
372/*! Returns the top center point where the cursor is drawn.
373*/
374
375QPoint QMultiLineEdit::cursorPoint() const
376{
377 return QPoint( textCursor()->x(), textCursor()->y() + textCursor()->paragraph()->rect().y() );
378}
379
380/*! \property QMultiLineEdit::alignment
381 \brief The editor's paragraph alignment
382
383 Sets the alignment to flag, which must be \c AlignLeft, \c
384 AlignHCenter or \c AlignRight.
385
386 If flag is an illegal flag nothing happens.
387
388 \sa Qt::AlignmentFlags
389*/
390void QMultiLineEdit::setAlignment( int flag )
391{
392 if ( flag == AlignCenter )
393 flag = AlignHCenter;
394 if ( flag != AlignLeft && flag != AlignRight && flag != AlignHCenter )
395 return;
396 QTextParagraph *p = document()->firstParagraph();
397 while ( p ) {
398 p->setAlignment( flag );
399 p = p->next();
400 }
401}
402
403int QMultiLineEdit::alignment() const
404{
405 return document()->firstParagraph()->alignment();
406}
407
408
409void QMultiLineEdit::setEdited( bool e )
410{
411 setModified( e );
412}
413
414/*! \property QMultiLineEdit::edited
415 \brief whether the document has been edited by the user
416
417 This is the same as QTextEdit's "modifed" property.
418
419 \sa QTextEdit::modified
420*/
421bool QMultiLineEdit::edited() const
422{
423 return isModified();
424}
425
426/*! Moves the cursor one word to the right. If \a mark is TRUE, the text
427 is selected.
428
429 \sa cursorWordBackward()
430*/
431void QMultiLineEdit::cursorWordForward( bool mark )
432{
433 moveCursor( MoveWordForward, mark );
434}
435
436/*! Moves the cursor one word to the left. If \a mark is TRUE, the
437 text is selected.
438
439 \sa cursorWordForward()
440*/
441void QMultiLineEdit::cursorWordBackward( bool mark )
442{
443 moveCursor( MoveWordBackward, mark );
444}
445
446/*!
447 \fn QMultiLineEdit::insertAt( const QString &s, int line, int col )
448 \reimp
449*/
450
451/*! Inserts string \a s at paragraph number \a line, after character
452 number \a col in the paragraph. If \a s contains newline
453 characters, new lines are inserted.
454 If \a mark is TRUE the inserted string will be selected.
455
456 The cursor position is adjusted.
457 */
458
459void QMultiLineEdit::insertAt( const QString &s, int line, int col, bool mark )
460{
461 QTextEdit::insertAt( s, line, col );
462 if ( mark )
463 setSelection( line, col, line, col + s.length() );
464}
465
466// ### reggie - is this documentation correct?
467
468/*! Deletes text from the current cursor position to the end of the
469 line. (Note that this function still operates on lines, not paragraphs.)
470*/
471
472void QMultiLineEdit::killLine()
473{
474 doKeyboardAction( ActionKill );
475}
476
477/*! Moves the cursor one character to the left. If \a mark is TRUE,
478 the text is selected.
479 The \a wrap parameter is currently ignored.
480
481 \sa cursorRight() cursorUp() cursorDown()
482*/
483
484void QMultiLineEdit::cursorLeft( bool mark, bool )
485{
486 moveCursor( MoveBackward, mark );
487}
488
489/*! Moves the cursor one character to the right. If \a mark is TRUE,
490 the text is selected.
491 The \a wrap parameter is currently ignored.
492
493 \sa cursorLeft() cursorUp() cursorDown()
494*/
495
496void QMultiLineEdit::cursorRight( bool mark, bool )
497{
498 moveCursor( MoveForward, mark );
499}
500
501/*! Moves the cursor up one line. If \a mark is TRUE, the text is
502 selected.
503
504 \sa cursorDown() cursorLeft() cursorRight()
505*/
506
507void QMultiLineEdit::cursorUp( bool mark )
508{
509 moveCursor( MoveUp, mark );
510}
511
512/*!
513 Moves the cursor one line down. If \a mark is TRUE, the text
514 is selected.
515 \sa cursorUp() cursorLeft() cursorRight()
516*/
517
518void QMultiLineEdit::cursorDown( bool mark )
519{
520 moveCursor( MoveDown, mark );
521}
522
523
524/*! Returns the text at line number \a line (possibly the empty
525 string), or a \link QString::operator!() null string\endlink if \a
526 line is invalid.
527*/
528
529QString QMultiLineEdit::textLine( int line ) const
530{
531 if ( line < 0 || line >= numLines() )
532 return QString::null;
533 QString str = document()->paragAt( line )->string()->toString();
534 str.truncate( str.length() - 1 );
535 return str;
536}
537
538#endif
Note: See TracBrowser for help on using the repository browser.