Development/Tutorials/KCmdLineArgs (ko)

< Development‎ | Tutorials
Revision as of 16:50, 14 July 2012 by AnneW (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


커맨드 라인 인자 (Under construction User:milliams)
Tutorial Series   Beginner Tutorial
Previous   Tutorial 4 - 로드하기와 저장하기
What's Next   Tutorial 6 - ### (TODO User:milliams)
Further Reading   KCmdLineArgs KCmdLineOptions

Contents

개요

이제 우리는 파일을 열고, 저장하는 텍스트 에디터를 갖는다. 우리는 컴맨드 라인 인자로부터 또는 Dolphin에서 ~로 열기를 사용하여 파일을 여는 것을 가능한 데스크탑 어플리케이션 처럼 동작하도록 만들 것이다.

Now that we have a text editor which can open and save files. We will now make the editor act more like a desktop application by enabling it to open files from command line arguments or even using Open with from within Dolphin.

Introtokdetutorial5.png

코드

main.cpp

  1. #include <KApplication>
  2. #include <KAboutData>
  3. #include <KCmdLineArgs>
  4. #include <KUrl> //new
  5.  
  6. #include "mainwindow.h"
  7.  
  8. int main (int argc, char *argv[])
  9. {
  10.   KAboutData aboutData( "tutorial5", "tutorial5",
  11.       ki18n("Tutorial 5"), "1.0",
  12.       ki18n("A simple text area which can load and save."),
  13.       KAboutData::License_GPL,
  14.       ki18n("Copyright (c) 2007 Developer") );
  15.   KCmdLineArgs::init( argc, argv, &aboutData );
  16.  
  17.   KCmdLineOptions options; //new
  18.   options.add("+[file]", ki18n("Document to open")); //new
  19.   KCmdLineArgs::addCmdLineOptions(options); //new
  20.  
  21.   KApplication app;
  22.  
  23.   MainWindow* window = new MainWindow();
  24.   window->show();
  25.  
  26.   KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); //new
  27.   if(args->count()) //new
  28.   {
  29.     window->openFile(args->url(0).url()); //new
  30.   }
  31.  
  32.   return app.exec();
  33. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <KXmlGuiWindow>
  5. #include <KTextEdit>
  6.  
  7. class MainWindow : public KXmlGuiWindow
  8. {
  9.   Q_OBJECT
  10.  
  11.   public:
  12.     MainWindow(QWidget *parent=0);
  13.     void openFile(const QString &inputFileName); //new
  14.  
  15.   private:
  16.     KTextEdit* textArea;
  17.     void setupActions();
  18.     QString fileName;
  19.  
  20.   private slots:
  21.     void newFile();
  22.     void openFile();
  23.     void saveFile();
  24.     void saveFileAs();
  25.     void saveFileAs(const QString &outputFileName);
  26. };
  27.  
  28. #endif

mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <KApplication>
  4. #include <KAction>
  5. #include <KLocale>
  6. #include <KActionCollection>
  7. #include <KStandardAction>
  8. #include <KFileDialog>
  9. #include <KMessageBox>
  10. #include <KIO/NetAccess>
  11. #include <KSaveFile>
  12. #include <QTextStream>
  13.  
  14. MainWindow::MainWindow(QWidget *parent)
  15.     : KXmlGuiWindow(parent),
  16.       fileName(QString())
  17. {
  18.   textArea = new KTextEdit;
  19.   setCentralWidget(textArea);
  20.  
  21.   setupActions();
  22. }
  23.  
  24. void MainWindow::setupActions()
  25. {
  26.   KAction* clearAction = new KAction(this);
  27.   clearAction->setText(i18n("Clear"));
  28.   clearAction->setIcon(KIcon("document-new"));
  29.   clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
  30.   actionCollection()->addAction("clear", clearAction);
  31.   connect(clearAction, SIGNAL(triggered(bool)),
  32.           textArea, SLOT(clear()));
  33.  
  34.   KStandardAction::quit(kapp, SLOT(quit()),
  35.                         actionCollection());
  36.  
  37.   KStandardAction::open(this, SLOT(openFile()),
  38.                         actionCollection());
  39.  
  40.   KStandardAction::save(this, SLOT(saveFile()),
  41.                         actionCollection());
  42.  
  43.   KStandardAction::saveAs(this, SLOT(saveFileAs()),
  44.                         actionCollection());
  45.  
  46.   KStandardAction::openNew(this, SLOT(newFile()),
  47.                         actionCollection());
  48.  
  49.   setupGUI();
  50. }
  51.  
  52. void MainWindow::newFile()
  53. {
  54.   fileName.clear();
  55.   textArea->clear();
  56. }
  57.  
  58. void MainWindow::saveFileAs(const QString &outputFileName)
  59. {
  60.   KSaveFile file(outputFileName);
  61.   file.open();
  62.  
  63.   QByteArray outputByteArray;
  64.   outputByteArray.append(textArea->toPlainText());
  65.   file.write(outputByteArray);
  66.   file.finalize();
  67.   file.close();
  68.  
  69.   fileName = outputFileName;
  70. }
  71.  
  72. void MainWindow::saveFileAs()
  73. {
  74.   saveFileAs(KFileDialog::getSaveFileName());
  75. }
  76.  
  77. void MainWindow::saveFile()
  78. {
  79.   if(!fileName.isEmpty())
  80.   {
  81.     saveFileAs(fileName);
  82.   }
  83.   else
  84.   {
  85.     saveFileAs();
  86.   }
  87. }
  88.  
  89. void MainWindow::openFile() //changed
  90. {
  91.   openFile(KFileDialog::getOpenFileName());
  92. }
  93.  
  94. void MainWindow::openFile(const QString &inputFileName) //new
  95. {
  96.   QString tmpFile;
  97.   if(KIO::NetAccess::download(inputFileName, tmpFile, 
  98.          this))
  99.   {
  100.     QFile file(tmpFile);
  101.     file.open(QIODevice::ReadOnly);
  102.     textArea->setPlainText(QTextStream(&file).readAll());
  103.     fileName = inputFileName;
  104.  
  105.     KIO::NetAccess::removeTempFile(tmpFile);
  106.   }
  107.   else
  108.   {
  109.     KMessageBox::error(this, 
  110.         KIO::NetAccess::lastErrorString());
  111.   }
  112. }

tutorial4ui.rc

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <gui name="tutorial5"
  3.      version="1"
  4.      xmlns="http://www.kde.org/standards/kxmlgui/1.0"
  5.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.      xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
  7.                          http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >
  8.  
  9.   <MenuBar>
  10.     <Menu name="file" >
  11.       <Action name="clear" />
  12.     </Menu>
  13.   </MenuBar>
  14.  
  15.   <ToolBar name="mainToolBar" >
  16.     <text>Main Toolbar</text>
  17.     <Action name="clear" />
  18.   </ToolBar>
  19.  
  20. </gui>

이것은 tutorialx가 이제는 tutorial5라는 것을 제외하고는 마지막 두 튜트리얼의 tutorialxui.rc와 동일하다.

This is identical to the tutorialxui.rc from the last two tutorials except tutorialx is now tutorial5.

설명

mainwindow.h

여기서 우리는 아무것도 하지 않지만, QString을 인자로 갖는 openFile함수를 추가한다.

Here we have done nothing but add a new openFile function which takes a QString

void openFile(const QString &inputFileName);

mainwindow.cpp

여기에는 단지 재배열 되었을 뿐, 새로 추가된 코드가 없다. 모든 것은 void openFile()에서 KFileDialog::getOpenFileName()를 호출하는 것을 제외하고, void openFile(const QString &inputFileName)으로 이동하였다.

There's no new code here, only rearranging. Everything from void openFile() has been moved into void openFile(const QString &inputFileName) except the call to KFileDialog::getOpenFileName().


만약 다이얼로그를 보여주기를 원하다면, 우리는 openFile() 을 호출할 수 있으며, 파일의 이름을 이미 알고 있다면, openFile(QString)를 호출할 수 있다.

This way, we can call openFile() if we want to display a dialog, or we can call openFile(QString) if we know the name of the file already.

main.cpp

이것이 KCmdLineArgs 마술이 일어나는 곳이다.

This is where all the KCmdLineArgs magic happens.

Make, Install And Run

CMakeLists.txt

  1. project(tutorial5)
  2.  
  3. find_package(KDE4 REQUIRED)
  4. include_directories( ${KDE4_INCLUDES} )
  5.  
  6. set(tutorial5_SRCS 
  7.   main.cpp
  8.   mainwindow.cpp
  9. )
  10.  
  11. kde4_add_executable(tutorial5 ${tutorial5_SRCS})
  12.  
  13. target_link_libraries(tutorial5 ${KDE4_KDEUI_LIBS}
  14.                                 ${KDE4_KIO_LIBS})
  15.  
  16. install(TARGETS tutorial5 DESTINATION ${BIN_INSTALL_DIR})
  17. install( FILES tutorial5ui.rc 
  18.          DESTINATION  ${DATA_INSTALL_DIR}/tutorial5 )

이 파일로 튜트리얼은 빌드 가능하며, 튜트리얼 3와 4와 같은 방식으로 동작한다. 좀 더 많은 정보를 위해서는 튜트리얼 3을 보라.

With this file, the tutorial can built and run in the same way as tutorial 3 and 4. For more information, see tutorial 3.

mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
$HOME/bin/tutorial5

나아가기

Now you can move on to the ### (TODO User:milliams) tutorial.


This page was last modified on 14 July 2012, at 16:50. This page has been accessed 4,142 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal