Moved a lot of code to the Model, cleaning the interface.

Moved a lot of code that handled the positioning of the
DiveHandles on the interface to the model. there are a
few bugs left ( regressions ) that I will fix in the
next commits. With this commit an edition of the points
on the widget will trigger a repaint of the planner profile.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-08-26 16:34:06 -03:00
parent de2f5d9e60
commit 367fc6e1c3
2 changed files with 63 additions and 40 deletions

View file

@ -25,9 +25,7 @@
#define MAX_DEEPNESS 150 #define MAX_DEEPNESS 150
#define MIN_DEEPNESS 40 #define MIN_DEEPNESS 40
bool handlerLessThenMinutes(DiveHandler *d1, DiveHandler *d2){ static DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
return d1->sec < d2->sec;
}
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0) DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0)
{ {
@ -150,14 +148,11 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
void DivePlannerGraphics::pointInserted(const QModelIndex& parent, int start , int end) void DivePlannerGraphics::pointInserted(const QModelIndex& parent, int start , int end)
{ {
qDebug() << "Adicionou"; qDebug() << "Adicionou";
divedatapoint point = DivePlannerPointsModel::instance()->at(start); divedatapoint point = plannerModel->at(start);
DiveHandler *item = new DiveHandler (); DiveHandler *item = new DiveHandler ();
double xpos = timeLine->posAtValue(point.time); double xpos = timeLine->posAtValue(point.time / 60);
double ypos = depthLine->posAtValue(point.depth); double ypos = depthLine->posAtValue(point.depth / 1000);
item->sec = point.time * 60;
item->mm = point.depth * 1000;
item->setPos(QPointF(xpos, ypos)); item->setPos(QPointF(xpos, ypos));
qDebug() << xpos << ypos;
scene()->addItem(item); scene()->addItem(item);
handles << item; handles << item;
@ -176,12 +171,13 @@ void DivePlannerGraphics::keyDownAction()
if(scene()->selectedItems().count()){ if(scene()->selectedItems().count()){
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){ Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){ if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
if (handler->mm / 1000 >= depthLine->maximum()) int row = handles.indexOf(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.depth / 1000 >= depthLine->maximum())
continue; continue;
handler->mm += 1000; dp.depth += 1000;
double ypos = depthLine->posAtValue(handler->mm / 1000); plannerModel->editStop(row, dp);
handler->setPos(handler->pos().x(), ypos);
} }
} }
createDecoStops(); createDecoStops();
@ -192,12 +188,14 @@ void DivePlannerGraphics::keyUpAction()
{ {
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){ Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){ if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
if (handler->mm / 1000 <= 0) int row = handles.indexOf(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.depth / 1000 <= 0)
continue; continue;
handler->mm -= 1000; dp.depth -= 1000;
double ypos = depthLine->posAtValue(handler->mm / 1000); plannerModel->editStop(row, dp);
handler->setPos(handler->pos().x(), ypos);
} }
} }
createDecoStops(); createDecoStops();
@ -207,12 +205,15 @@ void DivePlannerGraphics::keyLeftAction()
{ {
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){ Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){ if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
if (handler->sec / 60 <= 0) int row = handles.indexOf(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.time / 60 <= 0)
continue; continue;
// don't overlap positions. // don't overlap positions.
// maybe this is a good place for a 'goto'? // maybe this is a good place for a 'goto'?
double xpos = timeLine->posAtValue((handler->sec - 60) / 60); double xpos = timeLine->posAtValue((dp.time - 60) / 60);
bool nextStep = false; bool nextStep = false;
Q_FOREACH(DiveHandler *h, handles){ Q_FOREACH(DiveHandler *h, handles){
if (h->pos().x() == xpos){ if (h->pos().x() == xpos){
@ -223,8 +224,8 @@ void DivePlannerGraphics::keyLeftAction()
if(nextStep) if(nextStep)
continue; continue;
handler->sec -= 60; dp.time -= 60;
handler->setPos(xpos, handler->pos().y()); plannerModel->editStop(row, dp);
} }
} }
createDecoStops(); createDecoStops();
@ -234,12 +235,14 @@ void DivePlannerGraphics::keyRightAction()
{ {
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){ Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){ if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
if (handler->sec / 60 >= timeLine->maximum()) int row = handles.indexOf(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.time / 60 >= timeLine->maximum())
continue; continue;
// don't overlap positions. // don't overlap positions.
// maybe this is a good place for a 'goto'? // maybe this is a good place for a 'goto'?
double xpos = timeLine->posAtValue((handler->sec + 60) / 60); double xpos = timeLine->posAtValue((dp.time + 60) / 60);
bool nextStep = false; bool nextStep = false;
Q_FOREACH(DiveHandler *h, handles){ Q_FOREACH(DiveHandler *h, handles){
if (h->pos().x() == xpos){ if (h->pos().x() == xpos){
@ -250,8 +253,8 @@ void DivePlannerGraphics::keyRightAction()
if(nextStep) if(nextStep)
continue; continue;
handler->sec += 60; dp.time += 60;
handler->setPos(xpos, handler->pos().y()); plannerModel->editStop(row, dp);
} }
} createDecoStops(); } createDecoStops();
} }
@ -367,7 +370,7 @@ void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
int minutes = rint(timeLine->valueAt(mappedPos)); int minutes = rint(timeLine->valueAt(mappedPos));
int meters = rint(depthLine->valueAt(mappedPos)); int meters = rint(depthLine->valueAt(mappedPos));
DivePlannerPointsModel::instance()->addStop(meters, minutes, tr("Air"), 0); plannerModel->addStop(meters * 1000, minutes * 60, tr("Air"), 0);
} }
void DivePlannerGraphics::prepareSelectGas() void DivePlannerGraphics::prepareSelectGas()
@ -389,7 +392,8 @@ void DivePlannerGraphics::createDecoStops()
{ {
qDeleteAll(lines); qDeleteAll(lines);
lines.clear(); lines.clear();
qSort(handles.begin(), handles.end(), handlerLessThenMinutes); //TODO: fix.
//qSort(handles.begin(), handles.end(), handlerLessThenMinutes);
// This needs to be done in the following steps: // This needs to be done in the following steps:
// Get the user-input and calculate the dive info // Get the user-input and calculate the dive info
@ -402,9 +406,12 @@ void DivePlannerGraphics::createDecoStops()
diveplan.gflow = 30; diveplan.gflow = 30;
diveplan.gfhigh = 70; diveplan.gfhigh = 70;
diveplan.surface_pressure = 1013; diveplan.surface_pressure = 1013;
#if 0
DiveHandler *lastH = NULL; DiveHandler *lastH = NULL;
Q_FOREACH(DiveHandler *h, handles) { Q_FOREACH(DiveHandler *h, handles) {
// these values need to come from the planner UI, eventually // these values need to come from the planner UI, eventually
int o2 = 209; int o2 = 209;
int he = 0; int he = 0;
int po2 = 0; int po2 = 0;
@ -414,6 +421,22 @@ void DivePlannerGraphics::createDecoStops()
dp->entered = TRUE; dp->entered = TRUE;
qDebug("time %d, depth %d", h->sec, h->mm); qDebug("time %d, depth %d", h->sec, h->mm);
} }
#else
int rowCount = plannerModel->rowCount();
int lastIndex = -1;
for(int i = 0; i < rowCount; i++){
// TODO: Dirk, the values already exists on the interface in the form of strings, can you
// give me a bit of help here?
divedatapoint thisPoint = plannerModel->at(i);
int o2 = 209;
int he = 0;
int po2 = 0;
int deltaT = lastIndex != -1 ? thisPoint.time - plannerModel->at(lastIndex).time : thisPoint.time;
lastIndex = i;
dp = plan_add_segment(&diveplan, deltaT, thisPoint.depth, o2, he, po2);
}
#endif
#if DEBUG_PLAN #if DEBUG_PLAN
dump_plan(&diveplan); dump_plan(&diveplan);
#endif #endif
@ -438,8 +461,10 @@ void DivePlannerGraphics::createDecoStops()
} }
// Re-position the user generated dive handlers // Re-position the user generated dive handlers
Q_FOREACH(DiveHandler *h, handles){ for(int i = 0; i < plannerModel->rowCount(); i++){
h->setPos(timeLine->posAtValue(h->sec / 60), depthLine->posAtValue(h->mm / 1000)); divedatapoint dp = plannerModel->at(i);
DiveHandler *h = handles.at(i);
h->setPos(timeLine->posAtValue(dp.time / 60), depthLine->posAtValue(dp.depth / 1000));
} }
int gasCount = gases.count(); int gasCount = gases.count();
@ -610,12 +635,12 @@ void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
} }
int pos = handles.indexOf(activeDraggedHandler); int pos = handles.indexOf(activeDraggedHandler);
divedatapoint data = DivePlannerPointsModel::instance()->at(pos); divedatapoint data = plannerModel->at(pos);
data.depth = rint(depthLine->valueAt(mappedPos)); data.depth = rint(depthLine->valueAt(mappedPos));
data.time = rint(timeLine->valueAt(mappedPos)); data.time = rint(timeLine->valueAt(mappedPos));
DivePlannerPointsModel::instance()->editStop(pos, data); plannerModel->editStop(pos, data);
activeDraggedHandler->setBrush(QBrush(Qt::white)); activeDraggedHandler->setBrush(QBrush(Qt::white));
activeDraggedHandler->setPos(QPointF(xpos, ypos)); activeDraggedHandler->setPos(QPointF(xpos, ypos));
@ -812,37 +837,37 @@ DivePlannerWidget::DivePlannerWidget(QWidget* parent, Qt::WindowFlags f): QWidge
void DivePlannerWidget::startTimeChanged(const QTime& time) void DivePlannerWidget::startTimeChanged(const QTime& time)
{ {
DivePlannerPointsModel::instance()->setStartTime(time); plannerModel->setStartTime(time);
} }
void DivePlannerWidget::atmPressureChanged(const QString& pressure) void DivePlannerWidget::atmPressureChanged(const QString& pressure)
{ {
DivePlannerPointsModel::instance()->setSurfacePressure(pressure.toInt()); plannerModel->setSurfacePressure(pressure.toInt());
} }
void DivePlannerWidget::bottomSacChanged(const QString& bottomSac) void DivePlannerWidget::bottomSacChanged(const QString& bottomSac)
{ {
DivePlannerPointsModel::instance()->setBottomSac(bottomSac.toInt()); plannerModel->setBottomSac(bottomSac.toInt());
} }
void DivePlannerWidget::decoSacChanged(const QString& decosac) void DivePlannerWidget::decoSacChanged(const QString& decosac)
{ {
DivePlannerPointsModel::instance()->setDecoSac(decosac.toInt()); plannerModel->setDecoSac(decosac.toInt());
} }
void DivePlannerWidget::gfhighChanged(const QString& gfhigh) void DivePlannerWidget::gfhighChanged(const QString& gfhigh)
{ {
DivePlannerPointsModel::instance()->setGFHigh(gfhigh.toShort()); plannerModel->setGFHigh(gfhigh.toShort());
} }
void DivePlannerWidget::gflowChanged(const QString& gflow) void DivePlannerWidget::gflowChanged(const QString& gflow)
{ {
DivePlannerPointsModel::instance()->setGFLow(gflow.toShort()); plannerModel->setGFLow(gflow.toShort());
} }
void DivePlannerWidget::lastStopChanged(bool checked) void DivePlannerWidget::lastStopChanged(bool checked)
{ {
DivePlannerPointsModel::instance()->setLastStop6m(checked); plannerModel->setLastStop6m(checked);
} }
int DivePlannerPointsModel::columnCount(const QModelIndex& parent) const int DivePlannerPointsModel::columnCount(const QModelIndex& parent) const

View file

@ -68,8 +68,6 @@ private:
class DiveHandler : public QGraphicsEllipseItem{ class DiveHandler : public QGraphicsEllipseItem{
public: public:
DiveHandler(); DiveHandler();
int sec;
int mm;
protected: protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event); void mousePressEvent(QGraphicsSceneMouseEvent* event);
}; };