[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    koffice/libs/main
From:       Thomas Zander <zander () kde ! org>
Date:       2010-12-27 13:15:09
Message-ID: 20101227131509.9664AAC8B6 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1209597 by zander:

Make the ruler update when the tabs change.

This avoids us seeing the wrong tabs, or even deleted tabs ;)

 M  +65 -13    KoRulerController.cpp  
 M  +2 -1      KoRulerController.h  


--- trunk/koffice/libs/main/KoRulerController.cpp #1209596:1209597
@@ -1,5 +1,5 @@
 /* This file is part of the KDE project
- * Copyright (C) 2007 Thomas Zander <zander@kde.org>
+ * Copyright (C) 2007, 2010 Thomas Zander <zander@kde.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -44,18 +44,46 @@
 class KoRulerController::Private
 {
 public:
-    Private(KoRuler *r, KoResourceManager *crp)
-            : ruler(r),
+
+    enum IndexNames {
+        Unknown = -2,
+        NewTab = -1
+    };
+    Private(KoRulerController *qq, KoRuler *r, KoResourceManager *crp)
+            : q(qq),
+            ruler(r),
             resourceManager(crp),
             lastPosition(-1),
-            originalTabIndex(-2),
-            currentTabIndex(-2) {
+            originalTabIndex(Unknown),
+            currentTabIndex(Unknown),
+            blockSignals(false)
+    {
     }
 
     void canvasResourceChanged(int key) {
-        if (key != KoText::CurrentTextPosition && key != \
KoText::CurrentTextDocument) +        if (key == KoText::CurrentTextDocument) {
+            // so, text doc has changed, lets disconnect from prev.
+            QTextDocument *doc = textDocument.data();
+            if (doc) {
+                disconnect(doc, SIGNAL(contentsChange(int,int,int)),
+                        q, SLOT(textChange(int,int,int)));
+            }
+            QVariant docVar = \
resourceManager->resource(KoText::CurrentTextDocument); +            if \
(docVar.isNull()) { +                textDocument.clear();
+            } else {
+                doc = static_cast<QTextDocument*>(docVar.value<void*>());
+                textDocument = doc;
+                connect(doc, SIGNAL(contentsChange(int,int,int)),
+                        q, SLOT(textChange(int,int,int)));
+            }
+        } else if (key != KoText::CurrentTextPosition) {
             return;
+        }
+        updateGui();
+    }
 
+    void updateGui() {
         QTextBlock block = currentBlock();
         if (! block.isValid()) {
             ruler->setShowIndents(false);
@@ -67,7 +95,7 @@
         if (block.position() <= lastPosition && block.position() + block.length() > \
lastPosition)  return; // nothing changed.
         lastPosition = block.position();
-        currentTabIndex = -2;
+        currentTabIndex = Unknown;
         tabList.clear();
 
         QTextBlockFormat format = block.blockFormat();
@@ -101,7 +129,9 @@
         bf.setLeftMargin(ruler->paragraphIndent());
         bf.setTextIndent(ruler->firstLineIndent());
         bf.setRightMargin(ruler->endIndent());
+        blockSignals = true;
         cursor.setBlockFormat(bf);
+        blockSignals = false;
     }
 
     void tabChanged(int originalIndex, KoRuler::Tab *tab) {
@@ -118,11 +148,13 @@
         cursor.setPosition(anchor);
         cursor.setPosition(position, QTextCursor::KeepAnchor);
 
-        if (originalTabIndex == -2 || originalTabIndex != originalIndex) {
+        if (originalTabIndex == Unknown || originalTabIndex != originalIndex) {
             originalTabIndex = originalIndex;
             KoParagraphStyle style(cursor.blockFormat(), cursor.blockCharFormat());
             tabList = style.tabPositions();
+qDebug() << tabList.count();
             if (originalTabIndex >= 0) { // modification
+                Q_ASSERT(originalTabIndex < tabList.count());
                 currentTab = tabList[originalTabIndex];
                 currentTabIndex = originalTabIndex;
             } else if (originalTabIndex == -1 && tab) { // new tab.
@@ -142,14 +174,14 @@
         if (tab) {
             currentTab.position = tab->position;
             currentTab.type = tab->type;
-            if (currentTabIndex == -2) { // add the new tab to the list, sorting in.
+            if (currentTabIndex == Unknown) { // add the new tab to the list, \
sorting in.  currentTabIndex = tabList.count();
                 tabList << currentTab;
             } else
                 tabList.replace(currentTabIndex, currentTab);
         } else if (currentTabIndex >= 0) { // lets remove it.
             tabList.removeAt(currentTabIndex);
-            currentTabIndex = -2;
+            currentTabIndex = Unknown;
         }
 
         QTextBlockFormat bf;
@@ -162,7 +194,9 @@
             list.append(v);
         }
         bf.setProperty(KoParagraphStyle::TabPositions, list);
+        blockSignals = true;
         cursor.mergeBlockFormat(bf);
+        blockSignals = false;
     }
 
     QTextBlock currentBlock() {
@@ -177,21 +211,39 @@
 
     void tabChangeInitiated() {
         tabList.clear();
-        originalTabIndex = -2;
+        originalTabIndex = Unknown;
     }
 
+    void textChange(int position, int charsRemoved, int charsAdded) {
+        if (blockSignals)
+            return;
+        if (charsAdded != charsRemoved)
+            return;
+        const int end = position + charsRemoved;
+        QTextBlock block = currentBlock();
+        const int blockEnd = block.position() + block.length();
+        if ((block.position() > position && block.position() + block.length() < end) \
// contains cur +                || block.position() == position || end == blockEnd) \
{ +            lastPosition = -1;
+            updateGui();
+        }
+    }
+
 private:
+    KoRulerController *q;
     KoRuler *ruler;
     KoResourceManager *resourceManager;
-    int lastPosition; // the last position in the text document.
+    QWeakPointer<QTextDocument> textDocument;
     QList<KoText::Tab> tabList;
     KoText::Tab currentTab;
+    int lastPosition; // the last position in the text document.
     int originalTabIndex, currentTabIndex;
+    bool blockSignals;
 };
 
 KoRulerController::KoRulerController(KoRuler *horizontalRuler, KoResourceManager \
*crp)  : QObject(horizontalRuler),
-        d(new Private(horizontalRuler, crp))
+        d(new Private(this, horizontalRuler, crp))
 {
     connect(crp, SIGNAL(resourceChanged(int, const QVariant &)), this, \
                SLOT(canvasResourceChanged(int)));
     connect(horizontalRuler, SIGNAL(indentsChanged(bool)), this, \
                SLOT(indentsChanged()));
--- trunk/koffice/libs/main/KoRulerController.h #1209596:1209597
@@ -1,5 +1,5 @@
 /* This file is part of the KDE project
- * Copyright (C) 2007 Thomas Zander <zander@kde.org>
+ * Copyright (C) 2007, 2010 Thomas Zander <zander@kde.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -53,6 +53,7 @@
     Q_PRIVATE_SLOT(d, void indentsChanged())
     Q_PRIVATE_SLOT(d, void tabChanged(int, KoRuler::Tab *tab))
     Q_PRIVATE_SLOT(d, void tabChangeInitiated())
+    Q_PRIVATE_SLOT(d, void textChange(int, int, int))
 };
 
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic