31 #include <QGraphicsSceneMouseEvent>
32 #include <QGraphicsScene>
36 MapGraphicItem::MapGraphicItem(
internals::Core *core, Configuration *configuration) :
37 core(core), config(configuration), MapRenderTransform(1), maxZoom(17),
38 minZoom(2), zoomReal(0), zoomDigi(0), isSelected(false), rotation(0)
40 dragons.load(QString::fromUtf8(
":/markers/images/grid.jpg"));
41 showTileGridLines=
false;
42 isMouseOverMarker=
false;
43 maprect=QRectF(0,0,1022,680);
47 this->setFlag(ItemIsFocusable);
48 connect(core,SIGNAL(OnNeedInvalidation()),
this,SLOT(Core_OnNeedInvalidation()));
49 connect(core,SIGNAL(OnMapDrag()),
this,SLOT(childPosRefresh()));
50 connect(core,SIGNAL(OnMapZoomChanged()),
this,SLOT(childPosRefresh()));
51 setCacheMode(QGraphicsItem::ItemCoordinateCache);
54 void MapGraphicItem::start()
63 this->prepareGeometryChange();
64 maprect=boundingBox(scene()->sceneRect(),rotation);
65 this->setTransform(QTransform().translate(-(maprect.width()-scene()->width())/2,-(maprect.height()-scene()->height())/2));
66 this->setTransformOriginPoint(maprect.center().x(),maprect.center().y());
67 this->setRotation(rotation);
70 if ((maprect.width() > 1024) || (maprect.height() > 1024)) {
76 if (Zoom() < minZoom) {
88 QRectF MapGraphicItem::boundingRect()
const
91 return maprect.adjusted(-Margin, -Margin, +Margin, +Margin);
93 void MapGraphicItem::Core_OnNeedInvalidation()
98 void MapGraphicItem::childPosRefresh()
102 void MapGraphicItem::setOverlayOpacity(qreal value)
110 void MapGraphicItem::ConstructLastImage(
int const& zoomdiff)
113 QSize size=boundingRect().size().toSize();
114 size.setWidth(size.width()*2*zoomdiff);
115 size.setHeight(size.height()*2*zoomdiff);
116 temp=QImage(size,QImage::Format_ARGB32_Premultiplied);
118 QPainter imagePainter(&temp);
119 imagePainter.translate(-boundingRect().topLeft());
120 imagePainter.scale(2*zoomdiff,2*zoomdiff);
121 paintImage(&imagePainter);
126 void MapGraphicItem::paintImage(QPainter *painter)
129 if(MapRenderTransform!=1)
131 QTransform transform;
132 transform.translate(-((boundingRect().width()*MapRenderTransform)-(boundingRect().width()))/2,-((boundingRect().height()*MapRenderTransform)-(boundingRect().height()))/2);
133 transform.scale(MapRenderTransform,MapRenderTransform);
134 painter->setWorldTransform(transform);
138 painter->resetTransform();
146 void MapGraphicItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
151 if(MapRenderTransform!=1)
153 QTransform transform;
154 transform.translate(-((boundingRect().width()*MapRenderTransform)-(boundingRect().width()))/2,-((boundingRect().height()*MapRenderTransform)-(boundingRect().height()))/2);
155 transform.scale(MapRenderTransform,MapRenderTransform);
157 painter->setWorldTransform(transform);
158 painter->setRenderHint(QPainter::SmoothPixmapTransform,
true);
159 painter->setRenderHint(QPainter::HighQualityAntialiasing,
true);
164 painter->resetTransform();
175 if(MapRenderTransform!=1)
177 qreal dx= (
event->pos().x()-core->mouseDown.
X())/(MapRenderTransform);
178 qreal dy= (
event->pos().y()-core->mouseDown.
Y())/(MapRenderTransform);
179 qreal nx=core->mouseDown.
X()+dx;
180 qreal ny=core->mouseDown.
Y()+dy;
181 core->mouseCurrent.
SetX(nx);
182 core->mouseCurrent.
SetY(ny);
186 core->mouseCurrent.
SetX(event->pos().x());
187 core->mouseCurrent.
SetY(event->pos().y());
190 core->
Drag(core->mouseCurrent);
194 else if(isSelected && !selectionStart.
IsEmpty() && (
event->modifiers() == Qt::ControlModifier ||
event->modifiers() == Qt::ShiftModifier))
196 selectionEnd = FromLocalToLatLng(event->pos().x(),
event->pos().y());
201 double x1 = qMin(p1.
Lng(), p2.
Lng());
202 double y1 = qMax(p1.
Lat(), p2.
Lat());
203 double x2 = qMax(p1.
Lng(), p2.
Lng());
204 double y2 = qMin(p1.
Lat(), p2.
Lat());
209 QGraphicsItem::mouseMoveEvent(event);
215 if(event->button() == config->
DragButton && CanDragMap()&& !((
event->modifiers()==Qt::ShiftModifier)||(event->modifiers()==Qt::ControlModifier)))
217 core->mouseDown.
SetX(event->pos().x());
218 core->mouseDown.
SetY(event->pos().y());
221 this->setCursor(Qt::SizeAllCursor);
227 else if(!isSelected && ((event->modifiers()==Qt::ControlModifier)||(event->modifiers()==Qt::ShiftModifier)))
232 selectionStart = FromLocalToLatLng(event->pos().x(),
event->pos().y());
247 this->setCursor(Qt::ArrowCursor);
250 if(!core->LastLocationInBounds.
IsEmpty())
260 if(!selectedArea.
IsEmpty() &&
event->modifiers() == Qt::ShiftModifier)
262 SetZoomToFitRect(SelectedArea());
271 if(event->modifiers()&(Qt::ShiftModifier|Qt::ControlModifier))
272 this->setCursor(Qt::CrossCursor);
273 if(event->key()==Qt::Key_Escape)
275 QGraphicsItem::keyPressEvent(event);
279 if((event->modifiers()&(Qt::ShiftModifier|Qt::ControlModifier))==0)
280 this->setCursor(Qt::ArrowCursor);
290 if(maxZoom > MaxZoom())
295 if((
int) Zoom() != maxZoom)
310 if(core->
GetmouseLastZoom().
X() !=
event->pos().x() && core->mouseLastZoom.
Y() !=
event->pos().y())
312 if(GetMouseWheelZoomType() == internals::MouseWheelZoomType::MousePositionAndCenter)
316 else if(GetMouseWheelZoomType() == internals::MouseWheelZoomType::ViewCenter)
318 core->
SetCurrentPosition(FromLocalToLatLng((qint64) maprect.width()/2, (qint64) maprect.height()/2));
320 else if(GetMouseWheelZoomType() == internals::MouseWheelZoomType::MousePositionWithoutCenter)
326 core->mouseLastZoom.
SetX((event->pos().x()));
327 core->mouseLastZoom.
SetY((event->pos().y()));
331 if(GetMouseWheelZoomType() != internals::MouseWheelZoomType::MousePositionWithoutCenter)
339 core->MouseWheelZooming =
true;
341 if(event->delta() > 0)
343 SetZoom(ZoomTotal()+1);
345 else if(event->delta() < 0)
347 SetZoom(ZoomTotal()-1);
350 core->MouseWheelZooming =
false;
353 void MapGraphicItem::DrawMap2D(QPainter *painter)
355 painter->drawImage(this->boundingRect(),dragons.toImage());
356 if(!lastimage.isNull())
387 painter->drawPixmap(core->tileRect.
X(),core->tileRect.
Y(), core->tileRect.
Width(), core->tileRect.
Height(),PureImageProxy::FromStream(img));
393 if(showTileGridLines)
396 painter->drawRect(core->tileRect.
X(), core->tileRect.
Y(), core->tileRect.
Width(), core->tileRect.
Height());
399 painter->setPen(Qt::red);
408 painter->fillRect(QRectF(core->tileRect.
X(), core->tileRect.
Y(), core->tileRect.
Width(), core->tileRect.
Height()),config->
EmptytileBrush);
410 painter->drawText(QRectF(core->tileRect.
X(), core->tileRect.
Y(), core->tileRect.
Width(), core->tileRect.
Height()),config->
EmptyTileText);
415 painter->drawRect(core->tileRect.
X(), core->tileRect.
Y(), core->tileRect.
Width(), core->tileRect.
Height());
422 core::Point p1 = FromLatLngToLocal(SelectedArea().LocationTopLeft());
423 core::Point p2 = FromLatLngToLocal(SelectedArea().LocationRightBottom());
428 painter->setPen(Qt::black);
429 painter->setBrush(QBrush(QColor(50,50,100,20)));
430 painter->drawRect(x1,y1,x2-x1,y2-y1);
445 if(MapRenderTransform!=1)
447 ret.
SetX((qint64) round(ret.
X() * MapRenderTransform));
448 ret.
SetY((qint64) round(ret.
Y() * MapRenderTransform));
449 ret.
SetX(round(ret.
X()-((boundingRect().width()*MapRenderTransform)-(boundingRect().width()))/2));
450 ret.
SetY(round(ret.
Y()-((boundingRect().height()*MapRenderTransform)-(boundingRect().height()))/2));
464 if(MapRenderTransform!=1)
466 x=x+((boundingRect().width()*MapRenderTransform)-(boundingRect().width()))/2;
467 y=y+((boundingRect().height()*MapRenderTransform)-(boundingRect().height()))/2;
469 x = (qint64) round(x / MapRenderTransform);
470 y = (qint64) round(y / MapRenderTransform);
476 double MapGraphicItem::Zoom()
480 double MapGraphicItem::ZoomDigi()
484 double MapGraphicItem::ZoomTotal()
486 return zoomDigi+zoomReal;
489 void MapGraphicItem::SetZoom(
double const& value)
491 if(ZoomTotal() != value)
494 if(value > MaxZoom())
496 zoomReal = MaxZoom();
497 zoomDigi = value-MaxZoom();
499 else if(value < MinZoom())
502 zoomReal = MinZoom();
512 double remainder = modf (value , &integer);
513 if(zoomDigi !=0 || remainder != 0)
515 float scaleValue = zoomDigi+remainder + 1;
516 MapRenderTransform = scaleValue;
517 if(integer>MaxZoom()){
526 MapRenderTransform = 1;
540 if((value - core->
Zoom()) > 0 && value<= MaxZoom())
541 ConstructLastImage(value - core->
Zoom());
542 else if(value!=MaxZoom())
545 if(value > MaxZoom())
548 emit
zoomChanged(MaxZoom()+ZoomDigi(),Zoom(),ZoomDigi());
550 else if(value < MinZoom())
553 emit
zoomChanged(MinZoom()+ZoomDigi(),Zoom(),ZoomDigi());
558 emit
zoomChanged(value+ZoomDigi(), Zoom(), ZoomDigi());;
563 void MapGraphicItem::Offset(qint64
const& x, qint64
const& y)
567 void MapGraphicItem::mapRotate(qreal angle)
569 if (rotation != angle) {
571 resize(scene()->sceneRect());
574 QRectF MapGraphicItem::boundingBox(
const QRectF &rect,
const qreal &angle)
577 float c=cos(angle*2*M_PI/360);
578 float s=sin(angle*2*M_PI/360);
579 ret.setHeight(rect.height()*fabs(c)+rect.width()*fabs(s));
580 ret.setWidth(rect.width()*fabs(c)+rect.height()*fabs(s));
583 QSize MapGraphicItem::sizeHint()
const
A graphicsItem representing a WayPoint.
The main graphicsItem used on the widget, contains the map and map logic.
A graphicsItem representing a Home Location.
A graphicsItem representing a WayPoint.