Types.cc 12.9 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
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
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen       *
*                           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 79 80 81
static std::map< QString , unsigned int > stringToTypeInfo;

/** This map maps an dataType id to its id in the types vector
 */
82
static std::map< DataType , unsigned int > typeToTypeInfo;
83 84 85 86 87

class TypeInfo {

  public:

88
  TypeInfo(DataType _type, QString _name, QString _iconName, QString _readableName ) :
89 90
    type(_type),
    name(_name),
91 92 93 94 95 96 97
    iconName(_iconName),
    readableName(_readableName)
  {
    // Use internal name if no external name is given
    if ( _readableName == "" )
      readableName = _name;
  }
98 99 100 101 102 103 104 105 106

  /// The id of the datatype
  DataType type;

  /// The name of the datatype
  QString  name;

  /// The icon of the datatype
  QString  iconName;
107 108
  
  QIcon    icon;
109

110
  /// Human readable name
111
  QString readableName;
112 113
};

114 115
static QIcon dummyIcon;

116 117 118 119
static std::vector< TypeInfo > types;

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

120 121 122 123 124 125 126
std::ostream &operator<<(std::ostream &stream, DataType type)
{
  stream << type.value() ;
  
  return stream;
}

127 128
void initializeTypes() {
  stringToTypeInfo["Unknown"] = types.size();
129 130
  DataType test(DATA_UNKNOWN);
  typeToTypeInfo[test]   = types.size();
131 132 133 134
  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
135
  types.push_back( TypeInfo(DATA_GROUP           ,"Group"          ,"group.png", QCoreApplication::translate("Types","Group")) );
136 137 138 139 140 141 142 143 144 145

  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
146
  setTypeIcon(DATA_GROUP,"group.png");
147 148
}

149
/// Adds a datatype and returns the id for the new type
150
DataType addDataType(QString _name, QString _readableName) {
151
  
152 153
  // Check if datatype already exists.
  // If so, we return the typeId that is used for it
154
  if ( typeExists(_name) ) {
155 156 157 158
    std::cerr << "Redefinition of existing data type!" << std::endl;
    return typeId(_name);
  }
  
159 160 161 162
  int type = nextTypeId_;

  stringToTypeInfo[ _name ] = types.size();
  typeToTypeInfo[ type ] = types.size();
163
  types.push_back( TypeInfo(type, _name, "Unknown.png", _readableName ));
164 165 166

  typeToString[type] = _name;

167
  nextTypeId_ *= 2;
168
  return( type );
169 170
}

171
/// Get the id of a type with given name
172
DataType typeId(QString _name) {
173 174 175 176 177

  std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );

  if ( index != stringToTypeInfo.end() )
    return types[ index->second ].type;
178
  else {
179
    #ifdef DEBUG
180
    std::cerr << "Unknown Data type with name " << _name.toStdString() << std::endl;
181
    #endif
182
    return DATA_UNKNOWN;
183
  }
184 185 186 187 188
}

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

189
  std::map<DataType, QString>::iterator name = typeToString.find(_id);
190 191 192

  if ( name != typeToString.end() )
    return name->second;
193
  else {
194
    #ifdef DEBUG
195
    std::cerr << "Unable to retrieve typeName for id " << _id << std::endl;
196
    #endif
197
    return "Unknown";
198
  }
199 200
}

201 202 203 204 205
/// Check if a type with the given name exists
bool typeExists( QString _name ) {
  return ( stringToTypeInfo.find( _name ) !=  stringToTypeInfo.end() );
}

206

207 208 209 210 211
/// Return the number of registered types
uint typeCount() {
  return types.size();
}

212
/// Get the icon of a given dataType
213
QString typeIconName(QString  _name) {
214 215 216 217 218 219 220

  std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );

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

223
/// get the icon of a given dataType
224
QString typeIconName(DataType _id) {
225

226
  std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
227 228 229 230 231

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

234 235 236
/// get the icon of a given dataType
QIcon& typeIcon(DataType _id) {
  
237
  std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
238 239 240 241 242 243 244
  
  if ( index != typeToTypeInfo.end() )
    return types[ index->second ].icon;
  else
    return dummyIcon;
}

245
/// Set the icon for a given dataType
246 247
void setTypeIcon( DataType _id   , QString _icon ) {

248
  std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
249

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

257
/// Set the icon for a given dataType
258 259
void setTypeIcon( QString  _name , QString _icon ) {

260 261
  std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );

262
  if ( index != stringToTypeInfo.end() ) {
263
    types[ index->second ].iconName = _icon;
264 265
    types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
  } else
266
    std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
267 268
}

269 270 271 272 273 274



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

275
  std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
276 277 278 279

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

282 283 284 285 286 287 288 289 290 291 292
  return QString(QCoreApplication::translate("Types","Unknown Type"));
}

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

  std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _typeName );

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

295 296 297 298 299 300 301 302
  return QString(QCoreApplication::translate("Types","Unknown Type"));
}



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

303
  std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
304 305 306 307

  if ( index != typeToTypeInfo.end() )
    types[ index->second ].readableName = _name;
  else
308
    std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
309 310 311 312 313 314 315 316 317 318
}

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

  std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _typeName );

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

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

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

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

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

bool DataType::operator!=( const unsigned int& _i ) { 
  return (_i != field); 
}

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

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

bool DataType::operator==( const unsigned int& _i ) {
  return (_i == field); 
}

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

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

bool DataType::operator=( const unsigned int& _i ) {
  return (field = _i); 
}

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

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

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

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

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

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

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

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

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
DataType DataType::operator!() {
  DataType inv = (*this);
  inv.field = !inv.field;
  return inv; 
}

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

bool DataType::contains( const DataType& _i ){
  //its not magic
  return ( (_i.field & field) == _i.field);
}

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

Jan Möbius's avatar
Jan Möbius committed
400 401 402
DataType& DataType::operator|=( const unsigned int& _i ) {
  field |= _i;
  return (*this);
Jan Möbius's avatar
Jan Möbius committed
403 404
}

Jan Möbius's avatar
Jan Möbius committed
405 406 407
DataType& DataType::operator|=( const DataType& _i )  {
  field |= _i.field ;
  return (*this);
Jan Möbius's avatar
Jan Möbius committed
408 409 410 411
}

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

412 413 414 415 416 417
DataType DataType::operator|( const DataType& _i ) const {
  return (field | _i.field); 
}    

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

Dirk Wilden's avatar
Dirk Wilden committed
418
bool DataType::operator++(int /*_unused*/) {
419 420 421 422 423 424 425 426 427 428 429 430 431 432
  return (field *= 2); 
}  

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

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

QString DataType::name() {
  return typeName(field);
}


433 434
//=============================================================================
//=============================================================================