#include "FractoriumPch.h"
#include "Fractorium.h"
///
/// Initialize the xforms xaos UI.
///
void Fractorium::InitXformsXaosUI()
{
connect(ui.XaosToRadio, SIGNAL(toggled(bool)), this, SLOT(OnXaosFromToToggled(bool)), Qt::QueuedConnection);
connect(ui.ClearXaosButton, SIGNAL(clicked(bool)), this, SLOT(OnClearXaosButtonClicked(bool)), Qt::QueuedConnection);
}
///
/// Fill the xaos table with the values from the current xform.
///
template
void FractoriumEmberController::FillXaosWithCurrentXform()
{
Xform* currXform = CurrentXform();
if (!IsFinal(currXform))
{
for (int i = 0; i < m_Ember.XformCount(); i++)
{
DoubleSpinBox* spinBox = dynamic_cast(m_Fractorium->ui.XaosTable->cellWidget(i, 1));
//Fill in values column.
if (m_Fractorium->ui.XaosToRadio->isChecked())//"To": Single xform, advance index.
spinBox->SetValueStealth(currXform->Xaos(i));
else//"From": Advance xforms, single index.
if (currXform = m_Ember.GetXform(i))
spinBox->SetValueStealth(currXform->Xaos(m_Fractorium->ui.CurrentXformCombo->currentIndex()));
//Fill in name column.
Xform* xform = m_Ember.GetXform(i);
QTableWidgetItem* xformNameItem = m_Fractorium->ui.XaosTable->item(i, 0);
if (xform && xformNameItem)
xformNameItem->setText(MakeXaosNameString(i));
}
}
m_Fractorium->ui.XaosTable->setEnabled(!IsFinal(currXform));//Disable if final, else enable.
}
///
/// Create and return a xaos name string.
///
/// The index of the xform whose xaos will be used
/// The xaos name string
template
QString FractoriumEmberController::MakeXaosNameString(unsigned int i)
{
Xform* xform = m_Ember.GetXform(i);
QString name;
if (xform)
{
int i = m_Ember.GetXformIndex(xform) + 1;//GUI is 1 indexed to avoid confusing the user.
int curr = m_Fractorium->ui.CurrentXformCombo->currentIndex() + 1;
if (i != -1)
{
if (m_Fractorium->ui.XaosToRadio->isChecked())
name = QString("From ") + QString::number(curr) + QString(" To ") + QString::number(i);
else
name = QString("From ") + QString::number(i) + QString(" To ") + QString::number(curr);
//if (xform->m_Name != "")
// name = name + " (" + QString::fromStdString(xform->m_Name) + ")";
}
}
return name;
}
///
/// Set the xaos value.
/// Called when any xaos spinner is changed.
/// Different action taken based on the state of to/from radio button.
/// Resets the rendering process.
///
/// The DoubleSpinBox that triggered this event
template
void FractoriumEmberController::XaosChanged(DoubleSpinBox* sender)
{
UpdateCurrentXform([&] (Xform* xform)
{
QTableWidget* xaosTable = m_Fractorium->ui.XaosTable;
if (!IsFinal(xform))//This should never get called for the final xform because the table will be disabled, but check just to be safe.
{
for (int i = 0; i < xaosTable->rowCount(); i++)//Find the spin box that triggered the event.
{
DoubleSpinBox* spinBox = dynamic_cast(xaosTable->cellWidget(i, 1));
if (spinBox == sender)
{
if (m_Fractorium->ui.XaosToRadio->isChecked())//"To": Single xform, advance index.
{
xform->SetXaos(i, spinBox->value());
}
else//"From": Advance xforms, single index.
{
if (xform = m_Ember.GetXform(i))//Single = is intentional.
xform->SetXaos(m_Fractorium->ui.CurrentXformCombo->currentIndex(), spinBox->value());
}
break;
}
}
}
});
}
void Fractorium::OnXaosChanged(double d)
{
if (DoubleSpinBox* senderSpinBox = dynamic_cast(this->sender()))
m_Controller->XaosChanged(senderSpinBox);
}
///
/// Update xaos display to use either "to" or "from" logic.
/// Called when xaos to/from radio buttons checked.
///
/// Ignored
void Fractorium::OnXaosFromToToggled(bool checked)
{
m_Controller->FillXaosWithCurrentXform();
}
///
/// Clear xaos table, recreate all spinners based on the xaos used by the current xform in the current ember.
/// Called every time the current xform changes.
///
void Fractorium::FillXaosTable()
{
int spinHeight = 20;
QWidget* w;
ui.XaosTable->setRowCount(m_Controller->XformCount());//This will grow or shrink the number of rows and call the destructor for previous DoubleSpinBoxes.
for (int i = 0; i < m_Controller->XformCount(); i++)
{
DoubleSpinBox* spinBox = new DoubleSpinBox(ui.XaosTable, spinHeight, 0.1);
QTableWidgetItem* xformNameItem = new QTableWidgetItem(m_Controller->MakeXaosNameString(i));
spinBox->DoubleClick(true);
spinBox->DoubleClickZero(1);
spinBox->DoubleClickNonZero(0);
ui.XaosTable->setItem(i, 0, xformNameItem);
ui.XaosTable->setCellWidget(i, 1, spinBox);
connect(spinBox, SIGNAL(valueChanged(double)), this, SLOT(OnXaosChanged(double)), Qt::QueuedConnection);
if (i > 0)
w = SetTabOrder(this, w, spinBox);
else
w = spinBox;
}
w = SetTabOrder(this, w, ui.XaosToRadio);
w = SetTabOrder(this, w, ui.XaosFromRadio);
w = SetTabOrder(this, w, ui.ClearXaosButton);
}
///
/// Clear all xaos from the current ember.
/// Called when xaos to/from radio buttons checked.
///
/// Ignored
template
void FractoriumEmberController::ClearXaos()
{
UpdateCurrentXform([&] (Xform* xform)
{
m_Ember.ClearXaos();
});
//Can't just call FillXaosWithCurrentXform() because the current xform might the final.
for (int i = 0; i < m_Ember.XformCount(); i++)
if (DoubleSpinBox* spinBox = dynamic_cast(m_Fractorium->ui.XaosTable->cellWidget(i, 1)))
spinBox->SetValueStealth(1.0);
}
void Fractorium::OnClearXaosButtonClicked(bool checked) { m_Controller->ClearXaos(); }