// select a gradient from the browse list
void PaletteEditor::selectGradientAction(const QModelIndex& idx)
{
	if (idx.isValid())
	{
		if (hasUGR)
		{
			logFine("PaletteEditor::selectGradientAction : UGR %d", idx.row());
			setPalette(ugrList[idx.row()].pa);
			emit paletteChanged();
			emit undoStateSignal();
		}
		else
		{
			QFileInfo file = m_browseFileList.at(idx.row());
			logFine("PaletteEditor::selectGradientAction : selecting %d", idx.row());
			if ((file.suffix() == "ggr" && loadGIMPGradient(file, p))
			||  (file.suffix() == "cpt" && loadCPTGradient(file, p)))
			{
				m_gradientEnds->resetStops();
				m_gradientStops->setStops(p_stops);
				emit undoStateSignal();
			}
			else
				QMessageBox::warning(this, tr("Invalid path"),
					tr("The directory path %1 couldn't be opened.")
					.arg(file.absoluteFilePath()), QMessageBox::NoButton);
		}
	}
}
PaletteEditor::PaletteEditor ( QWidget* parent )
: QWidget(parent), checkers(15)
{
	setupUi ( this );
	loadPalette(0);

	m_palettesView->setModel(&m_flamPalettes);
	m_browseView->setModel(&m_browsePalettes);
	hasUGR = false;
	QSettings settings;
	settings.beginGroup("paletteeditor");
	m_tabWidget->setCurrentIndex(settings.value("tabwidgetindex", 0).toInt());
	m_lastBrowseDir = settings.value("lastdirectory", QDir::homePath()).toString();
	m_gradientEnds->setArrowDirection(Qt::DownArrow);
	m_gradientEnds->setMenuEnabled(false);

	m_gradientSpreadGroup = new QButtonGroup(this);
	m_gradientSpreadGroup->addButton(m_padSpreadButton, QGradient::PadSpread);
	m_gradientSpreadGroup->addButton(m_repeatSpreadButton, QGradient::RepeatSpread);
	m_gradientSpreadGroup->addButton(m_reflectSpreadButton, QGradient::ReflectSpread);
	m_padSpreadButton->setChecked(true);

	// restore the gradient from settings
	int nstops( settings.beginReadArray("gradient") );
	if (nstops > 1)
	{
		GradientStops stops;
		for (int n = 0 ; n < nstops ; n++)
		{
			settings.setArrayIndex(n);
			double pos( settings.value("pos").toDouble() );
			QColor color( settings.value("color").value<QColor>()) ;
			stops.append(GradientStop( pos, color ));
		}
		settings.endArray();
		p_stops = stops;
		m_gradientStops->setStops(stops);
	}
	stopsChangedAction();

	connect(m_palettesView, SIGNAL(pressed(const QModelIndex&)), this, SLOT(paletteIndexChangedAction(const QModelIndex&)));
	connect(m_browseView, SIGNAL(pressed(const QModelIndex&)), this, SLOT(selectGradientAction(const QModelIndex&)));
	connect(m_rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(paletteRotatedAction(int)));
	connect(m_rotateSlider, SIGNAL(sliderReleased()), this, SIGNAL(undoStateSignal()));
	connect(m_openButton, SIGNAL(clicked(bool)), this, SLOT(openGradientAction(bool)));
	connect(m_gradientStops, SIGNAL(stopsChanged()), this, SLOT(stopsChangedAction()));
	connect(m_gradientStops, SIGNAL(stopsDropped()), this, SIGNAL(undoStateSignal()));
	connect(m_gradientEnds, SIGNAL(stopsChanged()), this, SLOT(stopsChangedAction()));
	connect(m_gradientEnds, SIGNAL(stopsDropped()), this, SIGNAL(undoStateSignal()));
	connect(m_gradientSpreadGroup, SIGNAL(buttonClicked(int)), this, SLOT(stopsChangedAction()));
	connect(m_gradientSpreadGroup, SIGNAL(buttonClicked(int)), this, SIGNAL(undoStateSignal()));
	connect(m_resetGradientButton, SIGNAL(clicked(bool)), this, SLOT(resetGradientAction()));
	connect(m_resetGradientButton, SIGNAL(clicked(bool)), this, SIGNAL(undoStateSignal()));
	connect(m_saveGradientButton, SIGNAL(clicked(bool)), this, SLOT(saveGradientAction()));
	connect(m_browseLineEdit, SIGNAL(returnPressed()), this, SLOT(browsePathChangedAction()));
	connect(m_randomGradientButton, SIGNAL(clicked(bool)), this, SLOT(createRandomGradientAction()));
	connect(m_randomGradientButton, SIGNAL(clicked(bool)), this, SIGNAL(undoStateSignal()));
}
ColorSettingsWidget::ColorSettingsWidget(GenomeVector* g, QWidget* parent)
    : QWidget(parent), genome(g)
{
    setupUi(this);

    m_colorSelector->setGenomeVector(genome);

    m_colorLineEdit->setWheelEventUpdate(true);
    m_colorLineEdit->restoreSettings();
    connect(m_colorLineEdit, SIGNAL(valueUpdated()), this, SLOT(colorChangedAction()));
    connect(m_colorLineEdit, SIGNAL(undoStateSignal()), this, SIGNAL(undoStateSignal()));

    m_speedLineEdit->setWheelEventUpdate(true);
    m_speedLineEdit->restoreSettings();
    connect(m_speedLineEdit, SIGNAL(valueUpdated()), this, SLOT(fieldEditedAction()));
    connect(m_speedLineEdit, SIGNAL(valueUpdated()), m_colorSelector, SLOT(repaintLabel()));
    connect(m_speedLineEdit, SIGNAL(undoStateSignal()), this, SIGNAL(undoStateSignal()));

    m_opacityLineEdit->setWheelEventUpdate(true);
    m_opacityLineEdit->restoreSettings();
    connect(m_opacityLineEdit, SIGNAL(valueUpdated()), this, SLOT(fieldEditedAction()));

    connect(m_backgroundLabel, SIGNAL(colorSelected(QColor)), this, SLOT(changeBackground(QColor)));
    connect(m_backgroundLabel, SIGNAL(undoStateSignal()), this, SIGNAL(undoStateSignal()));
    connect(m_colorSelector, SIGNAL(colorSelected(double)), this, SLOT(colorSelectedAction(double)));
    connect(m_colorSelector, SIGNAL(undoStateSignal()), this, SIGNAL(undoStateSignal()));
}
void IntValueEditor::keyPressEvent(QKeyEvent* e)
{
	switch (e->key())
	{
		case Qt::Key_BracketLeft:
			setSingleStep((int)(singleStep() * 10.0));
			QSettings().setValue(QString("intvalueeditor/%1/singlestep").arg(objectName()), singleStep());
			break;

		case Qt::Key_BracketRight:
			setSingleStep(qMax((int)(default_step * 0.10), 1));
			QSettings().setValue(QString("intvalueeditor/%1/singlestep").arg(objectName()), singleStep());
			break;

		case Qt::Key_Return:
			QSpinBox::keyPressEvent(e);
			emit undoStateSignal();
			break;

		case Qt::Key_Backslash:
			WheelValueEditor::WHEEL_EVENTS_ENABLED
			= ! WheelValueEditor::WHEEL_EVENTS_ENABLED;
			break;

		default:
			QSpinBox::keyPressEvent(e);
	}
}
void PaletteEditor::paletteIndexChangedAction(const QModelIndex& idx)
{
	if (!idx.isValid()) return;
	loadPalette(idx.row());
	setPaletteView();
	emit paletteChanged();
	emit undoStateSignal();
}
void GenomeColorSelector::mouseReleaseEvent (QMouseEvent* e)
{
	if (e->button() == Qt::LeftButton)
	{
		if (last_y != selected_y)
			emit undoStateSignal();
	}
}
void TriangleDensityWidget::reset()
{
	TriangleList triangles = triangleScene->triangles();

	// make more sliders if necessary
	while (sliders.size() < triangles.size())
	{
		GroupedSlider* s
			= new GroupedSlider(Qt::Horizontal, this, sliders.size());
		QLabel* name = new QLabel(QString::number(sliders.size() + 1));
		QHBoxLayout* hl = new QHBoxLayout();
		hl->addWidget(name);
		hl->addWidget(s);
		dynamic_cast<QVBoxLayout*>
			(m_scrollAreaWidgetContents->layout())->insertLayout(-1,hl,0);
		s->setRange(0, 1000);
		s->setVisible(false);
		sliders.append(s);
		slider_names.append(name);
		connect(s, SIGNAL(valueChanged(int)), this, SLOT(groupedSliderChangedSlot(int)));
		connect(s, SIGNAL(sliderPressed()), this, SLOT(groupedSliderPressedSlot()));
		connect(s, SIGNAL(undoStateSignal()), this, SIGNAL(undoStateSignal()));
	}
	// hide non-used sliders
	for (int n = triangles.size() ; n < sliders.size() ; n++)
	{
		sliders.at(n)->setVisible(false);
		slider_names.at(n)->setVisible(false);
	}
	resetSliders(getNorm());

	QStringList items;
	items << tr("None");
	for (int n = 1 ; n <= genome->size() ; n++)
		items << QString::number(n);
	int n = m_crossComboBox->currentIndex();
	m_crossComboBox->blockSignals(true);
	m_crossComboBox->clear();
	m_crossComboBox->addItems(items);
	m_crossComboBox->setCurrentIndex(n);
	m_crossComboBox->blockSignals(false);
}
void DoubleValueEditor::mouseReleaseEvent(QMouseEvent* e)
{
	if (e->button() == Qt::LeftButton)
	{
		if (last_press && start_pos == e->posF())
		{
			QDoubleSpinBox::mousePressEvent(last_press);
			QDoubleSpinBox::mouseReleaseEvent(e);
			last_press = 0;
		}

		if (start_value != value())
			emit undoStateSignal();
	}
}
void DoubleValueEditor::keyPressEvent(QKeyEvent* e)
{
	switch (e->key())
	{
		case Qt::Key_BracketLeft:
			setSingleStep(singleStep() * 10.0);
			QSettings().setValue(QString("doublevalueeditor/%1/singlestep").arg(objectName()), singleStep());
			break;

		case Qt::Key_BracketRight:
			setSingleStep(singleStep() * 0.10);
			QSettings().setValue(QString("doublevalueeditor/%1/singlestep").arg(objectName()), singleStep());
			break;

		case Qt::Key_Return:
			QDoubleSpinBox::keyPressEvent(e);
			emit undoStateSignal();
			break;

		case Qt::Key_Backslash:
			WheelValueEditor::WHEEL_EVENTS_ENABLED
					= ! WheelValueEditor::WHEEL_EVENTS_ENABLED;
			break;

		case Qt::Key_Plus:
			if (e->modifiers() & Qt::ControlModifier)
			{
				setDecimals(decimals() + 1);
				QSettings().setValue(QString("doublevalueeditor/%1/decimals").arg(objectName()), decimals());
			}
			break;

		case Qt::Key_Minus:
			if (e->modifiers() & Qt::ControlModifier)
			{
				setDecimals(qMax(1, decimals() - 1));
				QSettings().setValue(QString("doublevalueeditor/%1/decimals").arg(objectName()), decimals());
			}
			break;

		default:
			QDoubleSpinBox::keyPressEvent(e);
	}
}