Skip to content

Commit afa6e65

Browse files
author
MickaelG
committed
fixed issue 7 (segfault when no plant and plot is selected)
1 parent b306db9 commit afa6e65

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

src/core/plants.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Plant* Plants::find(const std::string& key)
3030
return plant_up.get();
3131
}
3232
}
33-
return NULL;
33+
return nullptr;
3434
}
3535

3636
void Plants::remove(int iplant)

src/gui/gui_widgets/EditCropWidget.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,20 @@ void EditCropWidget::edit_crop()
7474
{
7575
QString plot_key = ui->plotInput->currentElem();
7676
Plot* p_plot = dataset.get_plots().find(fromQString(plot_key));
77+
if (p_plot == nullptr) {
78+
QMessageBox::critical(NULL, QObject::tr("Error"),
79+
QObject::tr("Please select a plot."));
80+
return;
81+
}
7782
Rectangle rect = ui->shapeInput->get_rect();
7883

7984
QString plant_key = ui->plantInput->currentElem();
8085
Plant* p_plant = dataset.get_plants().find(fromQString(plant_key));
86+
if (p_plant == nullptr) {
87+
QMessageBox::critical(NULL, QObject::tr("Error"),
88+
QObject::tr("Please select a plant."));
89+
return;
90+
}
8191
QString var_key = ui->varInput->currentElem();
8292

8393
QDate start_date = ui->startdateInput->selectedDate();

src/gui/gui_widgets/PlantsModel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ QVariant PlantsModel::data(const QModelIndex& index, int role) const
6464
{
6565
if (! index.parent().isValid()) // parent is invalid: we want the plant
6666
{
67+
if (index.row() >= plants.size() || index.row() < 0) {
68+
return QVariant();
69+
}
6770
if (role == Qt::DisplayRole)
6871
{
6972
if (index.column() == 0)
@@ -82,6 +85,9 @@ QVariant PlantsModel::data(const QModelIndex& index, int role) const
8285
else
8386
{
8487
int plant_index = index.parent().row();
88+
if (plant_index >= plants.size() || plant_index < 0) {
89+
return QVariant();
90+
}
8591
if (role == Qt::DisplayRole)
8692
{
8793
if (index.column() == 0)

src/gui/gui_widgets/PlotsModel.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ int PlotsModel::columnCount(const QModelIndex& index) const
3030

3131
QVariant PlotsModel::data(const QModelIndex& index, int role) const
3232
{
33+
if (index.row() >= plots.size() || index.row() < 0) {
34+
return QVariant();
35+
}
36+
3337
if (role == Qt::DisplayRole)
3438
{
3539
if (index.column() == 0)

tomate_fr.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!DOCTYPE TS>
3-
<TS version="2.0" language="fr_FR">
3+
<TS version="2.1" language="fr_FR">
44
<context>
55
<name>AddDialog</name>
66
<message>
@@ -247,22 +247,22 @@
247247
<context>
248248
<name>PhysInput</name>
249249
<message>
250-
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="37"/>
250+
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="62"/>
251251
<source>x position:</source>
252252
<translation>position x :</translation>
253253
</message>
254254
<message>
255-
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="39"/>
255+
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="64"/>
256256
<source>y position:</source>
257257
<translation>position y :</translation>
258258
</message>
259259
<message>
260-
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="41"/>
260+
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="66"/>
261261
<source>width:</source>
262262
<translation>largeur :</translation>
263263
</message>
264264
<message>
265-
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="43"/>
265+
<location filename="src/gui/gui_widgets/PhysInput.cpp" line="68"/>
266266
<source>height:</source>
267267
<translation>hauteur :</translation>
268268
</message>
@@ -337,6 +337,8 @@
337337
<location filename="src/main.cpp" line="38"/>
338338
<location filename="src/main.cpp" line="59"/>
339339
<location filename="src/main.cpp" line="81"/>
340+
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="78"/>
341+
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="87"/>
340342
<source>Error</source>
341343
<translation>Erreur</translation>
342344
</message>
@@ -366,12 +368,22 @@
366368
<translation>Erreur d&apos;écriture du fichier de données.</translation>
367369
</message>
368370
<message>
369-
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="115"/>
371+
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="79"/>
372+
<source>Please select a plot.</source>
373+
<translation>Veuillez sélectionner une planche.</translation>
374+
</message>
375+
<message>
376+
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="88"/>
377+
<source>Please select a plant.</source>
378+
<translation>Veuillez sélectionner une plante.</translation>
379+
</message>
380+
<message>
381+
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="125"/>
370382
<source>Delete crop</source>
371383
<translation>Suppression d&apos;une culture</translation>
372384
</message>
373385
<message>
374-
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="116"/>
386+
<location filename="src/gui/gui_widgets/EditCropWidget.cpp" line="126"/>
375387
<source>Are you sure you want to delete the crop ?</source>
376388
<translation>Êtes-vous sûr de vouloir supprimer cette culture ?</translation>
377389
</message>

0 commit comments

Comments
 (0)