Move duplicated code to the base class

All pattern sublcasses contain the same code for handling the
feature list. This code is now moved into the base class that handles
the common ui.

The subclasses now only need to call the setupUI/setupParameterUI function
in their constructors and implement the setupParameterUI function
to create their parameter ui into the specified widget.

The MultiTransform also handles it's common ui and the subclasses
can reuse setupParameterUI without code duplication.
This commit is contained in:
André Althaus
2024-02-17 11:16:34 +01:00
parent 5139182dbe
commit 24c138373c
12 changed files with 249 additions and 653 deletions

View File

@@ -25,7 +25,6 @@
#ifndef _PreComp_
# include <QMessageBox>
# include <QAction>
# include <QTimer>
#endif
@@ -64,103 +63,22 @@ TaskPolarPatternParameters::TaskPolarPatternParameters(ViewProviderTransformed *
: TaskTransformedParameters(TransformedView, parent)
, ui(new Ui_TaskPolarPatternParameters)
{
// we need a separate container widget to add all controls to
proxy = new QWidget(this);
ui->setupUi(proxy);
QMetaObject::connectSlotsByName(this);
this->groupLayout()->addWidget(proxy);
ui->buttonOK->hide();
ui->checkBoxUpdateView->setEnabled(true);
selectionMode = none;
blockUpdate = false; // Hack, sometimes it is NOT false although set to false in Transformed::Transformed()!!
setupUI();
}
TaskPolarPatternParameters::TaskPolarPatternParameters(TaskMultiTransformParameters *parentTask, QLayout *layout)
TaskPolarPatternParameters::TaskPolarPatternParameters(TaskMultiTransformParameters *parentTask, QWidget* parameterWidget)
: TaskTransformedParameters(parentTask), ui(new Ui_TaskPolarPatternParameters)
{
proxy = new QWidget(parentTask);
ui->setupUi(proxy);
connect(ui->buttonOK, &QToolButton::pressed,
parentTask, &TaskMultiTransformParameters::onSubTaskButtonOK);
setupParameterUI(parameterWidget);
}
void TaskPolarPatternParameters::setupParameterUI(QWidget *widget)
{
ui->setupUi(widget);
QMetaObject::connectSlotsByName(this);
layout->addWidget(proxy);
ui->buttonOK->setEnabled(true);
ui->buttonAddFeature->hide();
ui->buttonRemoveFeature->hide();
ui->listWidgetFeatures->hide();
ui->checkBoxUpdateView->hide();
selectionMode = none;
blockUpdate = false; // Hack, sometimes it is NOT false although set to false in Transformed::Transformed()!!
setupUI();
}
void TaskPolarPatternParameters::connectSignals()
{
connect(ui->buttonAddFeature, &QToolButton::toggled,
this, &TaskPolarPatternParameters::onButtonAddFeature);
connect(ui->buttonRemoveFeature, &QToolButton::toggled,
this, &TaskPolarPatternParameters::onButtonRemoveFeature);
// Create context menu
QAction* action = new QAction(tr("Remove"), this);
action->setShortcut(QKeySequence::Delete);
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
// display shortcut behind the context menu entry
action->setShortcutVisibleInContextMenu(true);
#endif
ui->listWidgetFeatures->addAction(action);
connect(action, &QAction::triggered,
this, &TaskPolarPatternParameters::onFeatureDeleted);
ui->listWidgetFeatures->setContextMenuPolicy(Qt::ActionsContextMenu);
connect(ui->listWidgetFeatures->model(), &QAbstractListModel::rowsMoved,
this, &TaskPolarPatternParameters::indexesMoved);
updateViewTimer = new QTimer(this);
updateViewTimer->setSingleShot(true);
updateViewTimer->setInterval(getUpdateViewTimeout());
connect(updateViewTimer, &QTimer::timeout,
this, &TaskPolarPatternParameters::onUpdateViewTimer);
connect(ui->comboAxis, qOverload<int>(&QComboBox::activated),
this, &TaskPolarPatternParameters::onAxisChanged);
connect(ui->comboMode, qOverload<int>(&QComboBox::activated),
this, &TaskPolarPatternParameters::onModeChanged);
connect(ui->checkReverse, &QCheckBox::toggled,
this, &TaskPolarPatternParameters::onCheckReverse);
connect(ui->polarAngle, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskPolarPatternParameters::onAngle);
connect(ui->angleOffset, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskPolarPatternParameters::onOffset);
connect(ui->spinOccurrences, &Gui::UIntSpinBox::unsignedChanged,
this, &TaskPolarPatternParameters::onOccurrences);
connect(ui->checkBoxUpdateView, &QCheckBox::toggled,
this, &TaskPolarPatternParameters::onUpdateView);
}
void TaskPolarPatternParameters::setupUI()
{
// Get the feature data
PartDesign::PolarPattern* pcPolarPattern = static_cast<PartDesign::PolarPattern*>(getObject());
std::vector<App::DocumentObject*> originals = pcPolarPattern->Originals.getValues();
// Fill data into dialog elements
for (auto obj : originals) {
if (obj) {
QListWidgetItem* item = new QListWidgetItem();
item->setText(QString::fromUtf8(obj->Label.getValue()));
item->setData(Qt::UserRole, QString::fromLatin1(obj->getNameInDocument()));
ui->listWidgetFeatures->addItem(item);
}
}
// ---------------------
ui->polarAngle->bind(pcPolarPattern->Angle);
ui->angleOffset->bind(pcPolarPattern->Offset);
@@ -200,7 +118,29 @@ void TaskPolarPatternParameters::setupUI()
adaptVisibilityToMode();
updateUI();
connectSignals();
updateViewTimer = new QTimer(this);
updateViewTimer->setSingleShot(true);
updateViewTimer->setInterval(getUpdateViewTimeout());
connect(updateViewTimer, &QTimer::timeout,
this, &TaskPolarPatternParameters::onUpdateViewTimer);
connect(ui->comboAxis, qOverload<int>(&QComboBox::activated),
this, &TaskPolarPatternParameters::onAxisChanged);
connect(ui->comboMode, qOverload<int>(&QComboBox::activated),
this, &TaskPolarPatternParameters::onModeChanged);
connect(ui->checkReverse, &QCheckBox::toggled,
this, &TaskPolarPatternParameters::onCheckReverse);
connect(ui->polarAngle, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskPolarPatternParameters::onAngle);
connect(ui->angleOffset, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskPolarPatternParameters::onOffset);
connect(ui->spinOccurrences, &Gui::UIntSpinBox::unsignedChanged,
this, &TaskPolarPatternParameters::onOccurrences);
}
void TaskPolarPatternParameters::retranslateParameterUI(QWidget* widget)
{
ui->retranslateUi(widget);
}
void TaskPolarPatternParameters::updateUI()
@@ -254,23 +194,6 @@ void TaskPolarPatternParameters::adaptVisibilityToMode()
ui->angleOffsetWrapper->setVisible(mode == PartDesign::PolarPatternMode::offset);
}
void TaskPolarPatternParameters::addObject(App::DocumentObject* obj)
{
QString label = QString::fromUtf8(obj->Label.getValue());
QString objectName = QString::fromLatin1(obj->getNameInDocument());
QListWidgetItem* item = new QListWidgetItem();
item->setText(label);
item->setData(Qt::UserRole, objectName);
ui->listWidgetFeatures->addItem(item);
}
void TaskPolarPatternParameters::removeObject(App::DocumentObject* obj)
{
QString label = QString::fromUtf8(obj->Label.getValue());
removeItemFromListWidget(ui->listWidgetFeatures, label);
}
void TaskPolarPatternParameters::onSelectionChanged(const Gui::SelectionChanges& msg)
{
if (selectionMode!=none && msg.Type == Gui::SelectionChanges::AddSelection) {
@@ -297,12 +220,6 @@ void TaskPolarPatternParameters::onSelectionChanged(const Gui::SelectionChanges&
}
}
void TaskPolarPatternParameters::clearButtons()
{
ui->buttonAddFeature->setChecked(false);
ui->buttonRemoveFeature->setChecked(false);
}
void TaskPolarPatternParameters::onCheckReverse(const bool on) {
if (blockUpdate)
return;
@@ -400,22 +317,6 @@ void TaskPolarPatternParameters::onUpdateView(bool on)
}
}
void TaskPolarPatternParameters::onFeatureDeleted()
{
PartDesign::Transformed* pcTransformed = getObject();
std::vector<App::DocumentObject*> originals = pcTransformed->Originals.getValues();
int currentRow = ui->listWidgetFeatures->currentRow();
if (currentRow < 0) {
Base::Console().Error("PartDesign PolarPattern: No feature selected for removing.\n");
return; //no current row selected
}
originals.erase(originals.begin() + currentRow);
setupTransaction();
pcTransformed->Originals.setValues(originals);
ui->listWidgetFeatures->model()->removeRow(currentRow);
recomputeFeature();
}
void TaskPolarPatternParameters::getAxis(App::DocumentObject*& obj, std::vector<std::string>& sub) const
{
const App::PropertyLinkSub &lnk = axesLinks.getCurrentLink();
@@ -453,17 +354,6 @@ TaskPolarPatternParameters::~TaskPolarPatternParameters()
} catch (const Base::Exception &ex) {
Base::Console().Error ("%s\n", ex.what () );
}
if (proxy)
delete proxy;
}
void TaskPolarPatternParameters::changeEvent(QEvent *e)
{
TaskBox::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(proxy);
}
}
void TaskPolarPatternParameters::apply()