// scale and adjust the upper slider group
void TriangleDensityWidget::resetSliders(double norm)
{
	TriangleList triangles = triangleScene->triangles();
	int n = 0;
	foreach (Triangle* t, triangles)
	{
		QSlider* sl = sliders[n];
		QLabel* la = slider_names[n];
		sl->blockSignals(true);
		sl->setSliderPosition((int)( t->xform()->density / norm * 1000. ));
		sl->blockSignals(false);
		sl->setVisible(true);
		la->setVisible(true);
		sl->setToolTip(triangleScene->getInfoLabel(t));
		if (genome_ptr->final_xform_enable
			&& genome_ptr->final_xform_index == n)
		{
			sl->setEnabled(false);
			la->setEnabled(false);
		}
		else
		{
			sl->setEnabled(true);
			la->setEnabled(true);
		}
		n++;
	}
void QtSliderFactory::slotSingleStepChanged(QtProperty *property, int step)
{
    if (!m_createdEditors.contains(property))
        return;
    QListIterator<QSlider *> itEditor(m_createdEditors[property]);
    while (itEditor.hasNext()) {
        QSlider *editor = itEditor.next();
        editor->blockSignals(true);
        editor->setSingleStep(step);
        editor->blockSignals(false);
    }
}
void QtSliderFactory::slotRangeChanged(QtProperty *property, int min, int max)
{
    if (!m_createdEditors.contains(property))
        return;

    QtIntPropertyManager *manager = this->propertyManager(property);
    if (!manager)
        return;

    QListIterator<QSlider *> itEditor(m_createdEditors[property]);
    while (itEditor.hasNext()) {
        QSlider *editor = itEditor.next();
        editor->blockSignals(true);
        editor->setRange(min, max);
        editor->setValue(manager->value(property));
        editor->blockSignals(false);
    }
}
QWidget* CanvasContainer::scaleSlider()
{
    if (!scaleSliderWidget)
    {
        scaleSliderWidget = new QWidget(this);
        QHBoxLayout *layout = new QHBoxLayout(scaleSliderWidget);
        QSlider *slider = new QSlider(Qt::Horizontal, scaleSliderWidget);
        QLabel *label = new QLabel("100%", scaleSliderWidget);
        label->setFixedWidth(label->fontMetrics().width("100.%"));
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); //not working?

        scaleSliderWidget->setLayout(layout);
        layout->addWidget(slider);
        layout->addWidget(label);
        layout->setContentsMargins(0, 0, 0, 0);

        qreal internalFactor = -100 * qLn(MIN_SCALE_FACTOR) / MIN_SCALE_FACTOR / qLn(2);
        slider->setRange(-qCeil(MIN_SCALE_FACTOR * internalFactor),
                         qCeil(MAX_SCALE_FACTOR - 1) * 100);
        slider->setPageStep(100);
        slider->setTickPosition(QSlider::TicksBelow);

        auto setScaleOrigin = [=]()
        {
            if (proxy)
            {
                QPointF position = proxy->mapFromScene(mapToScene(viewport()->rect().center()));
                if (proxy->rect().contains(position))
                    proxy->setTransformOriginPoint(position);
            }
        };

        auto calculateScale = [=](int sliderValue)
        {
            if (sliderValue >= 0)
            {
                qreal newScale = 1.0 + 0.01 * sliderValue;
                if (!qFuzzyCompare(newScale, currentScaleFactor()))
                {
                    setScaleOrigin();
                    setScaleFactor(1.0 + 0.01 * sliderValue);
                    label->setText(QString("%1%").arg(100 + sliderValue));
                }
              }
            else
            {
                qreal newScale = qPow(MIN_SCALE_FACTOR, -sliderValue / internalFactor / MIN_SCALE_FACTOR);
                if (!qFuzzyCompare(newScale, currentScaleFactor()))
                {
                    setScaleOrigin();
                    setScaleFactor(newScale);
                    label->setText(QString("%1%").arg(newScale * 100, 0, 'f', 1));
                }
            }
        };
        auto calculateValue = [=](qreal scaleFactor)
        {
            slider->blockSignals(true);
            if (scaleFactor >= 1.0)
            {
                int newValue = 100 * scaleFactor - 100;
                if (newValue != slider->value())
                {
                    slider->setValue(newValue);
                    label->setText(QString("%1%").arg(qFloor(100 * scaleFactor)));
                }
            }
            else
            {
                int newValue = -internalFactor * MIN_SCALE_FACTOR / qLn(MIN_SCALE_FACTOR) * qLn(scaleFactor);
                if (newValue != slider->value())
                {
                    slider->setValue(newValue);
                    label->setText(QString("%1%").arg(100 * scaleFactor, 0, 'f', 1));
                }
            }
            slider->blockSignals(false);
        };

        connect(slider, &QSlider::valueChanged, calculateScale);
        connect(this, &CanvasContainer::scaled, calculateValue);
    }
    return scaleSliderWidget;
}