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

From KDE TechBase
(New page: {{Template:I18n/Language Navigation Bar|Development/Tutorials/Using_KParts}} {{TutorialBrowser| series=插件和KParts| name=如何使用KParts| pre=[[Development/Tutorials/KCmdLineArg...)
 
No edit summary
Line 23: Line 23:
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.
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.


The following code creates a KParts::MainWindow with a kpart inside it.
下面代码创建一个有KPart的KParts::MainWindow。


=== main.cpp ===
=== main.cpp ===
Line 64: Line 64:
</code>
</code>


The main.cpp file is the same as that used in [[Development/Tutorials/KCmdLineArgs|Tutorial 5 - Command line arguments]].The only difference is in the details of the KAboutData.
这个main.cpp文件和[[Development/Tutorials/KCmdLineArgs|教程五 -命令行参数]]里用的一样。唯一的不同就是KAboutData的一些具体内容。


=== mainwindow.h ===
=== mainwindow.h ===
Line 88: Line 88:
public:
public:
     /**
     /**
     * Default Constructor
     * 缺省构造寒食。
     */
     */
     MainWindow();
     MainWindow();


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


     /**
     /**
     * Use this method to load whatever file/URL you have
     * 用于加载文件或URL。
     */
     */
     void load(const KUrl& url);
     void load(const KUrl& url);


     /**
     /**
     * Use this method to display an openUrl dialog and
     * 显示打开URL的对话框,加载输入的URL。
    * load the URL that gets entered
     */
     */
     void load();
     void load();
Line 119: Line 118:
</code>
</code>


The mainwindow.h file is very simple. The important thing to notice here is that the MainWindow class inherits from KParts::MainWindow.
mainwindow.h文件很简单。注意这里MainWindow类从KParts::MainWindow继承而来。


=== mainwindow.cpp ===
=== mainwindow.cpp ===
Line 145: Line 144:
{
{


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


     // this routine will find and load our Part. 
     //加载定制,本地Part的例行调用。
     KLibFactory *factory = KLibLoader::self()->factory("katepart");
     KLibFactory *factory = KLibLoader::self()->factory("katepart");
     if (factory)
     if (factory)
     {
     {
         // now that the Part is loaded, we cast it to a Part to get
         // Part已被加载,我们把它转换到我们要的,并通过它来进一步操作
        // our hands on it
         m_part = static_cast<KParts::ReadWritePart *>
         m_part = static_cast<KParts::ReadWritePart *>
                 (factory->create(this, "KatePart" ));
                 (factory->create(this, "KatePart" ));
Line 159: Line 157:
         if (m_part)
         if (m_part)
         {
         {
             // tell the KParts::MainWindow that this is indeed
             // 告诉KParts::MainWindow,这是个主widget
            // the main widget
             setCentralWidget(m_part->widget());
             setCentralWidget(m_part->widget());


             setupGUI(ToolBar | Keys | StatusBar | Save);
             setupGUI(ToolBar | Keys | StatusBar | Save);


             // and integrate the part's GUI with the shell's
             //将part的GUI与shell继承
             createGUI(m_part);
             createGUI(m_part);
         }
         }
Line 205: Line 202:
</code>
</code>


The mainwindow.cpp file contains the implementation of MainWindow. The constructor of this class contains all the code used to load the 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.
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.
Line 232: Line 229:
</code>
</code>


kparttut1ui.rc文件用来定义Part中的动作(Action)如何于主窗口的动作(Action)合并起来。The <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 after the file_open action and before the file_quit action.


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

Revision as of 05:03, 22 February 2009


Development/Tutorials/Using_KParts


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

介绍

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.

This tutorial will show you how to use a KPart in your application, and how to create your own KPart.

使用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.

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

main.cpp

  1. include <KApplication>
  2. include <KAboutData>
  3. include <KCmdLineArgs>
  4. include <KUrl>
  1. 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

  1. ifndef KPARTTUTORIAL1_H
  2. define KPARTTUTORIAL1_H
  1. 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;

};

  1. endif // KPARTTUT1_H

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

mainwindow.cpp

  1. include "mainwindow.h"
  1. include <kaction.h>
  2. include <kactioncollection.h>
  3. include <kconfig.h>
  4. include <kedittoolbar.h>
  5. include <kfiledialog.h>
  6. include <kshortcutsdialog.h>
  7. include <klibloader.h>
  8. include <kmessagebox.h>
  9. include <kstandardaction.h>
  10. include <kstatusbar.h>
  11. include <kurl.h>
  1. 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
   {
       // if we couldn't find our Part, we exit since the Shell by
       // itself can't do anything useful
       KMessageBox::error(this, "Could not find our Part!");
       qApp->quit();
       // we return here, cause qApp->quit() only means "exit the
       // next time we enter the event loop...
       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的代码。

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.

kparttut1ui.rc

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

 <Menu noMerge="1" name="file"><text>&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 after the file_open action and before the 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})

                      1. 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就是要加在的文本文件。源代码文件也可以。

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.

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。