Refactoring. Reduce code repetition.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-01-18 13:46:19 +02:00
parent 83dd68c36c
commit 4f69c7766f
2 changed files with 48 additions and 107 deletions

View File

@ -117,8 +117,8 @@ DialogIncrements::DialogIncrements(VContainer *data, VPattern *doc, QWidget *par
connect(ui->tableWidgetPC, &QTableWidget::itemSelectionChanged, this, connect(ui->tableWidgetPC, &QTableWidget::itemSelectionChanged, this,
&DialogIncrements::ShowIncrementDetails); &DialogIncrements::ShowIncrementDetails);
InitIncrementVarTypeMenu(); ui->toolButtonAdd->setMenu(InitVarTypeMenu(ui->toolButtonAdd->menu(), true /*increments tab*/));
InitPreviewCalculationVarTypeMenu(); ui->toolButtonAddPC->setMenu(InitVarTypeMenu(ui->toolButtonAddPC->menu(), false /*preview calculations tab*/));
connect(ui->toolButtonAdd, &QToolButton::clicked, this, &DialogIncrements::AddIncrement); connect(ui->toolButtonAdd, &QToolButton::clicked, this, &DialogIncrements::AddIncrement);
connect(ui->toolButtonAddPC, &QToolButton::clicked, this, &DialogIncrements::AddIncrement); connect(ui->toolButtonAddPC, &QToolButton::clicked, this, &DialogIncrements::AddIncrement);
@ -723,51 +723,65 @@ void DialogIncrements::ShowTableIncrementDetails(QTableWidget *table)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::InitIncrementVarTypeMenu() QMenu *DialogIncrements::InitVarTypeMenu(QMenu *menu, bool incrementTab)
{ {
auto varTypeMenu = ui->toolButtonAdd->menu(); if (menu == nullptr)
if (varTypeMenu == nullptr)
{ {
varTypeMenu = new QMenu(this); menu = new QMenu(this);
} }
else else
{ {
varTypeMenu->clear(); menu->clear();
} }
QAction *action = varTypeMenu->addAction(tr("Increment")); QAction *action = menu->addAction(tr("Increment"));
action->setData(true); // Increments tab action->setData(incrementTab); // Increments tab
connect(action, &QAction::triggered, this, &DialogIncrements::AddIncrement); connect(action, &QAction::triggered, this, &DialogIncrements::AddIncrement);
action = varTypeMenu->addAction(tr("Separator")); action = menu->addAction(tr("Separator"));
action->setData(true); // Increments tab action->setData(incrementTab); // Increments tab
connect(action, &QAction::triggered, this, &DialogIncrements::AddSeparator); connect(action, &QAction::triggered, this, &DialogIncrements::AddSeparator);
ui->toolButtonAdd->setMenu(varTypeMenu); return menu;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::InitPreviewCalculationVarTypeMenu() void DialogIncrements::AddNewIncrement(IncrementType type)
{ {
auto varTypeMenu = ui->toolButtonAddPC->menu(); qCDebug(vDialog, "Add new increment");
if (varTypeMenu == nullptr)
auto *action = qobject_cast<QAction *>(sender());
if (action == nullptr)
{ {
varTypeMenu = new QMenu(this); return;
}
const bool incrementMode = action->data().toBool();
QTableWidget *table = incrementMode ? ui->tableWidgetIncrement : ui->tableWidgetPC;
const QString name = GetCustomName();
qint32 currentRow = -1;
if (table->currentRow() == -1)
{
currentRow = table->rowCount();
incrementMode ? doc->AddEmptyIncrement(name, type) : doc->AddEmptyPreviewCalculation(name, type);
} }
else else
{ {
varTypeMenu->clear(); currentRow = table->currentRow()+1;
const QTableWidgetItem *nameField = table->item(table->currentRow(), 0);
incrementMode ? doc->AddEmptyIncrementAfter(nameField->text(), name, type) :
doc->AddEmptyPreviewCalculationAfter(nameField->text(), name, type);
} }
QAction *action = varTypeMenu->addAction(tr("Preview calculation")); hasChanges = true;
action->setData(false); // Preview calculation tab LocalUpdateTree();
connect(action, &QAction::triggered, this, &DialogIncrements::AddIncrement);
action = varTypeMenu->addAction(tr("Separator")); table->selectRow(currentRow);
action->setData(false); // Preview calculation tab table->repaint(); // Force repain to fix paint artifacts on Mac OS X
connect(action, &QAction::triggered, this, &DialogIncrements::AddSeparator);
ui->toolButtonAddPC->setMenu(varTypeMenu);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -897,89 +911,13 @@ void DialogIncrements::FillIncrementsTable(QTableWidget *table,
*/ */
void DialogIncrements::AddIncrement() void DialogIncrements::AddIncrement()
{ {
qCDebug(vDialog, "Add new increment"); AddNewIncrement(IncrementType::Increment);
auto *button = qobject_cast<QToolButton *>(sender());
auto *action = qobject_cast<QAction *>(sender());
bool incrementMode = true;
if (button == ui->toolButtonAdd || ((action != nullptr) && action->data().toBool()))
{
incrementMode = true;
}
else if (button == ui->toolButtonAddPC || ((action != nullptr) && not action->data().toBool()))
{
incrementMode = false;
}
else
{
return;
}
QTableWidget *table = incrementMode ? ui->tableWidgetIncrement : ui->tableWidgetPC;
const QString name = GetCustomName();
qint32 currentRow = -1;
if (table->currentRow() == -1)
{
currentRow = table->rowCount();
incrementMode ? doc->AddEmptyIncrement(name) : doc->AddEmptyPreviewCalculation(name);
}
else
{
currentRow = table->currentRow()+1;
const QTableWidgetItem *nameField = table->item(table->currentRow(), 0);
incrementMode ? doc->AddEmptyIncrementAfter(nameField->text(), name) :
doc->AddEmptyPreviewCalculationAfter(nameField->text(), name);
}
hasChanges = true;
LocalUpdateTree();
table->selectRow(currentRow);
table->repaint(); // Force repain to fix paint artifacts on Mac OS X
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::AddSeparator() void DialogIncrements::AddSeparator()
{ {
qCDebug(vDialog, "Add new increment"); AddNewIncrement(IncrementType::Separator);
auto *action = qobject_cast<QAction *>(sender());
if (action == nullptr)
{
return;
}
const bool incrementMode = action->data().toBool();
QTableWidget *table = incrementMode ? ui->tableWidgetIncrement : ui->tableWidgetPC;
const QString name = GetCustomName();
qint32 currentRow = -1;
const IncrementType type = IncrementType::Separator;
if (table->currentRow() == -1)
{
currentRow = table->rowCount();
incrementMode ? doc->AddEmptyIncrement(name, type) : doc->AddEmptyPreviewCalculation(name, type);
}
else
{
currentRow = table->currentRow()+1;
const QTableWidgetItem *nameField = table->item(table->currentRow(), 0);
incrementMode ? doc->AddEmptyIncrementAfter(nameField->text(), name, type) :
doc->AddEmptyPreviewCalculationAfter(nameField->text(), name, type);
}
hasChanges = true;
LocalUpdateTree();
table->selectRow(currentRow);
table->repaint(); // Force repain to fix paint artifacts on Mac OS X
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -1451,8 +1389,10 @@ void DialogIncrements::changeEvent(QEvent *event)
{ {
// retranslate designer form (single inheritance approach) // retranslate designer form (single inheritance approach)
ui->retranslateUi(this); ui->retranslateUi(this);
InitIncrementVarTypeMenu();
InitPreviewCalculationVarTypeMenu(); ui->toolButtonAdd->setMenu(InitVarTypeMenu(ui->toolButtonAdd->menu(), true /*increments tab*/));
ui->toolButtonAddPC->setMenu(InitVarTypeMenu(ui->toolButtonAddPC->menu(), false /*preview calculations tab*/));
FullUpdateFromFile(); FullUpdateFromFile();
} }
// remember to call base class implementation // remember to call base class implementation

View File

@ -141,8 +141,9 @@ private:
void ShowTableIncrementDetails(QTableWidget *table); void ShowTableIncrementDetails(QTableWidget *table);
void InitIncrementVarTypeMenu(); QMenu *InitVarTypeMenu(QMenu *menu, bool incrementTab);
void InitPreviewCalculationVarTypeMenu();
void AddNewIncrement(IncrementType type);
}; };
#endif // DIALOGINCREMENTS_H #endif // DIALOGINCREMENTS_H