1 /************************************************************************
2 * file name         : common_functions.h
3 * ----------------- :
4 * creation time     : 2017/12/06
5 * author            : Victor Zarubkin
6 * email             : v.s.zarubkin@gmail.com
7 * ----------------- :
8 * description       : The file contains common functions used by different UI widgets.
9 * ----------------- :
10 * change log        : * 2017/12/06 Victor Zarubkin: Initial commit. Moved sources from common_types.h
11 *                   :
12 *                   : *
13 * ----------------- :
14 * license           : Lightweight profiler library for c++
15 *                   : Copyright(C) 2016-2017  Sergey Yagovtsev, Victor Zarubkin
16 *                   :
17 *                   : Licensed under either of
18 *                   :     * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
19 *                   :     * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
20 *                   : at your option.
21 *                   :
22 *                   : The MIT License
23 *                   :
24 *                   : Permission is hereby granted, free of charge, to any person obtaining a copy
25 *                   : of this software and associated documentation files (the "Software"), to deal
26 *                   : in the Software without restriction, including without limitation the rights
27 *                   : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 *                   : of the Software, and to permit persons to whom the Software is furnished
29 *                   : to do so, subject to the following conditions:
30 *                   :
31 *                   : The above copyright notice and this permission notice shall be included in all
32 *                   : copies or substantial portions of the Software.
33 *                   :
34 *                   : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
35 *                   : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 *                   : PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
37 *                   : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
38 *                   : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39 *                   : USE OR OTHER DEALINGS IN THE SOFTWARE.
40 *                   :
41 *                   : The Apache License, Version 2.0 (the "License")
42 *                   :
43 *                   : You may not use this file except in compliance with the License.
44 *                   : You may obtain a copy of the License at
45 *                   :
46 *                   : http://www.apache.org/licenses/LICENSE-2.0
47 *                   :
48 *                   : Unless required by applicable law or agreed to in writing, software
49 *                   : distributed under the License is distributed on an "AS IS" BASIS,
50 *                   : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51 *                   : See the License for the specific language governing permissions and
52 *                   : limitations under the License.
53 ************************************************************************/
54 
55 #ifndef EASY_PROFILER_GUI_COMMON_FUNCTIONS_H
56 #define EASY_PROFILER_GUI_COMMON_FUNCTIONS_H
57 
58 #include <QRgb>
59 #include <QString>
60 #include <QFont>
61 #include <type_traits>
62 #include "common_types.h"
63 
64 //////////////////////////////////////////////////////////////////////////
65 //////////////////////////////////////////////////////////////////////////
66 
67 #define PROF_MICROSECONDS(timestamp) ((timestamp) * 1e-3)
68 //#define PROF_MICROSECONDS(timestamp) (timestamp)
69 
70 #define PROF_FROM_MICROSECONDS(to_timestamp) ((to_timestamp) * 1e3)
71 //#define PROF_FROM_MICROSECONDS(to_timestamp) (to_timestamp)
72 
73 #define PROF_MILLISECONDS(timestamp) ((timestamp) * 1e-6)
74 //#define PROF_MILLISECONDS(timestamp) ((timestamp) * 1e-3)
75 
76 #define PROF_FROM_MILLISECONDS(to_timestamp) ((to_timestamp) * 1e6)
77 //#define PROF_FROM_MILLISECONDS(to_timestamp) ((to_timestamp) * 1e3)
78 
79 #define PROF_NANOSECONDS(timestamp) (timestamp)
80 //#define PROF_NANOSECONDS(timestamp) ((timestamp) * 1000)
81 
82 //////////////////////////////////////////////////////////////////////////
83 
units2microseconds(qreal _value)84 EASY_FORCE_INLINE qreal units2microseconds(qreal _value) {
85     return _value;
86     //return _value * 1e3;
87 }
88 
microseconds2units(qreal _value)89 EASY_FORCE_INLINE qreal microseconds2units(qreal _value) {
90     return _value;
91     //return _value * 1e-3;
92 }
93 
94 #ifdef EASY_CONSTEXPR_AVAILABLE
95 template <class TEnum>
int_cast(TEnum _enumValue)96 EASY_FORCE_INLINE EASY_CONSTEXPR_FCN typename ::std::underlying_type<TEnum>::type int_cast(TEnum _enumValue) {
97     return static_cast<typename ::std::underlying_type<TEnum>::type>(_enumValue);
98 }
99 #else
100 # define int_cast(_enumValue) static_cast<::std::underlying_type<decltype(_enumValue)>::type>(_enumValue)
101 #endif
102 
103 //////////////////////////////////////////////////////////////////////////
104 
105 namespace profiler_gui {
106 
107 //////////////////////////////////////////////////////////////////////////
108 
109 template <class T> inline
numeric_max()110 EASY_CONSTEXPR_FCN T numeric_max() {
111     return ::std::numeric_limits<T>::max();
112 }
113 
114 template <class T> inline
numeric_max(T)115 EASY_CONSTEXPR_FCN T numeric_max(T) {
116     return ::std::numeric_limits<T>::max();
117 }
118 
119 template <class T> inline
is_max(const T & _value)120 EASY_CONSTEXPR_FCN bool is_max(const T& _value) {
121     return _value == ::std::numeric_limits<T>::max();
122 }
123 
124 template <class T> inline
set_max(T & _value)125 void set_max(T& _value) {
126     _value = ::std::numeric_limits<T>::max();
127 }
128 
129 //////////////////////////////////////////////////////////////////////////
130 
toRgb(uint32_t _red,uint32_t _green,uint32_t _blue)131 inline EASY_CONSTEXPR_FCN QRgb toRgb(uint32_t _red, uint32_t _green, uint32_t _blue) {
132     return (_red << 16) + (_green << 8) + _blue;
133 }
134 
fromProfilerRgb(uint32_t _red,uint32_t _green,uint32_t _blue)135 inline EASY_CONSTEXPR_FCN QRgb fromProfilerRgb(uint32_t _red, uint32_t _green, uint32_t _blue) {
136     return _red == 0 && _green == 0 && _blue == 0 ? ::profiler::colors::Default : toRgb(_red, _green, _blue) | 0x00141414;
137 }
138 
colorSum(::profiler::color_t _color)139 EASY_FORCE_INLINE EASY_CONSTEXPR_FCN qreal colorSum(::profiler::color_t _color) {
140     return 255. - (((_color & 0x00ff0000) >> 16) * 0.299 + ((_color & 0x0000ff00) >> 8) * 0.587 + (_color & 0x000000ff) * 0.114);
141 }
142 
isLightColor(::profiler::color_t _color)143 inline EASY_CONSTEXPR_FCN bool isLightColor(::profiler::color_t _color) {
144     return colorSum(_color) < 76.5 || ((_color & 0xff000000) >> 24) < 0x80;
145 }
146 
isLightColor(::profiler::color_t _color,qreal _maxSum)147 inline EASY_CONSTEXPR_FCN bool isLightColor(::profiler::color_t _color, qreal _maxSum) {
148     return colorSum(_color) < _maxSum || ((_color & 0xff000000) >> 24) < 0x80;
149 }
150 
textColorForFlag(bool _is_light)151 inline EASY_CONSTEXPR_FCN ::profiler::color_t textColorForFlag(bool _is_light) {
152     return _is_light ? ::profiler::colors::Dark : ::profiler::colors::CreamWhite;
153 }
154 
textColorForRgb(::profiler::color_t _color)155 inline EASY_CONSTEXPR_FCN ::profiler::color_t textColorForRgb(::profiler::color_t _color) {
156     return isLightColor(_color) ? ::profiler::colors::Dark : ::profiler::colors::CreamWhite;
157 }
158 
159 //////////////////////////////////////////////////////////////////////////
160 
161 qreal timeFactor(qreal _interval);
162 
163 QString autoTimeStringReal(qreal _interval, int _precision = 1);
164 QString autoTimeStringInt(qreal _interval);
165 QString autoTimeStringRealNs(::profiler::timestamp_t _interval, int _precision = 1);
166 QString autoTimeStringIntNs(::profiler::timestamp_t _interval);
167 
168 QString timeStringReal(TimeUnits _units, qreal _interval, int _precision = 1);
169 QString timeStringRealNs(TimeUnits _units, ::profiler::timestamp_t _interval, int _precision = 1);
170 QString timeStringInt(TimeUnits _units, qreal _interval);
171 QString timeStringIntNs(TimeUnits _units, ::profiler::timestamp_t _interval);
172 
173 //////////////////////////////////////////////////////////////////////////
174 
percentReal(::profiler::timestamp_t _partial,::profiler::timestamp_t _total)175 inline double percentReal(::profiler::timestamp_t _partial, ::profiler::timestamp_t _total) {
176     return _total != 0 ? 100. * static_cast<double>(_partial) / static_cast<double>(_total) : 0.;
177 }
178 
percent(::profiler::timestamp_t _partial,::profiler::timestamp_t _total)179 inline int percent(::profiler::timestamp_t _partial, ::profiler::timestamp_t _total) {
180     return static_cast<int>(0.5 + percentReal(_partial, _total));
181 }
182 
183 //////////////////////////////////////////////////////////////////////////
184 
185 QFont EFont(QFont::StyleHint _hint, const char* _family, int _size, int _weight = -1);
186 
187 inline QFont EFont(const char* _family, int _size, int _weight = -1) {
188     return EFont(QFont::Helvetica, _family, _size, _weight);
189 }
190 
191 //////////////////////////////////////////////////////////////////////////
192 
193 QString valueTypeString(::profiler::DataType _dataType);
194 QString valueTypeString(const ::profiler::ArbitraryValue& _serializedValue);
195 QString valueString(const ::profiler::ArbitraryValue& _serializedValue);
196 double value2real(const ::profiler::ArbitraryValue& _serializedValue, int _index = 0);
197 
198 //////////////////////////////////////////////////////////////////////////
199 
200 } // END of namespace profiler_gui.
201 
202 //////////////////////////////////////////////////////////////////////////
203 //////////////////////////////////////////////////////////////////////////
204 
205 #endif // EASY_PROFILER_GUI_COMMON_FUNCTIONS_H
206