TransferFunctionEditorWidget::TransferFunctionEditorWidget(QWidget *parent) :  
  QSplitter(Qt::Vertical, parent)
{
  m_parent = parent;

  m_splineEditorWidget = new SplineEditorWidget(this);
  m_gradientEditorWidget = new GradientEditorWidget(this);

  addWidget(m_splineEditorWidget);
  addWidget(m_gradientEditorWidget);

  QList<int> ssz;
  ssz << 150 << 75;
  setSizes(ssz);

  setChildrenCollapsible(false);

  QObject::connect(m_splineEditorWidget, SIGNAL(giveHistogram(int)),
		   this, SLOT(changeHistogram(int)));

  QObject::connect(m_gradientEditorWidget, SIGNAL(gradientChanged(QGradientStops)),
		   m_splineEditorWidget, SLOT(setGradientStops(QGradientStops)));

  QObject::connect(m_splineEditorWidget, SIGNAL(selectEvent(QGradientStops)),
		   m_gradientEditorWidget, SLOT(setColorGradient(QGradientStops)));

  QObject::connect(m_splineEditorWidget, SIGNAL(deselectEvent()),
		   m_gradientEditorWidget, SLOT(resetColorGradient()));

  QObject::connect(m_splineEditorWidget, SIGNAL(transferFunctionChanged(QImage)),
		   this, SLOT(transferFunctionChanged(QImage)));
}
void SELECTION_TOOL::toggleSelection( BOARD_ITEM* aItem )
{
    if( aItem->IsSelected() )
    {
        deselect( aItem );

        // Inform other potentially interested tools
        TOOL_EVENT deselectEvent( DeselectedEvent );
        m_toolMgr->ProcessEvent( deselectEvent );
    }
    else
    {
        if( !m_additive )
            clearSelection();

        // Prevent selection of invisible or inactive items
        if( selectable( aItem ) )
        {
            select( aItem );

            // Inform other potentially interested tools
            TOOL_EVENT selectEvent( SelectedEvent );
            m_toolMgr->ProcessEvent( selectEvent );
        }
    }
}
void
SplineEditorWidget::deselectSpineEvent()
{
  emit deselectEvent();
}
SplineEditorWidget::SplineEditorWidget(QWidget *parent) :
  QWidget(parent)
{
  m_parent = parent;

  QPen m_pointPen;
  QBrush m_pointBrush;
  QPen m_connectionPen;
  m_pointPen = QPen(QColor(0, 0, 0, 255), 1);
  m_connectionPen = QPen(QColor(200, 200, 200, 150), 1);
  m_pointBrush = QBrush(QColor(255, 255, 255, 200));

  m_1dHist  = new QRadioButton("1D", this);
  m_2dHist  = new QRadioButton("2D", this);

  m_vol1 = new QRadioButton("V1", this);
  m_vol2 = new QRadioButton("V2", this);
  m_vol3 = new QRadioButton("V3", this);
  m_vol4 = new QRadioButton("V4", this);

  QButtonGroup *butGrp1 = new QButtonGroup(this);
  butGrp1->setExclusive(true);
  butGrp1->addButton(m_1dHist);
  butGrp1->addButton(m_2dHist);
  QButtonGroup *butGrp2 = new QButtonGroup(this);
  butGrp2->setExclusive(true);
  butGrp2->addButton(m_vol1);
  butGrp2->addButton(m_vol2);
  butGrp2->addButton(m_vol3);
  butGrp2->addButton(m_vol4);

  m_2dHist->setChecked(true);
  m_vol1->setChecked(true);

  QHBoxLayout *hbox = new QHBoxLayout();
  hbox->addStretch();
  hbox->addWidget(m_vol4);
  hbox->addWidget(m_vol3);
  hbox->addWidget(m_vol2);
  hbox->addWidget(m_vol1);

  m_gbotValue = new QLineEdit("1.0");
  m_gbotValue->setMaximumSize(30, 25);
  QRegExp rx("(\\-?\\d*\\.?\\d*\\s?)");
  QRegExpValidator *validator = new QRegExpValidator(rx,0);
  m_gbotValue->setValidator(validator);

  m_gtopValue = new QLineEdit("1.0");
  m_gtopValue->setMaximumSize(30, 25);
  m_gtopValue->setValidator(validator);

  QHBoxLayout *hboxt = new QHBoxLayout();
  hboxt->addWidget(m_gbotValue);
  hboxt->addStretch();
  hboxt->addWidget(m_1dHist);
  hboxt->addWidget(m_2dHist);
  hboxt->addStretch();
  hboxt->addWidget(m_gtopValue);

  m_gbotSlider = new QSlider(Qt::Vertical);
  m_gbotSlider->setRange(0, 255);
  m_gbotSlider->setValue(0);

  m_gtopSlider = new QSlider(Qt::Vertical);
  m_gtopSlider->setRange(0, 255);
  m_gtopSlider->setValue(255);

  m_16bitEditor = new RemapHistogramWidget(this);
  m_16bitEditor->setSizePolicy(QSizePolicy::Expanding,
			       QSizePolicy::Expanding);

  m_splineEditor = new SplineEditor(this);
  m_splineEditor->setSizePolicy(QSizePolicy::Expanding,
				QSizePolicy::Expanding);
  QHBoxLayout *hbox1 = new QHBoxLayout;
  hbox1->addWidget(m_gbotSlider);
  hbox1->addWidget(m_splineEditor);
  hbox1->addWidget(m_16bitEditor);
  hbox1->addWidget(m_gtopSlider);

  QVBoxLayout *vbox = new QVBoxLayout();
  vbox->addLayout(hboxt);
  vbox->addLayout(hbox1);
  vbox->addLayout(hbox);

  setLayout(vbox);

  m_vol1->hide();
  m_vol2->hide();
  m_vol3->hide();
  m_vol4->hide();


  m_splineEditor->setShapePen(m_pointPen);
  m_splineEditor->setConnectionPen(m_connectionPen);
  m_splineEditor->setShapeBrush(m_pointBrush);


  connect(m_gtopSlider, SIGNAL(sliderReleased()),
	  this, SLOT(gtopSliderReleased()));
  connect(m_gbotSlider, SIGNAL(sliderReleased()),
	  this, SLOT(gbotSliderReleased()));
  connect(m_gtopValue, SIGNAL(editingFinished()),
	  this, SLOT(gValueChanged()));
  connect(m_gbotValue, SIGNAL(editingFinished()),
	  this, SLOT(gValueChanged()));

  connect(m_1dHist, SIGNAL(toggled(bool)),
	  this, SLOT(hist1DClicked(bool)));

  connect(m_2dHist, SIGNAL(toggled(bool)),
	  this, SLOT(hist2DClicked(bool)));

  connect(m_vol1, SIGNAL(toggled(bool)),
	  this, SLOT(vol1Clicked(bool)));

  connect(m_vol2, SIGNAL(toggled(bool)),
	  this, SLOT(vol2Clicked(bool)));

  connect(m_vol3, SIGNAL(toggled(bool)),
	  this, SLOT(vol3Clicked(bool)));

  connect(m_vol4, SIGNAL(toggled(bool)),
	  this, SLOT(vol4Clicked(bool)));

  connect(m_splineEditor, SIGNAL(refreshDisplay()),
	  this, SLOT(update()));

  connect(m_splineEditor, SIGNAL(splineChanged()),
	  this, SLOT(updateTransferFunction()));

  connect(m_splineEditor, SIGNAL(selectEvent(QGradientStops)),
	  this, SLOT(selectSpineEvent(QGradientStops)));

  connect(m_splineEditor, SIGNAL(deselectEvent()),
	  this, SLOT(deselectSpineEvent()));

  connect(m_splineEditor, SIGNAL(applyUndo(bool)),
	  this, SIGNAL(applyUndo(bool)));

  connect(m_16bitEditor, SIGNAL(newMapping(float, float)),
	  this, SLOT(updateTransferFunction(float, float)));

  setMinimumSize(200, 200);

  m_16bitEditor->hide();
}