00001 //# TBFormat.qo.h: Rules used to format displayed values for fields. 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 TBFORMAT_H_ 00028 #define TBFORMAT_H_ 00029 00030 #include <casaqt/QtBrowser/TBFormat.ui.h> 00031 #include <casaqt/QtBrowser/TBConstants.h> 00032 00033 #include <QtGui> 00034 00035 #include <casa/BasicSL/String.h> 00036 00037 namespace casa { 00038 00039 //# Forward Declarations 00040 class TBData; 00041 00042 // <summary> 00043 // QFont color is a convenience class containing a QFont and a QColor. 00044 // <summary> 00045 00046 class QFontColor { 00047 public: 00048 // Constructor that takes a QFont and a QColor. 00049 QFontColor(QFont f, QColor c); 00050 00051 // Copy Constructor. 00052 QFontColor(const QFontColor& fc); 00053 00054 ~QFontColor(); 00055 00056 00057 // Font. 00058 QFont font; 00059 00060 // Color. 00061 QColor color; 00062 }; 00063 00064 // <summary> 00065 // Rules used to format displayed values for fields. 00066 // <summary> 00067 // 00068 // <synopsis> 00069 // A TBFormat contains all information necessary to format a displayed value. 00070 // A format consists of different options depending on the type of the value 00071 // being formatted. For example, an integer value could be in scientific 00072 // notation and could have a different font style for negative and positive 00073 // numbers. Generally speaking, a format has two parts: type-specific options 00074 // such as scientific notation or date format, and font options. Some types 00075 // may have multiple font options (i.e., one for negative values and one for 00076 // non-negative values). 00077 // </synopsis> 00078 00079 class TBFormat { 00080 public: 00081 // Default Constructor. 00082 TBFormat(); 00083 00084 ~TBFormat(); 00085 00086 00087 // Returns the QFontColor that is applied to all values (i.e., regardless 00088 // of negative or positive etc.). If this format has value-dependent 00089 // QFontColors, this will return NULL. 00090 QFontColor* getAllFont(); 00091 00092 // Sets the QFontColor to be used on all values. 00093 void setAllFont(QFontColor* fc); 00094 00095 // Returns the QFontColors used for different values; the order depends on 00096 // the type. For example, for a format applied to integers the 00097 // non-negative QFontColor is first. If there is a format for all 00098 // values, this will return an empty vector. 00099 std::vector<QFontColor*>* getFonts(); 00100 00101 // Sets the value-dependent QFontColors to the values in the given vector. 00102 void setFonts(std::vector<QFontColor*>* f); 00103 00104 // Adds a font to the end of the value-dependent QFontColor vector. 00105 void addFont(QFontColor* fc); 00106 00107 // Returns the date format if this format applies to a date, a blank string 00108 // otherwise. See TBConstants::dateFormatIsValid(). 00109 String getDateFormat(); 00110 00111 // Sets the date format for this format. See 00112 // TBConstants::dateFormatIsValid(). 00113 void setDateFormat(String d); 00114 00115 // Returns the boolean format. If this format is not for a boolean value, 00116 // this operation is undefined. 00117 tb::BooleanFormat getBoolFormat(); 00118 00119 // Sets the boolean format. 00120 void setBoolFormat(tb::BooleanFormat f); 00121 00122 // Returns true if this format's scientific format is on, false otherwise. 00123 bool getScientificFormat(); 00124 00125 // Sets the scientific format flag. 00126 void setScientificFormat(bool sf); 00127 00128 // Returns the number of decimal places for this format. If there is an 00129 // unlimited number of decimal places, or this format doesn't apply to 00130 // decimals, -1 is returned. 00131 int getDecimalPlaces(); 00132 00133 // Sets the number of decimal places for this format. 00134 void setDecimalPlaces(int dp); 00135 00136 // Returns the vector threshold for this format. A vector threshold, when 00137 // applied to one-dimensional arrays, will "hide" the values in the array 00138 // and display the shape instead IF the size is greater than the specified 00139 // threshold. If the threshold is unlimited, or this format does not apply 00140 // to array types, -1 is returned. 00141 int getVectorThreshold(); 00142 00143 // Sets the vector threshold of this format. See getVectorThreshold(). 00144 void setVectorThreshold(int v); 00145 00146 00147 // Applies this format to the display value in the given QTableWidgetItem. 00148 // The pre-format data and the type must also be provided. 00149 void applyTo(QTableWidgetItem* item, TBData* data); 00150 00151 private: 00152 // The QFontColor to apply to all values, or NULL if there are 00153 // value-dependent QFontColors. 00154 QFontColor* allFont; 00155 00156 // The vector of value-dependent QFontColors (can be empty). 00157 std::vector<QFontColor*> fonts; 00158 00159 // The number of decimal places to display, or -1 for unlimited. 00160 int decimalPlaces; 00161 00162 // Whether scientific format should be used. 00163 bool scientificFormat; 00164 00165 // The format for boolean values. 00166 tb::BooleanFormat boolFormat; 00167 00168 // The format for displaying dates. See TBConstants::dateFormatIsValid(). 00169 String dateFormat; 00170 00171 // The vector threshold, or -1 for unlimited. 00172 int vectorThreshold; 00173 }; 00174 00175 // <summary> 00176 // Widget for entering format rules for a given field. 00177 // <summary> 00178 // 00179 // <synopsis> 00180 // A TBFormatter is a dialog that can be used to get format rules from the 00181 // user. The user can then command either to clear the format (and remove all 00182 // formatting) or to set the format for a given field. Important: this 00183 // behavior is implemented through the use of signals, which means that it is 00184 // the caller's/parent's responsibility for handling the signals and applying 00185 // the formats as needed. 00186 // </synopsis> 00187 00188 class TBFormatter : public QDialog, Ui::Format { 00189 Q_OBJECT 00190 00191 public: 00192 // Constructor that takes the field name, type, and index to be formatted, 00193 // along with the default (unformatted) QFontColor and an optional pointer 00194 // to the parent widget. If the parent pointer is NULL, this will be 00195 // displayed as a dialog; otherwise it can be displayed inside the parent. 00196 TBFormatter(String field, String type, int index, QFontColor font, 00197 QWidget* parent = NULL); 00198 00199 ~TBFormatter(); 00200 00201 00202 // Sets the displayed format to the given format. 00203 void setFormat(TBFormat* f); 00204 00205 signals: 00206 // This signal is emitted when the user clicks the "Clear Format" 00207 // button. The parent/caller should then remove the format for the field 00208 // with the specified index. 00209 void clearRequested(int index); 00210 00211 // This signal is emitted when the user enters a format and then clicks 00212 // the "Set Format" button. The parent/caller should then set the format 00213 // for the specified field to the specified format. 00214 void setRequested(int index, TBFormat* format); 00215 00216 private: 00217 // Field being formatted. 00218 String field; 00219 00220 // Type of the field being formatted. 00221 String type; 00222 00223 // Index of the field being formatted. 00224 int index; 00225 00226 // Vector of QFontColors for value-dependent formats. 00227 std::vector<QFontColor*> fonts; 00228 00229 // Flag indicating whether any GUI-generated events are "genuine." 00230 bool update; 00231 00232 00233 // Collects the QFontColor information currently set in the GUI and returns 00234 // it. 00235 QFontColor* getFont(); 00236 00237 // Sets the displayed font and color information to the given QFontColor. 00238 void setFontColor(QFontColor* color); 00239 00240 private slots: 00241 // Slot for when the user wants a color chooser. Opens a QColorDialog. 00242 void changeColor(); 00243 00244 // Slot for when the user clicks the "Clear Format" button. Emits the 00245 // clearFormat() signal and closes the window. 00246 void clearFormat(); 00247 00248 // Slot for when the user clicks the "Set Format" button. Collects the 00249 // format information from the widget, emits the setRequested() signal, 00250 // and closes the window. 00251 void setFormat(); 00252 00253 // Slot for when the user clicks the "apply to all" checkbox. If the all 00254 // box is checked, the list of value-dependent formats is disabled. 00255 void applyAllTurned(bool on); 00256 00257 // Slot for when the user clicks in the value-dependent format list. The 00258 // selected format is displayed in the widget. 00259 void applySelectionChanged(int newIndex); 00260 00261 // Slot for when the user changes a font or color parameter. The 00262 // QFontColor in the value-dependent vector is updated accordingly. 00263 void valuesChanged(); 00264 00265 private: 00266 // Unlimited decimals constant. 00267 static const int UNLIMITED_DECIMALS; 00268 }; 00269 00270 } 00271 00272 #endif /* TBFORMAT_H_ */