// --------------------------------------------------------------------------------
void QmvCanvasGrid::drawPixmap( const QPoint & origin, const QPixmap & pixmap, const QRect & bound )
{
    QPainter pt;
    pt.begin(&work_pixmap);
    pt.drawPixmap ( origin, pixmap, bound );
    pt.end();
    
    updateGridImage( work_pixmap );
}
QmvCanvasGrid::QmvCanvasGrid( QCanvas * c, QSize sz, QColor tp )
        : QCanvasRectangle( c ),
          transparent(tp), opaque_factor(0xff), image_depth(8)
{

    setSize( sz.width(), sz.height() );
    work_grid.create( size(), image_depth );
    work_pixmap.resize( size() );
    work_pixmap.fill( transparent );
    updateGridImage( work_pixmap );
    update_mode = true;
}
// --------------------------------------------------------------------------------
void QmvCanvasGrid::updateGridImage( bool resetmode )
{
        // remember the current update mode and then set updates
    bool hold_current = update_mode;
    update_mode = true;
    updateGridImage( work_pixmap );

        // reinstate the update mode, or ??
    if ( resetmode )
        update_mode = true;
    else
        update_mode = hold_current;
}
// --------------------------------------------------------------------------------
void QmvCanvasGrid::drawText( const QRect & rect,
                              const QPen & pen, const QFont font, QString text )
{
    QPainter pt;
    pt.begin(&work_pixmap);
    pt.setPen( pen );
    pt.setFont( font );
    
    pt.drawText( rect,
                 WordBreak|AlignCenter,
                 text );
    pt.end();
    
    updateGridImage( work_pixmap );
}
// --------------------------------------------------------------------------------
void QmvCanvasGrid::drawEllipses( const QPointArray & pa,
                                  int w, int h, const QPen & pen )
{

    QPainter pt;
    pt.begin(&work_pixmap);
    pt.setPen( pen );
    
        // we want the centre at pa.point
    for ( int i = 0; i < pa.count(); i++ )
        pt.drawEllipse( pa.point(i).x() - w/2,
                        pa.point(i).y() - h/2, w, h );
    pt.end();
    
    updateGridImage( work_pixmap );
        
}
void MRFRegistration2DParametersWidget::setUpGridControls()
{
	int fixedImageIndex = this->fixedImageSelector->currentIndex() - 1;
	this->imageSize    = this->imageHolder[fixedImageIndex]->GetDimensions();
	this->imageOrigin  = this->imageHolder[fixedImageIndex]->GetGeometry(0)->GetOrigin();
	this->imageSpacing = this->imageHolder[fixedImageIndex]->GetGeometry(0)->GetSpacing();


	///////////////////////////////////////////////////
	// set the initial default parameters of the grid
	// so that it covers the whole image
	///////////////////////////////////////////////////
	this->xOriginValue->setValue(0);
	this->xOriginValue->setMinimum(0);
	this->xOriginValue->setMaximum(this->imageSize[0]);

	this->yOriginValue->setValue(0);
	this->yOriginValue->setMinimum(0);
	this->yOriginValue->setMaximum(this->imageSize[1]);

	this->xSizeValue->setMaximum(this->imageSize[0]);
	this->xSizeValue->setValue(this->imageSize[0]);
	this->xSizeValue->setMinimum(0);
	this->xSizeValue->setSuffix(tr(" px"));

	this->ySizeValue->setMaximum(this->imageSize[1]);
	this->ySizeValue->setValue(this->imageSize[1]);
	this->ySizeValue->setMinimum(0);
	this->ySizeValue->setSuffix(tr(" px"));

	this->spacingValue->setValue(20.0);
	this->spacingValue->setMinimum(0.1);
	this->spacingValue->setSuffix(tr(" mm"));

	if(this->gridImage.IsNull())
	{
		this->gridImage = mitk::Surface::New();
	}

	updateGridImage();
}
// --------------------------------------------------------------------------------
void QmvCanvasGrid::drawText( const QPointArray & pa,
                              const QPen & pen, const QFont font, QString text, int w, int h )
{

    QPainter pt;
    pt.begin(&work_pixmap);
    
    pt.setPen( pen );
    pt.setFont( font );
    
        // we want the centre at pa.point
    int x, y;
    for ( int i = 0; i < pa.count(); i++ )
    {
        x = pa.point(i).x() - w/2;
        y = pa.point(i).y() - h/2;
        pt.drawText( x, y, w, h,
                     AlignHCenter | AlignVCenter,
                     text, text.length() );
    }
    pt.end();
    
    updateGridImage( work_pixmap );
}
bool MRFRegistration2DParametersWidget::initialise(mitk::DataStorage::Pointer storage)
{
	// Populate the combo boxes with the data
	//
	//
	//
	mitk::DataStorage::SetOfObjects::ConstPointer images = storage->GetAll();

	for(unsigned int i = 0; i < images->size(); i++)
	{
		// check that the item is an image
		mitk::Image::Pointer image = mitk::Image::New();
		image = dynamic_cast<mitk::Image*>(images->GetElement(i)->GetData());


		if(image)
		{
			// now break out the slices and times steps
			int timeSteps = image->GetTimeSteps();
			for(int t = 0; t < timeSteps; t++)
			{
				// now break out the z slices
				int zdims = image->GetDimension(2);
				for(int z = 0; z < zdims; z++)
				{
					// we now need to split them up so as to access the
					// 2d slices
					//
					//
					//
					mitk::ImageSliceSelector::Pointer timeImageSelector = mitk::ImageSliceSelector::New();
					timeImageSelector->SetInput(image);
					timeImageSelector->SetTimeNr(t);
					timeImageSelector->SetSliceNr(z);

					try
					{
						timeImageSelector->Update();
					}
					catch(mitk::Exception &e)
					{
						std::cout << "Error in extracting a slice" << std::endl;
						std::cout << e << std::endl;
						break;
					}

					mitk::Image::Pointer imageSlice = timeImageSelector->GetOutput();
					imageSlice->SetGeometry(image->GetGeometry(0));
					imageSlice->Update();
					imageHolder.push_back(imageSlice);

					// add an entry into the combo boxes
					//
					//
					//
					QString entryName = QString::fromStdString(images->GetElement(i)->GetName()) + " z: " + QString::number(z) + " t: " + QString::number(t);
					this->fixedImageSelector->addItem(entryName);
					this->movingImageSelector->addItem(entryName);

				}

			}
		}

	}

	// connect up all the buttons
	connect(this->makeGridButton, SIGNAL(pressed()), this, SLOT(updateGridImage()));
	connect(this->linkDisplacementToGrid, SIGNAL(pressed()), this, SLOT(setMaxDisplacementToGrid()));
	connect(this->resetParamsButton, SIGNAL(pressed()), this, SLOT(resetParams()));
	connect(this->startRegistrationButton, SIGNAL(pressed()), this, SLOT(startRegistration()));

	return true;

}
// --------------------------------------------------------------------------------
void QmvCanvasGrid::clearGrid()
{
    QPixmap pm( size() );
    pm.fill( transparent );
    updateGridImage( pm );
}
// --------------------------------------------------------------------------------
void QmvCanvasGrid::fill( const QRgb fc )
{
    work_pixmap.fill( fc );
    updateGridImage( work_pixmap );
}