1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "landscape_view.h"
19 #include "landscape_editor_constants.h"
21 #include "../core/icore.h"
22 #include "../core/core_constants.h"
25 #include <nel/misc/debug.h>
28 #include <QApplication>
30 namespace LandscapeEditor
33 LandscapeView::LandscapeView(QWidget
*parent
)
34 : QGraphicsView(parent
),
38 setTransformationAnchor(AnchorUnderMouse
);
39 setBackgroundBrush(QBrush(Qt::lightGray
));
46 //A modified version of centerOn(), handles special cases
47 setCenter(QPointF(500.0, 500.0));
50 LandscapeView::~LandscapeView()
54 bool LandscapeView::isVisibleGrid() const
59 void LandscapeView::setVisibleGrid(bool visible
)
61 m_visibleGrid
= visible
;
65 void LandscapeView::setVisibleText(bool visible
)
67 m_visibleText
= visible
;
71 void LandscapeView::wheelEvent(QWheelEvent
*event
)
74 float numSteps
= (( event
->delta() / 8 ) / 15) * 1.2;
76 QMatrix mat
= matrix();
77 QPointF mousePosition
= event
->pos();
79 mat
.translate((width() / 2) - mousePosition
.x(), (height() / 2) - mousePosition
.y());
82 mat
.scale(numSteps
, numSteps
);
84 mat
.scale(-1 / numSteps
, -1 / numSteps
);
86 mat
.translate(mousePosition
.x() - (width() / 2), mousePosition
.y() - (height() / 2));
88 //Adjust to the new center for correct zooming
93 void LandscapeView::mousePressEvent(QMouseEvent
*event
)
95 QGraphicsView::mousePressEvent(event
);
96 if (event
->button() != Qt::MiddleButton
)
99 //For panning the view
100 m_lastPanPoint
= event
->pos();
101 setCursor(Qt::ClosedHandCursor
);
104 void LandscapeView::mouseMoveEvent(QMouseEvent
*event
)
106 if(!m_lastPanPoint
.isNull())
108 //Get how much we panned
109 QPointF delta
= mapToScene(m_lastPanPoint
) - mapToScene(event
->pos());
110 m_lastPanPoint
= event
->pos();
112 //Update the center ie. do the pan
113 setCenter(getCenter() + delta
);
116 QGraphicsView::mouseMoveEvent(event
);
119 void LandscapeView::mouseReleaseEvent(QMouseEvent
*event
)
121 m_lastPanPoint
= QPoint();
122 setCursor(Qt::ArrowCursor
);
123 QGraphicsView::mouseReleaseEvent(event
);
126 void LandscapeView::resizeEvent(QResizeEvent
*event
)
128 //Get the rectangle of the visible area in scene coords
129 QRectF visibleArea
= mapToScene(rect()).boundingRect();
130 setCenter(visibleArea
.center());
132 //Call the subclass resize so the scrollbars are updated correctly
133 QGraphicsView::resizeEvent(event
);
136 void LandscapeView::setCenter(const QPointF
¢erPoint
)
138 //Get the rectangle of the visible area in scene coords
139 QRectF visibleArea
= mapToScene(rect()).boundingRect();
142 QRectF sceneBounds
= sceneRect();
144 double boundX
= visibleArea
.width() / 2.0;
145 double boundY
= visibleArea
.height() / 2.0;
146 double boundWidth
= sceneBounds
.width() - 2.0 * boundX
;
147 double boundHeight
= sceneBounds
.height() - 2.0 * boundY
;
149 //The max boundary that the centerPoint can be to
150 QRectF
bounds(boundX
, boundY
, boundWidth
, boundHeight
);
152 if(bounds
.contains(centerPoint
))
154 //We are within the bounds
155 m_currentCenterPoint
= centerPoint
;
159 //We need to clamp or use the center of the screen
160 if(visibleArea
.contains(sceneBounds
))
162 //Use the center of scene ie. we can see the whole scene
163 m_currentCenterPoint
= sceneBounds
.center();
167 m_currentCenterPoint
= centerPoint
;
169 //We need to clamp the center. The centerPoint is too large
170 if (centerPoint
.x() > bounds
.x() + bounds
.width())
171 m_currentCenterPoint
.setX(bounds
.x() + bounds
.width());
172 else if(centerPoint
.x() < bounds
.x())
173 m_currentCenterPoint
.setX(bounds
.x());
175 if(centerPoint
.y() > bounds
.y() + bounds
.height())
176 m_currentCenterPoint
.setY(bounds
.y() + bounds
.height());
177 else if(centerPoint
.y() < bounds
.y())
178 m_currentCenterPoint
.setY(bounds
.y());
182 //Update the scrollbars
183 centerOn(m_currentCenterPoint
);
186 QPointF
LandscapeView::getCenter() const
188 //return m_currentCenterPoint;
189 return mapToScene(viewport()->rect().center());
192 void LandscapeView::drawForeground(QPainter
*painter
, const QRectF
&rect
)
194 QGraphicsView::drawForeground(painter
, rect
);
199 painter
->setPen(QPen(Qt::white
, 0, Qt::SolidLine
));
200 drawGrid(painter
, rect
);
205 if (transform().m11() > m_maxViewText
)
207 painter
->setPen(QPen(Qt::white
, 0.5, Qt::SolidLine
));
208 drawZoneNames(painter
, rect
);
212 void LandscapeView::drawGrid(QPainter
*painter
, const QRectF
&rect
)
214 qreal left
= m_cellSize
* floor(rect
.left() / m_cellSize
);
215 qreal top
= m_cellSize
* floor(rect
.top() / m_cellSize
);
217 QVector
<QLine
> lines
;
219 // Calculate vertical lines
220 while (left
< rect
.right())
222 lines
.push_back(QLine(int(left
), int(rect
.bottom()), int(left
), int(rect
.top())));
226 // Calculate horizontal lines
227 while (top
< rect
.bottom())
229 lines
.push_back(QLine(int(rect
.left()), int(top
), int(rect
.right()), int(top
)));
234 painter
->drawLines(lines
);
237 void LandscapeView::drawZoneNames(QPainter
*painter
, const QRectF
&rect
)
239 int leftSide
= int(floor(rect
.left() / m_cellSize
));
240 int rightSide
= int(floor(rect
.right() / m_cellSize
));
241 int topSide
= int(floor(rect
.top() / m_cellSize
));
242 int bottomSide
= int(floor(rect
.bottom() / m_cellSize
));
244 for (int i
= leftSide
; i
< rightSide
+ 1; ++i
)
246 for (int j
= topSide
; j
< bottomSide
+ 1; ++j
)
248 QString text
= QString("%1_%2%3").arg(j
).arg(QChar('A' + (i
/ 26))).arg(QChar('A' + (i
% 26)));
249 painter
->drawText(i
* m_cellSize
+ 5, j
* m_cellSize
+ 15, text
);
254 } /* namespace LandscapeEditor */