graphwidget.cpp
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 #include "graphwidget.h"
15 #include "graph.h"
16 #include <QDialog>
17 #include <QPixmap>
18 #include <QPainter>
19 #include <QImage>
20 #include <QLayout>
21 #include <QMouseEvent>
22 #include "graphpainter.h"
23 #include "measure.h"
24 #include "graphdialogconnector.h"
25 #include "ui_graphdialog.h"
26 
28 
29 XQGraph::XQGraph( QWidget* parent, Qt::WindowFlags fl ) :
30  QOpenGLWidget(parent) {
31 // if( !parent->layout() ) {
32 // parent->setLayout(new QHBoxLayout(this));
33 // parent->layout()->addWidget(this);
34 // }
35  setMouseTracking(true);
36 
37  QSurfaceFormat format;
38  format.setAlphaBufferSize(8);
39  format.setDepthBufferSize(16);
40  format.setSamples(2);
41 // format.setVersion(2, 2);
42  format.setProfile(QSurfaceFormat::CoreProfile);
43 
44 // QGLFormat(QGL::AlphaChannel | QGL::DoubleBuffer | QGL::Rgba |
45 // QGL::DepthBuffer | QGL::AccumBuffer |
46 // QGL::SampleBuffers
47  setFormat(format);
48 }
49 XQGraph::~XQGraph() {
50  m_painter.reset();
51 }
52 void
53 XQGraph::setGraph(const shared_ptr<XGraph> &graph) {
54  m_conDialog.reset();
55  m_painter.reset();
56  m_graph = graph;
57 // if(graph && !isHidden()) {
58 // showEvent(NULL);
59 // }
60 }
61 void
62 XQGraph::mousePressEvent ( QMouseEvent* e) {
63  if( !m_painter ) return;
65  switch (e->button()) {
66  case Qt::RightButton:
67  mode = XQGraphPainter::SelAxis;
68  break;
69  case Qt::LeftButton:
70  mode = XQGraphPainter::SelPlane;
71  break;
72  case Qt::MidButton:
73  mode = XQGraphPainter::TiltTracking;
74  break;
75  default:
76  mode = XQGraphPainter::SelNone;
77  break;
78  }
79  m_painter->selectObjs(e->pos().x(), e->pos().y(), XQGraphPainter::SelStart, mode);
80 }
81 void
82 XQGraph::mouseMoveEvent ( QMouseEvent* e) {
83  static XTime lasttime = XTime::now();
84  if(XTime::now() - lasttime < 0.033) return;
85  if( !m_painter ) return;
86  m_painter->selectObjs(e->pos().x(), e->pos().y(), XQGraphPainter::Selecting);
87 }
88 void
89 XQGraph::mouseReleaseEvent ( QMouseEvent* e) {
90  if( !m_painter ) return;
91  m_painter->selectObjs(e->pos().x(), e->pos().y(), XQGraphPainter::SelFinish);
92 }
93 void
94 XQGraph::mouseDoubleClickEvent ( QMouseEvent* e) {
95  e->accept();
96  if( !m_painter ) return;
97  if(m_graph) {
98  switch (e->button()) {
99  case Qt::RightButton:
100  m_painter->showHelp();
101  break;
102  case Qt::LeftButton:
103  m_conDialog = xqcon_create<XQGraphDialogConnector>(
104  m_graph,
105  new DlgGraphSetup(parentWidget()));
106  //\todo setAttribute Qt::WA_DeleteOnClose
107  break;
108  case Qt::MidButton:
109  break;
110  default:
111  break;
112  }
113  }
114 }
115 void
116 XQGraph::wheelEvent ( QWheelEvent *e) {
117  e->accept();
118  if(m_painter )
119  m_painter->wheel(e->pos().x(), e->pos().y(), (double)e->delta() / 8.0);
120 }
121 void
122 XQGraph::showEvent ( QShowEvent *) {
123  shared_ptr<XGraph> graph = m_graph;
124  if(graph) {
125  m_painter.reset();
126  // m_painter will be re-set in the constructor.
127  new XQGraphPainter(graph, this);
128 // setMouseTracking(true);
129  initializeGL();
130  }
131 }
132 void
133 XQGraph::hideEvent ( QHideEvent * ) {
134  m_conDialog.reset();
135  m_painter.reset();
136 // setMouseTracking(false);
137 }
138 //! openGL stuff
139 void
141  if(m_painter )
142  m_painter->initializeGL();
143 }
144 void
145 XQGraph::resizeGL ( int width, int height ) {
146  // be aware of retina display.
147  double pixel_ratio = devicePixelRatio();
148  glViewport( 0, 0, (GLint)(width * pixel_ratio),
149  (GLint)(height * pixel_ratio));
150  if(m_painter )
151  m_painter->resizeGL(width, height);
152 }
153 
154 void
155 XQGraph::paintGL() {
156  if(m_painter )
157  m_painter->paintGL();
158 }

Generated for KAME4 by  doxygen 1.8.3