Development/Tutorials/Kate/KTextEditor Example

Jump to: navigation, search

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


The Code

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. }
  21. </code>
  22.  
  23. In <tt>main.cpp</tt> just defines ''aboutData'' and ''app'' and shows ''Mainwindow''.
  24.  
  25. ===mainwindow.h===
  26. <syntaxhighlight lang="cpp-qt" line>
  27. #ifndef MAINWINDOW_H
  28. #define MAINWINDOW_H
  29.  
  30. #include <KParts/MainWindow>
  31. #include <QtGui/QKeyEvent>
  32.  
  33.  
  34. namespace KTextEditor
  35. {
  36.   class Document;
  37.   class View;
  38. }
  39.  
  40. class MainWindow : public KParts::MainWindow
  41. {
  42.   Q_OBJECT
  43.  
  44.   public:
  45.     MainWindow(QWidget *parent=0);
  46.  
  47.   private slots:
  48.     void clear();
  49.     void openFile();
  50.  
  51.   private:
  52.     void setupActions();
  53.     KTextEditor::View *m_view;
  54.     KTextEditor::Document *m_doc;
  55. };
  56.  
  57. #endif
  58. </code>
  59.  
  60. Class MainWindow is a successor of KXmlGuiWindow and contains KTextEditor (document and view) as a private Variable. There are also some useful methods defined.
  61.  
  62. ===mainwindow.cpp===
  63. <syntaxhighlight lang="cpp-qt" line>
  64. #include "mainwindow.h"
  65.  
  66. #include <KApplication>
  67. #include <KAction>
  68. #include <KLocale>
  69. #include <KActionCollection>
  70. #include <KStandardAction>
  71. #include <KFileDialog>
  72. #include <KMessageBox>
  73. #include <KIO/NetAccess>
  74. #include <KSaveFile>
  75. #include <QTextStream>
  76. #include <KXMLGUIFactory>
  77.  
  78.  
  79. #include <KTextEditor/Document>
  80. #include <KTextEditor/View>
  81. #include <KTextEditor/Editor>
  82. #include <KTextEditor/EditorChooser>
  83.  
  84.  
  85. MainWindow::MainWindow(QWidget *)
  86. {
  87.   KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor();
  88.  
  89.   if (!editor) {
  90.     KMessageBox::error(this, i18n("A KDE text-editor component could not be found;\n"
  91. 				  "please check your KDE installation."));
  92.     kapp->exit(1);
  93.   }
  94.  
  95.   m_doc = editor->createDocument(0);
  96.   m_view = qobject_cast<KTextEditor::View*>(m_doc->createView(this));
  97.  
  98.   setCentralWidget(m_view);
  99.   setupActions();
  100.  
  101.   setXMLFile("editorui.rc");
  102.   createShellGUI(true);
  103.  
  104.   guiFactory()->addClient(m_view);
  105.  
  106.   show ();
  107. }
  108.  
  109. void MainWindow::setupActions()
  110. {
  111.   KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
  112.   KStandardAction::open(this, SLOT(openFile()), actionCollection());
  113.   KStandardAction::clear(this, SLOT(clear()), actionCollection());
  114. }
  115.  
  116. void MainWindow::clear()
  117. {
  118.   m_doc->clear();
  119. }
  120.  
  121. void MainWindow::openFile()
  122. {
  123.   m_view->document()->openUrl(KFileDialog::getOpenFileName());
  124. }
  125. </code>
  126.  
  127. The implementation is straight forward and self-explanatory. Some remarks:
  128.  
  129. '''MainWindow::Mainwindow()'''
  130.  
  131. 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.
  132.  
  133. '''MainWindow::setupAction()''' defines three additional actions.
  134.  
  135. ===editorui.rc===
  136. <code xml n>
  137. <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
  138. <kpartgui name="editor" version="1"> <!-- increase version number if you don't see changes after experimenting with this file -->
  139.  
  140. <MenuBar>
  141.   <Menu name="file" noMerge="1"><text>&amp;File</text>
  142.  
  143.     <Action name="file_open" />
  144.     <DefineGroup name="save_merge" append="save_merge" />
  145.     <Separator/>
  146.  
  147.     <DefineGroup name="revert_merge" append="revert_merge"/>
  148.     <DefineGroup name="print_merge" append="print_merge"/>
  149.     <Separator/>
  150.  
  151.     <Action name="file_quit"/>
  152.   </Menu>
  153.   <Merge />
  154. </MenuBar>
  155.  
  156. <ToolBar name="mainToolBar" noMerge="1"><text>Main Toolbar</text>
  157.   <Action name="file_open" />
  158.   <DefineGroup name="file_operations" />
  159.   <Separator />
  160.   <DefineGroup name="print_merge" />
  161.   <Separator />
  162.   <Action name="file_close" />
  163.   <Separator />
  164.   <DefineGroup name="edit_operations" />
  165.   <Separator />
  166.   <DefineGroup name="find_operations" />
  167.   <Separator />
  168.   <DefineGroup name="zoom_operations" />
  169. </ToolBar>
  170.  
  171. <Menu name="ktexteditor_popup" noMerge="1">
  172.   <DefineGroup name="popup_operations" />
  173. </Menu>
  174.  
  175. </kpartgui>
  176. </code>
  177.  
  178. ===CMakeLists.txt===
  179. <code ini n>
  180. project(editor)
  181.  
  182. find_package(KDE4 REQUIRED)
  183. include_directories(${KDE4_INCLUDES})
  184.  
  185. set(editor_SRCS 
  186.   main.cpp
  187.   mainwindow.cpp
  188. )
  189.  
  190. kde4_add_executable(editor ${editor_SRCS})
  191.  
  192. target_link_libraries(editor ${KDE4_KDEUI_LIBS} 
  193.                              ${KDE4_KIO_LIBS}
  194.                              ${KDE4_KTEXTEDITOR_LIBS})
  195.  
  196. install(TARGETS editor DESTINATION ${BIN_INSTALL_DIR})
  197. install(FILES editorui.rc 
  198.         DESTINATION ${DATA_INSTALL_DIR}/editor)
  199. </code>

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal