Types.cc 11 KB
Newer Older
1 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 33 34 35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 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/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61




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

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


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

#include "Types.hh"
#include <map>
62
#include <QCoreApplication>
63
#include <OpenFlipper/common/GlobalOptions.hh>
64 65 66 67 68


/** This field defines the start id for custom datatypes. It starts high to avoid conflicts with previously
 * hardcoded versions.
 */
Jan Möbius's avatar
Jan Möbius committed
69
static int nextTypeId_ = 8;
70 71 72

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

75
/** This map maps an dataType name to its id in the types vector
76
 */
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
 */
static std::map< unsigned int , unsigned int > typeToTypeInfo;
82 83 84 85 86

class TypeInfo {

  public:

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

  /// The id of the datatype
  DataType type;

  /// The name of the datatype
  QString  name;

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

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

113 114
static QIcon dummyIcon;

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

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

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

  stringToTypeInfo["TriangleMesh"]   = types.size();
  typeToTypeInfo[DATA_TRIANGLE_MESH_CONST_ID] = types.size();
  types.push_back( TypeInfo(DATA_TRIANGLE_MESH_CONST_ID   ,"TriangleMesh"   ,"TriangleType.png", QCoreApplication::translate("Types","Triangle Mesh")) );
  
  stringToTypeInfo["PolyMesh"]   = types.size();
  typeToTypeInfo[DATA_POLY_MESH_CONST_ID] = types.size();
  types.push_back( TypeInfo(DATA_POLY_MESH_CONST_ID       ,"PolyMesh"       ,"PolyType.png", QCoreApplication::translate("Types","Poly Mesh")) );

  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_TRIANGLE_MESH_CONST_ID] = "TriangleMesh";
  typeToString[DATA_POLY_MESH_CONST_ID] = "PolyMesh";
  typeToString[DATA_ALL] = "All";
  
  // Preload the static icons
Jan Möbius's avatar
Jan Möbius committed
147
  setTypeIcon(DATA_GROUP,"group.png");
148 149
  setTypeIcon(DATA_TRIANGLE_MESH_CONST_ID,"TriangleType.png");
  setTypeIcon(DATA_POLY_MESH_CONST_ID,"PolyType.png");
150 151 152

}

153
/// Adds a datatype and returns the id for the new type
154
DataType addDataType(QString _name, QString _readableName) {
155
  
156 157 158 159
  int type = nextTypeId_;

  stringToTypeInfo[ _name ] = types.size();
  typeToTypeInfo[ type ] = types.size();
160
  types.push_back( TypeInfo(type, _name, "Unknown.png", _readableName ));
161 162 163

  typeToString[type] = _name;

164
  nextTypeId_ *= 2;
165
  return( type );
166 167
}

168
/// Get the id of a type with given name
169
DataType typeId(QString _name) {
170 171 172 173 174

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

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

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

  std::map<unsigned int, QString>::iterator name = typeToString.find(_id);

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

198

199 200 201 202 203
/// Return the number of registered types
uint typeCount() {
  return types.size();
}

204
/// Get the icon of a given dataType
205
QString typeIconName(QString  _name) {
206 207 208 209 210 211 212

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

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

215
/// get the icon of a given dataType
216
QString typeIconName(DataType _id) {
217 218 219 220 221 222 223

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

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

226 227 228 229 230 231 232 233 234 235 236
/// get the icon of a given dataType
QIcon& typeIcon(DataType _id) {
  
  std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);
  
  if ( index != typeToTypeInfo.end() )
    return types[ index->second ].icon;
  else
    return dummyIcon;
}

237
/// Set the icon for a given dataType
238 239
void setTypeIcon( DataType _id   , QString _icon ) {

240 241
  std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);

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

249
/// Set the icon for a given dataType
250 251
void setTypeIcon( QString  _name , QString _icon ) {

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

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

261 262 263 264 265 266 267 268 269 270 271



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

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

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

274 275 276 277 278 279 280 281 282 283 284
  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
285 286
    std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;

287 288 289 290 291 292 293 294 295 296 297 298 299
  return QString(QCoreApplication::translate("Types","Unknown Type"));
}



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

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

  if ( index != typeToTypeInfo.end() )
    types[ index->second ].readableName = _name;
  else
300
    std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
301 302 303 304 305 306 307 308 309 310
}

/// 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
311
    std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
312 313
}

314 315
//=============================================================================
//=============================================================================