Types.cc

Go to the documentation of this file.
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8549 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-12 14:40:38 +0100 (Fr, 12. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  Types
00049 //
00050 //=============================================================================
00051 
00058 //== INCLUDES =================================================================
00059 
00060 #include "TypesInternal.hh"
00061 #include "DataTypes.hh"
00062 #include <map>
00063 #include <QCoreApplication>
00064 #include <OpenFlipper/common/GlobalOptions.hh>
00065 
00066 
00070 static int nextTypeId_ = 2;
00071 
00074 static std::map< DataType, QString > typeToString;
00075 
00078 static std::map< QString , unsigned int > stringToTypeInfo;
00079 
00082 static std::map< DataType , unsigned int > typeToTypeInfo;
00083 
00084 class TypeInfo {
00085 
00086   public:
00087 
00088   TypeInfo(DataType _type, QString _name, QString _iconName, QString _readableName ) :
00089     type(_type),
00090     name(_name),
00091     iconName(_iconName),
00092     readableName(_readableName)
00093   {
00094     // Use internal name if no external name is given
00095     if ( _readableName == "" )
00096       readableName = _name;
00097   }
00098 
00100   DataType type;
00101 
00103   QString  name;
00104 
00106   QString  iconName;
00107   
00108   QIcon    icon;
00109 
00111   QString readableName;
00112 };
00113 
00114 static QIcon dummyIcon;
00115 
00116 static std::vector< TypeInfo > types;
00117 
00118 //== Functions =========================================================
00119 
00120 std::ostream &operator<<(std::ostream &stream, DataType type)
00121 {
00122   stream << type.value() ;
00123   
00124   return stream;
00125 }
00126 
00127 void initializeTypes() {
00128   stringToTypeInfo["Unknown"] = types.size();
00129   DataType test(DATA_UNKNOWN);
00130   typeToTypeInfo[test]   = types.size();
00131   types.push_back( TypeInfo(DATA_UNKNOWN            ,"Unknown"        ,"Unknown.png", QCoreApplication::translate("Types","Unknown")) );
00132 
00133   stringToTypeInfo["Group"]  = types.size();
00134   typeToTypeInfo[DATA_GROUP] = types.size();
00135   types.push_back( TypeInfo(DATA_GROUP           ,"Group"          ,"group.png", QCoreApplication::translate("Types","Group")) );
00136 
00137   stringToTypeInfo["All"]  = types.size();
00138   typeToTypeInfo[DATA_ALL] = types.size();
00139   types.push_back( TypeInfo(DATA_ALL             ,"All"            ,"Unknown.png", QCoreApplication::translate("Types","All")) );
00140 
00141   typeToString[DATA_UNKNOWN] = "Unknown";
00142   typeToString[DATA_GROUP] = "Group";
00143   typeToString[DATA_ALL] = "All";
00144   
00145   // Preload the static icons
00146   setTypeIcon(DATA_GROUP,"group.png");
00147 }
00148 
00150 DataType addDataType(QString _name, QString _readableName) {
00151   
00152   int type = nextTypeId_;
00153 
00154   stringToTypeInfo[ _name ] = types.size();
00155   typeToTypeInfo[ type ] = types.size();
00156   types.push_back( TypeInfo(type, _name, "Unknown.png", _readableName ));
00157 
00158   typeToString[type] = _name;
00159 
00160   nextTypeId_ *= 2;
00161   return( type );
00162 }
00163 
00165 DataType typeId(QString _name) {
00166 
00167   std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );
00168 
00169   if ( index != stringToTypeInfo.end() )
00170     return types[ index->second ].type;
00171   else {
00172     #ifdef DEBUG
00173       std::cerr << "Unknown Data type with name " << _name.toStdString() << std::endl;
00174     #endif
00175     return DATA_UNKNOWN;
00176   }
00177 }
00178 
00180 QString typeName(DataType _id) {
00181 
00182   std::map<DataType, QString>::iterator name = typeToString.find(_id);
00183 
00184   if ( name != typeToString.end() )
00185     return name->second;
00186   else {
00187     #ifdef DEBUG
00188     std::cerr << "Unable to retrieve typeName for id " << _id << std::endl;
00189     #endif
00190     return "Unknown";
00191   }
00192 }
00193 
00194 
00196 uint typeCount() {
00197   return types.size();
00198 }
00199 
00201 QString typeIconName(QString  _name) {
00202 
00203   std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );
00204 
00205   if ( index != stringToTypeInfo.end() )
00206     return types[ index->second ].iconName;
00207   else
00208     return "Unknown.png";
00209 }
00210 
00212 QString typeIconName(DataType _id) {
00213 
00214   std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
00215 
00216   if ( index != typeToTypeInfo.end() )
00217     return types[ index->second ].iconName;
00218   else
00219     return "Unknown.png";
00220 }
00221 
00223 QIcon& typeIcon(DataType _id) {
00224   
00225   std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
00226   
00227   if ( index != typeToTypeInfo.end() )
00228     return types[ index->second ].icon;
00229   else
00230     return dummyIcon;
00231 }
00232 
00234 void setTypeIcon( DataType _id   , QString _icon ) {
00235 
00236   std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
00237 
00238   if ( index != typeToTypeInfo.end() ) {
00239     types[ index->second ].iconName = _icon;
00240     types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
00241   } else
00242     std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
00243 }
00244 
00246 void setTypeIcon( QString  _name , QString _icon ) {
00247 
00248   std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _name );
00249 
00250   if ( index != stringToTypeInfo.end() ) {
00251     types[ index->second ].iconName = _icon;
00252     types[ index->second ].icon = QIcon( OpenFlipper::Options::iconDirStr() + QDir::separator() + _icon );
00253   } else
00254     std::cerr << "Could not set icon for DataType. Type not found!" << std::endl;
00255 }
00256 
00257 
00258 
00259 
00261 QString dataTypeName( DataType _id ) {
00262 
00263   std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
00264 
00265   if ( index != typeToTypeInfo.end() )
00266     return types[ index->second ].readableName ;
00267   else
00268     std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;
00269 
00270   return QString(QCoreApplication::translate("Types","Unknown Type"));
00271 }
00272 
00274 QString dataTypeName( QString  _typeName ) {
00275 
00276   std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _typeName );
00277 
00278   if ( index != stringToTypeInfo.end() )
00279     return types[ index->second ].readableName ;
00280   else
00281     std::cerr << "Could not get human name for DataType. Type not found!" << std::endl;
00282 
00283   return QString(QCoreApplication::translate("Types","Unknown Type"));
00284 }
00285 
00286 
00287 
00289 void setDataTypeName( DataType _id   , QString _name ) {
00290 
00291   std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
00292 
00293   if ( index != typeToTypeInfo.end() )
00294     types[ index->second ].readableName = _name;
00295   else
00296     std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
00297 }
00298 
00300 void setDataTypeName( QString  _typeName , QString _name ) {
00301 
00302   std::map<QString, unsigned int>::iterator index = stringToTypeInfo.find( _typeName );
00303 
00304   if ( index != stringToTypeInfo.end() )
00305     types[ index->second ].readableName = _name;
00306   else
00307     std::cerr << "Could not set human name for DataType. Type not found!" << std::endl;
00308 }
00309 
00310 
00311 DataType::DataType():
00312   field(0)
00313 {
00314 };
00315 
00316 DataType::DataType(const unsigned int& _i):
00317   field(_i)
00318 {
00319 };
00320 
00321 //===========================================
00322 
00323 bool DataType::operator!=( const unsigned int& _i ) { 
00324   return (_i != field); 
00325 }
00326 
00327 bool DataType::operator!=( const DataType& _i ) {
00328   return (field != _i.field); 
00329 }    
00330 
00331 //===========================================
00332 
00333 bool DataType::operator==( const unsigned int& _i ) {
00334   return (_i == field); 
00335 }
00336 
00337 bool DataType::operator==(  const DataType& _i ) {
00338   return (_i.field == field); 
00339 }
00340 
00341 //===========================================
00342 
00343 bool DataType::operator=( const unsigned int& _i ) {
00344   return (field = _i); 
00345 }
00346 
00347 bool DataType::operator=( const DataType& _i ) {
00348   return (field = _i.field); 
00349 }    
00350 
00351 //===========================================
00352 
00353 bool DataType::operator<( const unsigned int& _i ) {
00354   return (field < _i); 
00355 }
00356 
00357 bool DataType::operator<( const DataType& _i ) const {
00358   return (field < _i.field); 
00359 }
00360 
00361 //===========================================
00362 
00363 bool DataType::operator&( const unsigned int& _i ) {
00364   return (field & _i); 
00365 }
00366 
00367 bool DataType::operator&( const DataType& _i ) const {
00368   return (field & _i.field); 
00369 }    
00370 
00371 //===========================================
00372 
00373 DataType& DataType::operator|=( const unsigned int& _i ) {
00374   field |= _i;
00375   return (*this);
00376 }
00377 
00378 DataType& DataType::operator|=( const DataType& _i )  {
00379   field |= _i.field ;
00380   return (*this);
00381 }
00382 
00383 //===========================================
00384 
00385 DataType DataType::operator|( const DataType& _i ) const {
00386   return (field | _i.field); 
00387 }    
00388 
00389 //===========================================
00390 
00391 bool DataType::operator++(int _unused) {
00392   return (field *= 2); 
00393 }  
00394 
00395 //===========================================
00396 
00397 unsigned int DataType::value() const {
00398   return( field );
00399 }
00400 
00401 QString DataType::name() {
00402   return typeName(field);
00403 }
00404 
00405 
00406 //=============================================================================
00407 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .