2013-06-20 15:33:26 +00:00
|
|
|
#include "diveplanner.h"
|
2013-06-24 01:04:35 +00:00
|
|
|
#include "../dive.h"
|
2013-06-21 19:28:17 +00:00
|
|
|
#include <cmath>
|
2013-06-20 15:37:41 +00:00
|
|
|
#include <QMouseEvent>
|
2013-06-20 16:39:41 +00:00
|
|
|
#include <QDebug>
|
2013-06-27 19:45:58 +00:00
|
|
|
#include <QGraphicsWidget>
|
|
|
|
#include <QGraphicsProxyWidget>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
#include "ui_diveplanner.h"
|
2013-06-27 19:45:58 +00:00
|
|
|
#include "mainwindow.h"
|
2013-06-20 15:33:26 +00:00
|
|
|
|
2013-06-27 14:10:03 +00:00
|
|
|
#define TIME_INITIAL_MAX 30
|
|
|
|
|
2013-06-27 13:38:12 +00:00
|
|
|
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0),
|
|
|
|
lastValidPos(0.0, 0.0)
|
2013-06-20 15:33:26 +00:00
|
|
|
{
|
2013-06-20 16:53:12 +00:00
|
|
|
setMouseTracking(true);
|
2013-06-23 20:09:29 +00:00
|
|
|
setScene(new QGraphicsScene());
|
2013-06-20 15:37:41 +00:00
|
|
|
scene()->setSceneRect(0,0,100,100);
|
2013-06-20 16:53:12 +00:00
|
|
|
|
|
|
|
verticalLine = new QGraphicsLineItem(0,0,0, 100);
|
|
|
|
verticalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
scene()->addItem(verticalLine);
|
|
|
|
|
|
|
|
horizontalLine = new QGraphicsLineItem(0,0,100,0);
|
|
|
|
horizontalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
scene()->addItem(horizontalLine);
|
2013-06-20 19:48:24 +00:00
|
|
|
|
|
|
|
timeLine = new Ruler();
|
|
|
|
timeLine->setMinimum(0);
|
2013-06-27 14:10:03 +00:00
|
|
|
timeLine->setMaximum(TIME_INITIAL_MAX);
|
2013-06-20 19:48:24 +00:00
|
|
|
timeLine->setTickInterval(10);
|
2013-06-23 20:09:29 +00:00
|
|
|
timeLine->setLine(10, 90, 99, 90);
|
2013-06-20 19:48:24 +00:00
|
|
|
timeLine->setOrientation(Qt::Horizontal);
|
|
|
|
timeLine->updateTicks();
|
|
|
|
scene()->addItem(timeLine);
|
|
|
|
|
|
|
|
depthLine = new Ruler();
|
|
|
|
depthLine->setMinimum(0);
|
2013-06-24 04:27:52 +00:00
|
|
|
depthLine->setMaximum(100);
|
2013-06-20 19:48:24 +00:00
|
|
|
depthLine->setTickInterval(10);
|
2013-06-23 20:09:29 +00:00
|
|
|
depthLine->setLine(10, 1, 10, 90);
|
2013-06-20 19:48:24 +00:00
|
|
|
depthLine->setOrientation(Qt::Vertical);
|
|
|
|
depthLine->updateTicks();
|
|
|
|
scene()->addItem(depthLine);
|
2013-06-20 20:54:36 +00:00
|
|
|
|
|
|
|
timeString = new QGraphicsSimpleTextItem();
|
|
|
|
timeString->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
|
|
|
scene()->addItem(timeString);
|
|
|
|
|
|
|
|
depthString = new QGraphicsSimpleTextItem();
|
|
|
|
depthString->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
|
|
|
scene()->addItem(depthString);
|
2013-06-21 19:44:38 +00:00
|
|
|
|
|
|
|
plusDepth = new Button();
|
2013-06-27 19:45:58 +00:00
|
|
|
plusDepth->setPixmap(QPixmap(":plus"));
|
2013-06-21 19:44:38 +00:00
|
|
|
plusDepth->setPos(15, 1);
|
|
|
|
scene()->addItem(plusDepth);
|
2013-06-21 19:51:13 +00:00
|
|
|
connect(plusDepth, SIGNAL(clicked()), this, SLOT(increaseDepth()));
|
2013-06-21 19:44:38 +00:00
|
|
|
|
|
|
|
plusTime = new Button();
|
2013-06-27 19:45:58 +00:00
|
|
|
plusTime->setPixmap(QPixmap(":plus"));
|
2013-06-23 20:09:29 +00:00
|
|
|
plusTime->setPos(95, 90);
|
2013-06-21 19:44:38 +00:00
|
|
|
scene()->addItem(plusTime);
|
2013-06-21 19:51:13 +00:00
|
|
|
connect(plusTime, SIGNAL(clicked()), this, SLOT(increaseTime()));
|
2013-06-27 19:45:58 +00:00
|
|
|
|
|
|
|
okBtn = new Button();
|
|
|
|
okBtn->setText(tr("Ok"));
|
|
|
|
okBtn->setPos(1, 95);
|
|
|
|
scene()->addItem(okBtn);
|
|
|
|
connect(okBtn, SIGNAL(clicked()), this, SLOT(okClicked()));
|
|
|
|
|
|
|
|
cancelBtn = new Button();
|
|
|
|
cancelBtn->setText(tr("Cancel"));
|
|
|
|
cancelBtn->setPos(10,95);
|
|
|
|
scene()->addItem(cancelBtn);
|
|
|
|
connect(cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::cancelClicked()
|
|
|
|
{
|
|
|
|
qDebug() << "clicked";
|
|
|
|
mainWindow()->showProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::okClicked()
|
|
|
|
{
|
|
|
|
// todo.
|
2013-06-21 19:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::increaseDepth()
|
|
|
|
{
|
|
|
|
qDebug() << "Increase Depth Clicked";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::increaseTime()
|
|
|
|
{
|
|
|
|
qDebug() << "Increase Time Clicked";
|
2013-06-20 15:33:26 +00:00
|
|
|
}
|
2013-06-20 15:37:41 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
|
2013-06-20 15:37:41 +00:00
|
|
|
{
|
2013-06-20 16:56:28 +00:00
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
2013-06-23 20:09:29 +00:00
|
|
|
if (isPointOutOfBoundaries(mappedPos))
|
2013-06-20 16:56:28 +00:00
|
|
|
return;
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
if (handles.count() && handles.last()->x() > mappedPos.x())
|
2013-06-20 17:20:45 +00:00
|
|
|
return;
|
|
|
|
|
2013-06-20 17:29:32 +00:00
|
|
|
DiveHandler *item = new DiveHandler ();
|
|
|
|
item->setRect(-5,-5,10,10);
|
2013-06-20 16:28:04 +00:00
|
|
|
item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2013-06-24 01:04:35 +00:00
|
|
|
int minutes = rint(timeLine->valueAt(mappedPos));
|
|
|
|
int meters = rint(depthLine->valueAt(mappedPos));
|
|
|
|
item->sec = minutes * 60;
|
|
|
|
item->mm = meters * 1000;
|
|
|
|
double xpos = timeLine->posAtValue(minutes);
|
|
|
|
double ypos = depthLine->posAtValue(meters);
|
2013-06-23 20:09:29 +00:00
|
|
|
item->setPos(QPointF(xpos, ypos));
|
|
|
|
scene()->addItem(item);
|
2013-06-20 16:20:41 +00:00
|
|
|
handles << item;
|
2013-06-26 23:41:39 +00:00
|
|
|
createDecoStops();
|
2013-06-20 16:20:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
void DivePlannerGraphics::clearGeneratedDeco()
|
2013-06-20 16:20:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
void DivePlannerGraphics::createDecoStops()
|
2013-06-20 16:20:41 +00:00
|
|
|
{
|
2013-06-26 23:41:39 +00:00
|
|
|
qDeleteAll(lines);
|
|
|
|
lines.clear();
|
|
|
|
|
2013-06-20 21:25:03 +00:00
|
|
|
// This needs to be done in the following steps:
|
|
|
|
// Get the user-input and calculate the dive info
|
2013-06-24 01:04:35 +00:00
|
|
|
// Not sure if this is the place to create the diveplan...
|
|
|
|
// We just start with a surface node at time = 0
|
2013-06-24 04:27:52 +00:00
|
|
|
struct diveplan diveplan;
|
2013-06-24 01:04:35 +00:00
|
|
|
struct divedatapoint *dp = create_dp(0, 0, 209, 0, 0);
|
|
|
|
dp->entered = TRUE;
|
2013-06-24 04:27:52 +00:00
|
|
|
diveplan.dp = dp;
|
|
|
|
diveplan.gflow = 30;
|
|
|
|
diveplan.gfhigh = 70;
|
|
|
|
diveplan.surface_pressure = 1013;
|
2013-06-24 01:04:35 +00:00
|
|
|
DiveHandler *lastH = NULL;
|
2013-06-23 20:09:29 +00:00
|
|
|
Q_FOREACH(DiveHandler *h, handles) {
|
2013-06-24 01:04:35 +00:00
|
|
|
// these values need to come from the planner UI, eventually
|
|
|
|
int o2 = 209;
|
|
|
|
int he = 0;
|
|
|
|
int po2 = 0;
|
|
|
|
int deltaT = lastH ? h->sec - lastH->sec : h->sec;
|
|
|
|
lastH = h;
|
2013-06-27 08:56:46 +00:00
|
|
|
dp = plan_add_segment(&diveplan, deltaT, h->mm, o2, he, po2);
|
|
|
|
dp->entered = TRUE;
|
2013-06-24 01:04:35 +00:00
|
|
|
qDebug("time %d, depth %d", h->sec, h->mm);
|
2013-06-20 21:25:03 +00:00
|
|
|
}
|
2013-06-24 01:04:35 +00:00
|
|
|
#if DEBUG_PLAN
|
2013-06-24 04:27:52 +00:00
|
|
|
dump_plan(&diveplan);
|
|
|
|
#endif
|
|
|
|
char *cache = NULL;
|
|
|
|
struct dive *dive = NULL;
|
|
|
|
char *errorString = NULL;
|
|
|
|
plan(&diveplan, &cache, &dive, &errorString);
|
|
|
|
#if DEBUG_PLAN
|
|
|
|
dump_plan(&diveplan);
|
2013-06-24 01:04:35 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-24 04:27:52 +00:00
|
|
|
while(dp->next)
|
|
|
|
dp = dp->next;
|
2013-06-26 23:41:39 +00:00
|
|
|
|
2013-06-27 14:10:03 +00:00
|
|
|
if (timeLine->maximum() < dp->time / 60.0 + 5 ||
|
|
|
|
dp->time / 60.0 + 15 < timeLine->maximum()) {
|
|
|
|
double newMax = fmax(dp->time / 60.0 + 5, TIME_INITIAL_MAX);
|
|
|
|
timeLine->setMaximum(newMax);
|
2013-06-26 22:14:55 +00:00
|
|
|
timeLine->updateTicks();
|
2013-06-27 14:10:03 +00:00
|
|
|
}
|
2013-06-26 22:14:55 +00:00
|
|
|
|
2013-06-20 21:25:03 +00:00
|
|
|
// Re-position the user generated dive handlers
|
2013-06-26 22:14:55 +00:00
|
|
|
Q_FOREACH(DiveHandler *h, handles){
|
|
|
|
h->setPos(timeLine->posAtValue(h->sec / 60), depthLine->posAtValue(h->mm) / 1000);
|
2013-06-20 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 12:54:17 +00:00
|
|
|
// (re-) create the profile with different colors for segments that were
|
|
|
|
// entered vs. segments that were calculated
|
|
|
|
double lastx = timeLine->posAtValue(0);
|
|
|
|
double lasty = depthLine->posAtValue(0);
|
2013-06-24 04:27:52 +00:00
|
|
|
for (dp = diveplan.dp; dp != NULL; dp = dp->next) {
|
2013-06-27 08:56:46 +00:00
|
|
|
double xpos = timeLine->posAtValue(dp->time / 60.0);
|
|
|
|
double ypos = depthLine->posAtValue(dp->depth / 1000.0);
|
|
|
|
QGraphicsLineItem *item = new QGraphicsLineItem(lastx, lasty, xpos, ypos);
|
|
|
|
item->setPen(QPen(QBrush(dp->entered ? Qt::black : Qt::red),0));
|
|
|
|
lastx = xpos;
|
|
|
|
lasty = ypos;
|
|
|
|
scene()->addItem(item);
|
|
|
|
lines << item;
|
2013-06-24 04:27:52 +00:00
|
|
|
}
|
2013-06-27 20:31:10 +00:00
|
|
|
|
|
|
|
deleteTemporaryDivePlan(diveplan.dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::deleteTemporaryDivePlan(divedatapoint* dp)
|
|
|
|
{
|
|
|
|
if (!dp)
|
|
|
|
return;
|
|
|
|
deleteTemporaryDivePlan(dp->next);
|
|
|
|
free(dp);
|
2013-06-20 15:37:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::resizeEvent(QResizeEvent* event)
|
2013-06-20 16:28:04 +00:00
|
|
|
{
|
2013-06-23 20:09:29 +00:00
|
|
|
QGraphicsView::resizeEvent(event);
|
2013-06-27 17:48:03 +00:00
|
|
|
fitInView(sceneRect(), Qt::IgnoreAspectRatio);
|
2013-06-20 16:28:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::showEvent(QShowEvent* event)
|
2013-06-20 16:28:04 +00:00
|
|
|
{
|
2013-06-23 20:09:29 +00:00
|
|
|
QGraphicsView::showEvent(event);
|
2013-06-27 17:48:03 +00:00
|
|
|
fitInView(sceneRect(), Qt::IgnoreAspectRatio);
|
2013-06-20 16:28:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mouseMoveEvent(QMouseEvent* event)
|
2013-06-20 16:53:12 +00:00
|
|
|
{
|
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
2013-06-20 16:56:28 +00:00
|
|
|
if (isPointOutOfBoundaries(mappedPos))
|
2013-06-20 16:53:12 +00:00
|
|
|
return;
|
2013-06-20 17:20:45 +00:00
|
|
|
|
2013-06-20 16:53:12 +00:00
|
|
|
verticalLine->setLine(mappedPos.x(), 0, mappedPos.x(), 100);
|
|
|
|
horizontalLine->setLine(0, mappedPos.y(), 100, mappedPos.y());
|
2013-06-24 00:32:23 +00:00
|
|
|
depthString->setText(QString::number(rint(depthLine->valueAt(mappedPos))) + "m" );
|
2013-06-20 20:54:36 +00:00
|
|
|
depthString->setPos(0, mappedPos.y());
|
2013-06-24 00:32:23 +00:00
|
|
|
timeString->setText(QString::number(rint(timeLine->valueAt(mappedPos))) + "min");
|
2013-06-20 20:54:36 +00:00
|
|
|
timeString->setPos(mappedPos.x()+1, 90);
|
2013-06-20 17:20:45 +00:00
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
if (activeDraggedHandler)
|
2013-06-20 18:15:10 +00:00
|
|
|
moveActiveHandler(mappedPos);
|
2013-06-20 17:20:45 +00:00
|
|
|
if (!handles.count())
|
|
|
|
return;
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
if (handles.last()->x() > mappedPos.x()) {
|
|
|
|
verticalLine->setPen(QPen(QBrush(Qt::red), 0, Qt::SolidLine));
|
|
|
|
horizontalLine->setPen(QPen(QBrush(Qt::red), 0, Qt::SolidLine));
|
|
|
|
} else {
|
2013-06-20 17:20:45 +00:00
|
|
|
verticalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
horizontalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
}
|
2013-06-20 16:53:12 +00:00
|
|
|
}
|
2013-06-20 16:56:28 +00:00
|
|
|
|
2013-06-21 19:28:17 +00:00
|
|
|
void DivePlannerGraphics::moveActiveHandler(const QPointF& pos)
|
2013-06-20 18:15:10 +00:00
|
|
|
{
|
|
|
|
int idx = handles.indexOf(activeDraggedHandler);
|
2013-06-21 19:28:17 +00:00
|
|
|
|
|
|
|
double xpos = timeLine->posAtValue(rint(timeLine->valueAt(pos)));
|
|
|
|
double ypos = depthLine->posAtValue(rint(depthLine->valueAt(pos)));
|
|
|
|
QPointF newPos(xpos, ypos);
|
2013-06-20 18:15:10 +00:00
|
|
|
// do not allow it to move between handlers.
|
2013-06-23 20:09:29 +00:00
|
|
|
if (handles.count() > 1) {
|
|
|
|
if (idx == 0 ) { // first
|
|
|
|
if (newPos.x() < handles[1]->x()) {
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-27 13:38:12 +00:00
|
|
|
lastValidPos = newPos;
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
2013-06-23 20:09:29 +00:00
|
|
|
} else if (idx == handles.count()-1) { // last
|
|
|
|
if (newPos.x() > handles[idx-1]->x()) {
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-27 13:38:12 +00:00
|
|
|
lastValidPos = newPos;
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
2013-06-23 20:09:29 +00:00
|
|
|
} else { // middle
|
|
|
|
if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()) {
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-27 13:38:12 +00:00
|
|
|
lastValidPos = newPos;
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-23 20:09:29 +00:00
|
|
|
} else {
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-27 13:38:12 +00:00
|
|
|
lastValidPos = newPos;
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
2013-06-27 08:56:46 +00:00
|
|
|
qDeleteAll(lines);
|
2013-06-26 23:41:39 +00:00
|
|
|
lines.clear();
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 19:28:17 +00:00
|
|
|
bool DivePlannerGraphics::isPointOutOfBoundaries(const QPointF& point)
|
2013-06-20 16:56:28 +00:00
|
|
|
{
|
2013-06-21 19:07:44 +00:00
|
|
|
double xpos = timeLine->valueAt(point);
|
|
|
|
double ypos = depthLine->valueAt(point);
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
if (xpos > timeLine->maximum() ||
|
|
|
|
xpos < timeLine->minimum() ||
|
|
|
|
ypos > depthLine->maximum() ||
|
|
|
|
ypos < depthLine->minimum())
|
2013-06-20 16:56:28 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
|
2013-06-20 17:46:40 +00:00
|
|
|
{
|
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
2013-06-23 20:09:29 +00:00
|
|
|
Q_FOREACH(QGraphicsItem *item, scene()->items(mappedPos)) {
|
|
|
|
if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)) {
|
2013-06-20 17:46:40 +00:00
|
|
|
activeDraggedHandler = h;
|
|
|
|
activeDraggedHandler->setBrush(Qt::red);
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 19:51:13 +00:00
|
|
|
QGraphicsView::mousePressEvent(event);
|
2013-06-20 17:46:40 +00:00
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
|
2013-06-20 17:46:40 +00:00
|
|
|
{
|
2013-06-23 20:09:29 +00:00
|
|
|
if (activeDraggedHandler) {
|
2013-06-27 13:38:12 +00:00
|
|
|
activeDraggedHandler->sec = rint(timeLine->valueAt(lastValidPos)) * 60;
|
|
|
|
activeDraggedHandler->mm = rint(depthLine->valueAt(lastValidPos)) * 1000;
|
2013-06-20 21:02:01 +00:00
|
|
|
activeDraggedHandler->setBrush(QBrush());
|
2013-06-26 23:41:39 +00:00
|
|
|
createDecoStops();
|
2013-06-20 17:46:40 +00:00
|
|
|
activeDraggedHandler = 0;
|
2013-06-20 20:34:42 +00:00
|
|
|
}
|
2013-06-20 17:46:40 +00:00
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
|
|
|
DiveHandler::DiveHandler(): QGraphicsEllipseItem(), from(0), to(0)
|
|
|
|
{
|
|
|
|
}
|
2013-06-20 18:52:27 +00:00
|
|
|
|
|
|
|
void Ruler::setMaximum(double maximum)
|
|
|
|
{
|
|
|
|
max = maximum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setMinimum(double minimum)
|
|
|
|
{
|
|
|
|
min = minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ruler::Ruler() : orientation(Qt::Horizontal)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setOrientation(Qt::Orientation o)
|
|
|
|
{
|
|
|
|
orientation = o;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::updateTicks()
|
|
|
|
{
|
2013-06-20 19:48:24 +00:00
|
|
|
qDeleteAll(ticks);
|
2013-06-23 20:21:01 +00:00
|
|
|
ticks.clear();
|
2013-06-20 19:48:24 +00:00
|
|
|
QLineF m = line();
|
2013-06-23 20:09:29 +00:00
|
|
|
if (orientation == Qt::Horizontal) {
|
2013-06-20 19:48:24 +00:00
|
|
|
double steps = (max - min) / interval;
|
|
|
|
double stepSize = (m.x2() - m.x1()) / steps;
|
2013-06-23 20:09:29 +00:00
|
|
|
for (qreal pos = m.x1(); pos < m.x2(); pos += stepSize) {
|
2013-06-23 20:21:01 +00:00
|
|
|
ticks.push_back(new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + 1, this));
|
2013-06-20 19:48:24 +00:00
|
|
|
}
|
2013-06-23 20:09:29 +00:00
|
|
|
} else {
|
2013-06-20 19:48:24 +00:00
|
|
|
double steps = (max - min) / interval;
|
|
|
|
double stepSize = (m.y2() - m.y1()) / steps;
|
2013-06-23 20:09:29 +00:00
|
|
|
for (qreal pos = m.y1(); pos < m.y2(); pos += stepSize) {
|
2013-06-23 20:21:01 +00:00
|
|
|
ticks.push_back(new QGraphicsLineItem(m.x1(), pos, m.x1() - 1, pos, this));
|
2013-06-20 19:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 18:52:27 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 19:48:24 +00:00
|
|
|
void Ruler::setTickInterval(double i)
|
2013-06-20 18:52:27 +00:00
|
|
|
{
|
2013-06-20 19:48:24 +00:00
|
|
|
interval = i;
|
2013-06-20 18:52:27 +00:00
|
|
|
}
|
2013-06-20 20:34:42 +00:00
|
|
|
|
|
|
|
qreal Ruler::valueAt(const QPointF& p)
|
|
|
|
{
|
|
|
|
QLineF m = line();
|
2013-06-23 20:09:29 +00:00
|
|
|
double retValue = orientation == Qt::Horizontal ?
|
|
|
|
max * (p.x() - m.x1()) / (m.x2() - m.x1()) :
|
|
|
|
max * (p.y() - m.y1()) / (m.y2() - m.y1());
|
2013-06-21 18:53:20 +00:00
|
|
|
return retValue;
|
2013-06-20 20:34:42 +00:00
|
|
|
}
|
2013-06-20 21:25:03 +00:00
|
|
|
|
|
|
|
qreal Ruler::posAtValue(qreal value)
|
|
|
|
{
|
|
|
|
QLineF m = line();
|
2013-06-21 18:53:20 +00:00
|
|
|
double size = max - min;
|
|
|
|
double percent = value / size;
|
2013-06-23 20:09:29 +00:00
|
|
|
double realSize = orientation == Qt::Horizontal ?
|
|
|
|
m.x2() - m.x1() :
|
|
|
|
m.y2() - m.y1();
|
2013-06-21 18:53:20 +00:00
|
|
|
double retValue = realSize * percent;
|
2013-06-23 20:09:29 +00:00
|
|
|
retValue = (orientation == Qt::Horizontal) ?
|
|
|
|
retValue + m.x1() :
|
|
|
|
retValue + m.y1();
|
2013-06-21 18:53:20 +00:00
|
|
|
return retValue;
|
2013-06-20 21:40:59 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 19:07:44 +00:00
|
|
|
double Ruler::maximum() const
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Ruler::minimum() const
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:45:58 +00:00
|
|
|
Button::Button(QObject* parent): QObject(parent), QGraphicsRectItem()
|
2013-06-21 19:44:38 +00:00
|
|
|
{
|
2013-06-27 19:45:58 +00:00
|
|
|
icon = new QGraphicsPixmapItem(this);
|
|
|
|
text = new QGraphicsSimpleTextItem(this);
|
|
|
|
icon->setPos(0,0);
|
|
|
|
text->setPos(0,0);
|
2013-06-21 19:44:38 +00:00
|
|
|
setFlag(ItemIgnoresTransformations);
|
2013-06-27 19:45:58 +00:00
|
|
|
setPen(QPen(QBrush(Qt::white), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setPixmap(const QPixmap& pixmap)
|
|
|
|
{
|
|
|
|
icon->setPixmap(pixmap.scaled(20,20));
|
|
|
|
if(pixmap.isNull()){
|
|
|
|
icon->hide();
|
|
|
|
}else{
|
|
|
|
icon->show();
|
|
|
|
}
|
|
|
|
setRect(childrenBoundingRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setText(const QString& t)
|
|
|
|
{
|
|
|
|
text->setText(t);
|
|
|
|
if(icon->pixmap().isNull()){
|
|
|
|
icon->hide();
|
|
|
|
text->setPos(0,0);
|
|
|
|
}else{
|
|
|
|
icon->show();
|
|
|
|
text->setPos(22,0);
|
|
|
|
}
|
|
|
|
setRect(childrenBoundingRect());
|
2013-06-21 19:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::mousePressEvent(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
emit clicked();
|
|
|
|
}
|