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

List:       koffice-devel
Subject:    Extending KoTextEditingPlugin
From:       "Fredy Yanardi" <fyanardi () gmail ! com>
Date:       2007-06-29 15:12:23
Message-ID: 1ec1a3270706290812s13fa50d7o92593ef277980312 () mail ! gmail ! com
[Download RAW message or body]

Hello all!

I'm thinking of extending the KoTextEditing plugin to provide better
user interaction by:

1. Adding a submenu to the PluginHelperAction. Since the
PluginHelperAction inherits QAction, we can easily add a submenu via
setMenu(). The menu itself must provided by the plugin factory and
passed to the PluginHelperAction. The factory is also responsible for
passing the menu to the plugin and the plugin will connect every
action in the menu to its internal state.
Example of this functionality is the Changecase plugin, if user right
clicks a selection, the changecase plugin will show 5 submenu,
uppercase, lowercase, initial caps, sentence case, and toggle case.

2. Providing undo for the KoTextEditingPlugin. The plugin will emit a
signal commandAvailable after it finishes processing the request and
the command will be added to the TextTool.

Attached patch shows my idea about the submenu and undo. I'm also
working on the changecase plugin which uses the submenu and undo. Any
comments on it?

Thanks,

Fredy Yanardi

["svn_diff" (text/plain)]

Index: plugins/CMakeLists.txt
===================================================================
--- plugins/CMakeLists.txt	(revision 679490)
+++ plugins/CMakeLists.txt	(working copy)
@@ -4,4 +4,5 @@
 add_subdirectory( autocorrection )
 add_subdirectory( defaultTools )
 add_subdirectory( dockers )
+#add_subdirectory( changecase )
 
Index: shapes/text/PluginHelperAction.h
===================================================================
--- shapes/text/PluginHelperAction.h	(revision 679490)
+++ shapes/text/PluginHelperAction.h	(working copy)
@@ -23,11 +23,13 @@
 
 class TextTool;
 class QString;
+class QMenu;
 
 class PluginHelperAction : public QAction {
     Q_OBJECT
 public:
     PluginHelperAction(const QString &name, TextTool *tool, const QString \
&pluginId); +    void setMenu(QMenu *menu);
 
 private slots:
     void executed();
Index: shapes/text/TextTool.cpp
===================================================================
--- shapes/text/TextTool.cpp	(revision 679490)
+++ shapes/text/TextTool.cpp	(working copy)
@@ -211,6 +211,9 @@
         m_textEditingPlugins.insert(factory->id(), factory->create());
     }
 
+    foreach (KoTextEditingPlugin* plugin, m_textEditingPlugins.values())
+        connect(plugin, SIGNAL(commandAvailable(QUndoCommand *)), this, \
SLOT(addCommand(QUndoCommand *))); +
     action = new QAction(i18n("Paragraph..."), this);
     addAction("format_paragraph", action);
     action->setShortcut(Qt::ALT + Qt::CTRL + Qt::Key_P);
@@ -235,8 +238,12 @@
     list.append(this->action("format_font"));
     foreach(QString key, KoTextEditingRegistry::instance()->keys()) {
         KoTextEditingFactory *factory =  \
                KoTextEditingRegistry::instance()->value(key);
-        if(factory->showInMenu())
-            list.append(new PluginHelperAction(factory->title(), this, \
factory->id())); +        if(factory->showInMenu()) {
+            PluginHelperAction *pluginHelper = new \
PluginHelperAction(factory->title(), this, factory->id()); +            if \
(factory->hasSubMenu()) +                pluginHelper->setMenu(factory->menu());
+            list.append(pluginHelper);
+        }
     }
     setPopupActionList(list);
 
Index: shapes/text/TextTool.h
===================================================================
--- shapes/text/TextTool.h	(revision 679490)
+++ shapes/text/TextTool.h	(working copy)
@@ -77,11 +77,13 @@
     /// reimplemented from superclass
     virtual QVariant inputMethodQuery(Qt::InputMethodQuery query, const \
KoViewConverter &converter) const;  
+
+    void startTextEditingPlugin(const QString &pluginId);
+
+public slots:
     /// add a command to the undo stack, executing it as well.
     void addCommand(QUndoCommand *command);
 
-    void startTextEditingPlugin(const QString &pluginId);
-
 signals:
     /// emitted every time a different styleManager is set.
     void styleManagerChanged(KoStyleManager *manager);
Index: shapes/text/PluginHelperAction.cpp
===================================================================
--- shapes/text/PluginHelperAction.cpp	(revision 679490)
+++ shapes/text/PluginHelperAction.cpp	(working copy)
@@ -20,15 +20,31 @@
 
 #include "TextTool.h"
 
+#include <QAction>
+#include <QMenu>
+#include <kdebug.h>
+
 PluginHelperAction::PluginHelperAction(const QString &name, TextTool *tool, const \
QString &pluginId)  : QAction(name, tool),
     m_tool(tool),
     m_pluginId(pluginId)
 {
+    connect(this, SIGNAL(triggered()), this, SLOT(executed()));
 }
 
 void PluginHelperAction::executed() {
     m_tool->startTextEditingPlugin(m_pluginId);
 }
 
+void PluginHelperAction::setMenu(QMenu *menu)
+{
+    if (menu != QAction::menu()) {
+        foreach(QAction *action, menu->actions())
+            disconnect(action, SIGNAL(triggered()), this, SIGNAL(triggered()));
+    }
+    QAction::setMenu(menu);
+    foreach(QAction *action, menu->actions())
+        connect(action, SIGNAL(triggered()), this, SIGNAL(triggered()));
+}
+
 #include <PluginHelperAction.moc>
Index: libs/kotext/KoTextEditingPlugin.cpp
===================================================================
--- libs/kotext/KoTextEditingPlugin.cpp	(revision 679490)
+++ libs/kotext/KoTextEditingPlugin.cpp	(working copy)
@@ -90,3 +90,6 @@
         block = block.next();
     }
 }
+
+#include "KoTextEditingPlugin.moc"
+
Index: libs/kotext/KoTextEditingFactory.cpp
===================================================================
--- libs/kotext/KoTextEditingFactory.cpp	(revision 679490)
+++ libs/kotext/KoTextEditingFactory.cpp	(working copy)
@@ -17,6 +17,8 @@
  * Boston, MA 02110-1301, USA.
  */
 
+#include <QMenu>
+
 #include "KoTextEditingFactory.h"
 
 class KoTextEditingFactory::Private {
@@ -30,12 +32,15 @@
     const QString id;
     bool showInMenu;
     QString title;
+    QMenu *menu;
+    bool hasSubMenu;
 };
 
 KoTextEditingFactory::KoTextEditingFactory(QObject *parent, const QString &id)
     : QObject(parent)
     , d( new Private(id) )
 {
+    d->hasSubMenu = false;
 }
 
 KoTextEditingFactory::~KoTextEditingFactory() {
@@ -62,4 +67,24 @@
     d->title = title;
 }
 
+QMenu *KoTextEditingFactory::menu() const
+{
+    return d->menu;
+}
+
+void KoTextEditingFactory::setMenu(QMenu *menu)
+{
+    if (menu) {
+        d->menu = menu;
+        d->hasSubMenu = true;
+    }
+    else
+        d->hasSubMenu = false;
+}
+
+bool KoTextEditingFactory::hasSubMenu() const
+{
+    return d->hasSubMenu;
+}
+
 #include "KoTextEditingFactory.moc"
Index: libs/kotext/KoTextEditingFactory.h
===================================================================
--- libs/kotext/KoTextEditingFactory.h	(revision 679490)
+++ libs/kotext/KoTextEditingFactory.h	(working copy)
@@ -27,6 +27,7 @@
 #include <kotext_export.h>
 
 class KoTextEditingPlugin;
+class QMenu;
 
 /**
  * A factory for text editing plugins. There should be one for each plugin type to
@@ -64,6 +65,10 @@
     /// If showInMenu() returns true; the returned text is used in the context menu.
     QString title() const;
 
+    QMenu *menu() const;
+
+    bool hasSubMenu() const;
+
 protected:
     /**
      * Set if the plugin this factory creates has to be shown in the context menu.
@@ -73,6 +78,8 @@
     /// set the title used in the context menu
     void setTitle(const QString &title);
 
+    void setMenu(QMenu *menu);
+
 private:
     class Private;
     Private * const d;
Index: libs/kotext/KoTextEditingPlugin.h
===================================================================
--- libs/kotext/KoTextEditingPlugin.h	(revision 679490)
+++ libs/kotext/KoTextEditingPlugin.h	(working copy)
@@ -19,11 +19,13 @@
 #ifndef KOTEXTEDITINGPLUGIN_H
 #define KOTEXTEDITINGPLUGIN_H
 
+#include <QObject>
 #include <QString>
 #include <kotext_export.h>
 
 class QTextDocument;
 class QTextCursor;
+class QUndoCommand;
 
 /**
  * This is a base class for a text editing plugin as used by the text tool.
@@ -36,7 +38,8 @@
  * a word and then moves the cursor out of the word, a similar approach happens with \
                the
  * finishedParagraph(), it will only be called after the cursor has been moved out \
                of the paragraph.
  */
-class KOTEXT_EXPORT KoTextEditingPlugin {
+class KOTEXT_EXPORT KoTextEditingPlugin : public QObject {
+    Q_OBJECT
 public:
     /// constructor
     KoTextEditingPlugin();
@@ -74,6 +77,9 @@
      */
     virtual void checkSection(QTextDocument *document, int startPosition, int \
endPosition);  
+signals:
+    void commandAvailable(QUndoCommand *command);
+
 protected:
     /**
      * Helper method that allows you to easily get the word out of the document.



_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


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

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