m (→mainwindow.h) |
(→mainwindow.cpp) |
||
| Line 85: | Line 85: | ||
#include <KSaveFile> | #include <KSaveFile> | ||
#include <QTextStream> | #include <QTextStream> | ||
| − | #include < | + | #include <KXMLGUIFactory> |
| − | #include < | + | #include <KTextEditor/Document> |
| − | #include < | + | #include <KTextEditor/View> |
| − | #include < | + | #include <KTextEditor/Editor> |
| − | #include < | + | #include <KTextEditor/EditorChooser> |
| − | MainWindow::MainWindow(QWidget * | + | MainWindow::MainWindow(QWidget *) |
| − | + | ||
| − | + | ||
{ | { | ||
KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor(); | KTextEditor::Editor *editor = KTextEditor::EditorChooser::editor(); | ||
| Line 113: | Line 111: | ||
setXMLFile("editorui.rc"); | setXMLFile("editorui.rc"); | ||
| + | createShellGUI(true); | ||
| + | |||
guiFactory()->addClient(m_view); | guiFactory()->addClient(m_view); | ||
| Line 120: | Line 120: | ||
void MainWindow::setupActions() | void MainWindow::setupActions() | ||
{ | { | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
KStandardAction::quit(kapp, SLOT(quit()), actionCollection()); | KStandardAction::quit(kapp, SLOT(quit()), actionCollection()); | ||
| − | KStandardAction::open(this, SLOT(openFile()), actionCollection()); | + | KStandardAction::open(this, SLOT(openFile()), actionCollection()); |
| − | KStandardAction:: | + | KStandardAction::clear(this, SLOT(clear()), actionCollection()); |
| − | + | ||
| − | + | ||
} | } | ||
| − | void MainWindow:: | + | void MainWindow::clear() |
{ | { | ||
| − | |||
m_doc->clear(); | m_doc->clear(); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| Line 155: | Line 136: | ||
</code> | </code> | ||
| − | The implementation is straight forward and self-explanatory. | + | 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=== | ===editorui.rc=== | ||
Contents |
We build a small application using KTexteditor. This example supports syntax highlighting and other useful features. We see how to use KTextEditor.
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.
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;
};
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::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.
<?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>
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)