İçindekiler Tablosu

Önceki konu

Dosya Açma ve Kaydetme

Sonraki konu

Mevcut KDE Oyunları

Bu Sayfa

Komut Satırı Argümanları

Giriş

Bir önceki örneğimizde dosya açma ve kaydetme özelliklerine sahip bir metin düzenleyicisi oluşturmuştuk. Şimdi örneğimizi her gün kullandığımız masaüstü uygulamalarına biraz daha benzetecek ve komut satırından veya Dolphin’in Birlikte menüsünden dosya açabilir hale getireceğiz.

../_images/arguman01.png

Kod

mainwindow.h dosyasında yeni olan tek ifade bir QString alan openFile fonksiyonu:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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);

  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 dosyasında da yeniden düzenlemelerden başka bir bir değişiklik yok. void openFile() fonksiyonundaki KFileDialog::getOpenFileName() çağrısı hariç her şey void openFile(const QString &inputFileName) fonksiyonuna taşınıyor. Böylece istediğimiz zaman openFile() ile bir iletişim penceresi gösterebiliyoruz, dosyanın ismini halihazırda bildiğimiz durumlarda ise openFile(QString) ile doğrudan dosyayı açabiliyoruz.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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-close"));
  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()
{
  openFile(KFileDialog::getOpenFileName());
}

void MainWindow::openFile(const QString &inputFileName)
{
  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());
  }
}

İşte KCmdLineArgs ile ilgili işlerin gerçekleştiği yer main.cpp dosyası:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include <KUrl>

#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) 2013 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->openFile(args->url(0).url());
  }

  return app.exec();
}

KCmdLineArgs, uygulamalar için kolayca erişilebilen komut satırı argümanları sunan bir komut satırı argüman yönetim sınıfıdır. Sunulan argümanlar, Qt’ye özel, KDE’ye özel ve uygulamanın kendisine ait argümanları içerir. Sınıf main() içinde statik init() metodu ile kullanılır.

tutorial5ui.rc dosyası ismi hariç son iki örneğimizle tamamen aynı:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?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>

İnşa

Örneğimizin derlenip çalıştırılabilmesi için CMakeLists.txt dosyasını da yazıyoruz:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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)

Derleme ve Çalıştırma

Şimdi örneğimizi çalıştırıp komut satırı argümanıyla bir dosya açmayı deneyelim:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
echo "Merhaba Dünya" > $HOME/hede.txt
$HOME/bin/tutorial5 $HOME/hede.txt