Developer Documentation
Types.cc
Go to the documentation of this file.
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 //=============================================================================
43 //
44 // Types
45 //
46 //=============================================================================
47 
54 //== INCLUDES =================================================================
55 
56 #include "TypesInternal.hh"
57 
60 
61 #include <QCoreApplication>
62 
63 
67 static int nextTypeId_ = 2;
68 
71 static std::map< DataType, QString > typeToString;
72 
75 static std::map< QString , size_t > stringToTypeInfo;
76 
79 static std::map< DataType , size_t > typeToTypeInfo;
80 
81 static QIcon dummyIcon;
82 
83 static std::vector< TypeInfo > types;
84 
85 //== Functions =========================================================
86 
87 std::ostream &operator<<(std::ostream &stream, DataType type)
88 {
89  stream << type.value() ;
90 
91  return stream;
92 }
93 
94 void initializeTypes() {
95  stringToTypeInfo["Unknown"] = types.size();
97  typeToTypeInfo[test] = types.size();
98  types.push_back( TypeInfo(DATA_UNKNOWN ,"Unknown" ,"Unknown.png", QCoreApplication::translate("Types","Unknown")) );
99 
100  stringToTypeInfo["Group"] = types.size();
101  typeToTypeInfo[DATA_GROUP] = types.size();
102  types.push_back( TypeInfo(DATA_GROUP ,"Group" ,"group.png", QCoreApplication::translate("Types","Group")) );
103 
104  stringToTypeInfo["All"] = types.size();
105  typeToTypeInfo[DATA_ALL] = types.size();
106  types.push_back( TypeInfo(DATA_ALL ,"All" ,"Unknown.png", QCoreApplication::translate("Types","All")) );
107 
108  typeToString[DATA_UNKNOWN] = "Unknown";
109  typeToString[DATA_GROUP] = "Group";
110  typeToString[DATA_ALL] = "All";
111 
112  // Preload the static icons
113  setTypeIcon(DATA_GROUP,"group.png");
114 }
115 
117 DataType addDataType(QString _name, QString _readableName) {
118 
119  // Check if datatype already exists.
120  // If so, we return the typeId that is used for it
121  if ( typeExists(_name) ) {
122  std::cerr << "Redefinition of existing data type!" << std::endl;
123  return typeId(_name);
124  }
125 
126  int type = nextTypeId_;
127 
128  stringToTypeInfo[ _name ] = types.size();
129  typeToTypeInfo[ type ] = types.size();
130  types.push_back( TypeInfo(type, _name, "Unknown.png", _readableName ));
131 
132  typeToString[type] = _name;
133 
134  nextTypeId_ *= 2;
135  return( type );
136 }
137 
139 DataType typeId(QString _name) {
140 
141  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _name );
142 
143  if ( index != stringToTypeInfo.end() )
144  return types[ index->second ].type;
145  else {
146  #ifdef DEBUG
147  std::cerr << "Unknown Data type with name " << _name.toStdString() << std::endl;
148  #endif
149  return DATA_UNKNOWN;
150  }
151 }
152 
154 QString typeName(DataType _id) {
155 
156  std::map<DataType, QString>::iterator name = typeToString.find(_id);
157 
158  if ( name != typeToString.end() )
159  return name->second;
160  else {
161  #ifdef DEBUG
162  std::cerr << "Unable to retrieve typeName for id " << _id << std::endl;
163  #endif
164  return "Unknown";
165  }
166 }
167 
169 bool typeExists( QString _name ) {
170  return ( stringToTypeInfo.find( _name ) != stringToTypeInfo.end() );
171 }
172 
173 
175 size_t typeCount() {
176  return types.size();
177 }
178 
180 std::vector< TypeInfo >::const_iterator typesBegin() {
181  return types.begin();
182 }
183 
185 std::vector< TypeInfo >::const_iterator typesEnd() {
186  return types.end();
187 }
188 
190 QString typeIconName(QString _name) {
191 
192  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _name );
193 
194  if ( index != stringToTypeInfo.end() )
195  return types[ index->second ].iconName;
196  else
197  return "Unknown.png";
198 }
199 
201 QString typeIconName(DataType _id) {
202 
203  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
204 
205  if ( index != typeToTypeInfo.end() )
206  return types[ index->second ].iconName;
207  else
208  return "Unknown.png";
209 }
210 
212 QIcon& typeIcon(DataType _id) {
213 
214  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
215 
216  if ( index != typeToTypeInfo.end() )
217  return types[ index->second ].icon;
218  else
219  return dummyIcon;
220 }
221 
223 void setTypeIcon( DataType _id , QString _icon ) {
224 
225  if ( OpenFlipper::Options::gui() ) {
226  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
227 
228  if ( index != typeToTypeInfo.end() ) {
229  types[ index->second ].iconName = _icon;
230  types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
231  } else
232  std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
233  }
234 }
235 
237 void setTypeIcon( QString _name , QString _icon ) {
238 
239  if ( OpenFlipper::Options::gui() ) {
240  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _name );
241 
242  if ( index != stringToTypeInfo.end() ) {
243  types[ index->second ].iconName = _icon;
244  types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
245  } else
246  std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
247  }
248 }
249 
250 
252 QString dataTypeName( DataType _id ) {
253 
254  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
255 
256  if ( index != typeToTypeInfo.end() )
257  return types[ index->second ].readableName ;
258  else
259  std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;
260 
261  return QString(QCoreApplication::translate("Types","Unknown Type"));
262 }
263 
265 QString dataTypeName( QString _typeName ) {
266 
267  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _typeName );
268 
269  if ( index != stringToTypeInfo.end() )
270  return types[ index->second ].readableName ;
271  else
272  std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;
273 
274  return QString(QCoreApplication::translate("Types","Unknown Type"));
275 }
276 
277 
278 
280 void setDataTypeName( DataType _id , QString _name ) {
281 
282  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
283 
284  if ( index != typeToTypeInfo.end() )
285  types[ index->second ].readableName = _name;
286  else
287  std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
288 }
289 
291 void setDataTypeName( QString _typeName , QString _name ) {
292 
293  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _typeName );
294 
295  if ( index != stringToTypeInfo.end() )
296  types[ index->second ].readableName = _name;
297  else
298  std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
299 }
300 
301 
302 DataType::DataType():
303  field(0)
304 {
305 };
306 
307 DataType::DataType(const unsigned int& _i):
308  field(_i)
309 {
310 };
311 
312 //===========================================
313 
314 bool DataType::operator!=( const unsigned int& _i ) const{
315  return (_i != field);
316 }
317 
318 bool DataType::operator!=( const DataType& _i ) const{
319  return (field != _i.field);
320 }
321 
322 //===========================================
323 
324 bool DataType::operator==( const unsigned int& _i ) const {
325  return (_i == field);
326 }
327 
328 bool DataType::operator==( const DataType& _i ) const{
329  return (_i.field == field);
330 }
331 
332 //===========================================
333 
334 DataType& DataType::operator=( const unsigned int& _i ) {
335  field = _i;
336  return (*this);
337 }
338 
339 DataType& DataType::operator=( const DataType& _i ) {
340  field = _i.field;
341  return (*this);
342 }
343 
344 //===========================================
345 
346 bool DataType::operator<( const unsigned int& _i ) const {
347  return (field < _i);
348 }
349 
350 bool DataType::operator<( const DataType& _i ) const {
351  return (field < _i.field);
352 }
353 
354 //===========================================
355 
356 bool DataType::operator&( const unsigned int& _i ) const {
357  return (field & _i);
358 }
359 
360 bool DataType::operator&( const DataType& _i ) const {
361  return (field & _i.field);
362 }
363 
364 //===========================================
365 
366 DataType DataType::operator!() {
367  DataType inv = (*this);
368  inv.field = !inv.field;
369  return inv;
370 }
371 
372 //===========================================
373 
374 bool DataType::contains( const DataType& _i )const{
375  //its not magic
376  return ( (_i.field & field) == _i.field);
377 }
378 
379 //===========================================
380 
381 DataType& DataType::operator|=( const unsigned int& _i ) {
382  field |= _i;
383  return (*this);
384 }
385 
386 DataType& DataType::operator|=( const DataType& _i ) {
387  field |= _i.field ;
388  return (*this);
389 }
390 
391 //===========================================
392 
393 DataType DataType::operator|( const DataType& _i ) const {
394  return (field | _i.field);
395 }
396 
397 //===========================================
398 
399 DataType DataType::operator++(int /*_unused*/) {
400  return (field *= 2);
401 }
402 
403 //===========================================
404 
405 DataType& DataType::operator++() {
406  field *= 2;
407  return (*this);
408 }
409 
410 //===========================================
411 
412 unsigned int DataType::value() const {
413  return( field );
414 }
415 
416 QString DataType::name() const {
417  return typeName(field);
418 }
419 
420 //=============================================================================
421 
423  qRegisterMetaType<IdList>("IdList");
424  qRegisterMetaType<DataType>("DataType");
425  qRegisterMetaType< QVector< int > >("QVector<int>");
426  qRegisterMetaType<Vector>("Vector");
427  qRegisterMetaType<Vector4>("Vector4");
428  qRegisterMetaType<Matrix4x4>("Matrix4x4");
429  qRegisterMetaType<UpdateType>("UpdateType");
430  qRegisterMetaType<Logtype>("LogType");
431 }
432 
433 //=============================================================================
434 //=============================================================================
size_t typeCount()
Return the number of registered types.
Definition: Types.cc:175
std::vector< TypeInfo >::const_iterator typesBegin()
Get iterator pointing to the first element in the tyoes list.
Definition: Types.cc:180
void setTypeIcon(DataType _id, QString _icon)
Set the icon for a given dataType.
Definition: Types.cc:223
QString typeIconName(QString _name)
Get the icon of a given dataType.
Definition: Types.cc:190
DataType typeId(QString _name)
Get the id of a type with given name.
Definition: Types.cc:139
QString typeName(DataType _id)
Get the name of a type with given id.
Definition: Types.cc:154
std::vector< TypeInfo >::const_iterator typesEnd()
Get iterator pointing to the last element in the tyoes list.
Definition: Types.cc:185
const DataType DATA_UNKNOWN(0)
None of the other Objects.
std::ostream & operator<<(std::ostream &_o, const Timer &_t)
Definition: Timer.hh:199
QString name() const
Return the name of this type as text.
Definition: Types.cc:416
void registerTypes()
Definition: Types.cc:422
bool typeExists(QString _name)
Check if a type with the given name exists.
Definition: Types.cc:169
const DataType DATA_GROUP(1)
Items used for Grouping.
Predefined datatypes.
Definition: DataTypes.hh:83
unsigned int value() const
Definition: Types.cc:412
void setDataTypeName(DataType _id, QString _name)
Set the icon for a given dataType.
Definition: Types.cc:280
QIcon & typeIcon(DataType _id)
get the icon of a given dataType
Definition: Types.cc:212
QString dataTypeName(DataType _id)
Get DataType Human readable name ( this name might change. Use the typeName insted! ) ...
Definition: Types.cc:252
DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
Definition: Types.cc:117
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.