diff --git a/DATA/PROJECT/test/data/crop.db b/DATA/PROJECT/test/data/crop.db index 594d7b9d..31308084 100644 Binary files a/DATA/PROJECT/test/data/crop.db and b/DATA/PROJECT/test/data/crop.db differ diff --git a/DATA/TEMPLATE/crop_default.db b/DATA/TEMPLATE/crop_default.db index 81ae169e..0506a966 100644 Binary files a/DATA/TEMPLATE/crop_default.db and b/DATA/TEMPLATE/crop_default.db differ diff --git a/agrolib/criteria1DWidget/criteria1DWidget.cpp b/agrolib/criteria1DWidget/criteria1DWidget.cpp index 5289000c..d15f54cb 100644 --- a/agrolib/criteria1DWidget/criteria1DWidget.cpp +++ b/agrolib/criteria1DWidget/criteria1DWidget.cpp @@ -1817,7 +1817,7 @@ void Criteria1DWidget::on_actionNewCrop() } Crit3DCrop* newCrop = new Crit3DCrop(); - DialogNewCrop dialog(newCrop); + DialogNewCrop dialog(&(myProject.dbCrop), newCrop); if (dialog.result() == QDialog::Accepted) { // write newCrop on Db diff --git a/agrolib/criteria1DWidget/dialogNewCrop.cpp b/agrolib/criteria1DWidget/dialogNewCrop.cpp index fbef6e71..c522a471 100644 --- a/agrolib/criteria1DWidget/dialogNewCrop.cpp +++ b/agrolib/criteria1DWidget/dialogNewCrop.cpp @@ -1,9 +1,10 @@ #include "dialogNewCrop.h" #include "crop.h" +#include "cropDbQuery.h" #include "commonConstants.h" -DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop) - :newCrop(newCrop) +DialogNewCrop::DialogNewCrop(QSqlDatabase *dbCrop, Crit3DCrop *newCrop) + :dbCrop(dbCrop), newCrop(newCrop) { setWindowTitle("New Crop"); QVBoxLayout *mainLayout = new QVBoxLayout(); @@ -25,12 +26,30 @@ DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop) typeCropComboBox->addItem(QString::fromStdString(getCropTypeString(type))); } + QString cropType = typeCropComboBox->currentText(); + newCrop->type = getCropType(cropType.toStdString()); + + QList cropList; + QString errorStr; + if (! getCropListFromType(*dbCrop, cropType, cropList, errorStr)) + { + QMessageBox::information(this, "Error in reading crop list", errorStr); + return; + } + + QLabel* templateCropLabel = new QLabel(tr("Copy other parameters from crop: ")); + templateCropComboBox = new QComboBox(); + for (int i=0; i < cropList.size(); i++) + { + templateCropComboBox->addItem(cropList[i]); + } + sowingDoY = new QLabel(tr("Enter sowing DOY: ")); sowingDoYValue = new QSpinBox(); sowingDoYValue->setMinimum(-365); sowingDoYValue->setMaximum(365); - cycleMaxDuration = new QLabel(tr("Enter cycle max duration: ")); + cycleMaxDuration = new QLabel(tr("Enter crop cycle max duration [days]: ")); cycleMaxDurationValue = new QSpinBox(); cycleMaxDurationValue->setMinimum(0); cycleMaxDurationValue->setMaximum(365); @@ -41,12 +60,13 @@ DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop) layoutCrop->addWidget(nameCropValue, 1 , 1); layoutCrop->addWidget(typeCropLabel, 2 , 0); layoutCrop->addWidget(typeCropComboBox, 2 , 1); - layoutCrop->addWidget(sowingDoY, 3 , 0); - layoutCrop->addWidget(sowingDoYValue, 3 , 1); - layoutCrop->addWidget(cycleMaxDuration, 4 , 0); - layoutCrop->addWidget(cycleMaxDurationValue, 4 , 1); + layoutCrop->addWidget(templateCropLabel, 3 , 0); + layoutCrop->addWidget(templateCropComboBox, 3, 1); + layoutCrop->addWidget(sowingDoY, 4, 0); + layoutCrop->addWidget(sowingDoYValue, 4, 1); + layoutCrop->addWidget(cycleMaxDuration, 5, 0); + layoutCrop->addWidget(cycleMaxDurationValue, 5, 1); - newCrop->type = getCropType(typeCropComboBox->currentText().toStdString()); if (newCrop->isSowingCrop()) { sowingDoY->setVisible(true); @@ -72,7 +92,6 @@ DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop) layoutOk->addWidget(&buttonBox); - mainLayout->addLayout(layoutCrop); mainLayout->addLayout(layoutOk); @@ -80,12 +99,13 @@ DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop) show(); exec(); - } -void DialogNewCrop::on_actionChooseType(QString type) + +void DialogNewCrop::on_actionChooseType(QString cropType) { - newCrop->type = getCropType(type.toStdString()); + newCrop->type = getCropType(cropType.toStdString()); + if (newCrop->isSowingCrop()) { sowingDoY->setVisible(true); @@ -102,6 +122,21 @@ void DialogNewCrop::on_actionChooseType(QString type) newCrop->sowingDoy = NODATA; newCrop->plantCycle = 365; } + + templateCropComboBox->clear(); + + QList cropList; + QString errorStr; + if (! getCropListFromType(*dbCrop, cropType, cropList, errorStr)) + { + QMessageBox::information(this, "Error in reading crop list", errorStr); + return; + } + + for (int i=0; i < cropList.size(); i++) + { + templateCropComboBox->addItem(cropList[i]); + } } @@ -140,24 +175,24 @@ bool DialogNewCrop::checkData() { if (idCropValue->text().isEmpty()) { - QMessageBox::information(nullptr, "Missing parameter", "Insert ID CROP"); + QMessageBox::information(nullptr, "Missing parameter", "Insert crop ID"); return false; } if (nameCropValue->text().isEmpty()) { - QMessageBox::information(nullptr, "Missing parameter", "Insert ID NAME"); + QMessageBox::information(nullptr, "Missing parameter", "Insert crop NAME"); return false; } if (sowingDoY->isVisible()) { - if (sowingDoYValue->text().isEmpty()) + if (sowingDoYValue->text().isEmpty() || sowingDoYValue->text().toInt() == 0) { QMessageBox::information(nullptr, "Missing parameter", "Insert sowing day of year"); return false; } - if (cycleMaxDurationValue->text().isEmpty()) + if (cycleMaxDurationValue->text().isEmpty() || cycleMaxDurationValue->text().toInt() == 0) { - QMessageBox::information(nullptr, "Missing parameter", "Insert plant cycle max duration"); + QMessageBox::information(nullptr, "Missing parameter", "Insert crop cycle max duration"); return false; } } diff --git a/agrolib/criteria1DWidget/dialogNewCrop.h b/agrolib/criteria1DWidget/dialogNewCrop.h index e63cc0e0..f12e5d52 100644 --- a/agrolib/criteria1DWidget/dialogNewCrop.h +++ b/agrolib/criteria1DWidget/dialogNewCrop.h @@ -3,26 +3,31 @@ #include class Crit3DCrop; + class QSqlDatabase; class DialogNewCrop : public QDialog { Q_OBJECT public: - DialogNewCrop(Crit3DCrop* newCrop); - void on_actionChooseType(QString type); + DialogNewCrop(QSqlDatabase* dbCrop, Crit3DCrop* newCrop); + + void on_actionChooseType(QString cropType); void done(int res); bool checkData(); QString getNameCrop(); private: + QSqlDatabase* dbCrop; Crit3DCrop* newCrop; QLineEdit* idCropValue; QLineEdit* nameCropValue; QLineEdit* typeCropValue; + QLineEdit* templateCropValue; QLabel *sowingDoY; QSpinBox* sowingDoYValue; QLabel *cycleMaxDuration; QSpinBox* cycleMaxDurationValue; + QComboBox* templateCropComboBox; }; #endif // DIALOGNEWCROP_H diff --git a/agrolib/crop/crop.cpp b/agrolib/crop/crop.cpp index 188abfd0..8b030d15 100644 --- a/agrolib/crop/crop.cpp +++ b/agrolib/crop/crop.cpp @@ -726,8 +726,6 @@ speciesType getCropType(std::string cropType) return HORTICULTURAL; else if (cropType == "grass") return GRASS; - else if (cropType == "grass_first_year") - return GRASS; else if (cropType == "fallow") return FALLOW; else if (cropType == "annual_fallow" || cropType == "fallow_annual") diff --git a/agrolib/crop/crop.h b/agrolib/crop/crop.h index 9af159b6..35766f17 100644 --- a/agrolib/crop/crop.h +++ b/agrolib/crop/crop.h @@ -12,7 +12,7 @@ #endif enum speciesType {HERBACEOUS_ANNUAL, HERBACEOUS_PERENNIAL, HORTICULTURAL, GRASS, TREE, FALLOW, FALLOW_ANNUAL}; - #define NR_CROP_SPECIES 6 + #define NR_CROP_SPECIES 7 /*! * \brief The Crit3DCrop class diff --git a/agrolib/crop/cropDbQuery.cpp b/agrolib/crop/cropDbQuery.cpp index 84210b57..970c582a 100644 --- a/agrolib/crop/cropDbQuery.cpp +++ b/agrolib/crop/cropDbQuery.cpp @@ -147,3 +147,30 @@ float getIrriRatioFromCropId(const QSqlDatabase &dbCrop, QString cropClassTable, else return NODATA; } + + +bool getCropListFromType(const QSqlDatabase &dbCrop, QString cropType, QList& cropList, QString& errorStr) +{ + QString queryString = "SELECT id_crop FROM crop WHERE type = '" + cropType + "'"; + + QSqlQuery query = dbCrop.exec(queryString); + query.last(); + if (! query.isValid()) + { + if (query.lastError().isValid()) + errorStr = "Error in reading crop list from type: " + cropType + "\n" + query.lastError().text(); + else + errorStr = "Missing crop type: " + cropType; + + return false; + } + + query.first(); + do + { + cropList.append(query.value("id_crop").toString()); + } + while (query.next()); + + return true; +} diff --git a/agrolib/crop/cropDbQuery.h b/agrolib/crop/cropDbQuery.h index 16b803ba..ee068393 100644 --- a/agrolib/crop/cropDbQuery.h +++ b/agrolib/crop/cropDbQuery.h @@ -20,5 +20,7 @@ float getIrriRatioFromCropId(const QSqlDatabase &dbCrop, QString cropClassTable, QString cropIdField, int cropId, QString& errorStr); + bool getCropListFromType(const QSqlDatabase &dbCrop, QString cropType, QList& cropList, QString& errorStr); + #endif // CROPDBQUERY_H