[vtkusers] vtkKWLoadSaveButton problem

Ashish Singh mrasingh at gmail.com
Tue Dec 12 14:02:15 EST 2006


Hi,

I have a vtk application to display a dicom image.I am trying to create a
button on main panel which when clicked opens up the directory tree and we
can choose the dicom image files to be displayed in this application. I am
using vtkkwloadsavebutton for this.I tried creating a callback function. On
running the code, I get the loadsave button but after selecting a file, the
program crashes. I can't figure out what's wrong.
I am attaching my complete code. It would be a great help if anyone in the
list can help me figure out what's going wrong. I have just modified the
MedicalImageViewer example from KWWidgets and am using the same
cmakelists.txt file also.

Thanks,
Ashish
-----------------------------------------------
file 1- vtkKWMyWindow.h---below
--------------------------------------------------
#ifndef __vtkKWMyWindow_h
#define __vtkKWMyWindow_h

#include "vtkKWWindow.h"

class vtkKWRenderWidget;
class vtkImageViewer2;
class vtkKWScale;
class vtkKWWindowLevelPresetSelector;
class vtkKWSimpleAnimationWidget;
class vtkKWLoadSaveButton;
class vtkDICOMImageReader;
class vtkImageData;

class vtkKWMyWindow : public vtkKWWindow
{
public:
  static vtkKWMyWindow* New();
  vtkTypeRevisionMacro(vtkKWMyWindow,vtkKWWindow);
  virtual void mycallback();

protected:
  vtkKWMyWindow();
  ~vtkKWMyWindow();

  // Description:
  // Create the widget.
  virtual void CreateWidget();

  vtkImageViewer2                *ImageViewer;
  vtkKWRenderWidget              *RenderWidget;
  vtkKWLoadSaveButton             *myLoadSaveButton;
  vtkDICOMImageReader             *mydicom;
  vtkImageData                     *img_data;

 // virtual void UpdateSliceRanges();

private:
  vtkKWMyWindow(const vtkKWMyWindow&);   // Not implemented.
  void operator=(const vtkKWMyWindow&);  // Not implemented.
};

#endif
------------------------------------------------------
file 2- vtkKWMyWindow.cxx---below
-----------------------------------------------------
#include "vtkKWMyWindow.h"

#include "vtkCornerAnnotation.h"
#include "vtkImageData.h"
#include "vtkImageViewer2.h"
#include "vtkKWApplication.h"
#include "vtkKWFrame.h"
#include "vtkKWFrameWithLabel.h"
#include "vtkKWMenu.h"
#include " vtkKWMenuButton.h"
#include "vtkKWMenuButtonWithSpinButtons.h"
#include "vtkKWMenuButtonWithSpinButtonsWithLabel.h"
#include "vtkKWNotebook.h"
#include "vtkKWRenderWidget.h "
#include "vtkKWScale.h"
#include "vtkKWSimpleAnimationWidget.h"
#include "vtkKWWindow.h"
#include "vtkKWWindowLevelPresetSelector.h"
#include "vtkObjectFactory.h "
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkXMLImageDataReader.h"

#include "vtkKWLoadSaveButton.h"
#include "vtkKWLoadSaveDialog.h "
#include "vtkDICOMImageReader.h"

#include "vtkKWWidgetsPaths.h"
#include "vtkToolkits.h"

#include <vtksys/SystemTools.hxx>

vtkStandardNewMacro( vtkKWMyWindow );
vtkCxxRevisionMacro(vtkKWMyWindow, "$Revision: 1.2 $");


vtkKWMyWindow::vtkKWMyWindow()
{
  this->RenderWidget = NULL;
  this->ImageViewer = NULL;
  this->mydicom = NULL;
  this->myLoadSaveButton = NULL;
  this->img_data = NULL;
}

vtkKWMyWindow::~vtkKWMyWindow()
{
  if (this->ImageViewer)
    {
    this->ImageViewer->Delete();
    }
  if (this->RenderWidget)
    {
    this->RenderWidget->Delete();
    }
  if(this->mydicom)
      this->mydicom->Delete();
  if(this->myLoadSaveButton)
      this->myLoadSaveButton->Delete();
  if(this->img_data)
      this->img_data->Delete();

}

void vtkKWMyWindow::CreateWidget()
{
  // Check if already created

  if (this->IsCreated())
    {
    vtkErrorMacro("class already created");
    return;
    }

  // Call the superclass to create the whole widget

  this->Superclass::CreateWidget();

  vtkKWApplication *app = this->GetApplication();

  // Add a render widget, attach it to the view frame, and pack

  if (!this->RenderWidget)
    {
    this->RenderWidget = vtkKWRenderWidget::New();
    }
  this->RenderWidget->SetParent(this->GetViewFrame());
  this->RenderWidget->Create();
  this->RenderWidget->ResetCamera();
  this->RenderWidget->CornerAnnotationVisibilityOn();

  app->Script("pack %s -expand y -fill both -anchor c -expand y",
              this->RenderWidget->GetWidgetName());

  //create loadsavebutton
  if(!this->myLoadSaveButton)
  {
      this->myLoadSaveButton = vtkKWLoadSaveButton::New();
  }
  this->myLoadSaveButton->SetParent(this->GetMainPanelFrame());
  this->myLoadSaveButton->Create();
  this->myLoadSaveButton->SetText("Click to Pick a File");
  this->myLoadSaveButton->GetLoadSaveDialog()->SaveDialogOff();
  this->myLoadSaveButton->GetLoadSaveDialog()->SetFileTypes("{ {Dicom
Document} {.dcm} }");

  this->myLoadSaveButton->SetCommand(this, "mycallback");

  app->Script("pack %s -side top -anchor nw -expand n -padx 2 -pady
2",this->myLoadSaveButton->GetWidgetName());
//  this->RenderWidget->ResetCamera();

}

void vtkKWMyWindow::mycallback()
{

this->mydicom->SetFileName(this->myLoadSaveButton->GetLoadSaveDialog()->GetFileName());
//    this->mydicom->Update();

    /*if (!this->ImageViewer)
    {
        this->ImageViewer = vtkImageViewer2::New();
    }*/
//
this->ImageViewer->SetRenderWindow(this->RenderWidget->GetRenderWindow());
//    this->ImageViewer->SetInput(this->mydicom->GetOutput());
//
this->ImageViewer->SetupInteractor(this->RenderWidget->GetRenderWindow()->GetInteractor());
//    this->ImageViewer->Render();

    this->img_data = this->mydicom->GetOutput();
    double *range = this->img_data->GetScalarRange();
    this->ImageViewer->SetColorWindow(range[1] - range[0]);
    this->ImageViewer->SetColorLevel(0.5 * (range[1] + range[0]));
    this->ImageViewer->SetInput(img_data);
    this->ImageViewer->Render();

}
----------------------------------------------------------------------------
file 3-KWMedicalImageViewerExample.cxx---below
---------------------------------------------------------------------------
#include "vtkKWApplication.h"
#include "vtkKWMyWindow.h"

#include <vtksys/SystemTools.hxx>
#include <vtksys/CommandLineArguments.hxx>

extern "C" int Kwmedicalimageviewerexamplelib_Init(Tcl_Interp *interp);

int my_main(int argc, char *argv[])
{
  // Initialize Tcl

  Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc, argv, &cerr);
  if (!interp)
    {
    cerr << "Error: InitializeTcl failed" << endl ;
    return 1;
    }

  // Initialize our Tcl library (i.e. our classes wrapped in Tcl).
  // This *is* required for the C++ methods to be used as callbacks.
  // See comment at the top of this file.

  Kwmedicalimageviewerexamplelib_Init(interp);

  // Process some command-line arguments
  // The --test option here is used to run this example as a non-interactive
  // test for software quality purposes. You can ignore it.

  int option_test = 0;
  vtksys::CommandLineArguments args;
  args.Initialize(argc, argv);
  args.AddArgument(
    "--test", vtksys::CommandLineArguments::NO_ARGUMENT, &option_test, "");
  args.Parse();

  // Create the application
  // If --test was provided, ignore all registry settings, and exit silently
  // Restore the settings that have been saved to the registry, like
  // the geometry of the user interface so far.

  vtkKWApplication *app = vtkKWApplication::New();
  app->SetName("KWMedicalImageViewerExample");
  if (option_test)
    {
    app->SetRegistryLevel(0);
    app->PromptBeforeExitOff();
    }
  app->RestoreApplicationSettingsFromRegistry();

  // Set a help link. Can be a remote link (URL), or a local file

  app->SetHelpDialogStartingPage(" http://www.kwwidgets.org");

  // Add our window
  // Set 'SupportHelp' to automatically add a menu entry for the help link

  vtkKWMyWindow *win = vtkKWMyWindow::New();
  win->SupportHelpOn();
  app->AddWindow(win);
  win->Create();
  //win->SecondaryPanelVisibilityOff();
    std::cout<<"hi"<<"\n";

  // Start the application
  // If --test was provided, do not enter the event loop and run this
example
  // as a non-interactive test for software quality purposes.

  int ret = 0;
  win->Display();
  if (!option_test)
    {
    app->Start(argc, argv);
    ret = app->GetExitStatus();
    }
  win->Close();

  // Deallocate and exit

  win->Delete();
  app->Delete();

  return ret;
}

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int)
{
  int argc;
  char **argv;
  vtksys::SystemTools::ConvertWindowsCommandLineToUnixArguments(
    lpCmdLine, &argc, &argv);
  int ret = my_main(argc, argv);
  for (int i = 0; i < argc; i++) { delete [] argv[i]; }
  delete [] argv;
  return ret;
}
#else
int main(int argc, char *argv[])
{
  return my_main(argc, argv);
}
#endif
-----------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061212/729fb228/attachment.htm>


More information about the vtkusers mailing list