casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TBConstants.h
Go to the documentation of this file.
00001 //# TBConstants.h: Constants, defaults, and common functions for the browser.
00002 //# Copyright (C) 2005
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 #ifndef CONSTANTS_H_
00028 #define CONSTANTS_H_
00029 
00030 #include <sstream>
00031 #include <vector>
00032 #include <map>
00033 
00034 #include <QFrame>
00035 
00036 #include <casa/BasicSL/String.h>
00037 #include <casa/Utilities/DataType.h>
00038 #include <casa/Containers/Record.h>
00039 
00040 #include <xercesc/util/PlatformUtils.hpp>
00041 #include <xercesc/util/XMLString.hpp>
00042 
00043 /*
00044 #ifdef AIPS_HAS_QWT
00045 #include <qwt_plot_curve.h>
00046 #include <qwt_symbol.h>
00047 #endif
00048 */
00049 #include <graphics/GenericPlotter/PlotFactory.h>
00050 
00051 #include <casa/namespace.h>
00052 using namespace std;
00053 using namespace xercesc;
00054 
00055 namespace casa {
00056 
00057 //# Forward Declarations
00058 class TBTableTabs;
00059 
00060 
00061 // Enum listing the possible types of comparisons that can be made in, for
00062 // example, a filter rule.
00063 enum Comparator {
00064     EQUALS, CONTAINS, BETWEEN, CONTAINSBT, LESSTHAN, CONTAINSLT, GREATERTHAN,
00065     CONTAINSGT
00066 };
00067 
00068 // Enum listing the format for boolean values: "true/false", "t/f", "1/0", etc.
00069 enum BooleanFormat {
00070     DEFAULT, TRUEFALSE, TF, B10
00071 };
00072 
00073 // Enum to indicate the different driver types.
00074 enum Driver {
00075     DIRECT//, XML
00076 };
00077 
00078 // Enum to indicate the different parsers available for XML drivers.
00079 enum Parser {
00080     HOME, XERCES_DOM, XERCES_SAX
00081 };
00082 
00083 
00084 // <summary>
00085 // Convenience class for a String/bool tuple.
00086 // <summary>
00087 //
00088 // <synopsis>
00089 // Result is nothing more than a String/bool pair.  Semantically, it can be
00090 // used to return the result of an operation: the String is the value and the
00091 // bool indicates whether that value is valid or the operation was successful.
00092 // </synopsis>
00093 
00094 class Result {
00095 public:
00096     // Construct a Result with the given values.
00097     Result(String r= "", bool v = true): result(r), valid(v) { }
00098 
00099     ~Result() { }
00100 
00101     // Result string.
00102     String result;
00103     
00104     // Result bool.
00105     bool valid;
00106 };
00107 
00108 
00109 // <summary>
00110 // Results of a row locate on at least one table.
00111 // </summary>
00112 //
00113 // <synopsis>
00114 // A TBLocatedRows basically consists of a map from TBTableTabs to vectors of
00115 // ints that represent row numbers.
00116 // </synopsis>
00117 
00118 class TBLocatedRows {
00119 public:
00120     // Default Constructor.
00121     TBLocatedRows();
00122     
00123     ~TBLocatedRows();
00124     
00125     // Returns a list of all the tables in this TBLocatedRows.
00126     vector<TBTableTabs*> tables();
00127     
00128     // Associates the given list of row numbers with the given TBTableTabs.
00129     void put(TBTableTabs* tt, vector<int>* r);
00130     
00131     
00132     // Results.
00133     map<TBTableTabs*, vector<int>*> results;
00134 };
00135 
00136 
00137 // <summary>
00138 // Constants, defaults, and commonly-used functions for the table browser.
00139 // <summary>
00140 //
00141 // <synopsis>
00142 // TBConstants contains definitions that may be common or useful to multiple
00143 // classes.
00144 // </synopsis>
00145 
00146 class TBConstants {
00147 public:
00148     /* Debugging Constants/Defaults */
00149     
00150     // The current debug level.
00151     static int debugThreshold;
00152     
00153     // Debug levels.
00154     // <group>
00155     static const int DEBUG_OFF;
00156     static const int DEBUG_LOW;
00157     static const int DEBUG_MED;
00158     static const int DEBUG_HIGH;
00159     // </group>
00160 
00161     // Print the given message to standard out with the given indentation level,
00162     // IF the message's level is at or below the current debugThreshold.
00163     static void dprint(int level, String message, int indentLevel = 0);
00164 
00165     
00166     /* Browsing Constants/Defaults */
00167 
00168     // The default number of rows to load from the table at a time.
00169     static const unsigned int DEFAULT_SELECT_NUM;
00170 
00171     // The default number of rows to load at a time while exporting.
00172     static const unsigned int DEFAULT_EXPORT_NUM;
00173 
00174     // The maximum number of rows to load from the table at a time.
00175     static const unsigned int MAX_SELECT_NUM;
00176 
00177     // The maximum number of actions to keep in action lists.
00178     static const unsigned int MAX_ACTION_BUFFER;
00179 
00180     // The default row interval for plotting.
00181     static const unsigned int DEFAULT_ROW_INTERVAL;
00182 
00183     // Kinds of queries that can be sent to the table through the TBXMLDriver.
00184     // <group>
00185     static const String QUERY_QUERY;
00186     static const String QUERY_ARRAY;
00187     static const String QUERY_UPDATE;
00188     static const String QUERY_FULL;
00189     // </group>
00190 
00191     // Returns the display value of the given Comparator.
00192     static String compToString(Comparator c);
00193 
00194     // Returns the Comparator corresponding to the given display value.
00195     static Comparator stringToComp(String str);
00196 
00197     // Returns the display value of the given BooleanFormat.
00198     static String bFormToString(BooleanFormat bf);
00199 
00200     // Returns the BooleanFormat corresponding to the given display value.
00201     static BooleanFormat stringToBForm(String str);
00202 
00203     // Constants used for an array slicer.  See TBSlicer.
00204     // <group>
00205     static const int SLICER_ROW_AXIS;
00206     static const int SLICER_COL_AXIS;
00207     // </group>
00208     
00209     // Number of rows in an embedded record widget to show by default.
00210     static const unsigned int DEFAULT_RECORD_VISIBLE_ROWS;
00211 
00212     // Returns the path of the CASA top-level directory.  This value is
00213     // retrieved from the environment variables the first time it is called,
00214     // and then returns the stored result on subsequent calls.
00215     static String aipspath();
00216 
00217     // Returns the absolute path of the ~/.casa directory.  This value is
00218     // retrieved from the environment variables the first time it is called,
00219     // and then returns the stored result on subsequent calls.
00220     static String dotCasapyDir();
00221     
00222     // Converts a vector<int> to an IPosition.
00223     static IPosition ipos(vector<int>& d);
00224     
00225     // Converts an IPosition to a vector<int>.
00226     static vector<int> ipos(IPosition& d);
00227     
00228     // Increments the given dimension, using the given shape as a maximum.
00229     static bool increment(IPosition& shape, IPosition& d);
00230     
00231     // Increments the given dimension, using the given shape as a maximum.
00232     static bool increment(vector<int>& shape, vector<int>& d);
00233     
00234     // Inserts the given widget into the given placeholder frame.
00235     static void insert(QFrame* frame, QWidget* widget);
00236     
00237     // Inserts the given layout into the given placeholder frame.
00238     static void insert(QFrame* frame, QLayout* layout);
00239     
00240     
00241     /* Data Types and Related Methods */
00242 
00243     // Table data types.
00244     // <group>
00245     static const String TYPE_STRING;
00246     static const String TYPE_DOUBLE;
00247     static const String TYPE_FLOAT;
00248     static const String TYPE_INT;
00249     static const String TYPE_UINT;
00250     static const String TYPE_BOOL;
00251     static const String TYPE_CHAR;
00252     static const String TYPE_UCHAR;
00253     static const String TYPE_SHORT;
00254     static const String TYPE_COMPLEX;
00255     static const String TYPE_DCOMPLEX;
00256     static const String TYPE_TABLE;
00257     static const String TYPE_RECORD;
00258     static const String TYPE_DATE;
00259 
00260     static const String TYPE_ARRAY_STRING;
00261     static const String TYPE_ARRAY_DOUBLE;
00262     static const String TYPE_ARRAY_FLOAT;
00263     static const String TYPE_ARRAY_INT;
00264     static const String TYPE_ARRAY_UINT;
00265     static const String TYPE_ARRAY_BOOL;
00266     static const String TYPE_ARRAY_CHAR;
00267     static const String TYPE_ARRAY_UCHAR;
00268     static const String TYPE_ARRAY_SHORT;
00269     static const String TYPE_ARRAY_COMPLEX;
00270     static const String TYPE_ARRAY_DCOMPLEX;
00271     // </group>
00272 
00273     // Unsupported types
00274     // static const String TYPE_USHORT;
00275     // static const String TYPE_ARRAY_USHORT;
00276     // static const String TYPE_OTHER;
00277     
00278     // Return a vector containing all the valid data types.
00279     static vector<String>* allTypes();
00280 
00281     // Return a vector containing all the valid array data types.
00282     static vector<String>* arrayTypes();
00283 
00284     // Return a vector containing all the valid non-array data types.
00285     static vector<String>* nonArrayTypes();
00286 
00287     // The comments for Double fields that indicate that they should be
00288     // interpreted as a date.
00289     static const String COMMENT_DATE;
00290     static const String COMMENT_TIMP;
00291     static const String COMMENT_TIMP2;
00292 
00293     // Convert a CASA DataType enum to its String equivalent.
00294     static String typeName(DataType dt);
00295 
00296     // Returns a human-readable name for the given type.
00297     static String typeName(String type);
00298 
00299     // Converts the given type into its corresponding VOTable type.  See
00300     // http://www.ivoa.net/Documents/REC/VOTable/VOTable-20040811.html#ToC11
00301     static String VOType(String& type);
00302 
00303     // Returns true if the given type is a table type, false otherwise.
00304     static bool typeIsTable(String& type);
00305 
00306     // Returns true if the given type is an array type, false otherwise.
00307     static bool typeIsArray(String& type);
00308 
00309     // Returns true if the given type is plottable, false otherwise.
00310     static bool typeIsPlottable(String& type);
00311 
00312     // Returns true if the given type is numberable, false otherwise.
00313     static bool typeIsNumberable(String& type);
00314 
00315     // Returns true if the given type can be used as an index (i.e.,
00316     // integer-like), false otherwise.
00317     static bool typeIsIndexable(String& type);
00318 
00319     // Retruns true if the given type is a complex or array of complex types,
00320     // false otherwise.
00321     static bool typeIsComplex(String& type);
00322 
00323     // Returns the value type from the given array type.  For example,
00324     // arrayType(TYPE_ARRAY_STRING) = TYPE_STRING.
00325     static String arrayType(String& at);
00326 
00327     // Returns true if the given value is valid for the given type, false
00328     // otherwise.
00329     static bool valueIsValid(String& value, String& type);
00330 
00331     // Formats the given value for the given data type and returns the
00332     // formatted value.
00333     static String formatValue(String& value, String& type);
00334 
00335     // Converts the given value of the given type to a double and return it.
00336     // Returns -1 for invalid doubles.
00337     static double valueToDouble(String& value, String& type);
00338     
00339     // Converts the given String in date format (year-month-day-hour:min:sec)
00340     // to its double representation.  The double returned is in modified
00341     // julian seconds.
00342     static double date(String d);
00343 
00344     // Converts the given double into a String in date format
00345     // (year-month-day-hour:min:sec).  The input double should be in modified
00346     // julian seconds.
00347     static String date(const double d, const int numDecimals = -1);
00348     
00349     // Holds the default number of decimals displayed in a number.
00350     static const int DEFAULT_DECIMALS;
00351     
00352     // Holds the default number of decimals displayed in a date.
00353     static const int DEFAULT_DATE_DECIMALS;
00354     
00355     // Returns true if the given String contains a valid date format, false
00356     // otherwise.  A valid date format has %y, %m, %d, %n, and %s appear
00357     // exactly once.  %y = year, %m = month, %d = day of month, %n = minute
00358     // %s = second; all fields are integers.
00359     static bool dateFormatIsValid(String& d);
00360 
00361     // Converts a String in complex format (a,b) into a pair of doubles.
00362     static pair<double, double> toComplex(String str);
00363     
00364     // String used in parsing arrays.
00365     static const String ARRAY_AXES_LENGTHS;
00366 
00367 
00368     /* XML Driver Constants/Defaults */
00369 
00370     // XML token names that are used in table parsing.
00371     // <group>
00372     static const String XML_VOTABLE;
00373     static const String XML_RESOURCE;
00374     static const String XML_TABLE;
00375     static const String XML_TOTAL;
00376     static const String XML_FIELD;
00377     static const String XML_KEYWORD;
00378     static const String XML_COLUMNKW;
00379     static const String XML_RWINFO;
00380     static const String XML_DATA;
00381     static const String XML_TABLEDATA;
00382     static const String XML_TR;
00383     static const String XML_TD;
00384     static const String XML_INSERTROW;
00385     static const String XML_REMOVEROW;
00386     static const String XML_TRUE;
00387     static const String XML_FALSE;
00388     static const String XML_ROW;
00389     static const String XML_NAME;
00390 
00391     static const String XML_ID;
00392     static const String XML_KEYWORD_NAME;
00393     static const String XML_KEYWORD_TYPE;
00394     static const String XML_KEYWORD_VAL;
00395     static const String XML_COLKW_COL;
00396     static const String XML_FIELD_NAME;
00397     static const String XML_FIELD_TYPE;
00398     static const String XML_FIELD_UNIT;
00399     static const String XML_FIELD_UCD;
00400     static const String XML_FIELD_REF;
00401     static const String XML_FIELD_PRECISION;
00402     static const String XML_FIELD_WIDTH;
00403     // </group>
00404 
00405     // Error text that is thrown/returned on an empty table or row.
00406     static const String ERROR_EMPTY;
00407 
00408     // Converts a char* to an XMLCh*.  See XMLString::transcode().
00409     static const XMLCh* xstr(char* str);
00410 
00411     // Converts a String to an XMLCh*.  See XMLString::transcode().
00412     static const XMLCh* xstr(String str);
00413 
00414     // Converts an XMLCh* to a String.  See XMLString::transcode().
00415     static String xstr(XMLCh* str);
00416 
00417     // Converts a const XMLCh* to a String.  See XMLString::transcode().
00418     static String xstr(const XMLCh* str);
00419 
00420 
00421     /* String and Number Methods */
00422 
00423     // Converts the given int into its String representation and returns it.
00424     static String itoa(int n);
00425     
00426     // Converts the given unsigned int into its String representation and
00427     // returns it.
00428     static String uitoa(unsigned int n);
00429     
00430     // Converts the given short int into its String representation and
00431     // returns it.
00432     static String sitoa(short int n);
00433 
00434     // Converts the given float into its String representation and returns it.
00435     static String ftoa(float f, int decimals = -1, bool scientific = false);
00436 
00437     // Converts the given double into its String representation and returns it.
00438     static String dtoa(double d, int decimals = -1, bool scientific = false);
00439 
00440     // Converts the given String into its int representation.  Returns the
00441     // result of sscanf.
00442     static int atoi(String& str, int* i);
00443     
00444     // Converts the given String into its unsigned int representation.  Returns
00445     // the result of sscanf.
00446     static int atoui(String& str, unsigned int* i);
00447     
00448     // Converts the given String into its short int representation.  Returns
00449     // the result of sscanf.
00450     static int atosi(String& str, short int* i);
00451 
00452     // Converts the given String into its float representation.  Returns the
00453     // result of sscanf.
00454     static int atof(String& str, float* f);
00455 
00456     // Converts the given String into its double representation.  Returns the
00457     // result of sscanf.
00458     static int atod(String& str, double* d);
00459     
00460     // Rounds the given double to the nearest int and returns it.
00461     static int round(double d);
00462 
00463     // Returns true if the given char is a whitespace, false otherwise.
00464     static bool isWS(char c);
00465 
00466     // Trims the given String of its leading and trailing whitespace using
00467     // String::erase().
00468     static void strtrim(String& str);
00469 
00470     // Converts all upper-case letters to lower-case letters in the given
00471     // String.
00472     static void toLower(String& str);
00473 
00474     // Returns the index of the next instance of a whitespace character in the
00475     // given String from the given index, or the string's length if there is none.
00476     static unsigned int findWS(String& str, int index = 0);
00477 
00478     // Returns the index of the next instance of the find String in the given
00479     // String starting from the given index, or str's length if there is none.
00480     static unsigned int findIgnoreCase(String& str, String& find,
00481                                        int index = 0);
00482 
00483     // Returns true if the two Strings are equal (ignoring case), false
00484     // otherwise.
00485     static bool equalsIgnoreCase(String str1, String str2);
00486 
00487     // Returns the name of the file from the given path (the last part of the
00488     // path).
00489     static String nameFromPath(String& path);
00490 
00491     // Returns the directory from the given path (all but the last part of the
00492     // path).
00493     static String dirFromPath(String& path);
00494 
00495 
00496     /* TBConnection Constants/Defaults */
00497 
00498     // Help descriptions used by TBConnection.
00499     // <group>
00500     static const String OPEN_TEXT_LOCAL;
00501     static const String OPEN_TEXT_REMOTE;
00502     static const String OPEN_TEXT_HOST;
00503     static const String OPEN_TEXT_PORT;
00504     static const String OPEN_TEXT_LOCATION;
00505     static const String OPEN_TEXT_DIRECT;
00506     static const String OPEN_TEXT_XML;
00507     static const String OPEN_TEXT_HOME;
00508     static const String OPEN_TEXT_DOM;
00509     static const String OPEN_TEXT_SAX;
00510     static const String OPEN_TEXT_START;
00511     static const String OPEN_TEXT_NUM;
00512     // </group>
00513 
00514 
00515     /* Help Constants/Defaults */
00516 
00517     // Constants for the help system.
00518     // <group>
00519     static const String HELP_DIR;
00520     static const String HELP_INDEX;
00521     static const String HELP_XML;
00522     static const String HELP_XML_HELP;
00523     static const String HELP_XML_CATEGORY;
00524     static const String HELP_XML_NAME;
00525     static const String HELP_XML_GROUP;
00526     static const String HELP_XML_ITEM;
00527     // </group>
00528 
00529 
00530     /* Plotting Constants/Defaults */
00531 
00532     // Plotting defaults.
00533     // <group>
00534     
00535     // The default plotter implementation for the browser.
00536     static const Plotter::Implementation defaultPlotterImplementation;
00537     
00538     // Returns a default line to use with new plots, using the given factory
00539     // (should match the implementation of defaultPlotterImplementation).
00540     static PlotLinePtr defaultPlotLine(PlotFactoryPtr factory);
00541     
00542     // Returns a default plot symbol to use with new plots, using the given
00543     // factory (should match the implementation of
00544     // defaultPlotterImplementation).
00545     static PlotSymbolPtr defaultPlotSymbol(PlotFactoryPtr factory);
00546 
00547 
00548     /* View Constants/Defaults */
00549 
00550     // View constants.
00551     // <group>
00552     static const String VIEW_SAVE_LOC;
00553 
00554     static const String VIEW_DOCUMENT;
00555     static const String VIEW_VIEW;
00556     static const String VIEW_LASTDIR;
00557     static const String VIEW_HISTLIMIT;
00558     static const String VIEW_TABLE;
00559     static const String VIEW_LOCATION;
00560     static const String VIEW_SELECTED;
00561     static const String VIEW_TAQL;
00562     static const String VIEW_HIDDEN;
00563     static const String VIEW_HIDDEN_LENGTH;
00564     static const String VIEW_VISIND;
00565     static const String VIEW_ROWS;
00566     static const String VIEW_ROWS_FROM;
00567     static const String VIEW_ROWS_NUM;
00568     static const String VIEW_FILTER;
00569     static const String VIEW_FILTER_RULE;
00570     static const String VIEW_FILTER_RULE_FIELD;
00571     static const String VIEW_FILTER_RULE_COMPARATOR;
00572     static const String VIEW_FILTER_RULE_VALUE;
00573     static const String VIEW_FILTER_RULE_VALUE2;
00574     static const String VIEW_FILTER_RULE_NOT;
00575     static const String VIEW_FILTER_RULE_ANY;
00576     static const String VIEW_FILTER_RULE_TYPE;
00577     static const String VIEW_FORMATS;
00578     static const String VIEW_FORMAT;
00579     static const String VIEW_FORMAT_COL;
00580     static const String VIEW_FORMAT_DECIMALS;
00581     static const String VIEW_FORMAT_SFORMAT;
00582     static const String VIEW_FORMAT_BFORMAT;
00583     static const String VIEW_FORMAT_DFORMAT;
00584     static const String VIEW_FORMAT_VTHRESHOLD;
00585     static const String VIEW_FORMAT_ALLFONT;
00586     static const String VIEW_FORMAT_FONT;
00587     static const String VIEW_FORMAT_COLOR;
00588     static const String VIEW_FORMAT_FAMILY;
00589     static const String VIEW_FORMAT_SIZE;
00590     static const String VIEW_FORMAT_BOLD;
00591     static const String VIEW_FORMAT_ITALICS;
00592     static const String VIEW_FORMAT_ULINE;
00593     static const String VIEW_FORMAT_STRIKE;
00594     static const String VIEW_SORT;
00595     static const String VIEW_NAME;
00596     static const String VIEW_SORT_ASCENDING;
00597     // </group>
00598     
00599 private:
00600     // Path to the CASA top-level directory.
00601     static String AIPS_PATH;
00602 
00603     // Common string in the TBConnection defaults.
00604     static const String OPEN_PAGE;
00605 
00606     // Holds the absolute location of the ~/.casa directory.
00607     static String DOT_CASAPY_DIR;
00608 };
00609 
00610 }
00611 
00612 #endif /*CONSTANTS_H_*/