1 /*************************************************************************** 2 kmagview.h - description 3 ------------------- 4 begin : Mon Feb 12 23:45:41 EST 2001 5 copyright : (C) 2001-2003 by Sarang Lakare 6 email : sarang#users.sf.net 7 copyright : (C) 2003-2004 by Olaf Schmidt 8 email : ojschmidt@kde.org 9 copyright : (C) 2008 by Matthew Woehlke 10 email : mw_triad@users.sourceforge.net 11 copyright (C) 2010 Sebastian Sauer 12 email sebsauer@kdab.com 13 ***************************************************************************/ 14 15 /*************************************************************************** 16 * * 17 * This program is free software; you can redistribute it and/or modify * 18 * it under the terms of the GNU General Public License as published by * 19 * the Free Software Foundation; either version 2 of the License, or * 20 * (at your option) any later version. * 21 * * 22 ***************************************************************************/ 23 24 #ifndef KMagZoomView_h 25 #define KMagZoomView_h 26 27 // include files for Qt 28 #include <QWidget> 29 #include <QPixmap> 30 #include <QTimer> 31 #include <QAbstractScrollArea> 32 #include <QRect> 33 #include <QFocusEvent> 34 #include <QHideEvent> 35 #include <QKeyEvent> 36 #include <QShowEvent> 37 #include <QResizeEvent> 38 #include <QMouseEvent> 39 40 //class KMagSelRect; 41 #include "kmagselrect.h" 42 43 #include "focustrackconfig.h" 44 #ifdef QAccessibilityClient_FOUND 45 #include <qaccessibilityclient/registry.h> 46 #endif 47 48 /** 49 * The KMagZoomView class provides the view widget for the KmagApp instance. 50 * 51 * @author Sarang Lakare <sarang#users.sourceforge.net> 52 */ 53 class KMagZoomView : public QAbstractScrollArea 54 { 55 Q_OBJECT 56 public: 57 /// Constructor for the main view 58 explicit KMagZoomView(QWidget *parent = nullptr, const char *name=nullptr); 59 60 /// Destructor for the main view 61 ~KMagZoomView() override; 62 63 /// Toggles the refreshing of the window 64 void toggleRefresh(); 65 66 /// Returns the currently displayed zoomed view 67 QImage getImage(); 68 69 /// Returns the state of the refresh switch getRefreshStatus()70 bool getRefreshStatus() const { return m_refreshSwitch; } 71 72 /// Returns the status of followMouse getFollowMouse()73 bool getFollowMouse() const { return m_followMouse; } 74 75 #ifdef QAccessibilityClient_FOUND 76 /// Returns the status of followFocus getFollowFocus()77 bool getFollowFocus() const { return m_followFocus; } 78 #endif 79 80 /// Get the status of "show rect. always" getShowSelRect()81 bool getShowSelRect() const { return (m_selRect.getAlwaysVisible()); } 82 83 /// Get the coordinates of the selection rectangle getSelRectPos()84 QRect getSelRectPos() const { return static_cast<QRect>(m_selRect); } 85 86 /// Returns the current state of show mouse 87 unsigned int getShowMouseType() const; 88 89 /// Returns the different ways of showing mouse cursor 90 QStringList getShowMouseStringList() const; 91 92 /// Returns the status of "fit to window" option getFitToWindow()93 bool getFitToWindow() const { return (m_fitToWindow); } 94 95 public Q_SLOTS: 96 97 /// Sets zoom to the given value 98 void setZoom(float zoom = 0.0); 99 100 /// Sets the rotation to the given value 101 void setRotation(int rotation = 0); 102 103 /// Sets the color mode to the given value 104 void setColorMode(int mode = 0); 105 106 /// Grabs a frame from the given portion of the display 107 void grabFrame(); 108 109 /// Update the mouse cursor in the zoom view 110 void updateMouseView(); 111 112 /// Set grab-window-follows-mouse mode 113 void followMouse(bool follow = true); 114 115 #ifdef QAccessibilityClient_FOUND 116 /// Set grab-window-follows-mouse-and-keyboard-focus mode 117 void followBoth(bool follow = true); 118 119 /// Set grab-window-follows-keyboard-focus mode 120 void followFocus(bool follow = true); 121 #endif 122 123 /// Shows/Hides the selection marker 124 void showSelRect(bool show=true); 125 126 /// Set the position of the selection region to the given pos 127 void setSelRectPos(const QRect & rect); 128 129 /// Set the refresh rate in fps (frames per second) 130 void setRefreshRate(float fps); 131 132 /// Shows/Hides mouse cursor in the zoomed view 133 bool showMouse(unsigned int type); 134 135 /// Set the status of "fit to window" option 136 void setFitToWindow (bool fit=true); 137 138 /// Fits the zoom view to the zoom view window 139 void fitToWindow(); 140 141 #ifdef QAccessibilityClient_FOUND 142 private Q_SLOTS: 143 /// Called from a dbus service when followFocus is true 144 void focusChanged(const QAccessibleClient::AccessibleObject &object); 145 #endif 146 protected: 147 /// Called when the widget is hidden 148 void hideEvent( QHideEvent * e) override; 149 150 /// Called when the widget is shown 151 void showEvent( QShowEvent * e) override; 152 153 /// Called when the widget has been resized 154 void resizeEvent(QResizeEvent *e) override; 155 156 /// Called when the widget is to be repainted 157 void paintEvent(QPaintEvent *e) override; 158 159 /// This function calculates the mouse position relative to the image 160 QPoint calcMousePos(bool updateMousePos=true); 161 162 /// This function draws the mouse cursor 163 void paintMouseCursor(QPaintDevice *dev, const QPoint & mousePos); 164 165 /// Called when mouse click is detected 166 void mousePressEvent (QMouseEvent *e) override; 167 168 /// Called when mouse is moved 169 void mouseMoveEvent(QMouseEvent *e) override; 170 171 /// Mouse button release event handler 172 void mouseReleaseEvent(QMouseEvent *e) override; 173 174 /// Mouse button release event handler 175 void keyPressEvent(QKeyEvent *e) override; 176 177 /// Mouse button release event handler 178 void keyReleaseEvent(QKeyEvent *e) override; 179 180 /// Mouse button release event handler 181 void focusOutEvent(QFocusEvent *e) override; 182 183 /// Returns the rectangle where the pixmap will be drawn 184 QRect pixmapRect(); 185 186 /// Q3ScrollView porting helpers, maybe inline them 187 int contentsX() const; 188 int contentsY() const; 189 int contentsWidth() const; 190 int contentsHeight() const; 191 int visibleWidth() const; 192 int visibleHeight() const; 193 void setContentsPos(int x, int y); 194 195 /// Setup transformation matrix for zooming, rotating, and mirroring 196 void setupMatrix(); 197 198 private: 199 200 #ifdef QAccessibilityClient_FOUND 201 /// Global Accessibility Registry 202 QAccessibleClient::Registry m_registry; 203 #endif 204 205 /// Stores the pixmap which is recolored from the grabbed one 206 QPixmap m_coloredPixmap; 207 208 /// The selected rectangle which is to be grabbed 209 KMagSelRect m_selRect; 210 211 /// Grabs a window when the timer goes off 212 QTimer m_grabTimer; 213 214 /// Updates the mouse view 215 QTimer m_mouseViewTimer; 216 217 /// Zoom matrix 218 QTransform m_zoomMatrix; 219 220 /// Saves the mouse position when a button is clicked and b4 the cursor is moved to new position 221 QPoint m_oldMousePos; 222 223 /// Saves the center of the grab window 224 QPoint m_oldCenter; 225 226 #ifdef QAccessibilityClient_FOUND 227 /// Saves the keyboard focus position 228 QPoint m_oldFocus; 229 #endif 230 231 /// Possible modes for the mouse to be in 232 enum KMagMouseMode { 233 Normal, 234 StartSelect, 235 ResizeSelection, 236 MoveSelection, 237 GrabSelection 238 }; 239 240 /// The current mode which the mouse is 241 KMagMouseMode m_mouseMode; 242 243 /// stores the state of the Ctrl key 244 bool m_ctrlKeyPressed; 245 246 /// stores the state of the Shift key 247 bool m_shiftKeyPressed; 248 249 /// Store the more recent updated cursor position 250 QPoint m_latestCursorPos; 251 252 /// Various ways of showing mouse cursor 253 QStringList m_showMouseTypes; 254 255 // configuration options: 256 257 /// To follow mouse motion or not when no key is pressed 258 bool m_followMouse; 259 260 /// To follow keyboard focus or not 261 bool m_followFocus; 262 bool m_followBoth; 263 264 /// State of refreshing - on or off 265 bool m_refreshSwitch; 266 267 /// Stores the state of the refresh switch on hide event 268 bool m_refreshSwitchStateOnHide; 269 270 /// Show mouse cursor type - 0 : do not show, non zero: show 271 unsigned int m_showMouse; 272 273 /// Frames per second for refresh 274 unsigned int m_fps; 275 276 /// Stores the amount to zoom the pixmap 277 float m_zoom; 278 279 /// Stores the degrees to rotate the pixmap 280 int m_rotation; 281 282 /// Stores color simulation mode to apply 283 int m_colormode; 284 285 /// Fit the zoom view to the zoom window 286 bool m_fitToWindow; 287 }; 288 289 #endif // KMagZoomView_h 290