Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

enumparam.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  File:    enumparam.h
00004  Created: by Aidan Lane, December 08, 2003
00005  Updated: by Aidan Lane, February 15, 2004
00006  
00007  This file is part of Glitch
00008  Copyright (C) 2003-2004  Monash University, Clayton Campus, Australia
00009  Created by Aidan Lane, under the supervision of Jon McCormack.
00010  
00011  This program was developed to aid the students studying the CSE3313
00012  Computer Graphics course at Monash University.
00013  
00014  This software may contain portions that are copyright (C) 1993,
00015  Silicon Graphics, Inc. All Rights Reserved.
00016  
00017  Glitch is free software; you can redistribute it and/or modify
00018  it under the terms of the GNU General Public License as published by
00019  the Free Software Foundation, version 2.
00020  
00021  Glitch is distributed in the hope that it will be useful,
00022  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  GNU General Public License for more details.
00025  
00026  You should have received a copy of the GNU General Public License
00027  along with this program; if not, write to the Free Software
00028  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00029 
00030 *****************************************************************************/
00031 
00032 #ifndef __ENUMPARAM_H__
00033 #define __ENUMPARAM_H__
00034 
00035 
00036 #include <qcombobox.h>
00037 #include <qstringlist.h>
00038 #include <qvaluelist.h>
00039 
00040 #include "cmdparam.h"
00041 #include "supervaluelist.h"
00042 
00043 
00044 // Note: combo boxes will be created as wide AS REQUIRED BY THE CONTENTS
00045 #define MAX_COMBO_BOX_WIDTH 300
00046 
00047 
00059 class EnumParam : public CmdParam {
00060 
00061     Q_OBJECT    // make QT's signal/slot system work
00062 
00063 public:
00064     EnumParam( const char* formalVarName, const char* defaultValue,
00065         const SuperValueList<char*>& enumStrs, const SuperValueList<int>& enumVals,
00066         const char* exactTypeName = "GLenum" )
00067         : CmdParam( formalVarName, defaultValue, exactTypeName ),
00068             myEnumVals(enumVals)
00069     {
00070         // Copy the enum char*'s into QStrings
00071         uint i;
00072         for ( i=0; i < enumStrs.count(); ++i ) {
00073             myEnumStrs.append( enumStrs[i] );
00074         }
00075     }
00076 
00077     // widgets
00078     virtual QWidget* createWidget( QWidget* parent, const char* name,
00079         const QObject *slotOwner, const char* member )
00080     {
00081         QComboBox *w = new QComboBox(parent,name);
00082         connect( w, SIGNAL( activated(int) ), slotOwner, member );
00083         // insert the strings
00084         uint i;
00085         for ( i=0; i < myEnumStrs.count(); ++i ) {
00086             w->insertItem( myEnumStrs[i] );
00087         }
00088         return (QWidget*)w;
00089     }
00090     // VALUE IN THIS CASE VALUE IS ACTUALLY THE MACRO VALUE (AN INTEGER), NOT THE MACRO STRING REPRESENTATION!
00091     virtual void setWidgetValue( QWidget* widget, const QString& value )
00092     {
00093         if ( widget == NULL ) {
00094             return;
00095         }
00096 
00097         int index = myEnumVals.findIndex( value.toInt() );
00098 
00099         // If the value was invalid, then select the first possible value... if possible!
00100         if ( index == -1 )
00101         {
00102             if ( myEnumStrs.count() > 0 ) {
00103                 index = 0; // handle the error - set index to the first element
00104             } else {
00105                 return; // FATAL ERROR - return without doing anything
00106             }
00107         }
00108 
00109         ((QComboBox*)widget)->setCurrentItem( index );
00110     }
00111     virtual QString getWidgetValue( QWidget* widget ) {
00112         return (widget!=NULL) ? QString::number( myEnumVals[((QComboBox*)widget)->currentItem()] ) : QString::null;
00113     }
00114 
00115     // Re-implement this, as the string representation is different to that of
00116     // the internal value
00117     virtual QString getStringRep( const QString& value ) const
00118     {
00119         int index = myEnumVals.findIndex( value.toInt() );
00120 
00121         // If the value was invalid, then select the first possible value... if possible!
00122         if ( index == -1 )
00123         {
00124             if ( myEnumStrs.count() > 0 ) {
00125                 return myEnumStrs[0]; // handle the error - subsitution
00126             } else {
00127                 return "ERROR"; // FATAL ERROR - return with an error notifier
00128             }
00129         }
00130 
00131         return myEnumStrs[index];
00132     }
00133 
00134     // Note: combo boxes will be created as wide AS REQUIRED BY THE CONTENTS
00135     virtual int maxWidgetWidth() const {
00136         return MAX_COMBO_BOX_WIDTH;
00137     }
00138 
00139 private:
00140     // After lots of testing (like with classes, structs, etc.), the following proved
00141     // to be the most efficient and flexile manner of storage
00142     QStringList     myEnumStrs;
00143     QValueList<int> myEnumVals;
00144 };
00145 
00146 
00147 #endif  // __ENUMPARAM_H__

Generated on Fri Feb 27 12:01:39 2004 for Glitch by doxygen 1.3.2