KDE Application Tutorials: Using KImageIO with KScribble

Next Previous Table of Contents

### 12. Using KImageIO with KScribble

When it comes to images, the Qt and KDE libraries offer a wide variety of operations. Besides actual drawing routines, the libraries support a whole set of image formats which they can read and write - all without any cost on our view. The main class for these operations is `QImageIO`, which has a support library by KDE: `KImageIO`. As a preparation before we can make use of these nice methods, we have to add the according library to KScribble: libkimgio. Open "Project"->"Options" in KDevelop and add the line -lkimgio to the additional libraries to link KScribble with.

### 12.1 Preparing the Document

Now we can go on and apply our changes. First, we have to remove the current restriction in the document class to only read and write PNG files. Just replace "PNG" in the following methods with format, as given by the `openDocument()` and `saveDocument()` methods:

In method `KScribbleDoc::openDocument()`:

`if(!buffer.load( filename, format ))`

In method `KScribbleDoc:: saveDocument()`:

`if(!buffer.save( filename, format ))`

Then we have a default behavoir of these methods. Now, for now we don´t have all available file formats yet. This will be our next task, together with adapting the slots in KScribble that deliver us filenames.

### 12.2 Registering File Formats

To use `KImageIO`, we have to initialize the library first. For this, we add a call for `registerFormats()` in our `main()` function:

    main.cpp:

    ............
      KApplication app;
      KImageIO::registerFormats();

      if (app.isRestored())
    ............

Note that this call is after the application is instanciated with KApplication app - without the application instance, our program will not run, as `KImageIO` then doen´t know on which application to register the formats. The include file for this call will be added to kscribble.h, as we´re going to use some of its methods in `KScribbleApp`:

kscribble.h:

#include <kimgio.h>

### 12.3 Opening Images

Now that we can make use of `KImageIO`, we have to apply the first change to the most important method of `KScribbleApp`: `openDocumentFile()`. This method opens us any document until now only on the behalf of the filename. It just leaves out the extension, as the format is not required by the document class by default. But as we have changed that, we just need a format - and have to adapt the call for `KScribbleDoc::openDocument()` in the method `openDocumentFile()`:

    kscribble.cpp:

      void KScribbleApp::openDocumentFile(const char* file)
      {
            ...........

            else
        {
    ->    QString format=KImageIO::type(file);
    ->        if(!doc->openDocument(file,format))
                    KMessageBox::error (this,i18n("Could not open document !"), i18n("Error !"));
                    addRecentFile(file);
            }
        ............
      }

Of course, this works the same as using `QString format=QImageIO::imageFormat(file);`. Here, `KImageIO` delivers us the format of the image and we can call the document to open the file by filename and format. (Another possiblity would be to detect the format in the document class as well).

### 12.4 Setting File Filters with KImageIO

Here, we´re finishing our tutorial with the last section - we will adapt the file dialogs of KScribble to make use of file filters. For these, `KImageIO` provides nice methods to give us the needed strings for all image file formats that are available for opening and saving. The following implementation replaces the default file filter (which is in fact none - you have to change your own applications´ file filter to your mime type accordingly) with the `pattern()` method of `KImageIO`:

      void KScribbleApp::slotFileOpen()
      {
        slotStatusMsg(i18n("Opening file..."));

    ->    QString fileToOpen=KFileDialog::getOpenFileName(QDir::currentDirPath(),
    ->              KImageIO::pattern(KImageIO::Reading), this, i18n("Open File..."));
        if(!fileToOpen.isEmpty())
        {
                    openDocumentFile(fileToOpen);
        }

        slotStatusMsg(i18n("Ready."));
      }

Here, the mode in `pattern()` is set to Reading - which may differ from the patterns that are retrieved when set to writing. Now we have finished opening files by its filename and format completely. What is missing to complete this structure, is setting the patterns as well for saving a file. For that, the `slotFileSaveAs()` is called, which itself invokes the file dialog that retrieves a file name. There, we will set the pattern mode to Writing:

    void KScribbleApp::slotFileSaveAs()
    {
      slotStatusMsg(i18n("Saving file with a new filename..."));

    ->  QString newName=KFileDialog::getSaveFileName(QDir::currentDirPath(),
    ->                               KImageIO::pattern(KImageIO::Writing), this, i18n("Save as..."));
      if(!newName.isEmpty())
      {
        KScribbleView* m = (KScribbleView*)pWorkspace->activeWindow();
        if( m )
        {
          KScribbleDoc* doc =       m->getDocument();
              QString format=QFileInfo(newName).extension();
              format=format.upper();
                      if(!doc->saveDocument(newName,format))
              {
                      KMessageBox::error (this,i18n("Could not save the current document !"), i18n("I/O Error !"));
                                    return;
                            }
          doc->changedViewList();
          setWndTitle(m);
        }
      }
      slotStatusMsg(i18n("Ready."));
    }

Next Previous Table of Contents