Skip to content

Commit 88194f2

Browse files
author
MickaelG
committed
select crops from space view or time view, plants and plots edit in modal windows
1 parent 551950e commit 88194f2

15 files changed

+385
-133
lines changed

src/gui/gui_mainwin.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,69 @@
33
#include "gui_plots.h"
44
#include "gui_spaceview.h"
55
#include "gui_timeline.h"
6+
#include "EditCropWidget.h"
67

78
#include "xml.h"
89

910
#include <QGridLayout>
1011
#include <QToolBar>
1112

12-
QTabWidget* createTabsWidget(Dataset& data)
13+
QWidget* createTabsWidget(Dataset& data)
1314
{
14-
QTabWidget* widget = new QTabWidget;
15+
QTabWidget* tab_widget = new QTabWidget;
1516

1617
SpaceViewWindow* spacewidget = new SpaceViewWindow(data);
17-
widget->addTab(spacewidget, QObject::tr("Space view"));
18+
tab_widget->addTab(spacewidget, QObject::tr("Space view"));
1819

1920
TimelineWindow* timewidget = new TimelineWindow(data);
20-
widget->addTab(timewidget, QObject::tr("Time view"));
21+
tab_widget->addTab(timewidget, QObject::tr("Time view"));
2122

2223
PlantsWindow* plantswidget = new PlantsWindow(data.get_plants());
23-
widget->addTab(plantswidget, QObject::tr("Plants"));
24-
2524
PlotsWindow* plotswidget = new PlotsWindow(data.get_plots());
26-
widget->addTab(plotswidget, QObject::tr("Plots"));
25+
26+
EditCropWidget* edit_crop_widget = new EditCropWidget(data);
2727

2828
QObject::connect(plantswidget, SIGNAL(timeline_need_update()), spacewidget, SLOT(update_draw()));
29-
QObject::connect(plantswidget, SIGNAL(timeline_need_update()), timewidget, SLOT(update()));
29+
QObject::connect(plantswidget, SIGNAL(timeline_need_update()), timewidget, SLOT(update_draw()));
3030

3131
QObject::connect(plotswidget, SIGNAL(timeline_need_update()), spacewidget, SLOT(update_draw()));
32-
QObject::connect(plotswidget, SIGNAL(timeline_need_update()), timewidget, SLOT(update()));
32+
QObject::connect(plotswidget, SIGNAL(timeline_need_update()), timewidget, SLOT(update_draw()));
33+
34+
QObject::connect(edit_crop_widget, SIGNAL(dataset_changed()), timewidget, SLOT(update_draw()));
35+
QObject::connect(edit_crop_widget, SIGNAL(dataset_changed()), spacewidget, SLOT(update_draw()));
3336

34-
//QObject::connect(timewidget->get_view()->get_scene()->get_ecd(), SIGNAL(dataset_changed()), timewidget, SLOT(update_draw()));
35-
//QObject::connect(timewidget->get_view()->get_scene()->get_ecd(), SIGNAL(dataset_changed()), spacewidget, SLOT(update_draw()));
37+
//Date of the spacewidget
3638
QObject::connect(timewidget->get_view()->get_scene(), SIGNAL(current_date_changed(QDate)), spacewidget->get_view()->get_scene(), SLOT(set_date(QDate)));
3739

40+
//Crops selection synchronisation
41+
QObject::connect(timewidget->get_view()->get_scene(), SIGNAL(crop_selected(Crop*)), edit_crop_widget, SLOT(set_crop_values(Crop*)));
42+
QObject::connect(spacewidget->get_view()->get_scene(), SIGNAL(crop_selected(Crop*)), edit_crop_widget, SLOT(set_crop_values(Crop*)));
43+
QObject::connect(timewidget->get_view()->get_scene(), SIGNAL(crop_selected(Crop*)),
44+
spacewidget->get_view()->get_scene(), SLOT(selectCrop(Crop*)));
45+
QObject::connect(spacewidget->get_view()->get_scene(), SIGNAL(crop_selected(Crop*)),
46+
timewidget->get_view()->get_scene(), SLOT(selectCrop(Crop*)));
47+
48+
QObject::connect(edit_crop_widget->ui->EditPlantsBtn, SIGNAL(clicked()), plantswidget, SLOT(show()));
49+
QObject::connect(edit_crop_widget->ui->EditPlotsBtn, SIGNAL(clicked()), plotswidget, SLOT(show()));
50+
51+
QWidget* widget = new QWidget;
52+
QGridLayout* main_layout = new QGridLayout;
53+
main_layout->addWidget(tab_widget);
54+
main_layout->addWidget(edit_crop_widget);
55+
widget->setLayout(main_layout);
56+
3857
return widget;
3958
}
4059

4160

4261
GuiMainWin::GuiMainWin(Dataset& dataset) : dataset(dataset) {
4362
showMaximized();
4463

45-
/*
46-
QToolBar* toolbar = new QToolBar();
47-
QAction* write_action = toolbar->addAction(tr("Write"));
48-
QObject::connect(write_action, SIGNAL(triggered()), this, SLOT(write_file()));
49-
addToolBar(toolbar);
50-
*/
51-
5264
setCentralWidget(new QWidget);
5365
centralWidget()->setLayout(new QGridLayout);
5466
centralWidget()->layout()->addWidget(createTabsWidget(dataset));
5567
}
5668

57-
void GuiMainWin::write_file()
58-
{
59-
xml_write_data("../user_data/data_out.sfg", dataset);
60-
}
61-
62-
6369
GuiMainWin::~GuiMainWin() {
6470
//TODO: delete tabwidget, timewidget, plantswidget, spacewidget
6571
}

src/gui/gui_mainwin.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class GuiMainWin : public QMainWindow
1313
GuiMainWin(Dataset& dataset);
1414
~GuiMainWin();
1515

16-
private slots:
17-
void write_file();
18-
1916
private:
2017
Dataset& dataset;
2118
};

src/gui/gui_plants.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ vector<QString> PlantsWindow::plants_str_list()
9090
}
9191

9292
PlantsWindow::PlantsWindow(Plants& plants, QWidget* parent) :
93-
QWidget(parent), plants(plants)
93+
QDialog(parent), plants(plants)
9494
{
95+
setModal(true);
96+
9597
color_dialog = new QColorDialog;
9698
QVBoxLayout* plants_layout = new QVBoxLayout();
9799
vector<QString> plants_str = plants_str_list();
@@ -107,6 +109,10 @@ PlantsWindow::PlantsWindow(Plants& plants, QWidget* parent) :
107109
plants_layout->addWidget(add_plant_btn);
108110
plants_layout->addWidget(del_plant_btn);
109111

112+
QPushButton* close_btn = new QPushButton(tr("Close"));
113+
plants_layout->addWidget(close_btn);
114+
QObject::connect(close_btn, SIGNAL(clicked()), this, SLOT(accept()));
115+
110116
QVBoxLayout* var_layout = new QVBoxLayout();
111117
var_widget = new QListView();
112118
var_widget->setModel(new VarsModel(plants, plants_widget));

src/gui/gui_plants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private slots:
3434
QLineEdit* nameInput;
3535
};
3636

37-
class PlantsWindow: public QWidget
37+
class PlantsWindow: public QDialog
3838
{
3939
Q_OBJECT
4040

src/gui/gui_plots.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ void AddPlotDialog::add()
5454

5555

5656
PlotsWindow::PlotsWindow(Plots& plots, QWidget* parent) :
57-
QWidget(parent), plots(plots)
57+
QDialog(parent), plots(plots)
5858
{
59+
setModal(true);
60+
5961
QVBoxLayout* plots_layout = new QVBoxLayout();
6062
plots_widget = new ListWidget(new PlotsModel(plots));
6163
plots_layout->addWidget(plots_widget);
@@ -69,6 +71,10 @@ PlotsWindow::PlotsWindow(Plots& plots, QWidget* parent) :
6971
QObject::connect(del_plot_btn, SIGNAL(clicked()), this, SLOT(delete_plot()));
7072
plots_layout->addWidget(del_plot_btn);
7173

74+
QPushButton* close_btn = new QPushButton(tr("Close"));
75+
plots_layout->addWidget(close_btn);
76+
QObject::connect(close_btn, SIGNAL(clicked()), this, SLOT(accept()));
77+
7278
QObject::connect(add_plot, SIGNAL(list_updated()), plots_widget->model(), SIGNAL(layoutChanged()));
7379
QObject::connect(this, SIGNAL(plots_widget_layoutAboutToBeChanged()), plots_widget->model(), SIGNAL(layoutAboutToBeChanged()));
7480
QObject::connect(add_plot, SIGNAL(list_updated()), this, SIGNAL(timeline_need_update()));

src/gui/gui_plots.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private slots:
3535
QLineEdit* horInput;
3636
};
3737

38-
class PlotsWindow: public QWidget
38+
class PlotsWindow: public QDialog
3939
{
4040
Q_OBJECT
4141

src/gui/gui_spaceview.cpp

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QFont>
88
#include <QColor>
99
#include <QBrush>
10+
#include <QGraphicsSceneMouseEvent>
1011

1112
SpaceViewWindow::SpaceViewWindow(Dataset& dataset) : view(dataset, this)
1213
{
@@ -38,18 +39,17 @@ void WholeSceneView::zoom_fit()
3839
}
3940

4041
void WholeSceneView::resizeEvent(QResizeEvent *event)
41-
4242
{
4343
zoom_fit();
4444
}
4545

4646
PlotRepresentation::PlotRepresentation(Crops& crops, Plot& plot, QDate date) :
4747
crops(crops), plot(plot)
4848
{
49-
this->update(date);
49+
this->update_draw(date);
5050
}
5151

52-
void PlotRepresentation::update(QDate date)
52+
void PlotRepresentation::update_draw(QDate date)
5353
{
5454
Rectangle rect = plot.get_rect();
5555
this->setRect(rect.get_x(), - rect.get_y() - rect.get_height(),
@@ -60,6 +60,7 @@ void PlotRepresentation::update(QDate date)
6060
SubdRepresentation *subd_repr = new SubdRepresentation(subd.get_rect(), crop, date);
6161
subd_repr->setPos(rect.get_x(), -rect.get_y());
6262
subd_repr->setParentItem(this);
63+
subd_reprs.push_back(subd_repr);
6364
}
6465
}
6566
//TODO: delete subd_repr
@@ -95,9 +96,15 @@ SubdRepresentation::SubdRepresentation(Rectangle rect, Crop& crop, QDate date) :
9596
}
9697
}
9798

99+
Crop* SubdRepresentation::get_pcrop()
100+
{
101+
return &crop;
102+
}
103+
98104
WholeScene::WholeScene(Dataset& dataset) : dataset(dataset)
99105
{
100106
date = QDate::currentDate();
107+
selected_subd_repr = 0;
101108
this->draw_scene();
102109
}
103110

@@ -109,7 +116,7 @@ void WholeScene::set_date(QDate date)
109116

110117
void WholeScene::update_draw()
111118
{
112-
this->clear();
119+
this->clear_scene();
113120
this->draw_scene();
114121
}
115122

@@ -122,7 +129,89 @@ void WholeScene::draw_scene()
122129
PlotRepresentation *plot_repr = new PlotRepresentation(dataset.get_crops(),
123130
plot, date);
124131
this->addItem(plot_repr);
132+
plot_reprs.push_back(plot_repr);
125133
//TODO: delete plot_repr
126134
}
127135
}
128136
}
137+
138+
void WholeScene::clear_scene()
139+
{
140+
selected_subd_repr = 0;
141+
plot_reprs.clear();
142+
clear();
143+
}
144+
145+
Crop* WholeScene::getCropAtPos(QPointF scene_pos)
146+
{
147+
Crop* p_current_crop = 0;
148+
for (PlotRepresentation* plot_repr: plot_reprs)
149+
{
150+
for (SubdRepresentation* subd_repr: plot_repr->subd_reprs)
151+
{
152+
QRectF subd_rect = subd_repr->sceneTransform().mapRect(subd_repr->boundingRect());
153+
if (subd_rect.contains(scene_pos))
154+
{
155+
if (subd_repr->get_pcrop() != &NullCrop)
156+
{
157+
p_current_crop = subd_repr->get_pcrop();
158+
}
159+
return p_current_crop;
160+
}
161+
}
162+
}
163+
return p_current_crop;
164+
}
165+
166+
void WholeScene::drawCropSelection()
167+
{
168+
if (selected_crop && !selected_subd_repr)
169+
{
170+
for (PlotRepresentation* plot_repr: plot_reprs)
171+
{
172+
for (SubdRepresentation* subd_repr: plot_repr->subd_reprs)
173+
{
174+
if (subd_repr->get_pcrop() == selected_crop)
175+
{
176+
selected_subd_repr = subd_repr;
177+
break;
178+
}
179+
}
180+
}
181+
}
182+
if (selected_subd_repr)
183+
{
184+
QPen selected_pen;
185+
float scene_scale = views()[0]->transform().m11();
186+
float pen_width = 4 / scene_scale;
187+
selected_pen.setWidthF(pen_width);
188+
//selected_pen.setColor(QColor("#444444"));
189+
selected_subd_repr->setZValue(1);
190+
selected_subd_repr->setPen(selected_pen);
191+
}
192+
}
193+
194+
void WholeScene::removeCropSelection()
195+
{
196+
if (selected_subd_repr)
197+
{
198+
selected_subd_repr->setZValue(0);
199+
selected_subd_repr->setPen(QPen());
200+
}
201+
}
202+
203+
void WholeScene::selectCrop(Crop* p_crop)
204+
{
205+
removeCropSelection();
206+
selected_subd_repr = 0;
207+
selected_crop = p_crop;
208+
drawCropSelection();
209+
}
210+
211+
void WholeScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
212+
{
213+
QPointF clic_point = event->scenePos();
214+
Crop* p_current_crop = getCropAtPos(clic_point);
215+
selectCrop(p_current_crop);
216+
emit crop_selected(selected_crop);
217+
}

0 commit comments

Comments
 (0)