Types.cc 12.9 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59




//=============================================================================
//
//  Types
//
//=============================================================================

/**
 * \file Types.cc
 * This File contains the basic functions to handle datatypes
 */


//== INCLUDES =================================================================

60
#include "TypesInternal.hh"
61
#include "DataTypes.hh"
62
#include <map>
63
#include <QCoreApplication>
64
#include <OpenFlipper/common/GlobalOptions.hh>
65
66
67
68
69


/** This field defines the start id for custom datatypes. It starts high to avoid conflicts with previously
 * hardcoded versions.
 */
70
static int nextTypeId_ = 2;
71
72
73

/** This map maps an dataType id to an typeName
 */
74
static std::map< DataType, QString > typeToString;
75

76
/** This map maps an dataType name to its id in the types vector
77
 */
78
static std::map< QString , size_t > stringToTypeInfo;
79
80
81

/** This map maps an dataType id to its id in the types vector
 */
82
static std::map< DataType , size_t > typeToTypeInfo;
83

84
85
static QIcon dummyIcon;

86
87
88
89
static std::vector< TypeInfo > types;

//== Functions =========================================================

90
91
92
93
94
95
96
std::ostream &operator<<(std::ostream &stream, DataType type)
{
  stream << type.value() ;
  
  return stream;
}

97
98
void initializeTypes() {
  stringToTypeInfo["Unknown"] = types.size();
99
100
  DataType test(DATA_UNKNOWN);
  typeToTypeInfo[test]   = types.size();
101
102
103
104
  types.push_back( TypeInfo(DATA_UNKNOWN            ,"Unknown"        ,"Unknown.png", QCoreApplication::translate("Types","Unknown")) );

  stringToTypeInfo["Group"]  = types.size();
  typeToTypeInfo[DATA_GROUP] = types.size();
Jan Möbius's avatar
Jan Möbius committed
105
  types.push_back( TypeInfo(DATA_GROUP           ,"Group"          ,"group.png", QCoreApplication::translate("Types","Group")) );
106
107
108
109
110
111
112
113
114
115

  stringToTypeInfo["All"]  = types.size();
  typeToTypeInfo[DATA_ALL] = types.size();
  types.push_back( TypeInfo(DATA_ALL             ,"All"            ,"Unknown.png", QCoreApplication::translate("Types","All")) );

  typeToString[DATA_UNKNOWN] = "Unknown";
  typeToString[DATA_GROUP] = "Group";
  typeToString[DATA_ALL] = "All";
  
  // Preload the static icons
Jan Möbius's avatar
Jan Möbius committed
116
  setTypeIcon(DATA_GROUP,"group.png");
117
118
}

119
/// Adds a datatype and returns the id for the new type
120
DataType addDataType(QString _name, QString _readableName) {
121
  
122
123
  // Check if datatype already exists.
  // If so, we return the typeId that is used for it
124
  if ( typeExists(_name) ) {
125
126
127
128
    std::cerr << "Redefinition of existing data type!" << std::endl;
    return typeId(_name);
  }
  
129
130
131
132
  int type = nextTypeId_;

  stringToTypeInfo[ _name ] = types.size();
  typeToTypeInfo[ type ] = types.size();
133
  types.push_back( TypeInfo(type, _name, "Unknown.png", _readableName ));
134
135
136

  typeToString[type] = _name;

137
  nextTypeId_ *= 2;
138
  return( type );
139
140
}

141
/// Get the id of a type with given name
142
DataType typeId(QString _name) {
143

144
  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _name );
145
146
147

  if ( index != stringToTypeInfo.end() )
    return types[ index->second ].type;
148
  else {
149
    #ifdef DEBUG
150
    std::cerr << "Unknown Data type with name " << _name.toStdString() << std::endl;
151
    #endif
152
    return DATA_UNKNOWN;
153
  }
154
155
156
157
158
}

/// Get the name of a type with given id
QString typeName(DataType _id) {

159
  std::map<DataType, QString>::iterator name = typeToString.find(_id);
160
161
162

  if ( name != typeToString.end() )
    return name->second;
163
  else {
164
    #ifdef DEBUG
165
    std::cerr << "Unable to retrieve typeName for id " << _id << std::endl;
166
    #endif
167
    return "Unknown";
168
  }
169
170
}

171
172
173
174
175
/// Check if a type with the given name exists
bool typeExists( QString _name ) {
  return ( stringToTypeInfo.find( _name ) !=  stringToTypeInfo.end() );
}

176

177
/// Return the number of registered types
178
size_t typeCount() {
179
180
181
  return types.size();
}

182
183
184
185
186
187
188
189
190
191
/// Get iterator pointing to the first element in the tyoes list
std::vector< TypeInfo >::const_iterator typesBegin() {
    return types.begin();
}

/// Get iterator pointing to the last element in the tyoes list
std::vector< TypeInfo >::const_iterator typesEnd() {
    return types.end();
}

192
/// Get the icon of a given dataType
193
QString typeIconName(QString  _name) {
194

195
  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _name );
196
197
198
199
200

  if ( index != stringToTypeInfo.end() )
    return types[ index->second ].iconName;
  else
    return "Unknown.png";
201
202
}

203
/// get the icon of a given dataType
204
QString typeIconName(DataType _id) {
205

206
  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
207
208
209
210
211

  if ( index != typeToTypeInfo.end() )
    return types[ index->second ].iconName;
  else
    return "Unknown.png";
212
213
}

214
215
216
/// get the icon of a given dataType
QIcon& typeIcon(DataType _id) {
  
217
  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
218
219
220
221
222
223
224
  
  if ( index != typeToTypeInfo.end() )
    return types[ index->second ].icon;
  else
    return dummyIcon;
}

225
/// Set the icon for a given dataType
226
227
void setTypeIcon( DataType _id   , QString _icon ) {

228
  if (  OpenFlipper::Options::gui() ) {
229
    std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
230
231
232
233
234
235
236

    if ( index != typeToTypeInfo.end() ) {
      types[ index->second ].iconName = _icon;
      types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
    } else
      std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
  }
237
238
}

239
/// Set the icon for a given dataType
240
241
void setTypeIcon( QString  _name , QString _icon ) {

242
  if (  OpenFlipper::Options::gui() ) {
243
    std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _name );
244

245
246
247
248
249
250
    if ( index != stringToTypeInfo.end() ) {
      types[ index->second ].iconName = _icon;
      types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
    } else
      std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
  }
251
252
}

253
254
255
256

/// Get DataType Human readable name ( this name might change. Use the typeName insted! )
QString dataTypeName( DataType _id ) {

257
  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
258
259
260
261

  if ( index != typeToTypeInfo.end() )
    return types[ index->second ].readableName ;
  else
262
263
    std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;

264
265
266
267
268
269
  return QString(QCoreApplication::translate("Types","Unknown Type"));
}

/// Get DataType Human readable name ( this name might change. Use the typeName insted! )
QString dataTypeName( QString  _typeName ) {

270
  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _typeName );
271
272
273
274

  if ( index != stringToTypeInfo.end() )
    return types[ index->second ].readableName ;
  else
275
276
    std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;

277
278
279
280
281
282
283
284
  return QString(QCoreApplication::translate("Types","Unknown Type"));
}



/// Set the icon for a given dataType
void setDataTypeName( DataType _id   , QString _name ) {

285
  std::map<DataType, size_t>::iterator index = typeToTypeInfo.find(_id);
286
287
288
289

  if ( index != typeToTypeInfo.end() )
    types[ index->second ].readableName = _name;
  else
290
    std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
291
292
293
294
295
}

/// Set the icon for a given dataType
void setDataTypeName( QString  _typeName , QString _name ) {

296
  std::map<QString, size_t>::iterator index = stringToTypeInfo.find( _typeName );
297
298
299
300

  if ( index != stringToTypeInfo.end() )
    types[ index->second ].readableName = _name;
  else
301
    std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
302
303
}

304
305
306
307
308
309
310
311
312
313
314
315
316

DataType::DataType():
  field(0)
{
};

DataType::DataType(const unsigned int& _i):
  field(_i)
{
};

//===========================================

Matthias Möller's avatar
Matthias Möller committed
317
bool DataType::operator!=( const unsigned int& _i ) const{
318
319
320
  return (_i != field); 
}

Matthias Möller's avatar
Matthias Möller committed
321
bool DataType::operator!=( const DataType& _i ) const{
322
323
324
325
326
  return (field != _i.field); 
}    

//===========================================

Matthias Möller's avatar
Matthias Möller committed
327
bool DataType::operator==( const unsigned int& _i ) const {
328
329
330
  return (_i == field); 
}

Matthias Möller's avatar
Matthias Möller committed
331
bool DataType::operator==(  const DataType& _i ) const{
332
333
334
335
336
  return (_i.field == field); 
}

//===========================================

337
338
339
DataType& DataType::operator=( const unsigned int& _i ) {
  field = _i;
  return (*this);
340
341
}

342
343
344
DataType& DataType::operator=( const DataType& _i ) {
  field = _i.field;
  return (*this);
345
346
347
348
}    

//===========================================

Matthias Möller's avatar
Matthias Möller committed
349
bool DataType::operator<( const unsigned int& _i ) const {
350
351
352
353
354
355
356
357
358
  return (field < _i); 
}

bool DataType::operator<( const DataType& _i ) const {
  return (field < _i.field); 
}

//===========================================

Matthias Möller's avatar
Matthias Möller committed
359
bool DataType::operator&( const unsigned int& _i ) const {
360
361
362
363
364
365
366
367
368
  return (field & _i); 
}

bool DataType::operator&( const DataType& _i ) const {
  return (field & _i.field); 
}    

//===========================================

369
370
371
372
373
374
375
376
DataType DataType::operator!() {
  DataType inv = (*this);
  inv.field = !inv.field;
  return inv; 
}

//===========================================

Matthias Möller's avatar
Matthias Möller committed
377
bool DataType::contains( const DataType& _i )const{
378
379
380
381
382
383
  //its not magic
  return ( (_i.field & field) == _i.field);
}

//===========================================

Jan Möbius's avatar
Jan Möbius committed
384
385
386
DataType& DataType::operator|=( const unsigned int& _i ) {
  field |= _i;
  return (*this);
Jan Möbius's avatar
Jan Möbius committed
387
388
}

Jan Möbius's avatar
Jan Möbius committed
389
390
391
DataType& DataType::operator|=( const DataType& _i )  {
  field |= _i.field ;
  return (*this);
Jan Möbius's avatar
Jan Möbius committed
392
393
394
395
}

//===========================================

396
397
398
399
400
401
DataType DataType::operator|( const DataType& _i ) const {
  return (field | _i.field); 
}    

//===========================================

402
403
404
405
406
407
408
409
410
DataType DataType::operator++(int /*_unused*/) {
  return (field *= 2);
}

//===========================================

DataType& DataType::operator++() {
  field *= 2;
  return (*this);
411
412
413
414
415
416
417
418
}  

//===========================================

unsigned int DataType::value() const {
  return( field );
}

Matthias Möller's avatar
Matthias Möller committed
419
QString DataType::name() const {
420
421
422
423
  return typeName(field);
}


424
425
//=============================================================================
//=============================================================================