Skip to content

Commit

Permalink
update new crop
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Oct 31, 2023
1 parent 56fad63 commit abd3aef
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 23 deletions.
Binary file modified DATA/PROJECT/test/data/crop.db
Binary file not shown.
Binary file modified DATA/TEMPLATE/crop_default.db
Binary file not shown.
2 changes: 1 addition & 1 deletion agrolib/criteria1DWidget/criteria1DWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 52 additions & 17 deletions agrolib/criteria1DWidget/dialogNewCrop.cpp
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -25,12 +26,30 @@ DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop)
typeCropComboBox->addItem(QString::fromStdString(getCropTypeString(type)));
}

QString cropType = typeCropComboBox->currentText();
newCrop->type = getCropType(cropType.toStdString());

QList<QString> 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);
Expand All @@ -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);
Expand All @@ -72,20 +92,20 @@ DialogNewCrop::DialogNewCrop(Crit3DCrop *newCrop)

layoutOk->addWidget(&buttonBox);


mainLayout->addLayout(layoutCrop);
mainLayout->addLayout(layoutOk);

setLayout(mainLayout);

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);
Expand All @@ -102,6 +122,21 @@ void DialogNewCrop::on_actionChooseType(QString type)
newCrop->sowingDoy = NODATA;
newCrop->plantCycle = 365;
}

templateCropComboBox->clear();

QList<QString> 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]);
}
}


Expand Down Expand Up @@ -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;
}
}
Expand Down
9 changes: 7 additions & 2 deletions agrolib/criteria1DWidget/dialogNewCrop.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@

#include <QtWidgets>
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
2 changes: 0 additions & 2 deletions agrolib/crop/crop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion agrolib/crop/crop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions agrolib/crop/cropDbQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,30 @@ float getIrriRatioFromCropId(const QSqlDatabase &dbCrop, QString cropClassTable,
else
return NODATA;
}


bool getCropListFromType(const QSqlDatabase &dbCrop, QString cropType, QList<QString>& 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;
}
2 changes: 2 additions & 0 deletions agrolib/crop/cropDbQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString>& cropList, QString& errorStr);


#endif // CROPDBQUERY_H

0 comments on commit abd3aef

Please sign in to comment.