Types.cc 7.8 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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
Jan Möbius's avatar
Jan Möbius committed
26 27 28
//   $Revision$
//   $Author$
//   $Date$
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//
//=============================================================================




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

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


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

#include "Types.hh"
#include <map>


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

/// Variable used to automatically initialize the maps
static bool initialized_ = false;

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

65
/** This map maps an dataType name to its id in the types vector
66
 */
67 68 69 70 71
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;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

class TypeInfo {

  public:

  TypeInfo(DataType _type, QString _name, QString _iconName) :
    type(_type),
    name(_name),
    iconName(_iconName)
  {}

  /// The id of the datatype
  DataType type;

  /// The name of the datatype
  QString  name;

  /// The icon of the datatype
  QString  iconName;
};

static std::vector< TypeInfo > types;

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

void initialize() {
  if ( !initialized_ ) {
99 100
    stringToTypeInfo["Unknown"] = types.size();
    typeToTypeInfo[DATA_NONE]   = types.size();
101
    types.push_back( TypeInfo(DATA_NONE            ,"Unknown"        ,"Unknown.png") );
102 103
    stringToTypeInfo["Group"]  = types.size();
    typeToTypeInfo[DATA_GROUP] = types.size();
104
    types.push_back( TypeInfo(DATA_GROUP           ,"Group"          ,"Unknown.png") );
105 106
    stringToTypeInfo["Points"]  = types.size();
    typeToTypeInfo[DATA_POINTS] = types.size();
107 108
    types.push_back( TypeInfo(DATA_POINTS          ,"Points"         ,"Unknown.png") );

109 110
    stringToTypeInfo["PolyLine"]   = types.size();
    typeToTypeInfo[DATA_POLY_LINE] = types.size();
111
    types.push_back( TypeInfo(DATA_POLY_LINE       ,"PolyLine"       ,"PolyLineType.png") );
112 113
    stringToTypeInfo["TriangleMesh"]   = types.size();
    typeToTypeInfo[DATA_TRIANGLE_MESH] = types.size();
114
    types.push_back( TypeInfo(DATA_TRIANGLE_MESH   ,"TriangleMesh"   ,"TriangleType.png") );
115 116
    stringToTypeInfo["PolyMesh"]   = types.size();
    typeToTypeInfo[DATA_POLY_MESH] = types.size();
117 118
    types.push_back( TypeInfo(DATA_POLY_MESH       ,"PolyMesh"       ,"PolyType.png") );

119 120
    stringToTypeInfo["BSplineCurve"]   = types.size();
    typeToTypeInfo[DATA_BSPLINE_CURVE] = types.size();
121
    types.push_back( TypeInfo(DATA_BSPLINE_CURVE   ,"BSplineCurve"   ,"BSplineCurveType.png") );
122 123
    stringToTypeInfo["Volume"]  = types.size();
    typeToTypeInfo[DATA_VOLUME] = types.size();
124
    types.push_back( TypeInfo(DATA_VOLUME          ,"Volume"         ,"Unknown.png") );
125 126
    stringToTypeInfo["BSplineSurface"]   = types.size();
    typeToTypeInfo[DATA_BSPLINE_SURFACE] = types.size();
127 128
    types.push_back( TypeInfo(DATA_BSPLINE_SURFACE ,"BSplineSurface" ,"Unknown.png") );

129 130
    stringToTypeInfo["Skeleton"]  = types.size();
    typeToTypeInfo[DATA_SKELETON] = types.size();
131
    types.push_back( TypeInfo(DATA_SKELETON        ,"Skeleton"       ,"Unknown.png") );
132 133
    stringToTypeInfo["GIS"]  = types.size();
    typeToTypeInfo[DATA_GIS] = types.size();
134
    types.push_back( TypeInfo(DATA_GIS             ,"GIS"            ,"Unknown.png") );
135 136
    stringToTypeInfo["All"]  = types.size();
    typeToTypeInfo[DATA_ALL] = types.size();
137 138
    types.push_back( TypeInfo(DATA_ALL             ,"All"            ,"Unknown.png") );

139 140 141 142 143 144 145 146 147 148 149 150 151
    typeToString[DATA_NONE] = "Unknown";
    typeToString[DATA_GROUP] = "Group";
    typeToString[DATA_POINTS] = "Points";
    typeToString[DATA_POLY_LINE] = "PolyLine";
    typeToString[DATA_TRIANGLE_MESH] = "TriangleMesh";
    typeToString[DATA_POLY_MESH] = "PolyMesh";
    typeToString[DATA_BSPLINE_CURVE] = "BSplineCurve";
    typeToString[DATA_VOLUME] = "Volume";
    typeToString[DATA_BSPLINE_SURFACE] = "BSplineSurface";
    typeToString[DATA_SKELETON] = "Skeleton";
    typeToString[DATA_GIS] = "GIS";
    typeToString[DATA_ALL] = "All";

152 153 154 155
    initialized_ = true;
  }
}

156
/// Adds a datatype and returns the id for the new type
157 158
DataType addDataType(QString _name) {
  initialize();
159 160 161 162 163 164 165 166 167

  int type = nextTypeId_;

  stringToTypeInfo[ _name ] = types.size();
  typeToTypeInfo[ type ] = types.size();
  types.push_back( TypeInfo(type, _name, "Unknown.png") );

  typeToString[type] = _name;

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

172
/// Get the id of a type with given name
173 174
DataType typeId(QString _name) {
  initialize();
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

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

  if ( index != stringToTypeInfo.end() )
    return types[ index->second ].type;
  else
    return -1;
}

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

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

  if ( name != typeToString.end() )
    return name->second;
  else
    return "Unknown";
194 195
}

196
/// Get the icon of a given dataType
197 198
QString typeIcon(QString  _name) {
  initialize();
199 200 201 202 203 204 205

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

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

208
/// get the icon of a given dataType
209 210
QString typeIcon(DataType _id) {
  initialize();
211 212 213 214 215 216 217

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

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

220
/// Set the icon for a given dataType
221 222 223
void setTypeIcon( DataType _id   , QString _icon ) {
  initialize();

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

  if ( index != typeToTypeInfo.end() )
    types[ index->second ].iconName = _icon;
  else
    std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
230 231
}

232
/// Set the icon for a given dataType
233 234 235
void setTypeIcon( QString  _name , QString _icon ) {
  initialize();

236 237 238 239 240 241
  std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );

  if ( index != stringToTypeInfo.end() )
    types[ index->second ].iconName = _icon;
  else
    std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
242 243 244 245
}

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