1 /*********************************************************************** 2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License as published by 5 the Free Software Foundation; either version 2, or (at your option) 6 any later version. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 ***********************************************************************/ 13 14 #ifndef FC__MAPVIEW_H 15 #define FC__MAPVIEW_H 16 17 // In this case we have to include fc_config.h from header file. 18 // Some other headers we include demand that fc_config.h must be 19 // included also. Usually all source files include fc_config.h, but 20 // there's moc generated meta_mapview.cpp file which does not. 21 #ifdef HAVE_CONFIG_H 22 #include <fc_config.h> 23 #endif 24 25 extern "C" { 26 #include "mapview_g.h" 27 } 28 29 // gui-qt 30 #include "fonts.h" 31 32 // Qt 33 #include <QFrame> 34 #include <QLabel> 35 #include <QMutex> 36 #include <QQueue> 37 #include <QThread> 38 #include <QTimer> 39 40 // Forward declarations 41 class QMutex; 42 class QPixmap; 43 44 class minimap_view; 45 46 bool is_point_in_area(int x, int y, int px, int py, int pxe, int pye); 47 void unscale_point(double scale_factor, int &x, int &y); 48 void draw_calculated_trade_routes(QPainter *painter); 49 50 /************************************************************************** 51 Struct used for idle callback to execute some callbacks later 52 **************************************************************************/ 53 struct call_me_back { 54 void (*callback) (void *data); 55 void *data; 56 }; 57 58 /************************************************************************** 59 Class to handle idle callbacks 60 **************************************************************************/ 61 class mr_idle : public QObject 62 { 63 Q_OBJECT 64 public: 65 mr_idle(); 66 ~mr_idle(); 67 68 void add_callback(call_me_back *cb); 69 private slots: 70 void idling(); 71 private: 72 QQueue<call_me_back*> callback_list; 73 QTimer timer; 74 }; 75 76 /************************************************************************** 77 QWidget used for displaying map 78 **************************************************************************/ 79 class map_view : public QWidget 80 { 81 Q_OBJECT 82 void shortcut_pressed(int key); 83 void shortcut_released(Qt::MouseButton mb); 84 public: 85 map_view(); 86 void paint(QPainter *painter, QPaintEvent *event); 87 void find_place(int pos_x, int pos_y, int &w, int &h, int wdth, int hght, 88 int recursive_nr); 89 void resume_searching(int pos_x,int pos_y,int &w, int &h, 90 int wdtht, int hght, int recursive_nr); 91 void update_cursor(enum cursor_type); 92 bool menu_click; 93 94 protected: 95 void paintEvent(QPaintEvent *event); 96 void keyPressEvent(QKeyEvent *event); 97 void mousePressEvent(QMouseEvent *event); 98 void mouseReleaseEvent(QMouseEvent *event); 99 void mouseMoveEvent(QMouseEvent *event); 100 void focusOutEvent(QFocusEvent *event); 101 void leaveEvent(QEvent *event); 102 private slots: 103 void timer_event(); 104 private: 105 void update_font(const QString &name, const QFont &font); 106 107 bool stored_autocenter; 108 int cursor_frame; 109 int cursor; 110 111 }; 112 113 /************************************************************************** 114 Information label about clicked tile 115 **************************************************************************/ 116 class info_tile: public QLabel 117 { 118 Q_OBJECT 119 QFont info_font; 120 public: 121 info_tile(struct tile *ptile, QWidget *parent = 0); 122 struct tile *itile; 123 protected: 124 void paintEvent(QPaintEvent *event); 125 void paint(QPainter *painter, QPaintEvent *event); 126 private: 127 QStringList str_list; 128 void calc_size(); 129 void update_font(const QString &name, const QFont &font); 130 }; 131 132 /************************************************************************** 133 Widget allowing resizing other widgets 134 **************************************************************************/ 135 class resize_widget : public QLabel 136 { 137 Q_OBJECT 138 public: 139 resize_widget(QWidget* parent); 140 void put_to_corner(); 141 142 protected: 143 void mouseMoveEvent(QMouseEvent *event); 144 void mousePressEvent(QMouseEvent *event); 145 private: 146 QPoint point; 147 }; 148 149 150 /************************************************************************** 151 Widget allowing moving other widgets 152 **************************************************************************/ 153 class move_widget : public QLabel 154 { 155 Q_OBJECT 156 public: 157 move_widget(QWidget* parent); 158 void put_to_corner(); 159 160 protected: 161 void mouseMoveEvent(QMouseEvent *event); 162 void mousePressEvent(QMouseEvent *event); 163 private: 164 QPoint point; 165 }; 166 167 /************************************************************************** 168 Abstract class for widgets wanting to do custom action 169 when closing widgets is called (eg. update menu) 170 **************************************************************************/ 171 class fcwidget : public QFrame 172 { 173 Q_OBJECT 174 public: 175 virtual void update_menu() = 0; 176 bool was_destroyed; 177 }; 178 179 /************************************************************************** 180 Widget allowing closing other widgets 181 **************************************************************************/ 182 class close_widget : public QLabel 183 { 184 Q_OBJECT 185 public: 186 close_widget(QWidget *parent); 187 void put_to_corner(); 188 protected: 189 void mousePressEvent(QMouseEvent *event); 190 void notify_parent(); 191 }; 192 193 /************************************************************************** 194 Thread helper for drawing minimap 195 **************************************************************************/ 196 class minimap_thread : public QThread 197 { 198 Q_OBJECT 199 public: 200 minimap_thread(QObject *parent = 0); 201 ~minimap_thread(); 202 void render(double scale_factor, int width, int height); 203 204 signals: 205 void rendered_image(const QImage &image); 206 protected: 207 void run() Q_DECL_OVERRIDE; 208 209 private: 210 int mini_width, mini_height; 211 double scale; 212 QMutex mutex; 213 }; 214 215 /************************************************************************** 216 QLabel used for displaying overview (minimap) 217 **************************************************************************/ 218 class minimap_view: public fcwidget 219 { 220 Q_OBJECT 221 public: 222 minimap_view(QWidget *parent); 223 ~minimap_view(); 224 void paint(QPainter *painter, QPaintEvent *event); 225 virtual void update_menu(); 226 void update_image(); 227 void reset(); 228 229 protected: 230 void paintEvent(QPaintEvent *event); 231 void resizeEvent(QResizeEvent *event); 232 void mousePressEvent(QMouseEvent *event); 233 void mouseMoveEvent(QMouseEvent *event); 234 void mouseReleaseEvent(QMouseEvent *event); 235 void wheelEvent(QWheelEvent *event); 236 void moveEvent(QMoveEvent *event); 237 void showEvent(QShowEvent *event); 238 239 private slots: 240 void update_pixmap(const QImage &image); 241 void zoom_in(); 242 void zoom_out(); 243 244 private: 245 void draw_viewport(QPainter *painter); 246 void scale(double factor); 247 void scale_point(int &x, int &y); 248 double scale_factor; 249 float w_ratio, h_ratio; 250 minimap_thread thread; 251 QBrush background; 252 QPixmap *pix; 253 QPoint cursor; 254 QPoint position; 255 resize_widget *rw; 256 }; 257 258 259 void mapview_freeze(void); 260 void mapview_thaw(void); 261 bool mapview_is_frozen(void); 262 void pixmap_put_overlay_tile(int canvas_x, int canvas_y, 263 struct sprite *ssprite); 264 265 #endif /* FC__MAPVIEW_H */ 266