Archive:Development/Tutorials/Using KParts (zh CN): Difference between revisions

From KDE TechBase
No edit summary
m (AnneW moved page Development/Tutorials/Using KParts (zh CN) to Archive:Development/Tutorials/Using KParts (zh CN) without leaving a redirect: Obsolete)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KParts}}
 


{{TutorialBrowser|
{{TutorialBrowser|
Line 15: Line 15:
== 介绍 ==
== 介绍 ==


KPart technology is used in kde to reuse GUI components. The advantage that a KPart presents is that it comes with predefined toolbar actions. By using kparts in applications developers can spend less time implementing text editor or command line features, for example and just use a katepart or a konsolepart instead. KParts are also used with Plugin technology to embed applications inside another, such as integrating PIM applications into Kontact.
KPart技术是KDE用来重用图形界面组件的。出现的优势在于它预定义了工具条和一些相应动作(actions)。开发人员可以用KParts来实现一些文字编辑器和命令行功能,从而减少开发时间,例如在应用程序中使用KatePart或Konsolepart。 KParts同时采用插件技术,一个应用可以内嵌在另一个里面,就像PIM之于Kontact。


This tutorial will show you how to use a KPart in your application, and how to create your own KPart.
本教程讲述如何在你的程序中使用KPart, 如何创建你自己的KPart.


== 使用Katepart ==
== 使用Katepart ==


Simple KDE applications use a MainWindow derived from KMainWindow (such as in previous tutorials). To use a KPart in an application, the MainWindow must instead be derived from KParts::MainWindow. This will then take care of integrating the toolbar and menu items of the kpart together.
简单的KDE应用程序直接从KMainWindow继承一个MainWindow(见前叙教程)。如要使用KPart,那MainWindow必须从KParts::MainWindow集成下来。这个类会管理所有KPart的工具栏,菜单的集成。


下面代码创建一个有KPart的KParts::MainWindow。
下面代码创建一个有KPart的KParts::MainWindow。
Line 27: Line 27:
=== main.cpp ===
=== main.cpp ===


<code cppqt>
<syntaxhighlight lang="cpp-qt">
#include <KApplication>
#include <KApplication>
#include <KAboutData>
#include <KAboutData>
Line 62: Line 62:
}
}


</code>
</syntaxhighlight>


这个main.cpp文件和[[Development/Tutorials/KCmdLineArgs|教程五 -命令行参数]]里用的一样。唯一的不同就是KAboutData的一些具体内容。
这个main.cpp文件和[[Development/Tutorials/KCmdLineArgs|教程五-命令行参数]]里用的一样。唯一的不同就是KAboutData的一些具体内容。


=== mainwindow.h ===
=== mainwindow.h ===


<code cppqt>
<syntaxhighlight lang="cpp-qt">


#ifndef KPARTTUTORIAL1_H         
#ifndef KPARTTUTORIAL1_H         
Line 88: Line 88:
public:
public:
     /**
     /**
     * 缺省构造寒食。
     * 缺省构造函数。
     */
     */
     MainWindow();
     MainWindow();
Line 116: Line 116:
#endif // KPARTTUT1_H
#endif // KPARTTUT1_H


</code>
</syntaxhighlight>


mainwindow.h文件很简单。注意这里MainWindow类从KParts::MainWindow继承而来。
mainwindow.h文件很简单。注意这里MainWindow类从KParts::MainWindow继承而来。
Line 122: Line 122:
=== mainwindow.cpp ===
=== mainwindow.cpp ===


<code cppqt>
<syntaxhighlight lang="cpp-qt">


#include "mainwindow.h"
#include "mainwindow.h"
Line 168: Line 168:
     else
     else
     {
     {
         // if we couldn't find our Part, we exit since the Shell by
         // 如果找不到Part, 我们就退出,因为Shell也干不了什么了
        // itself can't do anything useful
         KMessageBox::error(this, "Could not find our Part!");
         KMessageBox::error(this, "Could not find our Part!");
         qApp->quit();
         qApp->quit();
         // we return here, cause qApp->quit() only means "exit the
         // 从这里退出,qApp->quit()只是说下次进入循环的时候退出...
        // next time we enter the event loop...
         return;
         return;
     }
     }
Line 179: Line 177:


MainWindow::~MainWindow()
MainWindow::~MainWindow()
{
{}
}


void MainWindow::load(const KUrl& url)
void MainWindow::load(const KUrl& url)
Line 200: Line 197:
}
}


</code>
</syntaxhighlight>


mainwindow.cpp文件有MainWindow的实现。它的构造函数包含所有加载KPart的代码。
mainwindow.cpp文件有MainWindow的实现。它的构造函数包含所有加载KPart的代码。


First it sets up the actions used by the main window (Open and Quit), and then sets up the gui elements to go with these items (toolbar items, menu items, keyboard shortcuts). Next is the standard code used to load the KPart. The createGUI method is responsible for merging the toolbars and menus of the KPart with the rest of the application.
首先设置主窗口所使用的actions(打开,退出),然后设置图形界面元素(如工具栏,菜单项,键盘快捷键)。下一步就是加载KPart的标准代码了。createGUI方法负责把KPart的工具栏,菜单和程序其他部分集成起来。


=== kparttut1ui.rc ===
=== kparttut1ui.rc ===


<code xml>
<syntaxhighlight lang="xml">
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="kparttut1" version="1">
<kpartgui name="kparttut1" version="1">
Line 227: Line 224:
</ToolBar>
</ToolBar>
</kpartgui>
</kpartgui>
</code>
</syntaxhighlight>


kparttut1ui.rc文件用来定义Part中的动作(Action)如何于主窗口的动作(Action)合并起来。这些<tt><Merge /></tt> element in the file menu for example indicates that any part containing actions in a file menu should list its parts after the file_open action and before the file_quit action.
kparttut1ui.rc文件用来定义Part中的动作(Action)如何于主窗口的动作(Action)合并起来。这些<tt><Merge /></tt> element in the file menu for example indicates that any part containing actions in a file menu should list its parts在file_open action之后以及file_quit action之前.


=== CMakeLists.txt ===
=== CMakeLists.txt ===


<code ini>
<syntaxhighlight lang="ini">


project(kparttut1)
project(kparttut1)
Line 253: Line 250:
install( FILES kparttut1ui.rc  
install( FILES kparttut1ui.rc  
     DESTINATION  ${DATA_INSTALL_DIR}/kparttut1 )
     DESTINATION  ${DATA_INSTALL_DIR}/kparttut1 )
</code>
</syntaxhighlight>
这里,CMakeLists.txt文件很简单。
这里,CMakeLists.txt文件很简单。


Line 264: Line 261:
''文件名filename''就是要加在的文本文件。源代码文件也可以。
''文件名filename''就是要加在的文本文件。源代码文件也可以。


When the file loads you will have a full-featured kate editor running in its own window. All of the editor features of kate are available in the toolbars and menu.
文件加载后,你就有了一个全功能的Kate编辑器运行在你的程序窗口里。所有Kate编辑器的功能在工具栏和菜单里都有。


You will notice that the 'Open' action you defined in the MainWindow class has also appeared in the toolbar and in the menu along with the 'Quit' action.
You will notice that the 'Open' action you defined in the MainWindow class has also appeared in the toolbar and in the menu along with the 'Quit' action.


下一个教程会讲述如何在应用程序里使用,重用你创建的KPart。
下一个教程会讲述如何在应用程序里使用,重用你创建的KPart。

Latest revision as of 13:05, 23 June 2013


如何使用KParts
Tutorial Series   插件和KParts
Previous   教程五 -命令行参数
What's Next   n/a
Further Reading   KParts文档

介绍

KPart技术是KDE用来重用图形界面组件的。出现的优势在于它预定义了工具条和一些相应动作(actions)。开发人员可以用KParts来实现一些文字编辑器和命令行功能,从而减少开发时间,例如在应用程序中使用KatePart或Konsolepart。 KParts同时采用插件技术,一个应用可以内嵌在另一个里面,就像PIM之于Kontact。

本教程讲述如何在你的程序中使用KPart, 如何创建你自己的KPart.

使用Katepart

简单的KDE应用程序直接从KMainWindow继承一个MainWindow(见前叙教程)。如要使用KPart,那MainWindow必须从KParts::MainWindow集成下来。这个类会管理所有KPart的工具栏,菜单的集成。

下面代码创建一个有KPart的KParts::MainWindow。

main.cpp

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include <KUrl>
 
#include "mainwindow.h"
 
int main (int argc, char *argv[])
{
    KAboutData aboutData( "kparttutorial1", "kparttutorial1",
        ki18n("KPart Tutorial 1"), "0.1",
        ki18n("A MainWindow for a KatePart."),
        KAboutData::License_GPL,
        ki18n("Copyright (c) 2007 Developer") );
    KCmdLineArgs::init( argc, argv, &aboutData );
 
    KCmdLineOptions options;
    options.add("+[file]", ki18n("Document to open"));
    KCmdLineArgs::addCmdLineOptions(options);
 
    KApplication app;
 
    MainWindow* window = new MainWindow();
    window->show();

    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    if(args->count())
    {
        window->load(args->url(0).url());
    }

    return app.exec();
}

这个main.cpp文件和教程五-命令行参数里用的一样。唯一的不同就是KAboutData的一些具体内容。

mainwindow.h

#ifndef KPARTTUTORIAL1_H        
#define KPARTTUTORIAL1_H        
                                
#include <kparts/mainwindow.h>  
                                
/**                             
 * This is the application "Shell".  It has a menubar, toolbar, and
 * statusbar but relies on the "Part" to do all the real work.     
 *                                                                 
 * @short Application Shell                                        
 * @author Developer <[email protected]>                           
 * @version 0.1
 */
class MainWindow : public KParts::MainWindow
{
    Q_OBJECT
public:
    /**
     * 缺省构造函数。
     */
    MainWindow();

    /**
     * 缺省析构函数。
     */
    virtual ~MainWindow();

    /**
     * 用于加载文件或URL。
     */
    void load(const KUrl& url);

    /**
     * 显示打开URL的对话框,加载输入的URL。
     */
    void load();

private:
    void setupActions();

private:
    KParts::ReadWritePart *m_part;
};

#endif // KPARTTUT1_H

mainwindow.h文件很简单。注意这里MainWindow类从KParts::MainWindow继承而来。

mainwindow.cpp

#include "mainwindow.h"

#include <kaction.h>
#include <kactioncollection.h>
#include <kconfig.h>
#include <kedittoolbar.h>
#include <kfiledialog.h>
#include <kshortcutsdialog.h>
#include <klibloader.h>
#include <kmessagebox.h>
#include <kstandardaction.h>
#include <kstatusbar.h>
#include <kurl.h>

#include <QApplication>

MainWindow::MainWindow()
    : KParts::MainWindow( )
{

    // 设置定制动作(actions)
    setupActions();

    //加载定制,本地Part的例行调用。
    KLibFactory *factory = KLibLoader::self()->factory("katepart");
    if (factory)
    {
        // Part已被加载,我们把它转换到我们要的,并通过它来进一步操作
        m_part = static_cast<KParts::ReadWritePart *>
                 (factory->create(this, "KatePart" ));

        if (m_part)
        {
            // 告诉KParts::MainWindow,这是个主widget
            setCentralWidget(m_part->widget());

            setupGUI(ToolBar | Keys | StatusBar | Save);

            //将part的GUI与shell继承
            createGUI(m_part);
        }
    }
    else
    {
        // 如果找不到Part, 我们就退出,因为Shell也干不了什么了
        KMessageBox::error(this, "Could not find our Part!");
        qApp->quit();
        // 从这里退出,qApp->quit()只是说下次进入循环的时候退出...
        return;
    }
}

MainWindow::~MainWindow()
{}

void MainWindow::load(const KUrl& url)
{
    m_part->openUrl( url );
}

void MainWindow::setupActions()
{
    KStandardAction::open(this, SLOT(fileOpen()), 
        actionCollection());
    KStandardAction::quit(qApp, SLOT(closeAllWindows()),
        actionCollection());
}

void MainWindow::load()
{
    load(KFileDialog::getOpenUrl());
}

mainwindow.cpp文件有MainWindow的实现。它的构造函数包含所有加载KPart的代码。

首先设置主窗口所使用的actions(打开,退出),然后设置图形界面元素(如工具栏,菜单项,键盘快捷键)。下一步就是加载KPart的标准代码了。createGUI方法负责把KPart的工具栏,菜单和程序其他部分集成起来。

kparttut1ui.rc

<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="kparttut1" version="1">
<MenuBar>
  <Menu noMerge="1" name="file"><text>&amp;File</text>
    <Action name="file_open"/>
    <Separator/>
    <Merge/>
    <Separator/>
    <Action name="file_quit"/>
  </Menu>

  <Merge />
</MenuBar>
<ToolBar noMerge="1" name="mainToolBar"><text>Main Toolbar</text>
  <Action name="file_open"/>
  <Merge/>
</ToolBar>
</kpartgui>

kparttut1ui.rc文件用来定义Part中的动作(Action)如何于主窗口的动作(Action)合并起来。这些<Merge /> element in the file menu for example indicates that any part containing actions in a file menu should list its parts在file_open action之后以及file_quit action之前.

CMakeLists.txt

project(kparttut1)

FIND_PACKAGE(KDE4 REQUIRED)
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )

set(kparttut1_SRCS
   main.cpp
   mainwindow.cpp
 )

kde4_add_executable(kparttut1 ${kparttut1_SRCS})

target_link_libraries(kparttut1 ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS})

########### install files ###############
install(TARGETS kparttut1 DESTINATION ${BIN_INSTALL_DIR} )
install( FILES kparttut1ui.rc 
    DESTINATION  ${DATA_INSTALL_DIR}/kparttut1 )

这里,CMakeLists.txt文件很简单。

运行

编译后,用下列方式连接,安装:

cmake . && make -j4 && make install

可以这样执行:

kparttut1 filename

文件名filename就是要加在的文本文件。源代码文件也可以。

文件加载后,你就有了一个全功能的Kate编辑器运行在你的程序窗口里。所有Kate编辑器的功能在工具栏和菜单里都有。

You will notice that the 'Open' action you defined in the MainWindow class has also appeared in the toolbar and in the menu along with the 'Quit' action.

下一个教程会讲述如何在应用程序里使用,重用你创建的KPart。