Development/Tutorials/Kate/KTextEditor Example: Difference between revisions

From KDE TechBase
(Mark for updating)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Review|
* Port to KF5
* Update screenshot
}}
==Abstract==
==Abstract==


Line 9: Line 14:


===main.cpp===
===main.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
#include <KApplication>
#include <KApplication>
#include <KAboutData>
#include <KAboutData>
Line 30: Line 35:
   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 54:
}
}


class MainWindow : public KXmlGuiWindow
class MainWindow : public KParts::MainWindow
{
{
   Q_OBJECT
   Q_OBJECT
Line 57: Line 62:
    
    
   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 72:


#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 90:
#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 116:


   setXMLFile("editorui.rc");
   setXMLFile("editorui.rc");
  createShellGUI(true);
   guiFactory()->addClient(m_view);
   guiFactory()->addClient(m_view);


Line 123: Line 125:
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()
void MainWindow::openFile()
{
{
   m_doc->documentSaveAs();
   m_view->document()->openUrl(KFileDialog::getOpenFileName());
}
}
</syntaxhighlight>
The implementation is straight forward and self-explanatory. Some remarks:


void MainWindow::saveFile()
'''MainWindow::Mainwindow()'''
{
  m_doc->documentSave();
}


void MainWindow::openFile()
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.
{
  m_view->document()->openUrl(KFileDialog::getOpenFileName());
}
</code>


The implementation is straight forward and self-explanatory.
'''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 213:
install(FILES editorui.rc  
install(FILES editorui.rc  
         DESTINATION ${DATA_INSTALL_DIR}/editor)
         DESTINATION ${DATA_INSTALL_DIR}/editor)
</code>
</syntaxhighlight>

Latest revision as of 12:28, 31 May 2019

Warning
This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

  • Port to KF5
  • Update screenshot

Abstract

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


The Code

main.cpp

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
 
#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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <KParts/MainWindow>
#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;
};

#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

#include "mainwindow.h"

#include <KApplication>
#include <KAction>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KFileDialog>
#include <KMessageBox>
#include <KIO/NetAccess>
#include <KSaveFile>
#include <QTextStream>
#include <KXMLGUIFactory>


#include <KTextEditor/Document>
#include <KTextEditor/View>
#include <KTextEditor/Editor>
#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 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"> <!-- increase version number if you don't see changes after experimenting with this file -->

<MenuBar>
  <Menu name="file" noMerge="1"><text>&amp;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>

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)