MT Showcase SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Utils.hpp
1 #pragma once
2 
3 #include <QString>
4 #include <QVariant>
5 
6 #include "GraphTypes.hpp"
7 #include "ShowcaseTypes.hpp"
8 #include "Export.hpp"
9 #include "Events.hpp"
10 
11 #include <MultiTouch/MultiTouch.hpp>
12 #include <MultiWidgets/Widget.hpp>
13 
14 namespace Showcase
15 {
16  template <typename T> class ValueStream;
17 
18 namespace Utils
19 {
20  enum {
21  OBJECT_GENERIC = 0x1,
22  OBJECT_MENU = 0x1 << 1,
23  OBJECT_EXCLUDE_FROM_SNAPSHOT = 0x1 << 2,
24  OBJECT_ANNOTATION = 0x1 << 3
25  };
26 
27  SHOWCASE_API QString protocol(const NodeId& id);
28  SHOWCASE_API bool hasProtocol(const QString& url);
29  SHOWCASE_API bool hasProtocol(const QString& url, const QString& protocol);
30  SHOWCASE_API QString toString(const QVariant& variant);
31  SHOWCASE_API QVariant readJson(const QString& path);
32 
36  SHOWCASE_API QByteArray formAssetKey(const QByteArray & refenceKey,
37  const QByteArray & propertyKey);
38  SHOWCASE_API QByteArray assetPrefix();
39 
40  SHOWCASE_API QString assetSearchPathName();
41 
42  SHOWCASE_API bool hasProtocol(const GraphNode& node, const QString& protocol);
43  SHOWCASE_API bool hasProtocol(const NodeId& id, const QString& protocol);
44  SHOWCASE_API QByteArray withoutProtocol(const NodeId& id);
45  SHOWCASE_API QString constructFullFilePath(const QString& fullPath, const QString& relative);
46  SHOWCASE_API void addCSSfile(const QString& path);
47  SHOWCASE_API void removeCSSfile(const QString& path);
48 
49  SHOWCASE_API QByteArray pinnedWidgetCSSClass();
50  SHOWCASE_API QByteArray pinToggleEventName();
51  SHOWCASE_API QByteArray mouseModeCSSClass();
52  SHOWCASE_API QByteArray mouseModeToggleEventName();
53  SHOWCASE_API float uiHideScale();
54 
55  SHOWCASE_API QString temporaryWidgetCSSClass();
56 
58  SHOWCASE_API float getHandOrientation(const MultiTouch::Hand & hand);
60  SHOWCASE_API float getHandInteractionOrientation(MultiWidgets::Widget & widget);
61 
64  SHOWCASE_API Nimble::Rect filteredChildrenRect(const MultiWidgets::Widget & w);
67  SHOWCASE_API Nimble::Rect filteredWidgetSceneBounds(const MultiWidgets::Widget & w);
68 
69  template <typename T>
70  struct IsGraphEvent {
71  static const bool value = std::is_same<T, PropertyEvent>::value ||
72  std::is_same<T, NodeEvent>::value;
73  };
74 
75 
77  template <typename StreamType,
78  typename T = typename StreamType::ValueType,
79  typename = typename std::enable_if<std::is_base_of<ValueStream<T>, StreamType>::value>::type
80  >
81  void forEachSync(std::shared_ptr<StreamType> stream,
82  const std::function<void(const T&)>& f, int max=-1)
83  {
84  static_assert(IsGraphEvent<T>::value, "This function is only safe to"
85  "use with NodeEvent or PropertyEvent.");
86 
87  static std::function<bool(const T&)> test = [](const T& ev) {
88  return ev.isPending();
89  };
90 
91  if(max < 0) {
92  const int blockSize = 1000;
93  int handled;
94  do {
95  handled = stream->forEachSync(f, blockSize, test);
96  } while (handled == blockSize);
97  } else {
98  stream->forEachSync(f, max, test);
99  }
100  }
101 
102  template <typename T>
103  FactoryKey<T> registerFactory(FactoryMap<T>& factories, const QString& name,
104  ShowcaseFactoryPtr<T> factory)
105  {
106  auto it = factories.find(name);
107  if(it != factories.end()) {
109  return factories.end();
110  }
111  return factories.insert(std::make_pair(name, factory)).first;
112  }
113 
114  template <typename T>
115  bool unregisterFactory(FactoryMap<T>& factories, FactoryKey<T> key)
116  {
117  if(key == factories.end()) {
118  return false;
119  }
120  factories.erase(key);
121  return true;
122  }
123 
124  template<typename T>
125  Radiant::IntrusivePtr<T> parentOfType(const MultiWidgets::Widget & w)
126  {
127  auto parent = w.parent();
128  while(parent) {
129  auto candidate = parent.dynamic_pointer_cast<T>();
130  if(candidate)
131  return candidate;
132  parent = parent->parent();
133  }
134  return nullptr;
135  }
136 }
137 }