Development/Tutorials/Kate/KTextEditor Example

Jump to: navigation, search

Contents

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. }

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

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.

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.

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>

CMakeLists.txt

project(editor)

find_package(KDE4 REQUIRED) include_directories(${KDE4_INCLUDES})

set(editor_SRCS

 main.cpp
 mainwindow.cpp

)

kde4_add_executable(editor ${editor_SRCS})

target_link_libraries(editor ${KDE4_KDEUI_LIBS}

                            ${KDE4_KIO_LIBS}
                            ${KDE4_KTEXTEDITOR_LIBS})

install(TARGETS editor DESTINATION ${BIN_INSTALL_DIR}) install(FILES editorui.rc

       DESTINATION ${DATA_INSTALL_DIR}/editor)

</syntaxhighlight>


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