src/EditProductExport.cpp

changeset 267
8af232524e64
parent 266
5f0782149028
child 268
72e8ade9aff2
--- a/src/EditProductExport.cpp	Tue Jun 07 11:29:55 2022 +0200
+++ b/src/EditProductExport.cpp	Tue Jun 07 20:57:42 2022 +0200
@@ -578,8 +578,269 @@
 }
 
 
+void EditProduct::split_show()
+{
+    double left = vol_availEdit->value();
+
+    qDebug() << "split_show" << product->splits.size();
+    const QSignalBlocker blocker1(splitTable);
+    split_delButton->setEnabled(product->splits.size() > 0);
+
+    for (int i = 0; i < product->splits.size(); i++) {
+
+	qDebug() << i << product->splits.at(i).code << product->splits.at(i).name << QString::number(product->splits.at(i).size);
+    	splitTable->setItem(i, 0, new QTableWidgetItem(product->splits.at(i).code));
+	splitTable->setItem(i, 1, new QTableWidgetItem(product->splits.at(i).name));
+	splitTable->setItem(i, 2, new QTableWidgetItem(QString::number(product->splits.at(i).size, 'f', 1)));
+	left -= product->splits.at(i).size;
+    }
+
+    if (left < 0)
+	left = 0;
+    vol_leftEdit->setValue(left);
+}
+
+
+void EditProduct::split_add_clicked()
+{
+    Splits news;
+    int	   entry = product->splits.size();
+
+    qDebug() << "split_add_clicked" << entry;
+
+    news.name = product->name + QString(" %1").arg(entry + 1);
+    news.code = product->code + QString("-%1").arg(entry + 1);
+    news.size = 0;
+    product->splits.append(news);
+    splitTable->setRowCount(entry + 1);
+    split_show();
+}
+
+
+void EditProduct::split_del_clicked()
+{
+    int last = product->splits.size() - 1;
+
+    qDebug() << "split_del_clicked" << last;
+
+    product->splits.removeAt(last);
+    splitTable->setRowCount(last);
+    split_show();
+}
+
+
+void EditProduct::split_splitat_changed(int val)
+{
+    qDebug() << "split_splitat_changed" << val;
+
+    switch (val) {
+	case 0:	vol_availEdit->setValue(0);
+		break;
+	case 1:	vol_availEdit->setValue(product->boil_size);
+		break;
+	case 2:	vol_availEdit->setValue(product->batch_size);
+		break;
+	case 3:	vol_availEdit->setValue(product->brew_fermenter_volume);
+		break;
+	case 4:
+	case 5:	vol_availEdit->setValue(round(product->brew_fermenter_volume * 9.2) / 10.0);	// Estimate without trub
+		break;
+	case 6:	vol_availEdit->setValue(product->package_volume);
+		break;
+    }
+    product->divide_type = val;
+    split_addButton->setEnabled(product->divide_type != 0);
+    split_show();
+}
+
+
+void EditProduct::split_table_changed(int nRow, int nCol)
+{
+    qDebug() << "split_table_changed" << nRow << nCol;
+
+    split_show();
+}
+
+
 void EditProduct::splitProduct()
 {
+    bool doit = false;
+
+    if (this->textIsChanged) {
+	DB_product::save(product, this);
+	this->textIsChanged = false;
+    }
+
+    QDialog* dialog = new QDialog(this);
+    dialog->setWindowTitle(tr("Split product"));
+
+    dialog->setObjectName(QString::fromUtf8("SplitDialog"));
+    dialog->resize(1024, 560);
+    QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
+    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
+    buttonBox->setGeometry(QRect(30, 510, 961, 32));
+    buttonBox->setOrientation(Qt::Horizontal);
+    buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
+    buttonBox->setCenterButtons(true);
+    QLabel *topLabel = new QLabel(dialog);
+    topLabel->setObjectName(QString::fromUtf8("topLabel"));
+    topLabel->setGeometry(QRect(30, 20, 961, 31));
+    topLabel->setText(tr("Split product"));
+    QFont font;
+    font.setPointSize(12);
+    font.setBold(true);
+    font.setWeight(75);
+    topLabel->setFont(font);
+    topLabel->setAlignment(Qt::AlignCenter);
+
+    QLabel *nameLabel = new QLabel(dialog);
+    nameLabel->setObjectName(QString::fromUtf8("nameLabel"));
+    nameLabel->setGeometry(QRect(20, 80, 141, 20));
+    nameLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    nameLabel->setText(tr("Product name:"));
+    QLineEdit *nameEdit = new QLineEdit(dialog);
+    nameEdit->setObjectName(QString::fromUtf8("nameEdit"));
+    nameEdit->setGeometry(QRect(170, 80, 481, 23));
+    nameEdit->setReadOnly(true);
+    nameEdit->setText(product->name);
+
+    QLabel *codeLabel = new QLabel(dialog);
+    codeLabel->setObjectName(QString::fromUtf8("codeLabel"));
+    codeLabel->setGeometry(QRect(20, 110, 141, 20));
+    codeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    codeLabel->setText(tr("Product code:"));
+    QLineEdit *codeEdit = new QLineEdit(dialog);
+    codeEdit->setObjectName(QString::fromUtf8("codeEdit"));
+    codeEdit->setGeometry(QRect(170, 110, 113, 23));
+    codeEdit->setReadOnly(true);
+    codeEdit->setText(product->code);
+
+    QLabel *vol_availLabel = new QLabel(dialog);
+    vol_availLabel->setObjectName(QString::fromUtf8("vol_availLabel"));
+    vol_availLabel->setGeometry(QRect(20, 140, 141, 20));
+    vol_availLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    vol_availLabel->setText(tr("Available volume:"));
+
+    QLabel *stageLabel = new QLabel(dialog);
+    stageLabel->setObjectName(QString::fromUtf8("stageLabel"));
+    stageLabel->setGeometry(QRect(660, 80, 141, 20));
+    stageLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    stageLabel->setText(tr("Current brew stage:"));
+    QLineEdit *stageEdit = new QLineEdit(dialog);
+    stageEdit->setObjectName(QString::fromUtf8("stageEdit"));
+    stageEdit->setGeometry(QRect(810, 80, 113, 23));
+    stageEdit->setReadOnly(true);
+    stageEdit->setText(prod_stages[product->stage]);
+
+    QLabel *splitatLabel = new QLabel(dialog);
+    splitatLabel->setObjectName(QString::fromUtf8("splitatLabel"));
+    splitatLabel->setGeometry(QRect(660, 110, 141, 20));
+    splitatLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    splitatLabel->setText(tr("Split at moment:"));
+    QLabel *vol_leftLabel = new QLabel(dialog);
+    vol_leftLabel->setObjectName(QString::fromUtf8("vol_leftLabel"));
+    vol_leftLabel->setGeometry(QRect(660, 140, 141, 20));
+    vol_leftLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    vol_leftLabel->setText(tr("Volume remaining:"));
+
+    splitatEdit = new QComboBox(dialog);
+    splitatEdit->setObjectName(QString::fromUtf8("splitatEdit"));
+    splitatEdit->setGeometry(QRect(810, 110, 181, 23));
+    splitatEdit->addItem(tr("Not divided"));
+    splitatEdit->addItem(tr("After mash"));
+    splitatEdit->addItem(tr("After boil"));
+    splitatEdit->addItem(tr("After cooling"));
+    splitatEdit->addItem(tr("After primary"));
+    splitatEdit->addItem(tr("After secondary"));
+    splitatEdit->addItem(tr("After tertiary"));
+    /* Disable items that have been done. */
+    if (product->stage > PROD_STAGE_BREW) {
+	splitatEdit->setItemData(1, false, Qt::UserRole -1);
+	splitatEdit->setItemData(2, false, Qt::UserRole -1);
+	splitatEdit->setItemData(3, false, Qt::UserRole -1);
+    }
+    if (product->stage > PROD_STAGE_PRIMARY) {
+	splitatEdit->setItemData(4, false, Qt::UserRole -1);
+    }
+    if (product->stage > PROD_STAGE_SECONDARY) {
+	splitatEdit->setItemData(5, false, Qt::UserRole -1);
+    }
+    if (product->stage > PROD_STAGE_TERTIARY) {
+	splitatEdit->setItemData(6, false, Qt::UserRole -1);
+    }
+
+    vol_leftEdit = new QDoubleSpinBox(dialog);
+    vol_leftEdit->setObjectName(QString::fromUtf8("vol_leftEdit"));
+    vol_leftEdit->setGeometry(QRect(810, 140, 111, 24));
+    vol_leftEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    vol_leftEdit->setReadOnly(true);
+    vol_leftEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
+    vol_leftEdit->setMaximum(1000000.000000000000000);
+    vol_leftEdit->setSuffix(tr(" L"));
+    vol_availEdit = new QDoubleSpinBox(dialog);
+    vol_availEdit->setObjectName(QString::fromUtf8("vol_availEdit"));
+    vol_availEdit->setGeometry(QRect(170, 140, 111, 24));
+    vol_availEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
+    vol_availEdit->setReadOnly(true);
+    vol_availEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
+    vol_availEdit->setMaximum(1000000.000000000000000);
+    vol_availEdit->setSuffix(tr(" L"));
+
+    const QStringList labels({tr("Split code"), tr("Split name"), tr("Split volume") });
+    splitTable = new QTableWidget(dialog);
+    splitTable->setObjectName(QString::fromUtf8("splitTable"));
+    splitTable->setGeometry(QRect(30, 220, 961, 281));
+    splitTable->setColumnCount(3);
+    splitTable->setColumnWidth(0, 140);		/* Split code	*/
+    splitTable->setColumnWidth(1, 680);		/* Split name	*/
+    splitTable->setColumnWidth(2, 116);		/* Split volume	*/
+    splitTable->setHorizontalHeaderLabels(labels);
+    splitTable->verticalHeader()->hide();
+    splitTable->setRowCount(0);
+
+    split_addButton = new QPushButton(dialog);
+    split_addButton->setObjectName(QString::fromUtf8("addButton"));
+    split_addButton->setGeometry(QRect(170, 180, 100, 23));
+    QIcon icon;
+    icon.addFile(QString::fromUtf8("../resources/icons/silk/add.png"), QSize(), QIcon::Normal, QIcon::Off);
+    split_addButton->setIcon(icon);
+    split_addButton->setText(tr("Add"));
+    split_addButton->setEnabled(product->divide_type != 0);
+
+    split_delButton = new QPushButton(dialog);
+    split_delButton->setObjectName(QString::fromUtf8("delButton"));
+    split_delButton->setGeometry(QRect(660, 180, 100, 23));
+    QIcon icon2;
+    icon2.addFile(QString::fromUtf8("../resources/icons/silk/delete.png"), QSize(), QIcon::Normal, QIcon::Off);
+    split_delButton->setIcon(icon2);
+    split_delButton->setText(tr("Delete"));
+    split_delButton->setEnabled(false);
+
+    QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
+    QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
+    connect(split_addButton, SIGNAL(clicked()), this, SLOT(split_add_clicked()));
+    connect(split_delButton, SIGNAL(clicked()), this, SLOT(split_del_clicked()));
+    connect(splitatEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditProduct::split_splitat_changed);
+    connect(splitTable, SIGNAL(cellChanged(int, int)), this, SLOT(split_table_changed(int, int)));
+
+    dialog->setModal(true);
+    dialog->exec();
+    if (dialog->result() == QDialog::Accepted) {
+	doit = true;
+    }
+
+    disconnect(buttonBox, nullptr, nullptr, nullptr);
+    disconnect(splitatEdit, nullptr, nullptr, nullptr);
+    disconnect(splitTable, nullptr, nullptr, nullptr);
+
+    if (! doit)
+	return;
+    qInfo() << "split starts now";
+
+    /* Make sure the product editor is closed after splitting */
+    qInfo() << "split ready, destroy windows";
+    this->close();
+    this->setResult(1);
 }
 
 
@@ -603,10 +864,6 @@
     copy_productButton->setObjectName(QString::fromUtf8("copy_productButton"));
     copy_productButton->setGeometry(QRect(50, 50, 171, 21));
     copy_productButton->setText(tr("Copy to product"));
-/*    QRadioButton *splitButton = new QRadioButton(dialog);
-    splitButton->setObjectName(QString::fromUtf8("splitButton"));
-    splitButton->setGeometry(QRect(50, 110, 171, 21));
-    splitButton->setText(tr("Split this batch")); */
     QRadioButton *copy_recipeButton = new QRadioButton(dialog);
     copy_recipeButton->setObjectName(QString::fromUtf8("copy_recipeButton"));
     copy_recipeButton->setGeometry(QRect(50, 80, 171, 21));
@@ -615,6 +872,10 @@
     toforumButton->setObjectName(QString::fromUtf8("toforumButton"));
     toforumButton->setGeometry(QRect(50, 110, 171, 21));
     toforumButton->setText(tr("Export to forum"));
+    QRadioButton *splitButton = new QRadioButton(dialog);
+    splitButton->setObjectName(QString::fromUtf8("splitButton"));
+    splitButton->setGeometry(QRect(50, 140, 171, 21));
+    splitButton->setText(tr("Split this batch"));
 
     QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
     QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
@@ -630,6 +891,8 @@
 	    copyRecipe();
 	if (toforumButton->isChecked())
 	    toforumProduct();
+	if (splitButton->isChecked())
+	    splitProduct();
     }
 
     disconnect(buttonBox, nullptr, nullptr, nullptr);

mercurial