casa  $Rev:20696$
QtParamGUI.qo.h
Go to the documentation of this file.
00001 //# QtParamGUI.qo.h: GUI for collecting user input for task parameters.
00002 //# Copyright (C) 2008
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 QTPARAMGUI_QO_H_
00028 #define QTPARAMGUI_QO_H_
00029 
00030 #include <Python.h>
00031 
00032 #include <casaqt/QtParamGui/PythonInterpreter.h>
00033 #include <casaqt/QtParamGui/QtParamGUI.ui.h>
00034 
00035 #include <casa/Containers/Record.h>
00036 
00037 #include <utility>
00038 
00039 #include <QDialog>
00040 #include <QFile>
00041 
00042 #include <casa/namespace.h>
00043 using namespace std;
00044 
00045 namespace casa {
00046 
00047 class ParamPanel;
00048 
00049 class QtParamGUI : public QDialog, Ui::ParamInput {
00050     Q_OBJECT
00051     
00052 public:
00053     // STATIC //
00054     
00055     // The running mode.
00056     enum Mode {
00057         PYTHON, SIGNAL
00058     };
00059     
00060     // Possible reset values.
00061     enum ResetMode {
00062         DEFAULT, GLOBAL, LAST
00063     };
00064     
00065     // String constants used in parsing the parameter records.
00066     // <group>
00067     static const String ALLOWED;
00068     static const String ANY;
00069     static const String CONSTRAINTS;
00070     static const String DEFAULTS;
00071     static const String DESCRIPTION;
00072     static const String EXAMPLE;
00073     static const String KIND;
00074     static const String KIND_FILE;
00075     static const String KIND_MS;
00076     static const String KIND_TABLE;
00077     static const String LIMITTYPES;
00078     static const String MUSTEXIST;
00079     static const String PARAMETERS;
00080     static const String PARAMETER_ORDER;
00081     static const String RANGE;
00082     static const String RANGE_MIN;
00083     static const String RANGE_MAX;
00084     static const String SHORT_DESCRIPTION;
00085     static const String SUBPARAMETERS;
00086     static const String TYPE;
00087     static const String UNITS;
00088     static const String VALUE;
00089     // </group>
00090     
00091     // Returns the type for the given type name.
00092     static DataType type(String typeName);
00093     
00094     // Returns the name for the given type.
00095     static String type(DataType type);
00096     
00097     // Converts the given record into a Python-like string representation.
00098     static String recordToString(const Record& record, String separator);
00099     
00100     // Converts the task name and parameter record into a Python command.
00101     static String pythonCommand(String taskName, const Record& params);
00102     
00103     // Sets up the given dialog to have the given name and given text.
00104     static void setupDialog(QDialog* dialog, String taskName, String textName,
00105                             const String& text);
00106     
00107     // Replaces all instances of \' and \" in the given string with ' and ",
00108     // respectively.
00109     static void replaceSlashQuotes(String& string);
00110 
00111     
00112     // NON-STATIC //
00113     
00114     // Constructor that takes the tasksRecord and builds the GUI.  Uses the
00115     // given mode and passes ipythonShell to the PythonInterpreter (see
00116     // PythonInterpreter constructor).  If the given globals record is
00117     // valid, any relevant values are used.  If the given parent is NULL, the
00118     // GUI is in dialog form whereas if a parent is given, it can be
00119     // embedded in another widget.
00120     // If the tasksRecord contains more than one entry, a QComboBox is
00121     // displayed at the top of the widget to choose between the different
00122     // possible tasks.
00123     QtParamGUI(const Record& tasksRecord, Mode mode = PYTHON,
00124                PyObject* ipythonShell = NULL, const Record* globals = NULL,
00125                QWidget* parent = NULL);
00126     
00127     // Destructor.
00128     ~QtParamGUI();
00129     
00130     // Returns the name of the currently displayed task.
00131     String taskName() const;
00132     
00133     // Returns a copy of the parameter attributes record for this task.
00134     Record taskParameters() const;
00135     
00136     // Returns a copy of the constraints record for this task.
00137     Record taskConstraints() const;
00138     
00139     // Returns the currently entered parameter names/values in a Record.
00140     Record enteredParameters() const;
00141     
00142 signals:
00143     // In SIGNAL mode, this is emitted when the user clicks "run".  The name
00144     // of the task and a copy of the entered parameter names/values are given.
00145     // It is the responsibility of the parent/caller to close the QtParamGUI
00146     // widget as desired.
00147     void runRequested(String taskName, Record parameters);
00148     
00149     // In SIGNAL mode, this is emitted when the user clicks "cancel".
00150     // It is the responsibility of the parent/caller to close the QtParamGUI
00151     // widget as desired.
00152     void cancelRequested();
00153     
00154 private:
00155     // All tasks and their parameters/attributes.
00156     Record m_tasks;
00157     
00158     // Global parameter values.
00159     Record m_globals;
00160     
00161     // Last parameter values.
00162     Record m_last;
00163     
00164     // When the user clicks "reset", indicates whether to reset to globals
00165     // or defaults or last.
00166     ResetMode m_resetMode;
00167     
00168     // Mode.
00169     Mode m_mode;
00170     
00171     // Description dialog to show/hide.
00172     QDialog* m_descDialog;
00173     
00174     // Example dialog to show/hide.
00175     QDialog* m_exampleDialog;
00176     
00177     // Constraints dialog to show/hide.
00178     // QDialog* m_constDialog;
00179     
00180     // Python interpreter.  Never used in SIGNAL mode.
00181     PythonInterpreter m_python;
00182     
00183     // Parameter panels for currently displayed task.
00184     vector<ParamPanel*> m_panels;
00185     
00186     // Currently displayed task.
00187     String m_taskName;
00188 
00189     
00190     // Sets the tasks record to the given, and then displays the task with
00191     // the given RecordFieldId.
00192     void setRecord(const Record& record, RecordFieldId id = 0);
00193     
00194     // Checks the value in the record at the given RecordFieldId for
00195     // validity.  If invalid, the second value in the pair holds the reason(s)
00196     // why.
00197     pair<bool, String> recordIsValid(const Record& record, RecordFieldId id);
00198     
00199     // Once the displayed task has been chosen, set up the panels.  If an
00200     // order is given, use that instead of the record's natural order.
00201     void setupRecord(vector<String> order);
00202     
00203     // Returns the text for the constraints dialog for the given param panel.
00204     String constraintsString(const String& id);
00205     
00206     // Show/hide the frame and radio buttons for the different reset modes.
00207     void showHideResets(bool globals, bool last);
00208     
00209     
00210     // STATIC
00211     
00212     // After this length, move the short description to two lines instead
00213     // of one.
00214     static const unsigned int MAX_SHORTDESC_LENGTH;
00215     
00216     // Trims the given String of leading and trailing whitespace, where
00217     // whitespace is ' ', '\t', '\n', and '\r'.
00218     static String strTrim(const String& str);
00219     
00220     // Trims the given String of leading newlines and trailing whitespace,
00221     // where newlines are '\n' or '\r' and whitespace is ' ', '\t', '\n', or
00222     // '\r'.
00223     static String strNLtrim(const String& str);
00224     
00225     // Returns true if the value at r1[id1] is equal to the value at
00226     // r2[id2].  If allowStrings is false, String values can be compared
00227     // to non-String values.
00228     static bool valuesEqual(const RecordInterface& r1, RecordFieldId id1,
00229                             const RecordInterface& r2, RecordFieldId id2,
00230                             bool allowStrings = false);
00231     
00232     // Returns true if the two records are equal or not.
00233     static bool recordEqual(const RecordInterface& r1,
00234                             const RecordInterface& r2);
00235     
00236     // Returns true if the two arrays are equals or not.
00237     template <class T>
00238     static bool arrayEqual(const Array<T>& a1, const Array<T>& a2);
00239     
00240     // Reads the python commands from the given file into the given record.
00241     // The stringstream is used to note any errors during read.
00242     static bool readLastFile(Record& rec, QFile& file, stringstream& ss);
00243     
00244 private slots:
00245     // When the user clicks "run".
00246     void run();
00247     
00248     // When the user clicks "reset".
00249     void reset();
00250     
00251     // When the user clicks "cancel".
00252     void cancel();
00253     
00254     // When the value for the given parameter changes.  If constraints are
00255     // given, check against those.
00256     void valueChanged(String paramName);
00257     
00258     // Show/hide the description dialog.
00259     void showHideDesc(bool checked);
00260     
00261     // Show/hide the example dialog.
00262     void showHideExample(bool checked);
00263     
00264     // When the description dialog is closed.
00265     void descClosed();
00266     
00267     // When the example dialog is closed.
00268     void exampleClosed();
00269     
00270     // When the user chooses a different task.
00271     void taskChosen(QString task);
00272     
00273     // When the user changes the reset option.
00274     void resetChanged();
00275     
00276     // When the user loads parameters from a .last file.
00277     void loadLast();
00278 };
00279 
00280 }
00281 
00282 #endif /*QTPARAMGUI_QO_H_*/
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines