1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkQtTableView.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /*-------------------------------------------------------------------------
16   Copyright 2008 Sandia Corporation.
17   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18   the U.S. Government retains certain rights in this software.
19 -------------------------------------------------------------------------*/
20 // .NAME vtkQtTableView - A VTK view based on a Qt Table view.
21 //
22 // .SECTION Description
23 // vtkQtTableView is a VTK view using an underlying QTableView.
24 //
25 // .SECTION Thanks
26 // Thanks to Brian Wylie from Sandia National Laboratories for implementing
27 // this class
28 
29 #ifndef vtkQtTableView_h
30 #define vtkQtTableView_h
31 
32 #include "vtkViewsQtModule.h" // For export macro
33 #include "QVTKWin32Header.h"
34 #include "vtkQtView.h"
35 
36 #include <QPointer>
37 #include <QSortFilterProxyModel>
38 #include "vtkQtAbstractModelAdapter.h"
39 #include "vtkSmartPointer.h"
40 
41 class vtkAddMembershipArray;
42 class vtkApplyColors;
43 class vtkDataObjectToTable;
44 class vtkIdTypeArray;
45 class QItemSelection;
46 class QTableView;
47 class vtkQtTableModelAdapter;
48 
49 class VTKVIEWSQT_EXPORT vtkQtTableView : public vtkQtView
50 {
51 Q_OBJECT
52 
53 public:
54   static vtkQtTableView *New();
55   vtkTypeMacro(vtkQtTableView, vtkQtView);
56   void PrintSelf(ostream& os, vtkIndent indent);
57 
58   // Description:
59   // Get the main container of this view (a  QWidget).
60   // The application typically places the view with a call
61   // to GetWidget(): something like this
62   // this->ui->box->layout()->addWidget(this->View->GetWidget());
63   virtual QWidget* GetWidget();
64 
65   // Description:
66   // Have the view show/hide its column headers
67   void SetShowVerticalHeaders(bool);
68 
69   // Description:
70   // Have the view show/hide its row headers
71   void SetShowHorizontalHeaders(bool);
72 
73   enum
74     {
75     FIELD_DATA = 0,
76     POINT_DATA = 1,
77     CELL_DATA = 2,
78     VERTEX_DATA = 3,
79     EDGE_DATA = 4,
80     ROW_DATA = 5,
81     };
82 
83   // Description:
84   // The field type to copy into the output table.
85   // Should be one of FIELD_DATA, POINT_DATA, CELL_DATA, VERTEX_DATA, EDGE_DATA.
86   vtkGetMacro(FieldType, int);
87   void SetFieldType(int);
88 
89   // Description:
90   // Enable drag and drop on this widget
91   void SetEnableDragDrop(bool);
92 
93   // Description:
94   // Whether the table allows individual columns to be sorted upon
95   // Sorting is enabled by default (turn off for large tables);
96   void SetSortingEnabled(bool);
97 
98   // Description:
99   // Whether or not to display all columns from the input table or to use the
100   // ColumnName provided.
101   // FIXME: This should be replaced with an Add/Remove column API.
102   void SetShowAll(bool);
103   vtkGetMacro(ShowAll, bool);
104 
105   // Description:
106   // The name of a single column to display.
107   // FIXME: This should be replaced with an Add/Remove column API.
108   vtkSetStringMacro(ColumnName);
109   vtkGetStringMacro(ColumnName);
110 
111   void SetColumnVisibility(const QString &name, bool status);
112 
113   // Description:
114   // Set whether or not the table view should split multi-component columns
115   // into multiple single-component columns
116   void SetSplitMultiComponentColumns(bool value);
117 
118   // Description:
119   // Get whether or not the table view splits multi-component columns into
120   // multiple single-component columns
121   bool GetSplitMultiComponentColumns();
122 
123   // Description:
124   // Whether or not to sort selections that the view receives to the top
125   void SetSortSelectionToTop(bool value);
126   vtkGetMacro(SortSelectionToTop, bool);
127 
128   // Description:
129   // Whether or not to add an icon to the row header denoting the color
130   // of an annotated row.
131   void SetApplyRowColors(bool value);
132   vtkGetMacro(ApplyRowColors, bool);
133 
134   // Description:
135   // Updates the view.
136   virtual void Update();
137 
138   // Description:
139   // The array to use for coloring items in view.  Default is "color".
140   void SetColorArrayName(const char* name);
141   const char* GetColorArrayName();
142 
143   // Description:
144   // Whether to color vertices.  Default is off.
145   void SetColorByArray(bool vis);
146   bool GetColorByArray();
147   vtkBooleanMacro(ColorByArray, bool);
148 
149   // Description:
150   // Apply a view theme to this view.
151   virtual void ApplyViewTheme(vtkViewTheme* theme);
152 
153   enum
154     {
155     SELECT_ITEMS = 0,
156     SELECT_ROWS,
157     SELECT_COLUMNS
158     };
159 
160   // Description:
161   // The selection mode for this view.
162   // SELECT_ITEMS (0) selects single items.
163   // SELECT_ROWS (1) selects rows.
164   // SELECT_COLUMNS (2) selects columns.
165   // Linked selection only works when in the default mode
166   // SELECT_ROWS. Selections from other modes may be retrieved
167   // using GetSelectedItems().
168   virtual void SetSelectionBehavior(int type);
169   virtual int GetSelectionBehavior();
170 
171   // Description:
172   // Fills the array with the selected items of the view.
173   // If the selection behavior is SELECT_ITEMS,
174   // arr will be a 2-component array containing (row,column)
175   // for each selected item.
176   // If the selection behavior is SELECT_ROWS or SELECT_COLUMNS,
177   // arr will contain a list of row or column indices.
178   virtual void GetSelectedItems(vtkIdTypeArray* arr);
179 
180 protected:
181   vtkQtTableView();
182   ~vtkQtTableView();
183 
184   virtual void AddRepresentationInternal(vtkDataRepresentation* rep);
185   virtual void RemoveRepresentationInternal(vtkDataRepresentation* rep);
186 
187 private slots:
188   void slotQtSelectionChanged(const QItemSelection&,const QItemSelection&);
189 
190 private:
191   void SetVTKSelection();
192   unsigned long LastSelectionMTime;
193   unsigned long LastInputMTime;
194   unsigned long LastMTime;
195 
196   vtkSetStringMacro(ColorArrayNameInternal);
197   vtkGetStringMacro(ColorArrayNameInternal);
198 
199   QPointer<QTableView> TableView;
200   vtkQtTableModelAdapter* TableAdapter;
201   QSortFilterProxyModel* TableSorter;
202   int FieldType;
203   bool ShowAll;
204   char* ColumnName;
205   bool InSelectionChanged;
206   bool SortSelectionToTop;
207   bool ApplyRowColors;
208   char* ColorArrayNameInternal;
209 
210   vtkSmartPointer<vtkAddMembershipArray> AddSelectedColumn;
211   vtkSmartPointer<vtkDataObjectToTable> DataObjectToTable;
212   vtkSmartPointer<vtkApplyColors> ApplyColors;
213 
214   vtkQtTableView(const vtkQtTableView&);  // Not implemented.
215   void operator=(const vtkQtTableView&);  // Not implemented.
216 
217 };
218 
219 #endif
220