#include "FractoriumPch.h" #include "Fractorium.h" /// /// Initialize the xforms UI. /// void Fractorium::InitXformsUI() { int spinHeight = 20, row = 0; connect(ui.AddXformButton, SIGNAL(clicked(bool)), this, SLOT(OnAddXformButtonClicked(bool)), Qt::QueuedConnection); connect(ui.DuplicateXformButton, SIGNAL(clicked(bool)), this, SLOT(OnDuplicateXformButtonClicked(bool)), Qt::QueuedConnection); connect(ui.ClearXformButton, SIGNAL(clicked(bool)), this, SLOT(OnClearXformButtonClicked(bool)), Qt::QueuedConnection); connect(ui.DeleteXformButton, SIGNAL(clicked(bool)), this, SLOT(OnDeleteXformButtonClicked(bool)), Qt::QueuedConnection); connect(ui.AddFinalXformButton, SIGNAL(clicked(bool)), this, SLOT(OnAddFinalXformButtonClicked(bool)), Qt::QueuedConnection); connect(ui.CurrentXformCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(OnCurrentXformComboChanged(int)), Qt::QueuedConnection); SetFixedTableHeader(ui.XformWeightNameTable->horizontalHeader()); //Use SetupSpinner() just to create the spinner, but use col of -1 to prevent it from being added to the table. SetupSpinner(ui.XformWeightNameTable, this, row, -1, m_XformWeightSpin, spinHeight, 0, 1000, 0.05, SIGNAL(valueChanged(double)), SLOT(OnXformWeightChanged(double)), false, 0, 1, 0); m_XformWeightSpin->setDecimals(3); m_XformWeightSpin->SmallStep(0.001); m_XformWeightSpinnerButtonWidget = new SpinnerButtonWidget(m_XformWeightSpin, "=", 20, 19, ui.XformWeightNameTable); m_XformWeightSpinnerButtonWidget->m_Button->setToolTip("Equalize weights"); m_XformWeightSpinnerButtonWidget->m_Button->setStyleSheet("text-align: center center"); connect(m_XformWeightSpinnerButtonWidget->m_Button, SIGNAL(clicked(bool)), this, SLOT(OnEqualWeightButtonClicked(bool)), Qt::QueuedConnection); ui.XformWeightNameTable->setCellWidget(0, 0, m_XformWeightSpinnerButtonWidget); ui.XformWeightNameTable->setItem(0, 1, new QTableWidgetItem()); connect(ui.XformWeightNameTable, SIGNAL(cellChanged(int, int)), this, SLOT(OnXformNameChanged(int, int)), Qt::QueuedConnection); ui.CurrentXformCombo->setProperty("soloxform", -1); } /// /// Get the current xform. /// /// The current xform as specified by the current xform combo box index. NULL if out of range (should never happen). template Xform* FractoriumEmberController::CurrentXform() { return m_Ember.GetTotalXform(m_Fractorium->ui.CurrentXformCombo->currentIndex()); } /// /// Set the current xform to the index passed in. /// /// The index to set the current xform to void Fractorium::CurrentXform(unsigned int i) { if (i < ui.CurrentXformCombo->count()) ui.CurrentXformCombo->setCurrentIndex(i); } /// /// Set the current xform and populate all GUI widgets. /// Called when the current xform combo box index changes. /// /// The selected combo box index template void FractoriumEmberController::CurrentXformComboChanged(int index) { if (Xform* xform = m_Ember.GetTotalXform(index)) { FillWithXform(xform); m_GLController->SetSelectedXform(xform); int solo = m_Fractorium->ui.CurrentXformCombo->property("soloxform").toInt(); m_Fractorium->ui.SoloXformCheckBox->blockSignals(true); m_Fractorium->ui.SoloXformCheckBox->setChecked(solo == index); m_Fractorium->ui.SoloXformCheckBox->blockSignals(false); bool enable = !IsFinal(CurrentXform()); m_Fractorium->ui.DuplicateXformButton->setEnabled(enable); m_Fractorium->m_XformWeightSpin->setEnabled(enable); m_Fractorium->ui.SoloXformCheckBox->setEnabled(enable); m_Fractorium->ui.AddFinalXformButton->setEnabled(enable); } } void Fractorium::OnCurrentXformComboChanged(int index) { m_Controller->CurrentXformComboChanged(index); } /// /// Add an empty xform in the current ember and set it as the current xform. /// Called when the add xform button is clicked. /// Resets the rendering process. /// /// Ignored template void FractoriumEmberController::AddXform() { UpdateCurrentXform([&] (Xform* xform) { Xform newXform; QComboBox* combo = m_Fractorium->ui.CurrentXformCombo; newXform.m_Weight = 0.25; newXform.m_ColorX = m_Rand.Frand01(); m_Ember.AddXform(newXform); m_Fractorium->FillXforms(); combo->setCurrentIndex(combo->count() - (m_Fractorium->HaveFinal() ? 2 : 1));//Set index to the last item before final. }); } void Fractorium::OnAddXformButtonClicked(bool checked) { m_Controller->AddXform(); } /// /// Duplicate the current xform in the current ember, and set it as the current xform. /// Called when the duplicate xform button is clicked. /// Resets the rendering process. /// /// Ignored template void FractoriumEmberController::DuplicateXform() { UpdateCurrentXform([&] (Xform* xform) { QComboBox* combo = m_Fractorium->ui.CurrentXformCombo; if (xform && !IsFinal(xform)) { m_Ember.AddXform(*xform); m_Fractorium->FillXforms(); combo->setCurrentIndex(combo->count() - (m_Fractorium->HaveFinal() ? 2 : 1));//Set index to the last item before final. } }); } void Fractorium::OnDuplicateXformButtonClicked(bool checked) { m_Controller->DuplicateXform(); } /// /// Clear all variations from the current xform, affine, palette and xaos are left untouched. /// Called when the clear xform button is clicked. /// Resets the rendering process. /// /// Ignored template void FractoriumEmberController::ClearCurrentXform() { UpdateCurrentXform([&] (Xform* xform) { xform->ClearAndDeleteVariations(); //Note xaos is left alone. FillVariationTreeWithXform(xform); }); } void Fractorium::OnClearXformButtonClicked(bool checked) { m_Controller->ClearCurrentXform(); } /// /// Delete the current xform. /// Will not delete the last remaining non-final xform. /// Called when the delete xform button is clicked. /// Resets the rendering process. /// /// Ignored template void FractoriumEmberController::DeleteCurrentXform() { UpdateCurrentXform([&] (Xform* xform) { bool haveFinal = m_Fractorium->HaveFinal(); QComboBox* combo = m_Fractorium->ui.CurrentXformCombo; int count = combo->count(); int index = combo->currentIndex(); //Do not allow deleting the only remaining non-final xform. if (haveFinal && count <= 2 && index == 0) return; if (!haveFinal && count == 1) return; m_Ember.DeleteTotalXform(index); m_Fractorium->FillXforms(); combo->setCurrentIndex(combo->count() - (haveFinal ? 2 : 1));//Set index to the last item before final. }); } void Fractorium::OnDeleteXformButtonClicked(bool checked) { m_Controller->DeleteCurrentXform(); } /// /// Add a final xform to the ember and set it as the current xform. /// Will only take action if a final xform is not already present. /// Called when the add final xform button is clicked. /// Resets the rendering process. /// /// Ignored template void FractoriumEmberController::AddFinalXform() { QComboBox* combo = m_Fractorium->ui.CurrentXformCombo; //Check to see if a final xform is already present. if (!m_Fractorium->HaveFinal()) { Xform xform; xform.AddVariation(new LinearVariation());//Just a placeholder so other parts of the code don't see it as being empty. m_Ember.SetFinalXform(xform); combo->addItem("Final"); combo->setItemIcon(combo->count() - 1, m_Fractorium->m_FinalXformComboIcon); combo->setCurrentIndex(combo->count() - 1);//Set index to the last item. UpdateRender(); } } void Fractorium::OnAddFinalXformButtonClicked(bool checked) { m_Controller->AddFinalXform(); } /// /// Set the weight of the current xform. /// Called when weight spinner changes. /// Resets the rendering process. /// /// The weight template void FractoriumEmberController::XformWeightChanged(double d) { UpdateCurrentXform([&] (Xform* xform) { xform->m_Weight = d; SetNormalizedWeightText(xform); }); } void Fractorium::OnXformWeightChanged(double d) { m_Controller->XformWeightChanged(d); } /// /// Equalize the weights of all xforms in the ember. /// template void FractoriumEmberController::EqualizeWeights() { UpdateCurrentXform([&] (Xform* xform) { m_Ember.EqualizeWeights(); m_Fractorium->m_XformWeightSpin->setValue(xform->m_Weight);//Will trigger an update, so pass false to updateRender below. }, false); } void Fractorium::OnEqualWeightButtonClicked(bool checked) { m_Controller->EqualizeWeights(); } /// /// Set the name of the current xform. /// Called when the user types in the name cell of the table. /// /// The row of the cell /// The col of the cell template void FractoriumEmberController::XformNameChanged(int row, int col) { UpdateCurrentXform([&] (Xform* xform) { int index = m_Ember.GetXformIndex(xform); xform->m_Name = m_Fractorium->ui.XformWeightNameTable->item(row, col)->text().toStdString(); if (index != -1) { if (QTableWidgetItem* xformNameItem = m_Fractorium->ui.XaosTable->item(index, 0)) xformNameItem->setText(MakeXaosNameString(index)); } }, false); } void Fractorium::OnXformNameChanged(int row, int col) { m_Controller->XformNameChanged(row, col); } /// /// Fill all GUI widgets with values from the passed in xform. /// /// The xform whose values will be used to populate the widgets template void FractoriumEmberController::FillWithXform(Xform* xform) { m_Fractorium->m_XformWeightSpin->SetValueStealth(xform->m_Weight); SetNormalizedWeightText(xform); if (QTableWidgetItem* item = m_Fractorium->ui.XformWeightNameTable->item(0, 1)) { m_Fractorium->ui.XformWeightNameTable->blockSignals(true); item->setText(QString::fromStdString(xform->m_Name)); m_Fractorium->ui.XformWeightNameTable->blockSignals(false); } FillVariationTreeWithXform(xform); FillColorWithXform(xform); FillAffineWithXform(xform, true); FillAffineWithXform(xform, false); FillXaosWithCurrentXform(); } /// /// Set the normalized weight of the current xform as the suffix text of the weight spinner. /// /// The current xform whose normalized weight will be shown template void FractoriumEmberController::SetNormalizedWeightText(Xform* xform) { if (xform) { int index = m_Ember.GetXformIndex(xform); m_Ember.CalcNormalizedWeights(m_NormalizedWeights); if (index != -1 && index < m_NormalizedWeights.size()) m_Fractorium->m_XformWeightSpin->setSuffix(QString(" (") + QLocale::system().toString((double)m_NormalizedWeights[index], 'g', 3) + ")"); } } /// /// Determine whether the specified xform is the final xform in the ember. /// /// The xform to examine /// True if final, else false. template bool FractoriumEmberController::IsFinal(Xform* xform) { return (m_Fractorium->HaveFinal() && (xform == m_Ember.FinalXform())); } /// /// Fill the xforms combo box with the xforms in the current ember. /// Select the first one and fill all widgets with its values. /// void Fractorium::FillXforms() { int i = 0, spinHeight = 20; QComboBox* combo = ui.CurrentXformCombo; combo->blockSignals(true); combo->clear(); for (i = 0; i < m_Controller->XformCount(); i++) { combo->addItem(ToString(i + 1)); combo->setItemIcon(i, m_XformComboIcons[i % XFORM_COLOR_COUNT]); } if (m_Controller->UseFinalXform()) { combo->addItem("Final"); combo->setItemIcon(i, m_FinalXformComboIcon); } combo->blockSignals(false); combo->setCurrentIndex(0); FillXaosTable(); OnSoloXformCheckBoxStateChanged(Qt::Unchecked); OnCurrentXformComboChanged(0);//Make sure the event gets called, because it won't if the zero index is already selected. }