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

gui_test_widget.hpp « qt_tstfrm - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e52eb9909ad66aaa757fc833926f85b03a6e586b (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
#pragma once

#include "gl_test_widget.hpp"
#include "../../gui/controller.hpp"
#include "../../base/strings_bundle.hpp"
#include <QMouseEvent>
#include <QObject>
#include <QTimerEvent>

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

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

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

template <typename TTest>
class GUITestWidget : public GLTestWidget<TTest>
{
private:

  typedef GLTestWidget<TTest> base_t;

  shared_ptr<gui::Controller> m_controller;
  shared_ptr<graphics::Screen> m_cacheScreen;
  shared_ptr<StringsBundle> m_stringBundle;

  shared_ptr<QObject> m_timerObj;
  int m_timerID;

public:

  void invalidate()
  {
    base_t::updateGL();
  }

  void initializeGL()
  {
    m_timerObj.reset(new QObject());
    m_timerObj->installEventFilter(this);
    m_timerID = m_timerObj->startTimer(1000 / 60);

    base_t::initializeGL();

    m_controller.reset(new gui::Controller());

    gui::Controller::RenderParams rp;

    graphics::Screen::Params cp;

    cp.m_threadSlot = 0;
    cp.m_storageType = graphics::ETinyStorage;
    cp.m_textureType = graphics::ESmallTexture;
    cp.m_resourceManager = base_t::m_resourceManager;
    cp.m_renderContext = base_t::m_primaryContext;

    m_cacheScreen = make_shared_ptr(new graphics::Screen(cp));

    rp.m_CacheScreen = m_cacheScreen.get();
    rp.m_GlyphCache = base_t::m_resourceManager->glyphCache(0);
    rp.m_InvalidateFn = bind(&GUITestWidget<TTest>::invalidate, this);
    rp.m_Density = graphics::EDensityMDPI;

    m_stringBundle.reset(new StringsBundle());
    m_stringBundle->SetDefaultString("country_status_download", "Download^");

    m_controller->SetStringsBundle(m_stringBundle.get());

    InitImpl(m_controller, bool_tag<has_init_with_controller<TTest, TTest>::value >());

    m_controller->SetRenderParams(rp);
  }

  void InitImpl(shared_ptr<gui::Controller> const & c, bool_tag<true> const &)
  {
    base_t::test.Init(c.get());
  }

  void InitImpl(shared_ptr<gui::Controller> const & c, bool_tag<false> const &)
  {}

  void DoDraw(shared_ptr<graphics::Screen> const & s)
  {
    base_t::DoDraw(s);
    m_controller->UpdateElements();
    m_controller->DrawFrame(s.get());
  }

  void mousePressEvent(QMouseEvent * e)
  {
    base_t::mousePressEvent(e);

    if (e->button() == Qt::LeftButton)
      m_controller->OnTapStarted(m2::PointU(e->pos().x(), e->pos().y()));
  }

  void mouseReleaseEvent(QMouseEvent * e)
  {
    base_t::mouseReleaseEvent(e);

    if (e->button() == Qt::LeftButton)
      m_controller->OnTapEnded(m2::PointU(e->pos().x(), e->pos().y()));
  }

  void mouseMoveEvent(QMouseEvent * e)
  {
    base_t::mouseMoveEvent(e);

    m_controller->OnTapMoved(m2::PointU(e->pos().x(), e->pos().y()));
  }

  bool eventFilter(QObject * obj, QEvent *event)
  {
    if (obj == m_timerObj.get() && event->type() == QEvent::Timer)
    {
      if (((QTimerEvent *)event)->timerId() == m_timerID)
      {
        invalidate();
        return true;
      }
    }

    return false;
  }
};

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

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