Clean up coding style

Just to keep me sane.

The code still shows compile time warnings which are the areas where we
need to complete things to actually do the deco calculations, etc. But now
seemed a good time to merge it into master as hopefully more people can
contribute.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-06-23 13:09:29 -07:00
parent 31449dca22
commit 106775b196
2 changed files with 76 additions and 77 deletions

View file

@ -7,7 +7,7 @@
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0) DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0)
{ {
setMouseTracking(true); setMouseTracking(true);
setScene( new QGraphicsScene()); setScene(new QGraphicsScene());
scene()->setSceneRect(0,0,100,100); scene()->setSceneRect(0,0,100,100);
verticalLine = new QGraphicsLineItem(0,0,0, 100); verticalLine = new QGraphicsLineItem(0,0,0, 100);
@ -22,7 +22,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
timeLine->setMinimum(0); timeLine->setMinimum(0);
timeLine->setMaximum(20); timeLine->setMaximum(20);
timeLine->setTickInterval(10); timeLine->setTickInterval(10);
timeLine->setLine( 10, 90, 99, 90); timeLine->setLine(10, 90, 99, 90);
timeLine->setOrientation(Qt::Horizontal); timeLine->setOrientation(Qt::Horizontal);
timeLine->updateTicks(); timeLine->updateTicks();
scene()->addItem(timeLine); scene()->addItem(timeLine);
@ -31,7 +31,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
depthLine->setMinimum(0); depthLine->setMinimum(0);
depthLine->setMaximum(10); depthLine->setMaximum(10);
depthLine->setTickInterval(10); depthLine->setTickInterval(10);
depthLine->setLine( 10, 1, 10, 90); depthLine->setLine(10, 1, 10, 90);
depthLine->setOrientation(Qt::Vertical); depthLine->setOrientation(Qt::Vertical);
depthLine->updateTicks(); depthLine->updateTicks();
scene()->addItem(depthLine); scene()->addItem(depthLine);
@ -50,7 +50,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
connect(plusDepth, SIGNAL(clicked()), this, SLOT(increaseDepth())); connect(plusDepth, SIGNAL(clicked()), this, SLOT(increaseDepth()));
plusTime = new Button(); plusTime = new Button();
plusTime->setPos( 95, 90); plusTime->setPos(95, 90);
scene()->addItem(plusTime); scene()->addItem(plusTime);
connect(plusTime, SIGNAL(clicked()), this, SLOT(increaseTime())); connect(plusTime, SIGNAL(clicked()), this, SLOT(increaseTime()));
} }
@ -68,12 +68,11 @@ void DivePlannerGraphics::increaseTime()
void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event) void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
{ {
QPointF mappedPos = mapToScene(event->pos()); QPointF mappedPos = mapToScene(event->pos());
if(isPointOutOfBoundaries(mappedPos)) if (isPointOutOfBoundaries(mappedPos))
return; return;
if(handles.count() && handles.last()->x() > mappedPos.x()){ if (handles.count() && handles.last()->x() > mappedPos.x())
return; return;
}
DiveHandler *item = new DiveHandler (); DiveHandler *item = new DiveHandler ();
item->setRect(-5,-5,10,10); item->setRect(-5,-5,10,10);
@ -81,46 +80,46 @@ void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
double xpos = timeLine->posAtValue(rint(timeLine->valueAt(mappedPos))); double xpos = timeLine->posAtValue(rint(timeLine->valueAt(mappedPos)));
double ypos = depthLine->posAtValue(rint(depthLine->valueAt(mappedPos))); double ypos = depthLine->posAtValue(rint(depthLine->valueAt(mappedPos)));
item->setPos( QPointF(xpos, ypos)); item->setPos(QPointF(xpos, ypos));
scene()->addItem(item); scene()->addItem(item);
handles << item; handles << item;
if (lines.empty()){ if (lines.empty()) {
double xpos = timeLine->posAtValue(0); double xpos = timeLine->posAtValue(0);
double ypos = depthLine->posAtValue(0); double ypos = depthLine->posAtValue(0);
QGraphicsLineItem *first = new QGraphicsLineItem(xpos,ypos, mappedPos.x(), mappedPos.y()); QGraphicsLineItem *first = new QGraphicsLineItem(xpos,ypos, mappedPos.x(), mappedPos.y());
item->from = first; item->from = first;
lines.push_back(first); lines.push_back(first);
create_deco_stop(); createDecoStops();
scene()->addItem(first); scene()->addItem(first);
}else{ } else {
clear_generated_deco(); clearGeneratedDeco();
DiveHandler *prevHandle = handles.at( handles.count()-2); DiveHandler *prevHandle = handles.at(handles.count()-2);
QGraphicsLineItem *line = new QGraphicsLineItem(prevHandle->x(), prevHandle->y(), item->x(), item->y()); QGraphicsLineItem *line = new QGraphicsLineItem(prevHandle->x(), prevHandle->y(), item->x(), item->y());
prevHandle->to = line; prevHandle->to = line;
item->from = line; item->from = line;
lines.push_back(line); lines.push_back(line);
scene()->addItem(line); scene()->addItem(line);
create_deco_stop(); createDecoStops();
} }
item->time = (timeLine->valueAt(mappedPos)); item->time = (timeLine->valueAt(mappedPos));
item->depth = (depthLine->valueAt(mappedPos)); item->depth = (depthLine->valueAt(mappedPos));
} }
void DivePlannerGraphics::clear_generated_deco() void DivePlannerGraphics::clearGeneratedDeco()
{ {
for(int i = handles.count(); i <= lines.count(); i++){ for (int i = handles.count(); i <= lines.count(); i++) {
scene()->removeItem(lines.last()); scene()->removeItem(lines.last());
delete lines.last(); delete lines.last();
lines.removeLast(); lines.removeLast();
} }
} }
void DivePlannerGraphics::create_deco_stop() void DivePlannerGraphics::createDecoStops()
{ {
// 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
Q_FOREACH(DiveHandler *h, handles){ Q_FOREACH(DiveHandler *h, handles) {
// use this somewhere. // use this somewhere.
h->time; h->time;
h->depth; h->depth;
@ -134,9 +133,9 @@ void DivePlannerGraphics::create_deco_stop()
timeLine->updateTicks(); timeLine->updateTicks();
// Re-position the user generated dive handlers // Re-position the user generated dive handlers
Q_FOREACH(DiveHandler *h, handles){ Q_FOREACH(DiveHandler *h, handles) {
// uncomment this as soon as the posAtValue is implemented. // uncomment this as soon as the posAtValue is implemented.
// h->setPos( timeLine->posAtValue(h->time), // h->setPos(timeLine->posAtValue(h->time),
// depthLine->posAtValue(h->depth)); // depthLine->posAtValue(h->depth));
} }
@ -151,13 +150,13 @@ void DivePlannerGraphics::create_deco_stop()
void DivePlannerGraphics::resizeEvent(QResizeEvent* event) void DivePlannerGraphics::resizeEvent(QResizeEvent* event)
{ {
QGraphicsView::resizeEvent(event); QGraphicsView::resizeEvent(event);
fitInView(sceneRect(), Qt::KeepAspectRatio); fitInView(sceneRect(), Qt::KeepAspectRatio);
} }
void DivePlannerGraphics::showEvent(QShowEvent* event) void DivePlannerGraphics::showEvent(QShowEvent* event)
{ {
QGraphicsView::showEvent(event); QGraphicsView::showEvent(event);
fitInView(sceneRect(), Qt::KeepAspectRatio); fitInView(sceneRect(), Qt::KeepAspectRatio);
} }
@ -169,20 +168,20 @@ void DivePlannerGraphics::mouseMoveEvent(QMouseEvent* event)
verticalLine->setLine(mappedPos.x(), 0, mappedPos.x(), 100); verticalLine->setLine(mappedPos.x(), 0, mappedPos.x(), 100);
horizontalLine->setLine(0, mappedPos.y(), 100, mappedPos.y()); horizontalLine->setLine(0, mappedPos.y(), 100, mappedPos.y());
depthString->setText(QString::number( (int) depthLine->valueAt(mappedPos)) + "m" ); depthString->setText(QString::number((int) depthLine->valueAt(mappedPos)) + "m" );
depthString->setPos(0, mappedPos.y()); depthString->setPos(0, mappedPos.y());
timeString->setText(QString::number( (int) timeLine->valueAt(mappedPos)) + "min"); timeString->setText(QString::number((int) timeLine->valueAt(mappedPos)) + "min");
timeString->setPos(mappedPos.x()+1, 90); timeString->setPos(mappedPos.x()+1, 90);
if(activeDraggedHandler) if (activeDraggedHandler)
moveActiveHandler(mappedPos); moveActiveHandler(mappedPos);
if (!handles.count()) if (!handles.count())
return; return;
if (handles.last()->x() > mappedPos.x()){ if (handles.last()->x() > mappedPos.x()) {
verticalLine->setPen( QPen(QBrush(Qt::red), 0, Qt::SolidLine)); verticalLine->setPen(QPen(QBrush(Qt::red), 0, Qt::SolidLine));
horizontalLine->setPen( QPen(QBrush(Qt::red), 0, Qt::SolidLine)); horizontalLine->setPen(QPen(QBrush(Qt::red), 0, Qt::SolidLine));
}else{ } else {
verticalLine->setPen(QPen(Qt::DotLine)); verticalLine->setPen(QPen(Qt::DotLine));
horizontalLine->setPen(QPen(Qt::DotLine)); horizontalLine->setPen(QPen(Qt::DotLine));
} }
@ -198,41 +197,41 @@ void DivePlannerGraphics::moveActiveHandler(const QPointF& pos)
QPointF newPos(xpos, ypos); QPointF newPos(xpos, ypos);
bool moveLines = false;; bool moveLines = false;;
// do not allow it to move between handlers. // do not allow it to move between handlers.
if (handles.count() > 1){ if (handles.count() > 1) {
if (idx == 0 ){ // first if (idx == 0 ) { // first
if (newPos.x() < handles[1]->x()){ if (newPos.x() < handles[1]->x()) {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
moveLines = true; moveLines = true;
} }
}else if (idx == handles.count()-1){ // last } else if (idx == handles.count()-1) { // last
if (newPos.x() > handles[idx-1]->x()){ if (newPos.x() > handles[idx-1]->x()) {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
moveLines = true; moveLines = true;
} }
}else{ // middle } else { // middle
if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()){ if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()) {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
moveLines = true; moveLines = true;
} }
} }
}else{ } else {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
moveLines = true; moveLines = true;
} }
if (moveLines){ if (moveLines) {
if (activeDraggedHandler->from){ if (activeDraggedHandler->from) {
QLineF f = activeDraggedHandler->from->line(); QLineF f = activeDraggedHandler->from->line();
activeDraggedHandler->from->setLine(f.x1(), f.y1(), newPos.x(), newPos.y()); activeDraggedHandler->from->setLine(f.x1(), f.y1(), newPos.x(), newPos.y());
} }
if (activeDraggedHandler->to){ if (activeDraggedHandler->to) {
QLineF f = activeDraggedHandler->to->line(); QLineF f = activeDraggedHandler->to->line();
activeDraggedHandler->to->setLine(newPos.x(), newPos.y(), f.x2(), f.y2()); activeDraggedHandler->to->setLine(newPos.x(), newPos.y(), f.x2(), f.y2());
} }
if(activeDraggedHandler == handles.last()){ if (activeDraggedHandler == handles.last()) {
clear_generated_deco(); clearGeneratedDeco();
create_deco_stop(); createDecoStops();
} }
} }
} }
@ -242,10 +241,10 @@ bool DivePlannerGraphics::isPointOutOfBoundaries(const QPointF& point)
double xpos = timeLine->valueAt(point); double xpos = timeLine->valueAt(point);
double ypos = depthLine->valueAt(point); double ypos = depthLine->valueAt(point);
if (xpos > timeLine->maximum() if (xpos > timeLine->maximum() ||
|| xpos < timeLine->minimum() xpos < timeLine->minimum() ||
|| ypos > depthLine->maximum() ypos > depthLine->maximum() ||
|| ypos < depthLine->minimum()) ypos < depthLine->minimum())
{ {
return true; return true;
} }
@ -255,8 +254,8 @@ bool DivePlannerGraphics::isPointOutOfBoundaries(const QPointF& point)
void DivePlannerGraphics::mousePressEvent(QMouseEvent* event) void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
{ {
QPointF mappedPos = mapToScene(event->pos()); QPointF mappedPos = mapToScene(event->pos());
Q_FOREACH(QGraphicsItem *item, scene()->items(mappedPos)){ Q_FOREACH(QGraphicsItem *item, scene()->items(mappedPos)) {
if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)){ if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)) {
activeDraggedHandler = h; activeDraggedHandler = h;
activeDraggedHandler->setBrush(Qt::red); activeDraggedHandler->setBrush(Qt::red);
} }
@ -266,7 +265,7 @@ void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event) void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
{ {
if (activeDraggedHandler){ if (activeDraggedHandler) {
QPointF mappedPos = mapToScene(event->pos()); QPointF mappedPos = mapToScene(event->pos());
activeDraggedHandler->time = (timeLine->valueAt(mappedPos)); activeDraggedHandler->time = (timeLine->valueAt(mappedPos));
activeDraggedHandler->depth = (depthLine->valueAt(mappedPos)); activeDraggedHandler->depth = (depthLine->valueAt(mappedPos));
@ -302,17 +301,17 @@ void Ruler::updateTicks()
{ {
qDeleteAll(ticks); qDeleteAll(ticks);
QLineF m = line(); QLineF m = line();
if(orientation == Qt::Horizontal){ if (orientation == Qt::Horizontal) {
double steps = (max - min) / interval; double steps = (max - min) / interval;
double stepSize = (m.x2() - m.x1()) / steps; double stepSize = (m.x2() - m.x1()) / steps;
for(qreal pos = m.x1(); pos < m.x2(); pos += stepSize){ for (qreal pos = m.x1(); pos < m.x2(); pos += stepSize) {
QGraphicsLineItem *l = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + 1, this); QGraphicsLineItem *l = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + 1, this);
} }
}else{ } else {
double steps = (max - min) / interval; double steps = (max - min) / interval;
double stepSize = (m.y2() - m.y1()) / steps; double stepSize = (m.y2() - m.y1()) / steps;
for(qreal pos = m.y1(); pos < m.y2(); pos += stepSize){ for (qreal pos = m.y1(); pos < m.y2(); pos += stepSize) {
QGraphicsLineItem *l = new QGraphicsLineItem(m.x1(), pos, m.x1() - 1, pos, this); QGraphicsLineItem *l = new QGraphicsLineItem(m.x1(), pos, m.x1() - 1, pos, this);
} }
} }
@ -326,9 +325,9 @@ void Ruler::setTickInterval(double i)
qreal Ruler::valueAt(const QPointF& p) qreal Ruler::valueAt(const QPointF& p)
{ {
QLineF m = line(); QLineF m = line();
double retValue = orientation == Qt::Horizontal double retValue = orientation == Qt::Horizontal ?
? max * (p.x() - m.x1()) / (m.x2() - m.x1()) max * (p.x() - m.x1()) / (m.x2() - m.x1()) :
: max * (p.y() - m.y1()) / (m.y2() - m.y1()); max * (p.y() - m.y1()) / (m.y2() - m.y1());
return retValue; return retValue;
} }
@ -337,13 +336,13 @@ qreal Ruler::posAtValue(qreal value)
QLineF m = line(); QLineF m = line();
double size = max - min; double size = max - min;
double percent = value / size; double percent = value / size;
double realSize = orientation == Qt::Horizontal double realSize = orientation == Qt::Horizontal ?
? m.x2() - m.x1() m.x2() - m.x1() :
: m.y2() - m.y1(); m.y2() - m.y1();
double retValue = realSize * percent; double retValue = realSize * percent;
retValue = (orientation == Qt::Horizontal) retValue = (orientation == Qt::Horizontal) ?
? retValue + m.x1() retValue + m.x1() :
: retValue + m.y1(); retValue + m.y1();
return retValue; return retValue;
} }
@ -375,7 +374,7 @@ double Ruler::minimum() const
Button::Button(QObject* parent): QObject(parent), QGraphicsPixmapItem() Button::Button(QObject* parent): QObject(parent), QGraphicsPixmapItem()
{ {
setPixmap(QPixmap(":plus").scaled(20,20)); setPixmap(QPixmap(":plus").scaled(20,20));
setFlag(ItemIgnoresTransformations); setFlag(ItemIgnoresTransformations);
} }

View file

@ -10,14 +10,14 @@ class Button : public QObject, public QGraphicsPixmapItem {
public: public:
explicit Button(QObject* parent = 0); explicit Button(QObject* parent = 0);
protected: protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent* event); virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
signals: signals:
void clicked(); void clicked();
}; };
class DiveHandler : public QGraphicsEllipseItem{ class DiveHandler : public QGraphicsEllipseItem{
public: public:
DiveHandler(); DiveHandler();
QGraphicsLineItem *from; QGraphicsLineItem *from;
QGraphicsLineItem *to; QGraphicsLineItem *to;
qreal time; qreal time;
@ -26,7 +26,7 @@ public:
class Ruler : public QGraphicsLineItem{ class Ruler : public QGraphicsLineItem{
public: public:
Ruler(); Ruler();
void setMinimum(double minimum); void setMinimum(double minimum);
void setMaximum(double maximum); void setMaximum(double maximum);
void setTickInterval(double interval); void setTickInterval(double interval);
@ -52,15 +52,15 @@ class DivePlannerGraphics : public QGraphicsView {
public: public:
DivePlannerGraphics(QWidget* parent = 0); DivePlannerGraphics(QWidget* parent = 0);
protected: protected:
virtual void mouseDoubleClickEvent(QMouseEvent* event); virtual void mouseDoubleClickEvent(QMouseEvent* event);
virtual void showEvent(QShowEvent* event); virtual void showEvent(QShowEvent* event);
virtual void resizeEvent(QResizeEvent* event); virtual void resizeEvent(QResizeEvent* event);
virtual void mouseMoveEvent(QMouseEvent* event); virtual void mouseMoveEvent(QMouseEvent* event);
virtual void mousePressEvent(QMouseEvent* event); virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event); virtual void mouseReleaseEvent(QMouseEvent* event);
void clear_generated_deco(); void clearGeneratedDeco();
void create_deco_stop(); void createDecoStops();
bool isPointOutOfBoundaries(const QPointF& point); bool isPointOutOfBoundaries(const QPointF& point);
private slots: private slots:
@ -69,7 +69,7 @@ private slots:
private: private:
void moveActiveHandler(const QPointF& pos); void moveActiveHandler(const QPointF& pos);
QList<QGraphicsLineItem*> lines; QList<QGraphicsLineItem*> lines;
QList<DiveHandler *> handles; QList<DiveHandler *> handles;
QGraphicsLineItem *verticalLine; QGraphicsLineItem *verticalLine;