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

Jump to: navigation, search
(mainwindow.cpp)
m (editorui.rc)
Line 146: Line 146:
 
===editorui.rc===
 
===editorui.rc===
 
<code xml n>
 
<code xml n>
<?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="16">
   <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"/>
 +
    <Separator/>
 +
 
 +
    <Action name="file_quit"/>
 +
   </Menu>
 +
</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>
 
</code>
 
</code>
  

Revision as of 22:10, 18 February 2008

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>
  1. include "mainwindow.h"

int main (int argc, char *argv[]) {

 KAboutData aboutData( "editor", "editor",
     ki18n("Editor"), "1.0",
     ki18n("A simple text area which can load and save."),
     KAboutData::License_GPL,
     ki18n("Copyright (c) 2007 Developer") );
 KCmdLineArgs::init( argc, argv, &aboutData );
 KApplication app;

 MainWindow* window = new MainWindow();
 window->show();
 return app.exec();

}

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

mainwindow.h

  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <KParts/MainWindow>
  2. include <QtGui/QKeyEvent>


namespace KTextEditor {

 class Document;
 class View;

}

class MainWindow : public KParts::MainWindow {

 Q_OBJECT
 
 public:
   MainWindow(QWidget *parent=0);
 
 private slots:
   void clear();
   void openFile();
 private:
   void setupActions();
   KTextEditor::View *m_view;
   KTextEditor::Document *m_doc;

};

  1. 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"
  1. include <KApplication>
  2. include <KAction>
  3. include <KLocale>
  4. include <KActionCollection>
  5. include <KStandardAction>
  6. include <KFileDialog>
  7. include <KMessageBox>
  8. include <KIO/NetAccess>
  9. include <KSaveFile>
  10. include <QTextStream>
  11. include <KXMLGUIFactory>


  1. include <KTextEditor/Document>
  2. include <KTextEditor/View>
  3. include <KTextEditor/Editor>
  4. include <KTextEditor/EditorChooser>


MainWindow::MainWindow(QWidget *) {

 KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor();
 if (!editor) {
   KMessageBox::error(this, i18n("A KDE text-editor component could not be found;\n"

"please check your KDE installation."));

   kapp->exit(1);
 }
 m_doc = editor->createDocument(0);
 m_view = qobject_cast<KTextEditor::View*>(m_doc->createView(this));
 setCentralWidget(m_view);
 setupActions();
 setXMLFile("editorui.rc");
 createShellGUI(true);
 guiFactory()->addClient(m_view);
 show ();

}

void MainWindow::setupActions() {

 KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
 KStandardAction::open(this, SLOT(openFile()), actionCollection());
 KStandardAction::clear(this, SLOT(clear()), actionCollection());

}

void MainWindow::clear() {

 m_doc->clear();

}

void MainWindow::openFile() {

 m_view->document()->openUrl(KFileDialog::getOpenFileName());

}

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

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

MainWindow::setupAction() Defines three additional actions.

editorui.rc

<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd"> <kpartgui name="editor" version="16">

<MenuBar>

 <Menu name="file" noMerge="1"><text>&File</text>
   <Action name="file_open" />
   <DefineGroup name="save_merge" append="save_merge" />
   <Separator/>
   <DefineGroup name="revert_merge" append="revert_merge"/>
   <DefineGroup name="print_merge" append="print_merge"/>
   <Separator/>
   <Action name="file_quit"/>
 </Menu>

</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>

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)


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