Development/Tutorials/KCmdLineArgs: Difference between revisions

From KDE TechBase
(→‎tutorial4ui.rc: change the number to 5 ;))
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/KCmdLineArgs}}
 


{{TutorialBrowser|
{{TutorialBrowser|
Line 19: Line 19:


[[image:introtokdetutorial5.png|frame|center]]
[[image:introtokdetutorial5.png|frame|center]]
{{Attention||The source code on this page applies only to KDE Development Platform 4 ("KDE 4"). For the KDE Frameworks 5 ("KF5") version, see [[Development/Tutorials/KCmdLineArgs/KF5]]}}


== The Code ==
== The Code ==


===main.cpp===
===main.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt">
#include <KApplication>
#include <KApplication>
#include <KAboutData>
#include <KAboutData>
Line 57: Line 59:
   return app.exec();
   return app.exec();
}
}
</code>
</syntaxhighlight>


===mainwindow.h===
===mainwindow.h===
<code cppqt n>
<syntaxhighlight lang="cpp-qt">
#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#define MAINWINDOW_H
Line 89: Line 91:
   
   
#endif
#endif
</code>
</syntaxhighlight>


===mainwindow.cpp===
===mainwindow.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt">
#include "mainwindow.h"
#include "mainwindow.h"
   
   
Line 205: Line 207:
   }
   }
}
}
</code>
</syntaxhighlight>


===tutorial5ui.rc===
===tutorial5ui.rc===
<code xml n>
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<gui name="tutorial5"
<gui name="tutorial5" version="1">
    version="1"
  <ToolBar name="mainToolBar" >
    xmlns="http://www.kde.org/standards/kxmlgui/1.0"
    <text>Main Toolbar</text>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <Action name="clear" />
    xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
  </ToolBar>
                        http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >
 
   <MenuBar>
   <MenuBar>
     <Menu name="file" >
     <Menu name="file" >
Line 221: Line 224:
     </Menu>
     </Menu>
   </MenuBar>
   </MenuBar>
  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="clear" />
  </ToolBar>
</gui>
</gui>
</code>
</syntaxhighlight>
This is identical to the <tt>tutorial''x''ui.rc</tt> from the last two tutorials except <tt>tutorial''x''</tt> is now <tt>tutorial5</tt>.
This is identical to the <tt>tutorial''x''ui.rc</tt> from the last two tutorials except <tt>tutorial''x''</tt> is now <tt>tutorial5</tt>.


Line 230: Line 239:


Here we have done nothing but add a new <tt>openFile</tt> function which takes a <tt>QString</tt>
Here we have done nothing but add a new <tt>openFile</tt> function which takes a <tt>QString</tt>
<code cppqt>
<syntaxhighlight lang="cpp-qt">
void openFile(const QString &inputFileName);
void openFile(const QString &inputFileName);
</code>
</syntaxhighlight>


===mainwindow.cpp===
===mainwindow.cpp===
Line 247: Line 256:


===CMakeLists.txt===
===CMakeLists.txt===
<code ini n>
<syntaxhighlight lang="cmake">
project(tutorial5)
project(tutorial5)
   
   
Line 266: Line 275:
install( FILES tutorial5ui.rc  
install( FILES tutorial5ui.rc  
         DESTINATION  ${DATA_INSTALL_DIR}/tutorial5 )
         DESTINATION  ${DATA_INSTALL_DIR}/tutorial5 )
</code>
</syntaxhighlight>


With this file, the tutorial can built and run in the same way as tutorial 3 and 4. For more information, see tutorial 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.
 
<syntaxhighlight lang="bash">
mkdir build && cd build
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
make install
$HOME/bin/tutorial5
$HOME/bin/tutorial5
</syntaxhighlight>


==Moving On==
==Moving On==

Revision as of 22:54, 18 August 2015


Command line arguments (Under construction User:milliams)
Tutorial Series   Beginner Tutorial
Previous   Tutorial 4 - Loading and saving
What's Next   Tutorial 6 - ### (TODO User:milliams)
Further Reading   KCmdLineArgs KCmdLineOptions

Abstract

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.

The source code on this page applies only to KDE Development Platform 4 ("KDE 4"). For the KDE Frameworks 5 ("KF5") version, see Development/Tutorials/KCmdLineArgs/KF5


The Code

main.cpp

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include <KUrl> //new
 
#include "mainwindow.h"
 
int main (int argc, char *argv[])
{
  KAboutData aboutData( "tutorial5", "tutorial5",
      ki18n("Tutorial 5"), "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 );

  KCmdLineOptions options; //new
  options.add("+[file]", ki18n("Document to open")); //new
  KCmdLineArgs::addCmdLineOptions(options); //new

  KApplication app;
 
  MainWindow* window = new MainWindow();
  window->show();

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

  return app.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <KXmlGuiWindow>
#include <KTextEdit>
 
class MainWindow : public KXmlGuiWindow
{
  Q_OBJECT
  
  public:
    MainWindow(QWidget *parent=0);
    void openFile(const QString &inputFileName); //new
  
  private:
    KTextEdit* textArea;
    void setupActions();
    QString fileName;
 
  private slots:
    void newFile();
    void openFile();
    void saveFile();
    void saveFileAs();
    void saveFileAs(const QString &outputFileName);
};
 
#endif

mainwindow.cpp

#include "mainwindow.h"
 
#include <KApplication>
#include <KAction>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KFileDialog>
#include <KMessageBox>
#include <KIO/NetAccess>
#include <KSaveFile>
#include <QTextStream>
 
MainWindow::MainWindow(QWidget *parent)
    : KXmlGuiWindow(parent),
      fileName(QString())
{
  textArea = new KTextEdit;
  setCentralWidget(textArea);
 
  setupActions();
}
 
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)),
          textArea, SLOT(clear()));
 
  KStandardAction::quit(kapp, SLOT(quit()),
                        actionCollection());
 
  KStandardAction::open(this, SLOT(openFile()),
                        actionCollection());
 
  KStandardAction::save(this, SLOT(saveFile()),
                        actionCollection());
 
  KStandardAction::saveAs(this, SLOT(saveFileAs()),
                        actionCollection());
 
  KStandardAction::openNew(this, SLOT(newFile()),
                        actionCollection());
 
  setupGUI();
}
 
void MainWindow::newFile()
{
  fileName.clear();
  textArea->clear();
}
 
void MainWindow::saveFileAs(const QString &outputFileName)
{
  KSaveFile file(outputFileName);
  file.open();
  
  QByteArray outputByteArray;
  outputByteArray.append(textArea->toPlainText());
  file.write(outputByteArray);
  file.finalize();
  file.close();
  
  fileName = outputFileName;
}
 
void MainWindow::saveFileAs()
{
  saveFileAs(KFileDialog::getSaveFileName());
}
 
void MainWindow::saveFile()
{
  if(!fileName.isEmpty())
  {
    saveFileAs(fileName);
  }
  else
  {
    saveFileAs();
  }
}
 
void MainWindow::openFile() //changed
{
  openFile(KFileDialog::getOpenFileName());
}

void MainWindow::openFile(const QString &inputFileName) //new
{
  QString tmpFile;
  if(KIO::NetAccess::download(inputFileName, tmpFile, 
         this))
  {
    QFile file(tmpFile);
    file.open(QIODevice::ReadOnly);
    textArea->setPlainText(QTextStream(&file).readAll());
    fileName = inputFileName;
 
    KIO::NetAccess::removeTempFile(tmpFile);
  }
  else
  {
    KMessageBox::error(this, 
        KIO::NetAccess::lastErrorString());
  }
}

tutorial5ui.rc

<?xml version="1.0" encoding="UTF-8"?>
<gui name="tutorial5"
     version="1"
     xmlns="http://www.kde.org/standards/kxmlgui/1.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
                         http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >

  <MenuBar>
    <Menu name="file" >
      <Action name="clear" />
    </Menu>
  </MenuBar>

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

</gui>

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

Explanation

mainwindow.h

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

void openFile(const QString &inputFileName);

mainwindow.cpp

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().

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

This is where all the KCmdLineArgs magic happens.

Make, Install And Run

CMakeLists.txt

project(tutorial5)
 
find_package(KDE4 REQUIRED)
include_directories( ${KDE4_INCLUDES} )
 
set(tutorial5_SRCS 
  main.cpp
  mainwindow.cpp
)
 
kde4_add_executable(tutorial5 ${tutorial5_SRCS})
 
target_link_libraries(tutorial5 ${KDE4_KDEUI_LIBS}
                                ${KDE4_KIO_LIBS})
 
install(TARGETS tutorial5 DESTINATION ${BIN_INSTALL_DIR})
install( FILES tutorial5ui.rc 
         DESTINATION  ${DATA_INSTALL_DIR}/tutorial5 )

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

Moving On

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