FFSM++  1.1.0
French Forest Sector Model ++
MapBox Class Reference

Widget to display the maps of various spacial aspects of the model. More...

#include <MapBox.h>

Inheritance diagram for MapBox:
Collaboration diagram for MapBox:

Public Slots

void updatePixel (QString layerName_h, int x_h, int y_h, QColor color_h)
 
void updateImage (QString layerName_h, const QImage &image_h)
 
void switchToLayer (QString layerName_h)
 Change the layer that currentLayer and currentLayerName points. More...
 
void addLayer (QString layerName_h)
 
void fitInWindow ()
 
void zoom (double zoomFactor)
 
void scroll (int deltaX, int deltaY)
 

Signals

void queryRequestOnPx (int px_ID, int currentLayerIndex, bool newRequest)
 

Public Member Functions

 MapBox (QWidget *parent=0)
 
int getLayerIndex (QString layerName_h="")
 Return the index of the specified layer (null to ask for the current one) More...
 

Private Member Functions

void updatePixmap (const QImage &image, bool reFit=false)
 
void paintEvent (QPaintEvent *event)
 Reimplementation of the standard paintEvent method. More...
 
void prepareQueryEvent (QPoint click)
 
void keyPressEvent (QKeyEvent *event)
 
void wheelEvent (QWheelEvent *event)
 
void mousePressEvent (QMouseEvent *event)
 
void mouseMoveEvent (QMouseEvent *event)
 

Private Attributes

vector< QImage > layersVector
 Vector of QImages. More...
 
vector< QString > layersNameVector
 Vector of layer names. More...
 
QImage currentLayer
 
QString currentLayerName
 
QPoint lastDragPos
 
double sx1
 
double sy1
 
double sx2
 
double sy2
 coordinates of corner pixels of source - pixmap - rectangle More...
 
double dx1
 
double dy1
 
double dx2
 
double dy2
 coordinates of corner pixels of destination - widget - rectangle More...
 

Detailed Description

Widget to display the maps of various spacial aspects of the model.

This class is based on QImage. It pick-ups from layersVector the choosed layer and display it.
It has methods to change the individual pixels or the whole image of a layer.

Definition at line 41 of file MapBox.h.

Constructor & Destructor Documentation

MapBox ( QWidget *  parent = 0)

Definition at line 35 of file MapBox.cpp.

35  :QWidget(parent) {
36 
37  currentLayerName = "";
38  setCursor(Qt::CrossCursor);
39 
40  // setting source and destination init corners..
41  sx1 = 0;
42  sy1 = 0;
43  sx2 = this->width();
44  sy2 = this->height();
45  dx1 = 0;
46  dy1 = 0;
47  dx2 = this->width();
48  dy2 = this->height();
49 }
QString currentLayerName
Definition: MapBox.h:71
double dy1
Definition: MapBox.h:74
double dx1
Definition: MapBox.h:74
double dy2
coordinates of corner pixels of destination - widget - rectangle
Definition: MapBox.h:74
double sy1
Definition: MapBox.h:73
double sy2
coordinates of corner pixels of source - pixmap - rectangle
Definition: MapBox.h:73
double sx2
Definition: MapBox.h:73
double sx1
Definition: MapBox.h:73
double dx2
Definition: MapBox.h:74

Member Function Documentation

void addLayer ( QString  layerName_h)
slot

Definition at line 135 of file MapBox.cpp.

135  {
136  static int counter = 0;
137  QImage newlayer = QImage(this->width(), this->height(), QImage::Format_RGB32);
138  newlayer.fill(qRgb(255, 255, 255));
139  layersVector.push_back(newlayer);
140  layersNameVector.push_back(layerName_h);
141  if (counter == 0) {
142  currentLayerName = layerName_h;
143  currentLayer = layersVector.at(0);
144  }
145  counter ++;
146 }
QString currentLayerName
Definition: MapBox.h:71
vector< QImage > layersVector
Vector of QImages.
Definition: MapBox.h:68
QImage currentLayer
Definition: MapBox.h:70
vector< QString > layersNameVector
Vector of layer names.
Definition: MapBox.h:69
void fitInWindow ( )
slot

Definition at line 217 of file MapBox.cpp.

Referenced by updateImage().

217  {
218 
219  QPixmap pixmap = QPixmap::fromImage(currentLayer);
220  double tempXScale = ( (double) this->width()) / ((double)pixmap.width());
221  double tempYScale = ( (double) this->height())/ ((double)pixmap.height());
222 
223  sx1 = 0;
224  sy1 = 0;
225  sx2 = pixmap.width();
226  sy2 = pixmap.height();
227  dx1 = 0;
228  dy1 = 0;
229 
230  if ( tempXScale >= tempYScale){
231  dx2 = ((double)pixmap.width())*tempYScale;
232  dy2 = this->height();
233  } else {
234  dx2 = this->width();
235  dy2 = ((double)pixmap.height())*tempXScale;
236  }
237  update();
238 }
double dy1
Definition: MapBox.h:74
QImage currentLayer
Definition: MapBox.h:70
double dx1
Definition: MapBox.h:74
double dy2
coordinates of corner pixels of destination - widget - rectangle
Definition: MapBox.h:74
double sy1
Definition: MapBox.h:73
double sy2
coordinates of corner pixels of source - pixmap - rectangle
Definition: MapBox.h:73
double sx2
Definition: MapBox.h:73
double sx1
Definition: MapBox.h:73
double dx2
Definition: MapBox.h:74

Here is the caller graph for this function:

int getLayerIndex ( QString  layerName_h = "")

Return the index of the specified layer (null to ask for the current one)

Definition at line 123 of file MapBox.cpp.

Referenced by prepareQueryEvent().

123  {
124  if( layerName_h == "") layerName_h = currentLayerName;
125  for (uint i=0;i<layersVector.size();i++){
126  if (layersNameVector.at(i) == layerName_h){
127  return i;
128  }
129  }
130  cout << "*** ERROR in MapBox:getLayerIndex(): layerName_h "<< qPrintable(layerName_h) << " not found "<< endl;
131  return -1;
132 }
QString currentLayerName
Definition: MapBox.h:71
vector< QImage > layersVector
Vector of QImages.
Definition: MapBox.h:68
vector< QString > layersNameVector
Vector of layer names.
Definition: MapBox.h:69

Here is the caller graph for this function:

void keyPressEvent ( QKeyEvent *  event)
private

Definition at line 149 of file MapBox.cpp.

149  {
150  switch (event->key()) {
151  case Qt::Key_Plus:
153  break;
154  case Qt::Key_Minus:
156  break;
157  case Qt::Key_Left:
158  scroll(+ScrollStep, 0);
159  break;
160  case Qt::Key_Right:
161  scroll(-ScrollStep, 0);
162  break;
163  case Qt::Key_Down:
164  scroll(0, -ScrollStep);
165  break;
166  case Qt::Key_Up:
167  scroll(0, +ScrollStep);
168  break;
169  default:
170  QWidget::keyPressEvent(event);
171  }
172 }
const double ZoomInFactor
Definition: MapBox.cpp:32
const int ScrollStep
Definition: MapBox.cpp:33
void scroll(int deltaX, int deltaY)
Definition: MapBox.cpp:255
void zoom(double zoomFactor)
Definition: MapBox.cpp:241
const double ZoomOutFactor
Definition: MapBox.cpp:31

Here is the call graph for this function:

void mouseMoveEvent ( QMouseEvent *  event)
private

Definition at line 209 of file MapBox.cpp.

209  {
210  if (event->buttons() & Qt::LeftButton) {
211  scroll(event->pos().x()-lastDragPos.x(), event->pos().y()-lastDragPos.y());
212  lastDragPos = event->pos();
213  update();
214  }
215 }
void scroll(int deltaX, int deltaY)
Definition: MapBox.cpp:255
QPoint lastDragPos
Definition: MapBox.h:72

Here is the call graph for this function:

void mousePressEvent ( QMouseEvent *  event)
private

Definition at line 182 of file MapBox.cpp.

182  {
183  if (event->button() == Qt::LeftButton){
184  lastDragPos = event->pos();
185  }
186  else if (event->button() == Qt::RightButton){
187  prepareQueryEvent(event->pos());
188  }
189 }
void prepareQueryEvent(QPoint click)
Definition: MapBox.cpp:192
QPoint lastDragPos
Definition: MapBox.h:72

Here is the call graph for this function:

void paintEvent ( QPaintEvent *  event)
private

Reimplementation of the standard paintEvent method.

We paint the image pixel by pixel picking up the colors from the map pointed by currentLayer.

Definition at line 55 of file MapBox.cpp.

55  {
56 
57  if (layersVector.size() < 1) return;
58  QPainter painter(this);
59  painter.fillRect(rect(), Qt::lightGray );
60  QPixmap pixmap = QPixmap::fromImage(currentLayer); // It doesn't get autmoatically refreshed if I use a separate function to update the pixmap from the image
61  QRectF source (sx1, sy1, sx2-sx1, sy2-sy1); // the second point is in coordinates origin of the firt point !!!!
62  QRectF destination(dx1, dy1, dx2-dx1, dy2-dy1); // the second point is in coordinates origin of the firt point !!!!
63  /*
64  This is the main function of the widget... the good pointa are:
65  A) It takes into account the low level details of scaling, such interpolation
66  B) If the destination is outside the widgets bounds, it doesn't matter. It make its job on the widget without any error (in this sence it isnot like an array luckily...)
67  */
68  painter.drawPixmap(destination, pixmap, source);
69 
70 }
double dy1
Definition: MapBox.h:74
vector< QImage > layersVector
Vector of QImages.
Definition: MapBox.h:68
QImage currentLayer
Definition: MapBox.h:70
double dx1
Definition: MapBox.h:74
double dy2
coordinates of corner pixels of destination - widget - rectangle
Definition: MapBox.h:74
double sy1
Definition: MapBox.h:73
double sy2
coordinates of corner pixels of source - pixmap - rectangle
Definition: MapBox.h:73
double sx2
Definition: MapBox.h:73
double sx1
Definition: MapBox.h:73
double dx2
Definition: MapBox.h:74
void prepareQueryEvent ( QPoint  click)
private

Definition at line 192 of file MapBox.cpp.

Referenced by mousePressEvent().

192  {
193  double cx = ((double) click.x()); //clicked x, casted to double
194  double cy = ((double) click.y()); //clicked y, casted to double
195  int mx, my = 0; // outputed x and y
196  int px_ID; // pixel ID
197  int layerIndex = getLayerIndex();
198  // checking it is not out of the destination border range..
199  if (cx>dx2 || cx<dx1 || cy>dy2 || cy<dy1) return;
200  mx = ( (int) (cx-dx1) * (sx2-sx1)/(dx2-dx1) + sx1); // casting to int, not round() !!
201  my = ( (int) (cy-dy1) * (sy2-sy1)/(dy2-dy1) + sy1); // casting to int, not round() !!
202  px_ID = mx+my*(sx2-sx1);
203  emit queryRequestOnPx(px_ID, layerIndex, true);
204 
205 }
int getLayerIndex(QString layerName_h="")
Return the index of the specified layer (null to ask for the current one)
Definition: MapBox.cpp:123
double dy1
Definition: MapBox.h:74
void queryRequestOnPx(int px_ID, int currentLayerIndex, bool newRequest)
double dx1
Definition: MapBox.h:74
double dy2
coordinates of corner pixels of destination - widget - rectangle
Definition: MapBox.h:74
double sy1
Definition: MapBox.h:73
double sy2
coordinates of corner pixels of source - pixmap - rectangle
Definition: MapBox.h:73
double sx2
Definition: MapBox.h:73
double sx1
Definition: MapBox.h:73
double dx2
Definition: MapBox.h:74

Here is the call graph for this function:

Here is the caller graph for this function:

void queryRequestOnPx ( int  px_ID,
int  currentLayerIndex,
bool  newRequest 
)
signal

Referenced by prepareQueryEvent().

Here is the caller graph for this function:

void scroll ( int  deltaX,
int  deltaY 
)
slot

Definition at line 255 of file MapBox.cpp.

Referenced by keyPressEvent(), and mouseMoveEvent().

255  {
256  dx1 += ((double) deltaX);
257  dx2 += ((double) deltaX);
258  dy1 += ((double) deltaY);
259  dy2 += ((double) deltaY);
260  update();
261 }
double dy1
Definition: MapBox.h:74
double dx1
Definition: MapBox.h:74
double dy2
coordinates of corner pixels of destination - widget - rectangle
Definition: MapBox.h:74
double dx2
Definition: MapBox.h:74

Here is the caller graph for this function:

void switchToLayer ( QString  layerName_h)
slot

Change the layer that currentLayer and currentLayerName points.

Definition at line 108 of file MapBox.cpp.

108  {
109  if (layerName_h != currentLayerName){
110  for (uint i=0;i<layersVector.size();i++){
111  if (layersNameVector.at(i) == layerName_h){
112  currentLayer = layersVector.at(i);
113  currentLayerName = layerName_h;
114  update();
115  return;
116  }
117  }
118  cout << "*** ERROR in MapBox::switchToLayer(): layerName_h "<< qPrintable(layerName_h) << " not found "<< endl;
119  }
120 }
QString currentLayerName
Definition: MapBox.h:71
vector< QImage > layersVector
Vector of QImages.
Definition: MapBox.h:68
QImage currentLayer
Definition: MapBox.h:70
vector< QString > layersNameVector
Vector of layer names.
Definition: MapBox.h:69
void updateImage ( QString  layerName_h,
const QImage &  image_h 
)
slot

Definition at line 87 of file MapBox.cpp.

87  {
88  static int counter = 0;
89  for (uint i=0;i<layersVector.size();i++){
90  if (layersNameVector.at(i) == layerName_h){
91  layersVector.at(i) = image_h;
92  if(layerName_h == currentLayerName){
93  currentLayer = layersVector.at(i);
94  update();
95  }
96  if (counter == 0) { // that's the first image we got !!
97  fitInWindow();
98  }
99  counter ++;
100  return;
101  }
102  }
103  counter ++;
104  cout << "*** ERROR in MapBox::updateImage(): layerName_h "<< qPrintable(layerName_h) << " not found "<< endl;
105 }
QString currentLayerName
Definition: MapBox.h:71
vector< QImage > layersVector
Vector of QImages.
Definition: MapBox.h:68
QImage currentLayer
Definition: MapBox.h:70
void fitInWindow()
Definition: MapBox.cpp:217
vector< QString > layersNameVector
Vector of layer names.
Definition: MapBox.h:69

Here is the call graph for this function:

void updatePixel ( QString  layerName_h,
int  x_h,
int  y_h,
QColor  color_h 
)
slot

Definition at line 73 of file MapBox.cpp.

73  {
74  for (uint i=0;i<layersVector.size();i++){
75  if (layersNameVector.at(i) == layerName_h){
76  layersVector.at(i).setPixel(x_h, y_h, color_h.rgb());
77  if(layerName_h == currentLayerName){
78  currentLayer = layersVector.at(i);
79  update();
80  }
81  return;
82  }
83  }
84 }
QString currentLayerName
Definition: MapBox.h:71
vector< QImage > layersVector
Vector of QImages.
Definition: MapBox.h:68
QImage currentLayer
Definition: MapBox.h:70
vector< QString > layersNameVector
Vector of layer names.
Definition: MapBox.h:69
void updatePixmap ( const QImage &  image,
bool  reFit = false 
)
private
void wheelEvent ( QWheelEvent *  event)
private

Definition at line 175 of file MapBox.cpp.

175  {
176  int numDegrees = event->delta() / 8;
177  double numSteps = numDegrees / 15.0f;
178  zoom(pow(ZoomInFactor, numSteps));
179 }
const double ZoomInFactor
Definition: MapBox.cpp:32
void zoom(double zoomFactor)
Definition: MapBox.cpp:241

Here is the call graph for this function:

void zoom ( double  zoomFactor)
slot

Definition at line 241 of file MapBox.cpp.

Referenced by keyPressEvent(), and wheelEvent().

241  {
242  double dx1new, dx2new, dy1new, dy2new;
243  dx1new = dx2- (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
244  dx2new = dx1+ (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
245  dy1new = dy2- (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
246  dy2new = dy1+ (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
247  dx1 = dx1new;
248  dy1 = dy1new;
249  dx2 = dx2new;
250  dy2 = dy2new;
251  update();
252 }
double dy1
Definition: MapBox.h:74
double dx1
Definition: MapBox.h:74
double dy2
coordinates of corner pixels of destination - widget - rectangle
Definition: MapBox.h:74
double dx2
Definition: MapBox.h:74

Here is the caller graph for this function:

Member Data Documentation

QImage currentLayer
private

Definition at line 70 of file MapBox.h.

Referenced by addLayer(), fitInWindow(), paintEvent(), switchToLayer(), updateImage(), and updatePixel().

QString currentLayerName
private

Definition at line 71 of file MapBox.h.

Referenced by addLayer(), getLayerIndex(), MapBox(), switchToLayer(), updateImage(), and updatePixel().

double dx1
private

Definition at line 74 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), prepareQueryEvent(), scroll(), and zoom().

double dx2
private

Definition at line 74 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), prepareQueryEvent(), scroll(), and zoom().

double dy1
private

Definition at line 74 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), prepareQueryEvent(), scroll(), and zoom().

double dy2
private

coordinates of corner pixels of destination - widget - rectangle

Definition at line 74 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), prepareQueryEvent(), scroll(), and zoom().

QPoint lastDragPos
private

Definition at line 72 of file MapBox.h.

Referenced by mouseMoveEvent(), and mousePressEvent().

vector<QString> layersNameVector
private

Vector of layer names.

Definition at line 69 of file MapBox.h.

Referenced by addLayer(), getLayerIndex(), switchToLayer(), updateImage(), and updatePixel().

vector<QImage> layersVector
private

Vector of QImages.

Definition at line 68 of file MapBox.h.

Referenced by addLayer(), getLayerIndex(), paintEvent(), switchToLayer(), updateImage(), and updatePixel().

double sx1
private

Definition at line 73 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), and prepareQueryEvent().

double sx2
private

Definition at line 73 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), and prepareQueryEvent().

double sy1
private

Definition at line 73 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), and prepareQueryEvent().

double sy2
private

coordinates of corner pixels of source - pixmap - rectangle

Definition at line 73 of file MapBox.h.

Referenced by fitInWindow(), MapBox(), paintEvent(), and prepareQueryEvent().


The documentation for this class was generated from the following files: