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

Jump to: navigation, search
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
m (Text replace - "</code>" to "</syntaxhighlight>")
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''.
Line 67: 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.
Line 134: Line 134:
 
   m_view->document()->openUrl(KFileDialog::getOpenFileName());
 
   m_view->document()->openUrl(KFileDialog::getOpenFileName());
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
The implementation is straight forward and self-explanatory. Some remarks:
 
The implementation is straight forward and self-explanatory. Some remarks:
Line 185: Line 185:
  
 
</kpartgui>
 
</kpartgui>
</code>
+
</syntaxhighlight>
  
 
===CMakeLists.txt===
 
===CMakeLists.txt===
Line 208: Line 208:
 
install(FILES editorui.rc  
 
install(FILES editorui.rc  
 
         DESTINATION ${DATA_INSTALL_DIR}/editor)
 
         DESTINATION ${DATA_INSTALL_DIR}/editor)
</code>
+
</syntaxhighlight>

Revision as of 20:52, 29 June 2011

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

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

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

<code ini n> 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>