examples/widgets/codeeditor/codeeditor.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 examples 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 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "codeeditor.h"
       
    45 
       
    46 //![constructor]
       
    47 
       
    48 CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
       
    49 {
       
    50     lineNumberArea = new LineNumberArea(this);
       
    51 
       
    52     connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
       
    53     connect(this, SIGNAL(updateRequest(const QRect &, int)), this, SLOT(updateLineNumberArea(const QRect &, int)));
       
    54     connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
       
    55 
       
    56     updateLineNumberAreaWidth(0);
       
    57     highlightCurrentLine();
       
    58 }
       
    59 
       
    60 //![constructor]
       
    61 
       
    62 //![extraAreaWidth]
       
    63 
       
    64 int CodeEditor::lineNumberAreaWidth()
       
    65 {
       
    66     int digits = 1;
       
    67     int max = qMax(1, blockCount());
       
    68     while (max >= 10) {
       
    69         max /= 10;
       
    70         ++digits;
       
    71     }
       
    72 
       
    73     int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
       
    74 
       
    75     return space;
       
    76 }
       
    77 
       
    78 //![extraAreaWidth]
       
    79 
       
    80 //![slotUpdateExtraAreaWidth]
       
    81 
       
    82 void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
       
    83 { 
       
    84     setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
       
    85 }
       
    86 
       
    87 //![slotUpdateExtraAreaWidth]
       
    88 
       
    89 //![slotUpdateRequest]
       
    90 
       
    91 void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
       
    92 {
       
    93     if (dy)
       
    94         lineNumberArea->scroll(0, dy);
       
    95     else
       
    96         lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
       
    97 
       
    98     if (rect.contains(viewport()->rect()))
       
    99         updateLineNumberAreaWidth(0);
       
   100 }
       
   101 
       
   102 //![slotUpdateRequest]
       
   103 
       
   104 //![resizeEvent]
       
   105 
       
   106 void CodeEditor::resizeEvent(QResizeEvent *e)
       
   107 {
       
   108     QPlainTextEdit::resizeEvent(e);
       
   109 
       
   110     QRect cr = contentsRect();
       
   111     lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
       
   112 }
       
   113 
       
   114 //![resizeEvent]
       
   115 
       
   116 //![cursorPositionChanged]
       
   117 
       
   118 void CodeEditor::highlightCurrentLine()
       
   119 {
       
   120     QList<QTextEdit::ExtraSelection> extraSelections;
       
   121 
       
   122     if (!isReadOnly()) {
       
   123         QTextEdit::ExtraSelection selection;
       
   124         
       
   125         QColor lineColor = QColor(Qt::yellow).lighter(160);
       
   126 
       
   127         selection.format.setBackground(lineColor);
       
   128         selection.format.setProperty(QTextFormat::FullWidthSelection, true);
       
   129         selection.cursor = textCursor();
       
   130         selection.cursor.clearSelection();
       
   131         extraSelections.append(selection);
       
   132     }
       
   133 
       
   134     setExtraSelections(extraSelections);
       
   135 }
       
   136 
       
   137 //![cursorPositionChanged]
       
   138 
       
   139 //![extraAreaPaintEvent_0]
       
   140 
       
   141 void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
       
   142 {
       
   143     QPainter painter(lineNumberArea);
       
   144     painter.fillRect(event->rect(), Qt::lightGray);
       
   145 
       
   146 //![extraAreaPaintEvent_0]
       
   147 
       
   148 //![extraAreaPaintEvent_1]
       
   149     QTextBlock block = firstVisibleBlock();
       
   150     int blockNumber = block.blockNumber();
       
   151     int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
       
   152     int bottom = top + (int) blockBoundingRect(block).height();
       
   153 //![extraAreaPaintEvent_1]
       
   154 
       
   155 //![extraAreaPaintEvent_2]
       
   156     while (block.isValid() && top <= event->rect().bottom()) {
       
   157         if (block.isVisible() && bottom >= event->rect().top()) {
       
   158             QString number = QString::number(blockNumber + 1);
       
   159             painter.setPen(Qt::black);
       
   160             painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
       
   161                              Qt::AlignRight, number);
       
   162         }
       
   163 
       
   164         block = block.next();
       
   165         top = bottom;
       
   166         bottom = top + (int) blockBoundingRect(block).height();
       
   167         ++blockNumber;
       
   168     }
       
   169 }
       
   170 //![extraAreaPaintEvent_2]
       
   171