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

From KDE TechBase
Line 39: Line 39:
#define MAINWINDOW_H
#define MAINWINDOW_H


#include <KXmlGuiWindow>
#include <KParts/MainWindow>
#include <QtGui/QKeyEvent>
#include <QtGui/QKeyEvent>


Line 49: Line 49:
}
}


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

Revision as of 21:03, 18 February 2008

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

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


  1. include <ktexteditor/document.h>
  2. include <ktexteditor/view.h>
  3. include <ktexteditor/editor.h>
  4. include <ktexteditor/editorchooser.h>


MainWindow::MainWindow(QWidget *parent)

   : KXmlGuiWindow(parent),
     fileName(QString()) //new

{

 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");
 guiFactory()->addClient(m_view);
 show ();

}

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::open(this, SLOT(openFile()), actionCollection()); //new
 KStandardAction::openNew(this, SLOT(newFile()), actionCollection()); //new

 setupGUI();

}

void MainWindow::newFile() {

 fileName.clear();
 m_doc->clear();

}

void MainWindow::saveFileAs() {

 m_doc->documentSaveAs();

}

void MainWindow::saveFile() {

 m_doc->documentSave();

}

void MainWindow::openFile() {

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

}

The implementation is straight forward and self-explanatory.

editorui.rc

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd"> <gui name="editor" version="1">

 <ToolBar name="mainToolBar" >
   <text>Main Toolbar</text>
   <Action name="clear" />
 </ToolBar>
 <MenuBar>
   <Menu name="file" >
     <Action name="clear" />
   </Menu>
 </MenuBar>

</gui>

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)