00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <multires_image/QGLMap.h>
00031
00032
00033 #include <cmath>
00034
00035 namespace multires_image
00036 {
00037 QGLMap::QGLMap(QWidget *parent) :
00038 QGLWidget(parent),
00039 m_initialized(false),
00040 m_scale(1.0),
00041 m_mouseDown(false),
00042 m_mouseDownX(0),
00043 m_mouseDownY(0),
00044 m_tileView(NULL),
00045 m_view_top_left(0, 0, 0),
00046 m_view_bottom_right(0, 0, 0),
00047 m_view_center(0, 0, 0),
00048 m_scene_top_left(0, 0, 0),
00049 m_scene_bottom_right(0, 0, 0),
00050 m_scene_center(0, 0, 0)
00051 {
00052 ui.setupUi(this);
00053 }
00054
00055 QGLMap::~QGLMap()
00056 {
00057 }
00058
00059 void QGLMap::Exit()
00060 {
00061 if (m_tileView != NULL)
00062 {
00063 m_tileView->Exit();
00064 }
00065 }
00066
00067 void QGLMap::UpdateView()
00068 {
00069 if (m_initialized)
00070 {
00071 Recenter();
00072
00073 if (m_tileView != NULL)
00074 {
00075 m_tileView->SetView(m_view_center.x(), m_view_center.y(), 1, m_scale);
00076 }
00077
00078 glViewport(0, 0, width(), height());
00079 glMatrixMode(GL_PROJECTION);
00080 glLoadIdentity();
00081 glOrtho(m_view_top_left.x(), m_view_bottom_right.x(),
00082 m_view_bottom_right.y(), m_view_top_left.y(), -0.5f, 0.5f);
00083
00084 update();
00085
00086
00087
00088 emit SignalViewChange(m_view_top_left.x(), m_view_top_left.y(),
00089 m_view_bottom_right.x(), m_view_bottom_right.y());
00090 }
00091 }
00092
00093 void QGLMap::SetTiles(TileSet* tiles)
00094 {
00095 double top, left, bottom, right;
00096 tiles->GeoReference().GetCoordinate(0, 0, left, top);
00097 tiles->GeoReference().GetCoordinate(tiles->GeoReference().Width(), tiles->GeoReference().Height(), right, bottom);
00098
00099 m_scene_top_left = tf::Point(left, top, 0);
00100 m_scene_bottom_right = tf::Point(right, bottom, 0);
00101 m_scene_center = (m_scene_top_left + m_scene_bottom_right) / 2.0;
00102
00103 m_view_center = m_scene_center;
00104
00105 m_tileView = new TileView(tiles, this);
00106
00107 connect(m_tileView->Cache(), SIGNAL(SignalMemorySize(int64_t)),
00108 SLOT(SetTextureMemory(int64_t)));
00109
00110
00111
00112
00113 m_tileView->SetView(m_view_center.x(), m_view_center.y(), 1, m_scale);
00114 }
00115
00116 void QGLMap::wheelEvent(QWheelEvent* e)
00117 {
00118 float numDegrees = e->delta() / -8;
00119
00120 m_scale *= pow(1.1, numDegrees / 10.0);
00121
00122 UpdateView();
00123 }
00124
00125 void QGLMap::LoadTexture(Tile* tile)
00126 {
00127 tile->LoadTexture();
00128 }
00129
00130 void QGLMap::DeleteTexture(Tile* tile)
00131 {
00132 tile->UnloadTexture();
00133 }
00134
00135 void QGLMap::SetTextureMemory(int64_t bytes)
00136 {
00137
00138
00139 emit SignalMemorySize(bytes);
00140 }
00141
00142 void QGLMap::ChangeCenter(double x, double y)
00143 {
00144 if (x != 0)
00145 m_view_center.setX(x);
00146
00147 if (y != 0)
00148 m_view_center.setY(y);
00149
00150 UpdateView();
00151 }
00152
00153 void QGLMap::initializeGL()
00154 {
00155 glClearColor(0.58f, 0.56f, 0.5f, 1);
00156 glEnable(GL_POINT_SMOOTH);
00157 glEnable(GL_LINE_SMOOTH);
00158 glEnable(GL_POLYGON_SMOOTH);
00159 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
00160 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
00161 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
00162 glEnable(GL_BLEND);
00163 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00164 glDepthFunc(GL_NEVER);
00165 glDisable(GL_DEPTH_TEST);
00166 m_initialized = true;
00167 }
00168
00169 void QGLMap::resizeGL(int w, int h)
00170 {
00171 UpdateView();
00172 }
00173
00174 void QGLMap::paintGL()
00175 {
00176 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00177
00178 if (m_tileView != NULL)
00179 {
00180 m_tileView->Draw();
00181 }
00182 }
00183
00184 void QGLMap::mousePressEvent(QMouseEvent* e)
00185 {
00186 m_mouseDownX = e->x();
00187 m_mouseDownY = e->y();
00188 m_mouseDown = true;
00189
00190 update();
00191 }
00192
00193 void QGLMap::mouseDoubleClickEvent(QMouseEvent* e)
00194 {
00195 update();
00196 }
00197
00198 void QGLMap::mouseReleaseEvent(QMouseEvent* e)
00199 {
00200 m_mouseDown = false;
00201
00202 update();
00203 }
00204
00205 void QGLMap::mouseMoveEvent(QMouseEvent* e)
00206 {
00207 if (m_mouseDown)
00208 MousePan(e->x(), e->y());
00209 }
00210
00211 void QGLMap::MousePan(int x, int y)
00212 {
00213 bool changed = false;
00214 if (m_mouseDown)
00215 {
00216 double diffX = ((m_mouseDownX - x) * m_scale);
00217 double diffY = ((m_mouseDownY - y) * m_scale);
00218
00219 if (diffX != 0)
00220 {
00221 m_view_center.setX(m_view_center.x() + diffX);
00222 m_mouseDownX = x;
00223 changed = true;
00224 }
00225 if (diffY != 0)
00226 {
00227 m_view_center.setY(m_view_center.y() + diffY);
00228 m_mouseDownY = y;
00229 changed = true;
00230 }
00231 }
00232
00233 if (changed)
00234 {
00235 UpdateView();
00236 }
00237 }
00238
00239 void QGLMap::Recenter()
00240 {
00241 double scene_width = std::fabs(m_scene_top_left.x() - m_scene_bottom_right.x());
00242 double scene_height = std::fabs(m_scene_top_left.y() - m_scene_bottom_right.y());
00243 double view_width = width() * m_scale;
00244 double view_height = height() * m_scale;
00245
00246 m_view_top_left.setX(m_view_center.x() - (view_width * 0.5));
00247 m_view_top_left.setY(m_view_center.y() - (view_width * 0.5));
00248
00249 m_view_bottom_right.setX(m_view_center.x() + (view_width * 0.5));
00250 m_view_bottom_right.setY(m_view_center.y() + (view_width * 0.5));
00251
00252 if (view_width > scene_width)
00253 {
00254 m_view_center.setX(m_scene_center.x());
00255 m_view_top_left.setX(m_view_center.x() - (view_width * 0.5));
00256 m_view_bottom_right.setX(m_view_center.x() + (view_width * 0.5));
00257 }
00258 else
00259 {
00260 if (m_view_top_left.x() < m_scene_top_left.x())
00261 {
00262 m_view_top_left.setX(m_scene_top_left.x());
00263 m_view_bottom_right.setX(m_view_top_left.x() + view_width);
00264 m_view_center.setX(m_view_top_left.x() + (view_width * 0.5));
00265 }
00266
00267 if (m_view_bottom_right.x() > m_scene_bottom_right.x())
00268 {
00269 m_view_bottom_right.setX(m_scene_bottom_right.x());
00270 m_view_top_left.setX(m_view_bottom_right.x() - view_width);
00271 m_view_center.setX(m_view_top_left.x() + (view_width * 0.5));
00272 }
00273 }
00274
00275 if (view_height < scene_height)
00276 {
00277 m_view_center.setY(m_scene_center.y());
00278 m_view_top_left.setY(m_scene_center.y() - (view_height * 0.5));
00279 m_view_bottom_right.setY(m_scene_center.y() + (view_height * 0.5));
00280 }
00281 else
00282 {
00283 if (m_view_top_left.y() > m_scene_top_left.y())
00284 {
00285 m_view_top_left.setY(m_scene_top_left.y());
00286 m_view_bottom_right.setY(m_view_top_left.y() + (view_height));
00287 m_view_center.setY(m_view_top_left.y() + (view_height * 0.5));
00288 }
00289
00290 if (m_view_bottom_right.y() < m_scene_bottom_right.y())
00291 {
00292 m_view_bottom_right.setY(m_scene_bottom_right.y());
00293 m_view_top_left.setY(m_view_bottom_right.y() - (view_height));
00294 m_view_center.setY(m_view_top_left.y() + (view_height * 0.5));
00295 }
00296 }
00297 }
00298
00299 }
00300