casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TBDataTab.qo.h
Go to the documentation of this file.
00001 //# TBDataTab.qo.h: Widget used to display table data.
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 TBDATATAB_H_
00028 #define TBDATATAB_H_
00029 
00030 #include <casaqt/QtBrowser/TBDataTab.ui.h>
00031 
00032 #include <vector>
00033 
00034 #include <casa/BasicSL/String.h>
00035 
00036 #include <casa/namespace.h>
00037 using namespace std;
00038 
00039 namespace casa {
00040 
00041 //# Forward Declarations
00042 class TBTable;
00043 class QCloseableWidget;
00044 class TBFilterRuleSequence;
00045 class TBFormat;
00046 class QFontColor;
00047 class TBTableTabs;
00048 class ProgressHelper;
00049 class TBArrayPanel;
00050 class TBData;
00051 
00052 // <summary>
00053 // Subclass of QTableWidgetItem that allows for custom sorting.
00054 // <summary>
00055 //
00056 // <synopsis>
00057 // TBDataItem takes a value and a type.  The object saves the value in two
00058 // ways using Qt::DisplayRole and Qt::UserRole.  The value is stored as a
00059 // string using QtDisplayRole, and this is what is actually shown in the
00060 // QTableWidget.  The data stored with Qt::UserRole, however, may be of a
00061 // different type, such as an integer.  This storage is used when sorting
00062 // so that, for example, numbers can be sorted numerically rather than
00063 // lexiographically.  Array types are sorted using the first element for
00064 // one-dimensional arrays and the first dimension for other arrays.
00065 // </synopsis>
00066 
00067 class TBDataItem : public QTableWidgetItem {
00068 public:
00069     // Constructor to take the String value and the type.
00070     //TBDataItem(String value, String type);
00071     
00072     TBDataItem(TBData* data);
00073 
00074     ~TBDataItem();
00075 
00076     
00077     // Override QTableWidgetItem's less-than operator.  Returns true if the
00078     // value of this TBDataItem is less than that of the given TBDataItem,
00079     // false otherwise.  If other is not of type TBDataItem, the behavior is
00080     // undefined.  Also, since this operator is only used for custom sorting,
00081     // the other TBDataItem is assumed to have the same type as this one.
00082     virtual bool operator<(const QTableWidgetItem& other) const;
00083     
00084 private:
00085     // Holds the data type.
00086     String type;
00087     
00088     // Is true if the value stored for Qt::UserRole is an array dimension,
00089     // false otherwise.
00090     bool arrayDimension;
00091     
00092     TBData* tdata;
00093 
00094     
00095     // Set the Qt::UserRole and, if text is true, the Qt::DisplayRole values.
00096     void set(String value, bool text);
00097 };
00098 
00099 // <summary>
00100 // Widget used to display table data.
00101 // <summary>
00102 //
00103 // <synopsis>
00104 // TBDataTab is the tab responsible for displaying table data in a
00105 // QTableWidget.  It also provides functionality for changing page and number
00106 // of rows loaded, catching and passing on edit events, and handling
00107 // double-click events.
00108 // </synopsis>
00109 
00110 class TBDataTab : public QWidget, Ui::DataTab {
00111     Q_OBJECT
00112 
00113 public:
00114     // Constructor that takes the parent TBTableTabs.
00115     TBDataTab(TBTableTabs* tt);
00116 
00117     ~TBDataTab();
00118 
00119     
00120     // Returns the QTableWidget that displays the data.
00121     QTableWidget* getTableWidget();
00122     
00123     // Returns the QBrush that is used to draw the background of any selected
00124     // cells in the QTableWidget.
00125     QBrush& getSelectedCellBackground();
00126 
00127     // Retruns the QBrush that is used to draw the background of any unselected
00128     // cells in the QTableWidget.
00129     QBrush& getUnselectedCellBackground();
00130     
00131     // Returns the current row filter, or NULL if there is none.
00132     TBFilterRuleSequence* getFilter();
00133     
00134     // Sets the current filter to the given rules, but does NOT apply it.
00135     void setFilter(TBFilterRuleSequence* rule);
00136 
00137     // Returns the current format for the field at the given index, or NULL if
00138     // there is none.
00139     TBFormat* formatAt(int index);
00140     
00141     // Returns the current sort on the displayed data.  Each pair in the vector
00142     // is the name of the field with a bool indicating whether the sort is
00143     // ascending or not.
00144     vector<pair<String, bool> >* getSortFields();
00145     
00146     
00147     // Updates the QTableWidget with new data that has been loaded into the
00148     // table backend.  If a ProgressHelper is provided, it will be updated
00149     // periodically with progress information.
00150     void updateTable(ProgressHelper* pp = NULL);
00151 
00152     // Sets the given cell in the QTableWidget to the new value, but does NOT
00153     // update the backend.  If format is true, then any existing format will
00154     // be applied to the new value.
00155     void setData(int row, int col, TBData* newVal, bool format = true);
00156 
00157     // Loads the given page into the table backend, which then updates the
00158     // display.  If the page is valid, the result of TBTableTabs::loadRows()
00159     // is returned; otherwise false is returned.
00160     bool loadPage(int p);
00161 
00162     // Refreshes the table backend using TBTable::refresh(), then updates the
00163     // displayed data using setData().
00164     void refresh(int row, int col, bool format = true);
00165 
00166     // Sorts the displayed data with the given order.  Each pair in the vector
00167     // is the name of the field with a bool indicating whether the sort is
00168     // ascending or not.
00169     void sortBy(vector<pair<String, bool> >& s);
00170     
00171     // Sets the sort indicator on the QTableWidget for the given column with an
00172     // ascending arrow if asc is true or a descending arrow otherwise.
00173     void setSortIndicator(int index, bool asc);
00174 
00175     // Clears the sort indicator on the QTableWidget.
00176     void clearSortIndicator();
00177     
00178     // Selects and highlights the given row in the table widget.
00179     void highlight(int row);
00180     
00181     // Returns the visible index for the row with the given logical index.
00182     // Rows may have differing visible and logical indices when a sort has
00183     // been entered.
00184     int visibleIndex(int logicalIndex);
00185     
00186     // Returns the logical index for the row with the given visible index.
00187     // Rows may have differing visible and logical indices when a sort has
00188     // been entered.
00189     int logicalIndex(int visibleIndex);
00190     
00191     // Hides (or shows) the column with the given index.  Whether the column
00192     // is shown or hidden depends on the "hidden" parameter.
00193     void hideColumn(int index, bool hidden);
00194     
00195     // Opens a format dialog for the field with the given index.  Applies the
00196     // format that the user enters.
00197     void formatField(int index);
00198 
00199     //Remembers whether you pressed the next or previous button. Uses this
00200     //when filter rules are applied to avoid displaying blank pages.
00201     bool goForward;
00202 
00203 public slots:
00204     // Shows the given widget in the side panel.  isArray should be true if
00205     // the widget is a TBViewArray, false otherwise.
00206     void showWidgetInSplitter(QWidget* widget, bool isArray = false);
00207 
00208     // Clears whatever widget (if any) is currently being displayed in the side
00209     // panel.
00210     void clearWidgetInSplitter();
00211 
00212     // Applies the given format to the field at the given index.
00213     void applyFormat(int index, TBFormat* format);
00214 
00215     // Clears the format on the field at the given index.
00216     void clearFormat(int index);
00217 
00218     // Clears all formats on all fields.
00219     void clearAllFormats();
00220 
00221 signals:
00222     // This signal is emitted when the user has edited a cell.  The parameter
00223     // describe which cell was edited and its new value.
00224     void dataChanged(int row, int col, String newVal);
00225 
00226     // This signal is emitted when the side panel is closed.  The QWidget
00227     // points to the widget that was just closed.
00228     void rightWidgetClosed(QWidget* which);
00229 
00230     // This signal is emitted when the user right-clicks on an index and
00231     // selects the "Follow subtable index reference" command.  The String
00232     // indicates which subtable was chosen, and the index indicates the chosen
00233     // index which is the value of the cell that was right-clicked on.
00234     void followReferenceRequested(String subtable, int index);
00235     
00236     // This signal is emitted when a sort is applied to the table.
00237     void sortEntered();
00238     
00239     // This signal is emitted when a sort is cleared from the table.
00240     void sortCleared();
00241     
00242     // This signal is emitted when a column is shown or hidden.
00243     void columnHidden(int index, bool hidden);
00244 
00245 protected:
00246     // Catches the right-click event; if the right click is on a table cell and
00247     // the displayed data could be an index, show the subtable index reference
00248     // menu.  Also show action for copying to the clipboard.
00249     void contextMenuEvent(QContextMenuEvent* event);
00250     
00251 private:
00252     // Useful pointers to table backend.
00253     // <group>
00254     TBTableTabs* ttabs;
00255     TBTable* table;
00256     // </group>
00257     
00258     // The currently loaded page.
00259     int page;
00260 
00261     // How many rows per page are loaded at once.
00262     int loadRows;
00263 
00264     // Side panel.
00265     QCloseableWidget* rightWidget;
00266 
00267     // List of cells that are highlighted (the row and column of the currently
00268     // selected cell).
00269     vector<QTableWidgetItem*> highlightedCells;
00270 
00271     // Brushes used to draw the backgrounds of unselected and selected cells in
00272     // the QTableWidget, respectively.
00273     // <group>
00274     QBrush unselectedBackground;
00275     QBrush selectedBackground;
00276     // </group>
00277     
00278     // Current row filter, or NULL if there is none.
00279     TBFilterRuleSequence* filter;
00280 
00281     // Current field formats.
00282     vector<TBFormat*> formats;
00283 
00284     // "Cleared" format for cell text.
00285     QFontColor* defaultFormat;
00286 
00287     // Indicates whether the side panel is currently open with an array or not.
00288     bool arrayOpened;
00289 
00290     // Points to the current array panel, or NULL if there is none.
00291     TBArrayPanel* arrayPanel;
00292 
00293     // Keeps track of the un-sorted order of the rows.
00294     // Doing rowItems[logicalRow]->row() returns the visualRow.
00295     vector<QTableWidgetItem*> rowItems;
00296 
00297     // Keeps track of the un-sorted order of the rows.
00298     // Doing rowIndices[visualRow] returns the logicalRow.
00299     vector<int> rowIndices;
00300 
00301     // The current sort order.
00302     vector<pair<String, bool> > currSort;
00303     
00304     // Used for handling right-clicks on headers.  Keeps track of the last
00305     // right-clicked header column number.
00306     int clickedHeader;
00307 
00308     
00309     // Update widgets that keep track of what page the browser is on.
00310     void updatePageTrackers();
00311 
00312     // Sorts on the given row.  If asc is true, sorts in ascending order,
00313     // otherwise sorts in descending order.
00314     void sort(int col, bool asc);
00315     
00316     // Applies the current filter to the displayed rows.
00317     void applyFilter();
00318 
00319 private slots:
00320     // Slot for page back button.  Goes back a page.
00321     void pageBack();
00322 
00323     // Slot for page forward button.  Goes forward a page.
00324     void pageForward();
00325     
00326     // Slot to go to first page.
00327     void pageFirst();
00328     
00329     // Slot to go to last page.
00330     void pageLast();
00331 
00332     // Slot for page go button.  Goes to the page specified in the page line
00333     // edit.
00334     void pageGo();
00335 
00336     // Slot for QTableWidget::cellChanged().  Catches changes from the
00337     // QTableWidget and emits signals as needed.
00338     void notifyDataChanged(int row, int col);
00339 
00340     // Slot for QTableWidget::currentCellChanged().  Updates the display to
00341     // highlight the row and column of the selected cell.
00342     void cellClicked(int row, int col);
00343 
00344     // Slot for the menu command chosen from the reference menu shown on a
00345     // right click.  Catches which subtable the user followed a reference and
00346     // emits signals as needed.
00347     void referenceMenuClicked(QAction* which);
00348 
00349     // Slot for QTableWidget::cellDoubleClicked().  Catches a double-click
00350     // event on a cell in the QTableWidget.  If the value is a table type,
00351     // opens the indicated table in a new tab.  If the value is an array type,
00352     // opens the array in the side panel.  Otherwise, if the table is currently
00353     // editable, enters into editing mode on that cell.
00354     void doubleClicked(int row, int col);
00355     
00356     // Displays the tooltip for the last clicked header in a dialog.
00357     void displayFieldInfo();
00358     
00359     // Displays statistics (min, max, mean) for the last clicked header in a
00360     // dialog.
00361     void displayFieldStatistics();
00362     
00363     // Hides the column that was just right-clicked.
00364     void hideColumn();
00365     
00366     // Formats the field that was just right-clicked.
00367     void formatField();
00368     
00369     // Slot for when a column header is moved.  Enables the "restore columns"
00370     // button.
00371     void headerMoved();
00372     
00373     // Slot for the restore columns button.  Moves all columns back to their
00374     // original locations.
00375     void restoreColumns();
00376 
00377     // Slot for the resize headers button.  Resizes the row and column headers
00378     // to fit displayed contents.  See QTableWidget::resizeRowsToContents() and
00379     // QTableWidget::resizeColumnsToContents().
00380     void resizeHeaders();
00381 
00382     // Slot for QHeaderView::sectionClicked().  Catches a sorting click event
00383     // on a column header and sorts accordingly.
00384     void headerClicked(int index);
00385 
00386     // Slot for the clear sort button.  Clears the sort on the table by moving
00387     // the rows back to their original order.
00388     void clearSort();
00389 
00390     // Slot for copying the currently selected text into the system clipboard.
00391     void copyData();
00392 };
00393 
00394 }
00395 
00396 #endif /* TBDATATAB_H_ */