GDCM 2.2.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Directories
- Examples

CStoreQtProgress.cxx

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library

      Copyright (c) 2006-2011 Mathieu Malaterre
      All rights reserved.
      See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/
    /*
     * This small example show how one can use the virtual function
     * mechanism of the SimpleSubjectWatcher class to redirect progress
     * report to a custom Qt classes
     *
     * http://doc.qt.nokia.com/latest/qprogressdialog.html
     *
     * Usage:
     * CStoreQtProgress dicom.example.com 11112 gdcmData/MR_Spectroscopy_SIEMENS_OF.dcm
     *
     */

    #include "gdcmServiceClassUser.h"
    #include "gdcmSimpleSubjectWatcher.h"
    #include "gdcmProgressEvent.h"
    #include "gdcmDirectory.h"
    #include "gdcmPresentationContextGenerator.h"

    #include <QApplication>
    #include <QProgressDialog>
    #include <QVBoxLayout>

    namespace gdcm {
    /*
     * This class is a little more complicated than what this example demonstrate
     * This watcher is capable of handling nested progress. Since the Progress
     * grows from [0 to 1] on a per file basis and we only have one instance of a
     * watcher per association, we need some calculation to compute the global
     * (total) progress
     * In fact we simply divide the per-file progress by the number of files.
     *
     * This QtWatcher class will then update the progress bar according to the
     * progress.
     */
    class MyQtWatcher : public SimpleSubjectWatcher
    {
      size_t nfiles;
      double progress;
      size_t index;
      double refprogress;
      QWidget* win;
      QProgressDialog* qtprogress;
    public:
      MyQtWatcher(Subject * s, const char *comment = "", QWidget *w = NULL, QProgressDialog* p = NULL, size_t n = 1):
        SimpleSubjectWatcher(s,comment),nfiles(n),progress(0),index(0),refprogress(0),win(w),qtprogress(p){}
      void ShowIteration()
        {
        index++;
        assert( index <= nfiles );
        // update refprogess (we are moving to the next file)
        refprogress = progress;
        }
      void ShowProgress(Subject *, const Event &evt)
        {
        // Retrieve the ProgressEvent:
        const ProgressEvent &pe = dynamic_cast<const ProgressEvent&>(evt);
        // compute global progress:
        progress = refprogress + (1. / nfiles ) * pe.GetProgress();
        // Print Global and local progress to stdout:
        std::cout << "Global Progress: " << progress << " per file progress " << pe.GetProgress() << std::endl;
        //set progress value in the QtProgress bar
        int i = progress * 100 + 0.5; // round to next int
        qtprogress->setValue(i);
        win->show();
        }
      virtual void ShowDataSet(Subject *caller, const Event &evt) {}
    };
    } // end namespace gdcm

    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);

      const char *remote = argv[1];
      int portno = atoi(argv[2]);
      const char *filename = argv[3];

      QVBoxLayout* layout = new QVBoxLayout;
      QWidget*  win = new QWidget;

      QProgressDialog* progress = new QProgressDialog("Sending data...", "Cancel", 0, 100);
      progress->setWindowModality(Qt::WindowModal);

      layout->addWidget(progress,Qt::AlignCenter);
      win->setLayout(layout);

      gdcm::SmartPointer<gdcm::ServiceClassUser> scup = new gdcm::ServiceClassUser;
      gdcm::ServiceClassUser &scu = *scup;
      //gdcm::SimpleSubjectWatcher w( &scu, "TestServiceClassUser" );
      // let's use a more complicated progress reported in this example
      gdcm::MyQtWatcher w( &scu, "QtWatcher", win, progress );

      scu.SetHostname( remote );
      scu.SetPort( portno );
      scu.SetTimeout( 1000 );
      scu.SetCalledAETitle( "GDCM_STORE" );

      if( !scu.InitializeConnection() )
        {
        return 1;
        }

      gdcm::Directory::FilenamesType filenames;
      filenames.push_back( filename );

      // setup the PC(s) based on the filenames:
      gdcm::PresentationContextGenerator generator;
      if( !generator.GenerateFromFilenames(filenames) )
        {
        return 1;
        }

      // Setup PresentationContext(s)
      scu.SetPresentationContexts( generator.GetPresentationContexts() );

      // Start ASSOCIATION
      if( !scu.StartAssociation() )
        {
        return 1;
        }

      // Send C-STORE
      if( !scu.SendStore( filename ) )
        {
        return 1;
        }

      // Stop ASSOCIATION
      if( !scu.StopAssociation() )
        {
        return 1;
        }

      win->show();

      return a.exec();
    }