Difference between revisions of "Development/Tutorials/Kate/KTextEditor Example"

Jump to: navigation, search
(New page: ==Abstract== We build a small application using KTexteditor. This example supports syntax highlighting and other useful features. We see how to use KTextEditor. [[image:TutorialEditor.pn...)
 
m (Text replace - "<code ini n>" to "<syntaxhighlight lang="ini" line>")
 
(9 intermediate revisions by 3 users not shown)
Line 9: Line 9:
  
 
===main.cpp===
 
===main.cpp===
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <KApplication>
 
#include <KApplication>
 
#include <KAboutData>
 
#include <KAboutData>
Line 19: Line 19:
 
{
 
{
 
   KAboutData aboutData( "editor", "editor",
 
   KAboutData aboutData( "editor", "editor",
       ki18n("Tutorial KTextEditor"), "1.0",
+
       ki18n("Editor"), "1.0",
 
       ki18n("A simple text area which can load and save."),
 
       ki18n("A simple text area which can load and save."),
 
       KAboutData::License_GPL,
 
       KAboutData::License_GPL,
Line 30: Line 30:
 
   return app.exec();
 
   return app.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
In <tt>main.cpp</tt> just defines ''aboutData'' and ''app'' and shows ''Mainwindow''.
 
In <tt>main.cpp</tt> just defines ''aboutData'' and ''app'' and shows ''Mainwindow''.
  
 
===mainwindow.h===
 
===mainwindow.h===
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#ifndef MAINWINDOW_H
 
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
 
#define MAINWINDOW_H
  
#include <KXmlGuiWindow>
+
#include <KParts/MainWindow>
 
#include <QtGui/QKeyEvent>
 
#include <QtGui/QKeyEvent>
  
Line 49: Line 49:
 
}
 
}
  
class MainWindow : public KXmlGuiWindow
+
class MainWindow : public KParts::MainWindow
 
{
 
{
 
   Q_OBJECT
 
   Q_OBJECT
Line 57: Line 57:
 
    
 
    
 
   private slots:
 
   private slots:
     void newFile();
+
     void clear();
 
     void openFile();
 
     void openFile();
    void saveFile();
 
    void saveFileAs();
 
  
 
   private:
 
   private:
 
     void setupActions();
 
     void setupActions();
    QString fileName;
 
 
     KTextEditor::View *m_view;
 
     KTextEditor::View *m_view;
 
     KTextEditor::Document *m_doc;
 
     KTextEditor::Document *m_doc;
Line 70: Line 67:
  
 
#endif
 
#endif
</code>
+
</syntaxhighlight>
  
 
Class MainWindow is a successor of KXmlGuiWindow and contains KTextEditor (document and view) as a private Variable. There are also some useful methods defined.
 
Class MainWindow is a successor of KXmlGuiWindow and contains KTextEditor (document and view) as a private Variable. There are also some useful methods defined.
  
 
===mainwindow.cpp===
 
===mainwindow.cpp===
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include "mainwindow.h"
 
#include "mainwindow.h"
  
Line 88: Line 85:
 
#include <KSaveFile>
 
#include <KSaveFile>
 
#include <QTextStream>
 
#include <QTextStream>
#include <kxmlguifactory.h>
+
#include <KXMLGUIFactory>
  
  
#include <ktexteditor/document.h>
+
#include <KTextEditor/Document>
#include <ktexteditor/view.h>
+
#include <KTextEditor/View>
#include <ktexteditor/editor.h>
+
#include <KTextEditor/Editor>
#include <ktexteditor/editorchooser.h>
+
#include <KTextEditor/EditorChooser>
  
  
MainWindow::MainWindow(QWidget *parent)
+
MainWindow::MainWindow(QWidget *)
    : KXmlGuiWindow(parent),
+
      fileName(QString()) //new
+
 
{
 
{
 
   KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor();
 
   KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor();
Line 116: Line 111:
  
 
   setXMLFile("editorui.rc");
 
   setXMLFile("editorui.rc");
 +
  createShellGUI(true);
 +
 
   guiFactory()->addClient(m_view);
 
   guiFactory()->addClient(m_view);
  
Line 123: Line 120:
 
void MainWindow::setupActions()
 
void MainWindow::setupActions()
 
{
 
{
  KAction* clearAction = new KAction(this);
 
  clearAction->setText(i18n("Clear"));
 
  clearAction->setIcon(KIcon("document-new"));
 
  clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
 
  actionCollection()->addAction("clear", clearAction);
 
  connect(clearAction, SIGNAL(triggered(bool)),m_doc, SLOT(clear()));
 
 
   KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
 
   KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
   KStandardAction::open(this, SLOT(openFile()), actionCollection()); //new
+
   KStandardAction::open(this, SLOT(openFile()), actionCollection());
   KStandardAction::openNew(this, SLOT(newFile()), actionCollection()); //new
+
   KStandardAction::clear(this, SLOT(clear()), actionCollection());
+
  setupGUI();
+
 
}
 
}
  
void MainWindow::newFile()
+
void MainWindow::clear()
 
{
 
{
  fileName.clear();
 
 
   m_doc->clear();
 
   m_doc->clear();
}
 
 
void MainWindow::saveFileAs()
 
{
 
  m_doc->documentSaveAs();
 
}
 
 
void MainWindow::saveFile()
 
{
 
  m_doc->documentSave();
 
 
}
 
}
  
Line 156: Line 134:
 
   m_view->document()->openUrl(KFileDialog::getOpenFileName());
 
   m_view->document()->openUrl(KFileDialog::getOpenFileName());
 
}
 
}
</code>
+
</syntaxhighlight>
  
The implementation is straight forward and self-explanatory.
+
The implementation is straight forward and self-explanatory. Some remarks:
 +
 
 +
'''MainWindow::Mainwindow()'''
 +
 
 +
First the editor component is created on the heap. After creating ''document()'' and ''view()'', the GUI definitions are loaded from editorui.rc. The call ''guiFactory()->addClient(m_view)'' adds the kate parts menue and toolbar definitions to MainWindow. After that a full featured texteditor with syntax highlighting etc. is available for  your application.
 +
 
 +
'''MainWindow::setupAction()''' defines three additional actions.
  
 
===editorui.rc===
 
===editorui.rc===
<code xml n>
+
<syntaxhighlight lang="xml" line>
<?xml version="1.0" encoding="UTF-8"?>
+
 
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
 
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<gui name="editor" version="1">
+
<kpartgui name="editor" version="1"> <!-- increase version number if you don't see changes after experimenting with this file -->
   <ToolBar name="mainToolBar" >
+
 
     <text>Main Toolbar</text>
+
<MenuBar>
    <Action name="clear" />
+
   <Menu name="file" noMerge="1"><text>&amp;File</text>
   </ToolBar>
+
 
   <MenuBar>
+
     <Action name="file_open" />
    <Menu name="file" >
+
    <DefineGroup name="save_merge" append="save_merge" />
      <Action name="clear" />
+
    <Separator/>
    </Menu>
+
 
   </MenuBar>
+
    <DefineGroup name="revert_merge" append="revert_merge"/>
</gui>
+
    <DefineGroup name="print_merge" append="print_merge"/>
</code>
+
    <Separator/>
 +
 
 +
    <Action name="file_quit"/>
 +
  </Menu>
 +
  <Merge />
 +
</MenuBar>
 +
 
 +
<ToolBar name="mainToolBar" noMerge="1"><text>Main Toolbar</text>
 +
  <Action name="file_open" />
 +
   <DefineGroup name="file_operations" />
 +
   <Separator />
 +
  <DefineGroup name="print_merge" />
 +
  <Separator />
 +
  <Action name="file_close" />
 +
  <Separator />
 +
  <DefineGroup name="edit_operations" />
 +
  <Separator />
 +
  <DefineGroup name="find_operations" />
 +
  <Separator />
 +
  <DefineGroup name="zoom_operations" />
 +
</ToolBar>
 +
 
 +
<Menu name="ktexteditor_popup" noMerge="1">
 +
   <DefineGroup name="popup_operations" />
 +
</Menu>
 +
 
 +
</kpartgui>
 +
</syntaxhighlight>
  
 
===CMakeLists.txt===
 
===CMakeLists.txt===
<code ini n>
+
<syntaxhighlight lang="ini" line>
 
project(editor)
 
project(editor)
 
   
 
   
Line 198: Line 208:
 
install(FILES editorui.rc  
 
install(FILES editorui.rc  
 
         DESTINATION ${DATA_INSTALL_DIR}/editor)
 
         DESTINATION ${DATA_INSTALL_DIR}/editor)
</code>
+
</syntaxhighlight>

Latest revision as of 22:05, 29 June 2011

Contents

[edit] Abstract

We build a small application using KTexteditor. This example supports syntax highlighting and other useful features. We see how to use KTextEditor.

TutorialEditor.png


[edit] The Code

[edit] main.cpp

  1. #include <KApplication>
  2. #include <KAboutData>
  3. #include <KCmdLineArgs>
  4.  
  5. #include "mainwindow.h"
  6.  
  7. int main (int argc, char *argv[])
  8. {
  9.   KAboutData aboutData( "editor", "editor",
  10.       ki18n("Editor"), "1.0",
  11.       ki18n("A simple text area which can load and save."),
  12.       KAboutData::License_GPL,
  13.       ki18n("Copyright (c) 2007 Developer") );
  14.   KCmdLineArgs::init( argc, argv, &aboutData );
  15.   KApplication app;
  16.  
  17.   MainWindow* window = new MainWindow();
  18.   window->show();
  19.   return app.exec();
  20. }

In main.cpp just defines aboutData and app and shows Mainwindow.

[edit] mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <KParts/MainWindow>
  5. #include <QtGui/QKeyEvent>
  6.  
  7.  
  8. namespace KTextEditor
  9. {
  10.   class Document;
  11.   class View;
  12. }
  13.  
  14. class MainWindow : public KParts::MainWindow
  15. {
  16.   Q_OBJECT
  17.  
  18.   public:
  19.     MainWindow(QWidget *parent=0);
  20.  
  21.   private slots:
  22.     void clear();
  23.     void openFile();
  24.  
  25.   private:
  26.     void setupActions();
  27.     KTextEditor::View *m_view;
  28.     KTextEditor::Document *m_doc;
  29. };
  30.  
  31. #endif

Class MainWindow is a successor of KXmlGuiWindow and contains KTextEditor (document and view) as a private Variable. There are also some useful methods defined.

[edit] mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <KApplication>
  4. #include <KAction>
  5. #include <KLocale>
  6. #include <KActionCollection>
  7. #include <KStandardAction>
  8. #include <KFileDialog>
  9. #include <KMessageBox>
  10. #include <KIO/NetAccess>
  11. #include <KSaveFile>
  12. #include <QTextStream>
  13. #include <KXMLGUIFactory>
  14.  
  15.  
  16. #include <KTextEditor/Document>
  17. #include <KTextEditor/View>
  18. #include <KTextEditor/Editor>
  19. #include <KTextEditor/EditorChooser>
  20.  
  21.  
  22. MainWindow::MainWindow(QWidget *)
  23. {
  24.   KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor();
  25.  
  26.   if (!editor) {
  27.     KMessageBox::error(this, i18n("A KDE text-editor component could not be found;\n"
  28. 				  "please check your KDE installation."));
  29.     kapp->exit(1);
  30.   }
  31.  
  32.   m_doc = editor->createDocument(0);
  33.   m_view = qobject_cast<KTextEditor::View*>(m_doc->createView(this));
  34.  
  35.   setCentralWidget(m_view);
  36.   setupActions();
  37.  
  38.   setXMLFile("editorui.rc");
  39.   createShellGUI(true);
  40.  
  41.   guiFactory()->addClient(m_view);
  42.  
  43.   show ();
  44. }
  45.  
  46. void MainWindow::setupActions()
  47. {
  48.   KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
  49.   KStandardAction::open(this, SLOT(openFile()), actionCollection());
  50.   KStandardAction::clear(this, SLOT(clear()), actionCollection());
  51. }
  52.  
  53. void MainWindow::clear()
  54. {
  55.   m_doc->clear();
  56. }
  57.  
  58. void MainWindow::openFile()
  59. {
  60.   m_view->document()->openUrl(KFileDialog::getOpenFileName());
  61. }

The implementation is straight forward and self-explanatory. Some remarks:

MainWindow::Mainwindow()

First the editor component is created on the heap. After creating document() and view(), the GUI definitions are loaded from editorui.rc. The call guiFactory()->addClient(m_view) adds the kate parts menue and toolbar definitions to MainWindow. After that a full featured texteditor with syntax highlighting etc. is available for your application.

MainWindow::setupAction() defines three additional actions.

[edit] editorui.rc

  1. <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
  2. <kpartgui name="editor" version="1"> <!-- increase version number if you don't see changes after experimenting with this file -->
  3.  
  4. <MenuBar>
  5.   <Menu name="file" noMerge="1"><text>&amp;File</text>
  6.  
  7.     <Action name="file_open" />
  8.     <DefineGroup name="save_merge" append="save_merge" />
  9.     <Separator/>
  10.  
  11.     <DefineGroup name="revert_merge" append="revert_merge"/>
  12.     <DefineGroup name="print_merge" append="print_merge"/>
  13.     <Separator/>
  14.  
  15.     <Action name="file_quit"/>
  16.   </Menu>
  17.   <Merge />
  18. </MenuBar>
  19.  
  20. <ToolBar name="mainToolBar" noMerge="1"><text>Main Toolbar</text>
  21.   <Action name="file_open" />
  22.   <DefineGroup name="file_operations" />
  23.   <Separator />
  24.   <DefineGroup name="print_merge" />
  25.   <Separator />
  26.   <Action name="file_close" />
  27.   <Separator />
  28.   <DefineGroup name="edit_operations" />
  29.   <Separator />
  30.   <DefineGroup name="find_operations" />
  31.   <Separator />
  32.   <DefineGroup name="zoom_operations" />
  33. </ToolBar>
  34.  
  35. <Menu name="ktexteditor_popup" noMerge="1">
  36.   <DefineGroup name="popup_operations" />
  37. </Menu>
  38.  
  39. </kpartgui>

[edit] CMakeLists.txt

  1. project(editor)
  2.  
  3. find_package(KDE4 REQUIRED)
  4. include_directories(${KDE4_INCLUDES})
  5.  
  6. set(editor_SRCS 
  7.   main.cpp
  8.   mainwindow.cpp
  9. )
  10.  
  11. kde4_add_executable(editor ${editor_SRCS})
  12.  
  13. target_link_libraries(editor ${KDE4_KDEUI_LIBS} 
  14.                              ${KDE4_KIO_LIBS}
  15.                              ${KDE4_KTEXTEDITOR_LIBS})
  16.  
  17. install(TARGETS editor DESTINATION ${BIN_INSTALL_DIR})
  18. install(FILES editorui.rc 
  19.         DESTINATION ${DATA_INSTALL_DIR}/editor)

This page was last modified on 29 June 2011, at 22:05. This page has been accessed 9,609 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal