src/qt3support/text/q3multilineedit.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt3Support module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <qplatformdefs.h>
       
    42 #include "q3multilineedit.h"
       
    43 #ifndef QT_NO_MULTILINEEDIT
       
    44 #include "qpainter.h"
       
    45 #include "qscrollbar.h"
       
    46 #include "qcursor.h"
       
    47 #include "qclipboard.h"
       
    48 #include "qpixmap.h"
       
    49 #include "qregexp.h"
       
    50 #include "qapplication.h"
       
    51 #include "q3dragobject.h"
       
    52 #include "qtimer.h"
       
    53 #include <private/q3richtext_p.h>
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 /*!
       
    58   \class Q3MultiLineEdit
       
    59 
       
    60   \brief The Q3MultiLineEdit widget is a simple editor for inputting text.
       
    61 
       
    62   \compat
       
    63 
       
    64   The Q3MultiLineEdit was a simple editor widget in former Qt versions.  Qt
       
    65   3.0 includes a new richtext engine which obsoletes Q3MultiLineEdit. It is
       
    66   still included for compatibility reasons. It is now a subclass of
       
    67   \l Q3TextEdit, and provides enough of the old Q3MultiLineEdit API to keep old
       
    68   applications working.
       
    69 
       
    70   If you implement something new with Q3MultiLineEdit, we suggest using
       
    71   \l Q3TextEdit instead and call Q3TextEdit::setTextFormat(Qt::PlainText).
       
    72 
       
    73   Although most of the old Q3MultiLineEdit API is still available, there is
       
    74   a few difference. The old Q3MultiLineEdit operated on lines, not on
       
    75   paragraphs.  As lines change all the time during wordwrap, the new
       
    76   richtext engine uses paragraphs as basic elements in the data structure.
       
    77   All functions (numLines(), textLine(), etc.) that operated on lines, now
       
    78   operate on paragraphs. Further, getString() has been removed completely.
       
    79   It revealed too much of the internal data structure.
       
    80 
       
    81   Applications which made normal and reasonable use of Q3MultiLineEdit
       
    82   should still work without problems. Some odd usage will require some
       
    83   porting. In these cases, it may be better to use \l Q3TextEdit now.
       
    84 
       
    85   \sa Q3TextEdit
       
    86 */
       
    87 
       
    88 /*!
       
    89   \fn bool Q3MultiLineEdit::autoUpdate() const
       
    90 
       
    91   This function is a noop that always returns true.
       
    92 */
       
    93 
       
    94 /*!
       
    95   \fn virtual void Q3MultiLineEdit::setAutoUpdate(bool b)
       
    96 
       
    97   \internal
       
    98 */
       
    99 
       
   100 /*!
       
   101   \fn int Q3MultiLineEdit::totalWidth() const
       
   102 */
       
   103 
       
   104 /*!
       
   105   \fn int Q3MultiLineEdit::totalHeight() const
       
   106 */
       
   107 
       
   108 /*!
       
   109   \fn int Q3MultiLineEdit::maxLines() const
       
   110 */
       
   111 
       
   112 /*!
       
   113   \fn void Q3MultiLineEdit::setMaxLines(int max)
       
   114 
       
   115   Sets the maximum number of lines this Q3MultiLineEdit will hold to
       
   116   \a max.
       
   117 */
       
   118 
       
   119 /*!
       
   120   \fn void Q3MultiLineEdit::deselect()
       
   121 */
       
   122 
       
   123 
       
   124 class Q3MultiLineEditData
       
   125 {
       
   126 };
       
   127 
       
   128 
       
   129 /*!
       
   130   Constructs a new, empty, Q3MultiLineEdit with parent \a parent called
       
   131   \a name.
       
   132 */
       
   133 
       
   134 Q3MultiLineEdit::Q3MultiLineEdit(QWidget *parent , const char *name)
       
   135     : Q3TextEdit(parent, name)
       
   136 {
       
   137     d = new Q3MultiLineEditData;
       
   138     setTextFormat(Qt::PlainText);
       
   139 }
       
   140 
       
   141 /*! \property Q3MultiLineEdit::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 
       
   148 int Q3MultiLineEdit::numLines() const
       
   149 {
       
   150     return document()->lastParagraph()->paragId() + 1;
       
   151 }
       
   152 
       
   153 /*! \property Q3MultiLineEdit::atEnd
       
   154   \brief whether the cursor is placed at the end of the text
       
   155 
       
   156   \sa atBeginning
       
   157 */
       
   158 
       
   159 bool Q3MultiLineEdit::atEnd() const
       
   160 {
       
   161     return textCursor()->paragraph() == document()->lastParagraph() && textCursor()->atParagEnd();
       
   162 }
       
   163 
       
   164 
       
   165 /*! \property Q3MultiLineEdit::atBeginning
       
   166   \brief whether the cursor is placed at the beginning of the text
       
   167 
       
   168   \sa atEnd
       
   169 */
       
   170 
       
   171 bool Q3MultiLineEdit::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 
       
   180 int Q3MultiLineEdit::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 /*! Destructor. */
       
   189 
       
   190 Q3MultiLineEdit::~Q3MultiLineEdit()
       
   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  */
       
   200 bool Q3MultiLineEdit::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 
       
   223 bool Q3MultiLineEdit::hasMarkedText() const
       
   224 {
       
   225     return hasSelectedText();
       
   226 }
       
   227 
       
   228 
       
   229 /*!
       
   230   Returns a copy of the selected text.
       
   231 */
       
   232 
       
   233 QString Q3MultiLineEdit::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 
       
   243 void Q3MultiLineEdit::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 
       
   254 void Q3MultiLineEdit::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 
       
   268 void Q3MultiLineEdit::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 
       
   278 void Q3MultiLineEdit::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 
       
   287 void Q3MultiLineEdit::insertAndMark(const QString& str, bool mark)
       
   288 {
       
   289     insert(str);
       
   290     if (mark)
       
   291         document()->setSelectionEnd(Q3TextDocument::Standard, *textCursor());
       
   292 }
       
   293 
       
   294 /*!  Splits the paragraph at the current cursor position.
       
   295 */
       
   296 
       
   297 void Q3MultiLineEdit::newLine()
       
   298 {
       
   299     insert(QString(QLatin1Char('\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 
       
   310 void Q3MultiLineEdit::backspace()
       
   311 {
       
   312     if (document()->hasSelection(Q3TextDocument::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 
       
   332 void Q3MultiLineEdit::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 
       
   344 void Q3MultiLineEdit::end(bool mark)
       
   345 {
       
   346     moveCursor(MoveLineEnd, mark);
       
   347 }
       
   348 
       
   349 
       
   350 /*!
       
   351   \fn void Q3MultiLineEdit::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 
       
   363 void Q3MultiLineEdit::setCursorPosition(int line, int col, bool mark)
       
   364 {
       
   365     if (!mark)
       
   366         selectAll(false);
       
   367     Q3TextEdit::setCursorPosition(line, col);
       
   368     if (mark)
       
   369         document()->setSelectionEnd(Q3TextDocument::Standard, *textCursor());
       
   370 }
       
   371 
       
   372 /*!  Returns the top center point where the cursor is drawn.
       
   373 */
       
   374 
       
   375 QPoint Q3MultiLineEdit::cursorPoint() const
       
   376 {
       
   377     return QPoint(textCursor()->x(), textCursor()->y() + textCursor()->paragraph()->rect().y());
       
   378 }
       
   379 
       
   380 /*!  \property Q3MultiLineEdit::alignment
       
   381   \brief The editor's paragraph alignment
       
   382 
       
   383   Sets the alignment to flag, which must be Qt::AlignLeft,
       
   384   Qt::AlignHCenter, or \c Qt::AlignRight.
       
   385 
       
   386   If flag is an illegal flag, nothing happens.
       
   387 */
       
   388 void Q3MultiLineEdit::setAlignment(Qt::Alignment flag)
       
   389 {
       
   390     if (flag == Qt::AlignCenter)
       
   391         flag = Qt::AlignHCenter;
       
   392     if (flag != Qt::AlignLeft && flag != Qt::AlignRight && flag != Qt::AlignHCenter)
       
   393         return;
       
   394     Q3TextParagraph *p = document()->firstParagraph();
       
   395     while (p) {
       
   396         p->setAlignment(flag);
       
   397         p = p->next();
       
   398     }
       
   399 }
       
   400 
       
   401 Qt::Alignment Q3MultiLineEdit::alignment() const
       
   402 {
       
   403     return QFlag(document()->firstParagraph()->alignment());
       
   404 }
       
   405 
       
   406 
       
   407 void Q3MultiLineEdit::setEdited(bool e)
       
   408 {
       
   409     setModified(e);
       
   410 }
       
   411 
       
   412 /*!  \property Q3MultiLineEdit::edited
       
   413   \brief whether the document has been edited by the user
       
   414 
       
   415   This is the same as Q3TextEdit's "modifed" property.
       
   416 */
       
   417 bool Q3MultiLineEdit::edited() const
       
   418 {
       
   419     return isModified();
       
   420 }
       
   421 
       
   422 /*!  Moves the cursor one word to the right.  If \a mark is true, the text
       
   423   is selected.
       
   424 
       
   425   \sa cursorWordBackward()
       
   426 */
       
   427 void Q3MultiLineEdit::cursorWordForward(bool mark)
       
   428 {
       
   429     moveCursor(MoveWordForward, mark);
       
   430 }
       
   431 
       
   432 /*!  Moves the cursor one word to the left.  If \a mark is true, the
       
   433   text is selected.
       
   434 
       
   435   \sa cursorWordForward()
       
   436 */
       
   437 void Q3MultiLineEdit::cursorWordBackward(bool mark)
       
   438 {
       
   439     moveCursor(MoveWordBackward, mark);
       
   440 }
       
   441 
       
   442 /*!
       
   443   \fn Q3MultiLineEdit::insertAt(const QString &s, int line, int col)
       
   444   \reimp
       
   445 */
       
   446 
       
   447 /*!  Inserts string \a s at paragraph number \a line, after character
       
   448   number \a col in the paragraph.  If \a s contains newline
       
   449   characters, new lines are inserted.
       
   450   If \a mark is true the inserted string will be selected.
       
   451 
       
   452   The cursor position is adjusted.
       
   453  */
       
   454 
       
   455 void Q3MultiLineEdit::insertAt(const QString &s, int line, int col, bool mark)
       
   456 {
       
   457     Q3TextEdit::insertAt(s, line, col);
       
   458     if (mark)
       
   459         setSelection(line, col, line, col + s.length());
       
   460 }
       
   461 
       
   462 // ### reggie - is this documentation correct?
       
   463 
       
   464 /*!  Deletes text from the current cursor position to the end of the
       
   465   line. (Note that this function still operates on lines, not paragraphs.)
       
   466 */
       
   467 
       
   468 void Q3MultiLineEdit::killLine()
       
   469 {
       
   470     doKeyboardAction(ActionKill);
       
   471 }
       
   472 
       
   473 /*!  Moves the cursor one character to the left. If \a mark is true,
       
   474   the text is selected.
       
   475   The \a wrap parameter is currently ignored.
       
   476 
       
   477   \sa cursorRight() cursorUp() cursorDown()
       
   478 */
       
   479 
       
   480 void Q3MultiLineEdit::cursorLeft(bool mark, bool)
       
   481 {
       
   482     moveCursor(MoveBackward, mark);
       
   483 }
       
   484 
       
   485 /*!  Moves the cursor one character to the right.  If \a mark is true,
       
   486   the text is selected.
       
   487   The \a wrap parameter is currently ignored.
       
   488 
       
   489   \sa cursorLeft() cursorUp() cursorDown()
       
   490 */
       
   491 
       
   492 void Q3MultiLineEdit::cursorRight(bool mark, bool)
       
   493 {
       
   494     moveCursor(MoveForward, mark);
       
   495 }
       
   496 
       
   497 /*!  Moves the cursor up one line.  If \a mark is true, the text is
       
   498   selected.
       
   499 
       
   500   \sa cursorDown() cursorLeft() cursorRight()
       
   501 */
       
   502 
       
   503 void Q3MultiLineEdit::cursorUp(bool mark)
       
   504 {
       
   505     moveCursor(MoveUp, mark);
       
   506 }
       
   507 
       
   508 /*!
       
   509   Moves the cursor one line down.  If \a mark is true, the text
       
   510   is selected.
       
   511   \sa cursorUp() cursorLeft() cursorRight()
       
   512 */
       
   513 
       
   514 void Q3MultiLineEdit::cursorDown(bool mark)
       
   515 {
       
   516     moveCursor(MoveDown, mark);
       
   517 }
       
   518 
       
   519 
       
   520 /*!  Returns the text at line number \a line (possibly the empty
       
   521   string), or a null if \a line is invalid.
       
   522 */
       
   523 
       
   524 QString Q3MultiLineEdit::textLine(int line) const
       
   525 {
       
   526     if (line < 0 || line >= numLines())
       
   527         return QString();
       
   528     QString str = document()->paragAt(line)->string()->toString();
       
   529     str.truncate(str.length() - 1);
       
   530     return str;
       
   531 }
       
   532 
       
   533 QT_END_NAMESPACE
       
   534 
       
   535 #endif