casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DParameterMapKeyChoice.h
Go to the documentation of this file.
00001 //# DParameterMapKeyChoice.h: class to store/retrieve a parameter and its key
00002 //# Copyright (C) 2000,2001,2002
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: 
00027 
00028 #ifndef TRIALDISPLAY_DPARAMETERMAPKEYCHOICE_H
00029 #define TRIALDISPLAY_DPARAMETERMAPKEYCHOICE_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/Arrays/Vector.h>
00033 #include <display/Display/DParameterChoice.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 // <summary>
00038 // Implementation of DParameterChoice to store parameter and 
00039 // associated key
00040 // </summary>
00041 
00042 // <use visibility=export>
00043 
00044 // <reviewed reviewer="" date="" test="" demos="">
00045 // </reviewed>
00046 
00047 // <prerequisite>
00048 // <li> <linkto class=DParameterChoice>DParameterChoice</linkto>
00049 // </prerequisite>
00050 
00051 // <etymology>
00052 // DParameterMapKeyChoice is an implementation of a DisplayParameter
00053 // providing a choice parameter type. It also maintains a "key". eg it
00054 // can associate a vector of strings with a vector of integers.
00055 // </etymology>
00056 
00057 // <synopsis>
00058 // This class is derived from <linkto
00059 // class=DParameterChoice>DParameterChoice</linkto> and provides a
00060 // choice-type parameter.  The string selection correspond to a "key"
00061 // selection. It is desgined for use with, for example, an enum. In such a
00062 // case, each string value can have an associated integer value which 
00063 // can be cast to / from a enum.
00064 // </synopsis>
00065 // <example>
00066 
00067 // A DParameterMapKeyChoice can easily be used to store and update any
00068 // parameter which can be expressed as a selection from two or more
00069 // options. 
00070 // <srcblock>
00071 // Vector<String> myStrings(2);
00072 //   myStrings(0) = "Ten";
00073 //   myStrings(1) = "Twenty";
00074 // Vector<Int> myInts(2);
00075 //   myInts(0) = 10;
00076 //   myInts(1) = 20;
00077 //
00078 // // ...
00079 // DParameterMapKeyChoice mchoice("number", "Choose a number of ...", 
00080 //                          "Select the number of ... to use for ...",
00081 //                          myStrings, myInts,
00082 //                          myStrings(0), myStrings(0));
00083 //
00084 // // ...
00085 // 
00086 // // update the parameter from some Record (String representation);
00087 // mchoice.fromRecord(rec);
00088 //
00089 // // examine the value of the parameter
00090 // cerr << "You have selected " << mchoice.value() << " things." << endl;
00091 // // ...
00092 // itsNumberThings = mchoice.keyValue();
00093 // </srcblock>
00094 // </example>
00095 
00096 // <motivation>
00097 // Often at C++ level, parameters will be described by enums etc.
00098 // At glish level however, it is often required for these parameters 
00099 // to be displayed as text options. This class is designed to make that
00100 // process easier. It can also be used more generally to associate any key
00101 // with a text option.
00102 // </motivation>
00103 
00104 // <thrown>
00105 // <li> Throws AipsErrors if number of keys do not match number of options
00106 // or if there is a problem looking up a key.
00107 // </thrown>
00108 
00109 // <todo asof="2002/05/08">
00110 // <li> Template the class so a "key" can be anything.
00111 // </todo>
00112 
00113 class DParameterMapKeyChoice : public DParameterChoice {
00114 
00115  public:
00116 
00117   // Constructor taking the name of the parameter, a short
00118   // description, some help text, the enum which is to form
00119   // the basis of the options (must have an overloaded ostream <<
00120   // operator), a vector of integers corresponding to the options
00121   // within that enum, a default value, an initial value, and the 
00122   // context of the parameter. Obviously the
00123   // <src>defaultvalue</src> and <src>value</src> parameters must
00124   // exist in the list of allowed options, otherwise an exception
00125   // is thrown.
00126   DParameterMapKeyChoice(const String name, const String description,
00127                          const String help, const Vector<String>& options,
00128                          const Vector<Int>& keys,
00129                          const String defaultvalue, const String value, 
00130                          const String context = "");
00131 
00132   
00133   // (Required) copy constructor.
00134   DParameterMapKeyChoice(const DParameterMapKeyChoice &other);
00135 
00136   // Destructor.
00137   virtual ~DParameterMapKeyChoice();
00138 
00139   // (Required) copy assignment.
00140   DParameterMapKeyChoice &operator=(const DParameterMapKeyChoice &other);
00141 
00142   // Return the list of all keys for this parameter.
00143   Vector<Int> keys() const
00144   { return itsKeys; }
00145 
00146   // Return the current value of this parameter.
00147   Int keyValue();
00148 
00149   // Thise function has very little implementation, it is here so if
00150   // a user changes the list of options, we can ensure we do not make a 
00151   // mistake when looking up its corresponding key
00152   void setOptions(const Vector<String>& newOptions) {
00153     itsLastString = "";
00154     DParameterChoice::setOptions(newOptions);
00155   }
00156 
00157   // Set the current value, based on a key.
00158   Bool setKeyValue(const Int newValue);
00159 
00160   // Set or change the list of allowed options for this parameter.
00161   void setKeys (const Vector<Int>& newKeys);
00162 
00163 protected:
00164   
00165   // (Required) default constructor.
00166   DParameterMapKeyChoice();
00167   
00168 private:
00169   // Lookup the key based on the string provided
00170   Bool lookUpKey(const String& value);
00171 
00172   // Store for the allowed options for this parameter.
00173   Vector<Int> itsKeys;
00174   Int itsKeyValue;
00175   String itsLastString;
00176 };
00177 
00178 
00179 } //# NAMESPACE CASA - END
00180 
00181 #endif