Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gl_test_widget.hpp « qt_tstfrm - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f1cad4a9c54c89bc3fa6c7415eaa8b17e051f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma once

#include "tstwidgets.hpp"

#include "../../testing/testing.hpp"

#include "../../map/qgl_render_context.hpp"

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  #include <QtGui/QApplication>
#else
  #include <QtWidgets/QApplication>
#endif

template<class T, bool (T::*)(QKeyEvent *)>
struct key_event_fn_bind
{
  typedef T type;
};

template <class T, bool  (T::*)(QMouseEvent*)>
struct mouse_event_fn_bind
{
  typedef T type;
};

template <class T, void (T::*)()>
struct void_fn_bind
{
  typedef T type;
};

template <class T, class RC, void (T::*)(shared_ptr<RC>)>
struct init_with_context_fn_bind
{
  typedef T type;
};

template<class T, class U>
struct has_on_keypress
{
  static const bool value = false;
};

template<class T>
struct has_on_keypress<T, typename key_event_fn_bind<T, &T::OnKeyPress>::type>
{
  static const bool value = true;
};

template <class T, class U>
struct has_on_mousemove
{
  static const bool value = false;
};

template <class T>
struct has_on_mousemove<T, typename mouse_event_fn_bind<T, &T::OnMouseMove>::type >
{
  static const bool value = true;
};

template <class T, class U>
struct has_on_mousepress
{
  static bool const value = false;
};

template <class T>
struct has_on_mousepress<T, typename mouse_event_fn_bind<T, &T::OnMousePress>::type >
{
  static const bool value = true;
};

template <class T, class U>
struct has_init
{
  static bool const value = false;
};

template <class T>
struct has_init<T, typename void_fn_bind<T, &T::Init>::type >
{
  static const bool value = true;
};

template <class T, class U, class RC>
struct has_init_with_context
{
  static bool const value = false;
};

template <class T, class RC>
struct has_init_with_context<T, typename init_with_context_fn_bind<T, RC, &T::Init>::type, RC >
{
  static const bool value = true;
};

template <bool T>
struct bool_tag{};

template <typename TTest>
class GLTestWidget : public tst::GLDrawWidget
{
public:

  TTest test;

  typedef tst::GLDrawWidget base_type;

  virtual void DoDraw(shared_ptr<graphics::Screen> const & p)
  {
    p->beginFrame();
    p->clear(graphics::Color(182, 182, 182, 255));
    test.DoDraw(p);
    p->endFrame();
  }

  virtual void DoResize(int, int)
  {
  }

  bool keyPressEventImpl(QKeyEvent * ev, bool_tag<true> const &)
  {
    return test.OnKeyPress(ev);
  }

  bool keyPressEventImpl(QKeyEvent *, bool_tag<false> const & )
  {
    return false;
  }

  virtual void keyPressEvent(QKeyEvent * ev)
  {
    if (keyPressEventImpl(ev, bool_tag<has_on_keypress<TTest, TTest>::value >()))
      repaint();
  }

  bool mousePressEventImpl(QMouseEvent * ev, bool_tag<true> const &)
  {
    return test.OnMousePress(ev);
  }

  bool mousePressEventImpl(QMouseEvent *, bool_tag<false> const &)
  {
    return false;
  }

  virtual void mousePressEvent(QMouseEvent * ev)
  {
    if (mousePressEventImpl(ev, bool_tag<has_on_mousepress<TTest, TTest>::value >()))
      repaint();
  }

  bool mouseMoveEventImpl(QMouseEvent * ev, bool_tag<true> const &)
  {
    return test.OnMouseMove(ev);
  }

  bool mouseMoveEventImpl(QMouseEvent *, bool_tag<false> const &)
  {
    return false;
  }

  void mouseMoveEvent(QMouseEvent * ev)
  {
    if (mouseMoveEventImpl(ev, bool_tag<has_on_mousemove<TTest, TTest>::value >()))
      repaint();
  }

  void InitImpl(bool_tag<true> const & )
  {
    test.Init();
  }

  void InitImpl(bool_tag<false> const & )
  {}

  void Init()
  {
    InitImpl(bool_tag<has_init<TTest, TTest>::value >());
  }

  void InitWithContextImpl(bool_tag<true> const &)
  {
    test.Init(shared_ptr<qt::gl::RenderContext>(new qt::gl::RenderContext(this)));
  }

  void InitWithContextImpl(bool_tag<false> const &)
  {}

  void initializeGL()
  {
    tst::GLDrawWidget::initializeGL();
    InitWithContextImpl(bool_tag<has_init_with_context<TTest, TTest, qt::gl::RenderContext>::value>());
  }
};

template <class Test> QWidget * create_widget()
{
  GLTestWidget<Test> * w = new GLTestWidget<Test>();
  w->Init();
  return w;
}

#define UNIT_TEST_GL(name)\
  void UnitTestGL_##name();\
  TestRegister g_TestRegister_##name("Test::"#name, __FILE__, &UnitTestGL_##name);\
  void UnitTestGL_##name()\
  {\
    char * argv[] = { const_cast<char *>(#name) };\
    int argc = 1;\
    QApplication app(argc, argv);\
    QWidget * w = create_widget<name>();\
    w->show();\
    app.exec();\
  }