void FunctionDialog::setCurveToModify(Graph *g, int curve)
{
    if (!g)
        return;

    graph = g;

    FunctionCurve *c = (FunctionCurve *)graph->curve(curve);
    if (!c)
        return;

    curveID = curve;
    QStringList formulas = c->formulas();

    QMap<QString, double> constants = c->constants();
    if (!constants.isEmpty()) {
        boxConstants->clearContents();
        boxConstants->setRowCount(constants.size());
        boxConstants->show();
        ApplicationWindow *app = (ApplicationWindow *)parent();
        QMapIterator<QString, double> i(constants);
        int row = 0;
        while (i.hasNext()) {
            i.next();
            boxConstants->setItem(row, 0, new QTableWidgetItem(i.key()));

            DoubleSpinBox *sb = new DoubleSpinBox();
            sb->setLocale(app->locale());
            sb->setDecimals(app->fit_output_precision);
            sb->setValue(i.value());
            boxConstants->setCellWidget(row, 1, sb);
            connect(sb, SIGNAL(valueChanged(double)), this, SLOT(acceptFunction()));
            row++;
        }
    }
void ContourLinesEditor::insertLevel()
{
	if (!d_spectrogram)
		return;

	int row = table->currentRow();
	DoubleSpinBox *sb = (DoubleSpinBox*)table->cellWidget(row, 0);
	if (!sb)
		return;

	QwtDoubleInterval range = d_spectrogram->data().range();
	double current_value = sb->value();
	double previous_value = range.minValue ();
	sb = (DoubleSpinBox*)table->cellWidget(row - 1, 0);
	if (sb)
		previous_value = sb->value();

	double val = 0.5*(current_value + previous_value);

	table->blockSignals(true);
	table->insertRow(row);

	sb = new DoubleSpinBox();
	sb->setLocale(d_locale);
	sb->setDecimals(d_precision);
	sb->setValue(val);
	sb->setRange(range.minValue (), range.maxValue ());
	connect(sb, SIGNAL(activated(DoubleSpinBox *)), this, SLOT(spinBoxActivated(DoubleSpinBox *)));
    table->setCellWidget(row, 0, sb);

	QPen pen = d_spectrogram->defaultContourPen();
	if (pen.style() == Qt::NoPen)
		pen = d_spectrogram->contourPen (val);

	int width = 80;
	int height = 20;
	QPixmap pix(width, height);
	pix.fill(Qt::white);
	QPainter paint(&pix);
	paint.setRenderHint(QPainter::Antialiasing);
	paint.setPen(pen);
	paint.drawLine(0, height/2, width, height/2);
	paint.end();

	QLabel *lbl = new QLabel();
	lbl->setPixmap(pix);

	table->setCellWidget(row, 1, lbl);
	table->blockSignals(false);

	enableButtons(table->currentRow());
	d_pen_list.insert(row, pen);
}
void ColorMapEditor::insertLevel()
{
  int row = table->currentRow();
  DoubleSpinBox *sb = (DoubleSpinBox*)table->cellWidget(row, 0);
  if (!sb)
    return;

  double current_value = sb->value();
  double previous_value = min_val;
  sb = (DoubleSpinBox*)table->cellWidget(row - 1, 0);
  if (sb)
    previous_value = sb->value();

  double val = 0.5*(current_value + previous_value);
  QwtDoubleInterval range = QwtDoubleInterval(min_val, max_val);
  double mapped_val = (val - min_val)/range.width();

  QColor c = QColor(color_map.rgb(QwtDoubleInterval(0, 1), mapped_val));

  table->blockSignals(true);
  table->insertRow(row);

  sb = new DoubleSpinBox();
  sb->setLocale(d_locale);
  sb->setDecimals(d_precision);
  sb->setValue(val);
  sb->setRange(min_val, max_val);
  connect(sb, SIGNAL(valueChanged(double)), this, SLOT(updateColorMap()));
  connect(sb, SIGNAL(activated(DoubleSpinBox *)), this, SLOT(spinBoxActivated(DoubleSpinBox *)));
  table->setCellWidget(row, 0, sb);

  QTableWidgetItem *it = new QTableWidgetItem(c.name());
// Avoid compiler warning
//#ifdef Q_CC_MSVC
  it->setFlags(it->flags() & (~Qt::ItemIsEditable));
//#else
//  it->setFlags(!Qt::ItemIsEditable);
//#endif
  it->setBackground(QBrush(c));
  it->setForeground(QBrush(c));
  table->setItem(row, 1, it);
  table->blockSignals(false);

  enableButtons(table->currentRow());
  updateColorMap();
}
void ContourLinesEditor::updateContents()
{
	if (!d_spectrogram)
		return;

	QwtValueList levels = d_spectrogram->contourLevels ();

	int rows = (int)levels.size();
	table->setRowCount(rows);
	table->blockSignals(true);

	QwtDoubleInterval range = d_spectrogram->data().range();
	for (int i = 0; i < rows; i++){
		DoubleSpinBox *sb = new DoubleSpinBox();
		sb->setLocale(d_locale);
		sb->setDecimals(d_precision);
		sb->setValue(levels[i]);
		sb->setRange(range.minValue (), range.maxValue ());
		connect(sb, SIGNAL(activated(DoubleSpinBox *)), this, SLOT(spinBoxActivated(DoubleSpinBox *)));
    	table->setCellWidget(i, 0, sb);

		QPen pen = d_spectrogram->defaultContourPen();
		if (pen.style() == Qt::NoPen)
			pen = d_spectrogram->contourPen (levels[i]);

		int width = 80;
		int height = 20;
    	QPixmap pix(width, height);
    	pix.fill(Qt::white);
    	QPainter paint(&pix);
    	paint.setRenderHint(QPainter::Antialiasing);
    	paint.setPen(pen);
    	paint.drawLine(0, height/2, width, height/2);
    	paint.end();

    	QLabel *lbl = new QLabel();
    	lbl->setPixmap(pix);

    	table->setCellWidget(i, 1, lbl);

    	d_pen_list << pen;
	}
	table->blockSignals(false);
}
void ColorMapEditor::setColorMap(const QwtLinearColorMap& map)
{
  scaleColorsBox->setChecked(map.mode() == QwtLinearColorMap::ScaledColors);

  QwtArray <double> colors = map.colorStops();
  int rows = (int)colors.size();
  table->setRowCount(rows);
  table->blockSignals(true);

  QwtDoubleInterval range = QwtDoubleInterval(min_val, max_val);
  for (int i = 0; i < rows; i++){
    DoubleSpinBox *sb = new DoubleSpinBox();
    sb->setLocale(d_locale);
    sb->setDecimals(d_precision);
    sb->setValue(min_val + colors[i] * range.width());

    if (i == 0)
      sb->setRange(min_val, min_val);
    else if (i == rows -1)
      sb->setRange(max_val, max_val);
    else
      sb->setRange(min_val, max_val);

    connect(sb, SIGNAL(valueChanged(double)), this, SLOT(updateColorMap()));
    connect(sb, SIGNAL(activated(DoubleSpinBox *)), this, SLOT(spinBoxActivated(DoubleSpinBox *)));
    table->setCellWidget(i, 0, sb);

    QColor c = QColor(map.rgb(QwtDoubleInterval(0, 1), colors[i]));
    QTableWidgetItem *it = new QTableWidgetItem(c.name());
// Avoid compiler warning
//#ifdef Q_CC_MSVC
    it->setFlags(it->flags() & (~Qt::ItemIsEditable));
//#else
//    it->setFlags(!Qt::ItemIsEditable);
//#endif
    it->setBackground(QBrush(c));
    it->setForeground(QBrush(c));
    table->setItem(i, 1, it);
  }
  table->blockSignals(false);

  color_map = map;
}