src/gui/widgets/qplaintextedit.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
--- a/src/gui/widgets/qplaintextedit.cpp	Tue Jan 26 12:42:25 2010 +0200
+++ b/src/gui/widgets/qplaintextedit.cpp	Tue Feb 02 00:43:10 2010 +0200
@@ -357,10 +357,8 @@
     Q_D(QPlainTextDocumentLayout);
     QTextDocument *doc = document();
     qreal margin = doc->documentMargin();
-    QFontMetrics fm(doc->defaultFont());
     qreal blockMaximumWidth = 0;
 
-    int leading = qMax(0, fm.leading());
     qreal height = 0;
     QTextLayout *tl = block.layout();
     QTextOption option = doc->defaultTextOption();
@@ -381,9 +379,8 @@
         QTextLine line = tl->createLine();
         if (!line.isValid())
             break;
+        line.setLeadingIncluded(true);
         line.setLineWidth(availableWidth);
-
-        height += leading;
         line.setPosition(QPointF(margin, height));
         height += line.height();
         blockMaximumWidth = qMax(blockMaximumWidth, line.naturalTextWidth() + 2*margin);
@@ -687,8 +684,12 @@
 
         qreal h = center ? line.naturalTextRect().center().y() : line.naturalTextRect().bottom();
 
+        QTextBlock previousVisibleBlock = block;
         while (h < height && block.previous().isValid()) {
-            block = block.previous();
+            previousVisibleBlock = block;
+            do {
+                block = block.previous();
+            } while (!block.isVisible() && block.previous().isValid());
             h += q->blockBoundingRect(block).height();
         }
 
@@ -702,8 +703,8 @@
             ++l;
         }
 
-        if (block.next().isValid() && l >= lineCount) {
-            block = block.next();
+        if (l >= lineCount) {
+            block = previousVisibleBlock;
             l = 0;
         }
         setTopBlock(block.blockNumber(), l);
@@ -733,9 +734,6 @@
     backgroundVisible = false;
     centerOnScroll = false;
     inDrag = false;
-#ifdef Q_WS_WIN
-    singleFingerPanEnabled = true;
-#endif
 }
 
 
@@ -767,6 +765,7 @@
     QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SLOT(_q_cursorPositionChanged()));
     QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorPositionChanged()));
 
+    QObject::connect(control, SIGNAL(textChanged()), q, SLOT(updateMicroFocus()));
 
     // set a null page size initially to avoid any relayouting until the textedit
     // is shown. relayoutDocument() will take care of setting the page size to the
@@ -792,6 +791,9 @@
     viewport->setCursor(Qt::IBeamCursor);
 #endif
     originalOffsetY = 0;
+#ifdef Q_WS_WIN
+    setSingleFingerPanEnabled(true);
+#endif
 }
 
 void QPlainTextEditPrivate::_q_repaintContents(const QRectF &contentsRect)
@@ -1453,6 +1455,29 @@
             d->sendControlEvent(e);
     }
 #endif
+    else if (e->type() == QEvent::Gesture) {
+        QGestureEvent *ge = static_cast<QGestureEvent *>(e);
+        QPanGesture *g = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture));
+        if (g) {
+            QScrollBar *hBar = horizontalScrollBar();
+            QScrollBar *vBar = verticalScrollBar();
+            if (g->state() == Qt::GestureStarted)
+                d->originalOffsetY = vBar->value();
+            QPointF offset = g->offset();
+            if (!offset.isNull()) {
+                if (QApplication::isRightToLeft())
+                    offset.rx() *= -1;
+                // QPlainTextEdit scrolls by lines only in vertical direction
+                QFontMetrics fm(document()->defaultFont());
+                int lineHeight = fm.height();
+                int newX = hBar->value() - g->delta().x();
+                int newY = d->originalOffsetY - offset.y()/lineHeight;
+                hBar->setValue(newX);
+                vBar->setValue(newY);
+            }
+        }
+        return true;
+    }
     return QAbstractScrollArea::event(e);
 }
 
@@ -1602,7 +1627,6 @@
             return;
         }
     }
-#endif // QT_NO_SHORTCUT
 
     if (!(tif & Qt::TextEditable)) {
         switch (e->key()) {
@@ -1630,6 +1654,7 @@
         }
         return;
     }
+#endif // QT_NO_SHORTCUT
 
     d->sendControlEvent(e);
 #ifdef QT_KEYPAD_NAVIGATION
@@ -1782,6 +1807,9 @@
     QTextBlock block = firstVisibleBlock();
     qreal maximumWidth = document()->documentLayout()->documentSize().width();
 
+    // Set a brush origin so that the WaveUnderline knows where the wave started
+    painter.setBrushOrigin(offset);
+
     // keep right margin clean from full-width selection
     int maxX = offset.x() + qMax((qreal)viewportRect.width(), maximumWidth)
                - document()->documentMargin();
@@ -1946,7 +1974,8 @@
         d->ensureCursorVisible();
     }
 
-    d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus);
+    if (!isReadOnly() && rect().contains(e->pos()))
+        d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus);
     d->clickCausedFocus = 0;
 }
 
@@ -2932,30 +2961,6 @@
     (\a available is true) or unavailable (\a available is false).
 */
 
-//void QPlainTextEditPrivate::_q_gestureTriggered()
-//{
-//    Q_Q(QPlainTextEdit);
-//    QPanGesture *g = qobject_cast<QPanGesture*>(q->sender());
-//    if (!g)
-//        return;
-//    QScrollBar *hBar = q->horizontalScrollBar();
-//    QScrollBar *vBar = q->verticalScrollBar();
-//    if (g->state() == Qt::GestureStarted)
-//        originalOffsetY = vBar->value();
-//    QSizeF totalOffset = g->totalOffset();
-//    if (!totalOffset.isNull()) {
-//        if (QApplication::isRightToLeft())
-//            totalOffset.rwidth() *= -1;
-//        // QPlainTextEdit scrolls by lines only in vertical direction
-//        QFontMetrics fm(q->document()->defaultFont());
-//        int lineHeight = fm.height();
-//        int newX = hBar->value() - g->lastOffset().width();
-//        int newY = originalOffsetY - totalOffset.height()/lineHeight;
-//        hbar->setValue(newX);
-//        vbar->setValue(newY);
-//    }
-//}
-
 QT_END_NAMESPACE
 
 #include "moc_qplaintextedit.cpp"