graphpainter.h
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #ifndef GRAPH_PAINTER_H
15 #define GRAPH_PAINTER_H
16 
17 #include "graph.h"
18 #include "graphwidget.h"
19 
20 #include <Qt>
21 #include <QOpenGLFunctions>
22 
23 //! A painter which holds off-screen pixmap
24 //! and provides a way to draw
25 //! not thread-safe
26 class XQGraphPainter : public enable_shared_from_this<XQGraphPainter>, protected QOpenGLFunctions {
27 public:
28  XQGraphPainter(const shared_ptr<XGraph> &graph, XQGraph* item);
29  virtual ~XQGraphPainter();
30 
31  //! Selections
32  enum SelectionMode {SelNone, SelPoint, SelAxis, SelPlane, TiltTracking};
33  enum SelectionState {SelStart, SelFinish, Selecting};
34  void selectObjs(int x, int y, SelectionState state, SelectionMode mode = SelNone);
35 
36  void wheel(int x, int y, double deg);
37  void zoom(double zoomscale, int x, int y);
38  void showHelp();
39 
40  //! Repaint Off-screen obj, Paint On-screen obj.
41  void repaintGraph(int x1, int y1, int x2, int y2);
42 
43  //! view
44  //! \param angle CCW
45  //! \param x,y,z rotate axis
46  //! \param init initialize view matrix or not
47  void viewRotate(double angle, double x, double y, double z, bool init = false);
48 
49  //! drawings
50 
51  void setColor(float r, float g, float b, float a = 1.0f) {
52  glColor4f(r, g, b, a );
53  m_curTextColor = QColor(lrintf(r * 256.0), lrintf(g * 256.0), lrintf(b * 256.0), a).rgba();
54 }
55  void setColor(unsigned int rgb, float a = 1.0f) {
56  QColor qc = QRgb(rgb);
57  glColor4f(qc.red() / 256.0, qc.green() / 256.0, qc.blue() / 256.0, a );
58  qc.setAlpha(lrintf(a * 255));
59  m_curTextColor = qc.rgba();
60 }
61  void setVertex(const XGraph::ScrPoint &p) {
62  glVertex3f(p.x, p.y, p.z);
63  }
64 
65  void beginLine(double size = 1.0);
66  void endLine();
67 
68  void beginPoint(double size = 1.0);
69  void endPoint();
70 
71  void beginQuad(bool fill = false);
72  void endQuad();
73 
74  void drawText(const XGraph::ScrPoint &p, const XString &str);
75 
76  //! make point outer perpendicular to \a dir by offset
77  //! \param offset > 0 for outer, < 0 for inner. unit is of screen coord.
78  void posOffAxis(const XGraph::ScrPoint &dir, XGraph::ScrPoint *src, XGraph::SFloat offset);
79  void defaultFont();
80  //! \param start where text be aligned
81  //! \param dir a direction where text be aligned
82  //! \param width perp. to \a dir, restricting font size
83  //! \return return 0 if succeeded
84  int selectFont(const XString &str, const XGraph::ScrPoint &start,
85  const XGraph::ScrPoint &dir, const XGraph::ScrPoint &width, int sizehint = 0);
86 
87  //! minimum resolution of screen coordinate.
88  float resScreen();
89  //! openGL stuff
90  void initializeGL ();
91  void resizeGL ( int width, int height );
92  void paintGL ();
93 private:
94  //! coordinate conversions
95  //! \ret zero for success
96  int windowToScreen(int x, int y, double z, XGraph::ScrPoint *scr);
97  int screenToWindow(const XGraph::ScrPoint &scr, double *x, double *y, double *z);
98  //for retina support.
99  double m_pixel_ratio;
100 
101  //! Selections
102  //! \param x,y center of clipping area
103  //! \param dx,dy clipping width,height
104  //! \param dz window of depth
105  //! \param scr hits
106  //! \param dsdx,dsdy diff. of 1 pixel
107  //! \return found depth
108  double selectPlane(int x, int y, int dx, int dy,
110  double selectAxis(int x, int y, int dx, int dy,
112  double selectPoint(int x, int y, int dx, int dy,
114 
115  shared_ptr<XListener> m_lsnRedraw;
116  void onRedraw(const Snapshot &shot, XGraph *graph);
117 
118  void repaintBuffer(int x1, int y1, int x2, int y2);
119  //! do as possible as you can without screen.
120  //! e.g. compile primitives, or make pixmap.
121  void redrawOffScreen();
122 
123  //! Draws plots, axes.
125  void drawOffScreenGrids(const Snapshot &shot);
126  void drawOffScreenPlaneMarkers(const Snapshot &shot); //!< for \a selectGL()
127  void drawOffScreenPoints(const Snapshot &shot);
128  void drawOffScreenAxes(const Snapshot &shot);
129  void drawOffScreenAxisMarkers(const Snapshot &shot); //!< for \a selectGL()
130  //! depends on viewpoint
131  void drawOnScreenObj(const Snapshot &shot);
132  //! independent of viewpoint. For coordinate, legend, hints. title,...
133  void drawOnScreenViewObj(const Snapshot &shot);
134  void drawOnScreenHelp(const Snapshot &shot, QPainter *qpainter);
135 
136  const shared_ptr<XGraph> m_graph;
137  XQGraph *const m_pItem;
138 
139  shared_ptr<XPlot> m_foundPlane;
140  shared_ptr<XAxis> m_foundPlaneAxis1, m_foundPlaneAxis2;
141  shared_ptr<XAxis> m_foundAxis;
142 
143  shared_ptr<XAxis> findAxis(const Snapshot &shot, const XGraph::ScrPoint &s1);
144  shared_ptr<XPlot> findPlane(const Snapshot &shot, const XGraph::ScrPoint &s1,
145  shared_ptr<XAxis> *axis1, shared_ptr<XAxis> *axis2);
146  SelectionState m_selectionStateNow;
147  SelectionMode m_selectionModeNow;
148  XGraph::ScrPoint m_startScrPos, m_startScrDX, m_startScrDY;
149  XGraph::ScrPoint m_finishScrPos, m_finishScrDX, m_finishScrDY;
150  XString m_onScreenMsg;
151  int m_selStartPos[2];
152  int m_tiltLastPos[2];
153  int m_pointerLastPos[2];
154 
155  double selectGL(int x, int y, int dx, int dy, GLint list,
157  void setInitView();
158 
159  GLint m_listpoints, m_listaxes,
160  m_listaxismarkers, m_listgrids, m_listplanemarkers;
161 
162  bool m_bIsRedrawNeeded;
163  bool m_bIsAxisRedrawNeeded;
164  bool m_bTilted;
165  bool m_bReqHelp;
166 
167  GLdouble m_proj_rot[16]; // Current Rotation matrix
168  GLdouble m_proj[16]; // Current Projection matrix
169  GLdouble m_model[16]; // Current Modelview matrix
170  GLint m_viewport[4]; // Current Viewport
171 
172  //! ghost stuff
173  std::vector<GLubyte> m_lastFrame;
174  XTime m_modifiedTime;
175  XTime m_updatedTime;
176 // XGraph::ScrPoint DirProj; //direction vector of z of window coord.
177  int m_curFontSize;
178  int m_curAlign;
179 
180  struct Text {
181  QString text;
182  int x; int y;
183  int fontsize;
184  QRgb rgba;
185  };
186  std::vector<Text> m_textOverpaint; //stores text to be overpainted.
187  QRgb m_curTextColor;
188  void drawTextOverpaint(QPainter &qpainter);
189 };
190 
191 #endif

Generated for KAME4 by  doxygen 1.8.3