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

List:       kde-commits
Subject:    koffice
From:       Fredy Yanardi <fyanardi () gmail ! com>
Date:       2007-07-03 3:55:28
Message-ID: 1183434928.354304.6457.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 682534 by fyanardi:

* change the undo for the text editing plugin to start/stop macro, even though \
                they're not working now.
* apply changecase one text block at a time, put start/stop macro for each changecase \
                type
* remove redundant code
CCMAIL: mecirt@gmail.com


 M  +4 -1      libs/kotext/KoTextEditingPlugin.h  
 M  +109 -41   plugins/changecase/Changecase.cpp  
 M  +0 -2      plugins/changecase/Changecase.h  
 M  +4 -2      shapes/text/TextTool.cpp  


--- trunk/koffice/libs/kotext/KoTextEditingPlugin.h #682533:682534
@@ -78,7 +78,10 @@
     virtual void checkSection(QTextDocument *document, int startPosition, int \
endPosition);  
 signals:
-    void commandAvailable(QUndoCommand *command);
+    /// emitted when a series of commands is started that together need to become 1 \
undo action. +    void startMacro(const QString &name);
+    /// emitted when a series of commands has ended that together should be 1 undo \
action. +    void stopMacro();
 
 protected:
     /**
--- trunk/koffice/plugins/changecase/Changecase.cpp #682533:682534
@@ -100,10 +100,11 @@
     QTextBlock block = m_document->findBlock(m_startPosition);
     QTextCursor backCursor(m_cursor);
     int pos = block.position() + block.length() - 1;
-    QChar replacedChar;
-    
+
     // TODO
     // * Exception?
+    emit startMacro("Change case");
+
     while (true) {
         QString text = block.text();
         int prevLetterIndex = -1;
@@ -133,6 +134,7 @@
                 pos--;
             }
 
+            // found end of sentence, go back to last found letter (indicating start \
                of a word)
             if (iter != text.begin() && (*iter == QChar('.') || *iter == QChar('!') \
                || *iter == QChar('?'))) {
                 if (prevLetterIndex >= m_startPosition && prevLetterIndex <= \
                m_endPosition) {
                     // kDebug() << "Found end of sentence " << *iter << " : " << \
currentWord << endl; @@ -165,40 +167,87 @@
         block = block.next();
     }
 
-    restoreSelection();
+    emit stopMacro();
 }
 
 void Changecase::lowerCase()
 {
-    // TODO: do for each text block instead of whole selection? clean up the code
-    QString text = m_cursor.selectedText();
-    QString result;
+    QTextBlock block = m_document->findBlock(m_startPosition);
+    int pos = block.position();
+    bool finished = false;
 
-    QString::ConstIterator constIter = text.constBegin();
+    emit startMacro("Change case");
 
-    while (constIter != text.constEnd())
-        result.append(constIter++->toLower());
+    while (true) {
+        QString text = block.text();
+        QString result;
 
-    if(text != result)
-        m_cursor.insertText(result);
+        QString::ConstIterator constIter = text.constBegin();
+        while (pos < m_endPosition && constIter != text.constEnd()) {
+            if (pos >= m_startPosition)
+                result.append(constIter->toLower());
 
-    restoreSelection();
+            pos++;
+            constIter++;
+        }
+
+        if (!(block.isValid() && block.position() + block.length() < m_endPosition))
+            finished = true;
+
+        if (result != text) {
+            m_cursor.setPosition(qMax(m_startPosition, block.position()));
+            m_cursor.setPosition(qMin(pos, m_endPosition), QTextCursor::KeepAnchor);
+            m_cursor.insertText(result);
+        }
+
+        if (finished)
+            break;
+
+        block = block.next();
+        pos = block.position();
+    }
+
+    emit stopMacro();
 }
 
 void Changecase::upperCase()
 {
-    QString text = m_cursor.selectedText();
-    QString result;
+    QTextBlock block = m_document->findBlock(m_startPosition);
+    int pos = block.position();
+    bool finished = false;
 
-    QString::ConstIterator constIter = text.constBegin();
+    emit startMacro("Change case");
 
-    while (constIter != text.constEnd())
-        result.append(constIter++->toUpper());
+    while (true) {
+        QString text = block.text();
+        QString result;
 
-    if(text != result)
-        m_cursor.insertText(result);
+        QString::ConstIterator constIter = text.constBegin();
+        while (pos < m_endPosition && constIter != text.constEnd()) {
+            if (pos >= m_startPosition)
+                result.append(constIter->toUpper());
 
-    restoreSelection();
+            pos++;
+            constIter++;
+        }
+
+        if (!(block.isValid() && block.position() + block.length() < m_endPosition))
+            finished = true;
+
+        if (result != text) {
+            m_cursor.setPosition(qMax(m_startPosition, block.position()));
+            m_cursor.setPosition(qMin(pos, m_endPosition), QTextCursor::KeepAnchor);
+            m_cursor.insertText(result);
+        }
+
+        if (finished)
+            break;
+
+        block = block.next();
+        pos = block.position();
+    }
+
+    emit stopMacro();
 }
 
 void Changecase::initialCaps()
@@ -207,6 +256,8 @@
     int pos = block.position();
     bool finished = false;
 
+    emit startMacro("Change case");
+
     while (true) {
         QString text = block.text();
         QString result;
@@ -242,36 +293,53 @@
         pos = block.position();
     }
 
-    restoreSelection();
+    emit stopMacro();
 }
 
 void Changecase::toggleCase()
 {
-    QString text = m_cursor.selectedText();
-    QString result;
+    QTextBlock block = m_document->findBlock(m_startPosition);
+    int pos = block.position();
+    bool finished = false;
 
-    QString::ConstIterator constIter = text.constBegin();
+    emit startMacro("Change case");
 
-    while (constIter != text.constEnd()) {
-        if (constIter->isLower())
-            result.append(constIter->toUpper());
-        else if (constIter->isUpper())
-            result.append(constIter->toLower());
-        else
-            result.append(*constIter);
-        constIter++;
-    }
+    while (true) {
+        QString text = block.text();
+        QString result;
 
-    if(text != result)
-        m_cursor.insertText(result);
+        QString::ConstIterator constIter = text.constBegin();
+        while (pos < m_endPosition && constIter != text.constEnd()) {
+            if (pos >= m_startPosition) {
+                if (constIter->isLower())
+                    result.append(constIter->toUpper());
+                else if (constIter->isUpper())
+                    result.append(constIter->toLower());
+                else
+                    result.append(*constIter);
+            }
 
-    restoreSelection();
-}
+            pos++;
+            constIter++;
+        }
 
-void Changecase::restoreSelection()
-{
-    m_cursor.setPosition(m_startPosition);
-    m_cursor.setPosition(m_endPosition, QTextCursor::KeepAnchor);
+        if (!(block.isValid() && block.position() + block.length() < m_endPosition))
+            finished = true;
+
+        if (result != text) {
+            m_cursor.setPosition(qMax(m_startPosition, block.position()));
+            m_cursor.setPosition(qMin(pos, m_endPosition), QTextCursor::KeepAnchor);
+            m_cursor.insertText(result);
+        }
+
+        if (finished)
+            break;
+
+        block = block.next();
+        pos = block.position();
+    }
+
+    emit stopMacro();
 }
 
 #include "Changecase.moc"
--- trunk/koffice/plugins/changecase/Changecase.h #682533:682534
@@ -48,8 +48,6 @@
     void initialCaps();
     void toggleCase();
 
-    void restoreSelection();
-
     QRadioButton *m_sentenceCaseRadio;
     QRadioButton *m_lowerCaseRadio;
     QRadioButton *m_upperCaseRadio;
--- trunk/koffice/shapes/text/TextTool.cpp #682533:682534
@@ -238,8 +238,10 @@
         m_textEditingPlugins.insert(factory->id(), factory->create());
     }
 
-    foreach (KoTextEditingPlugin* plugin, m_textEditingPlugins.values())
-        connect(plugin, SIGNAL(commandAvailable(QUndoCommand *)), this, \
SLOT(addCommand(QUndoCommand *))); +    foreach (KoTextEditingPlugin* plugin, \
m_textEditingPlugins.values()) { +        connect(plugin, SIGNAL(startMacro(const \
QString &)), this, SLOT(startMacro(const QString &))); +        connect(plugin, \
SIGNAL(stopMacro()), this, SLOT(stopMacro())); +    }
 
     action = new QAction(i18n("Paragraph..."), this);
     addAction("format_paragraph", action);


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

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