Isis 3 Programmer Reference
TableView.cpp
1 #include "IsisDebug.h"
2 
3 #include "TableView.h"
4 
5 #include <iostream>
6 
7 #include <QLabel>
8 #include <QMargins>
9 #include <QSettings>
10 #include <QVBoxLayout>
11 
12 #include "AbstractTableModel.h"
13 #include "IException.h"
14 #include "TableViewHeader.h"
15 #include "TableViewContent.h"
16 #include "TableColumn.h"
17 #include "TableColumnList.h"
18 
19 
20 namespace Isis {
29  QString pathForSettings,
30  QString objName) {
31  nullify();
32 
33  m_settingsPath = new QString(pathForSettings);
34  setObjectName(objName);
35 
36  m_warningLabel = new QLabel;
37 
38  m_model = someModel;
39  connect(m_model, SIGNAL(filterCountsChanged(int, int)),
40  this, SIGNAL(filterCountsChanged(int, int)));
41  connect(m_model, SIGNAL(userWarning(AbstractTableModel::Warning)),
42  this, SLOT(displayWarning(AbstractTableModel::Warning)));
43 
44  m_columns = m_model->getColumns();
45 
46  // Add a column for row numbers and global selection.
47  m_columns->prepend(new TableColumn("", true, false));
48 
49  QSettings settings(*m_settingsPath, QSettings::NativeFormat);
50  QString key;
51  for (int i = 0; i < m_columns->size(); i++) {
52  TableColumn *const &col = (*m_columns)[i];
53  QString colTitle = col->getTitle();
54  int defaultWidth = QFontMetrics(font()).width(colTitle) + 40;
55  if (colTitle.size()) {
56  key = objectName() + " " + colTitle + " width";
57  key.replace(" ", "_");
58  col->setWidth(settings.value(key, defaultWidth).toInt());
59 
60  key = objectName() + " " + colTitle + " ascending";
61  key.replace(" ", "_");
62  col->setSortAscending(settings.value(key, true).toBool());
63  }
64  else {
65  col->setWidth(defaultWidth);
66 
67  // no need to set sort order since it is already ascending by default
68  }
69  }
70 
71  key = objectName() + " sorting order";
72  key.replace(" ", "_");
73  try {
74  m_columns->setSortingOrder(
75  settings.value(key, QStringList()).toStringList());
76  }
77  catch (IException &) {
78  }
79 
81  connect(m_header, SIGNAL(requestedGlobalSelection(bool)),
82  this, SLOT(handleModelSelectionChanged()));
83  connect(m_header, SIGNAL(requestedGlobalSelection(bool)),
84  this, SIGNAL(selectionChanged()));
85 
87  connect(m_content, SIGNAL(tableSelectionChanged()),
88  this, SIGNAL(selectionChanged()));
89  connect(m_content,
90  SIGNAL(rebuildModels(QList< AbstractTreeItem * >)),
91  this,
92  SIGNAL(rebuildModels(QList< AbstractTreeItem * >)));
93  connect(m_content, SIGNAL(horizontalScrollBarValueChanged(int)),
94  m_header, SLOT(updateHeaderOffset(int)));
95  connect(m_content, SIGNAL(modelDataChanged()),
96  this, SIGNAL(modelDataChanged()));
97  connect(m_content, SIGNAL(tableSelectionChanged(QList<AbstractTreeItem *>)),
98  this, SIGNAL(tableSelectionChanged(QList<AbstractTreeItem *>)));
99  connect(m_content, SIGNAL(editControlPoint(ControlPoint *, QString)),
100  this, SIGNAL(editControlPoint(ControlPoint *, QString)));
101 
102  connect(m_header, SIGNAL(columnResized(bool)),
103  m_content, SLOT(updateHorizontalScrollBar(bool)));
104 
105  QVBoxLayout *layout = new QVBoxLayout;
106  layout->addWidget(m_header);
107  layout->addWidget(m_content);
108  layout->addWidget(m_warningLabel);
109  layout->setContentsMargins(0, 0, 0, 0);
110  layout->setSpacing(0);
111 
112  setLayout(layout);
113  }
114 
115 
120  // save column widths
121  ASSERT(objectName().size());
122  if (m_settingsPath->size() && objectName().size()) {
123  QSettings settings(*m_settingsPath, QSettings::NativeFormat);
124  QString key;
125  for (int i = 0; i < m_columns->size(); i++) {
126  TableColumn *const &col = (*m_columns)[i];
127  QString colTitle = col->getTitle();
128  if (colTitle.size()) {
129  key = objectName() + " " + colTitle + " width";
130  key.replace(" ", "_");
131  settings.setValue(key, col->getWidth());
132 
133  key = objectName() + " " + colTitle + " ascending";
134  key.replace(" ", "_");
135  settings.setValue(key, col->sortAscending());
136  }
137  }
138 
139  key = objectName() + " sorting order";
140  key.replace(" ", "_");
141  settings.setValue(key, m_columns->getSortingOrderAsStrings());
142  }
143 
144  delete m_model;
145  m_model = NULL;
146 
147  m_columns = NULL;
148  }
149 
150 
157  return m_header;
158  }
159 
160 
167  void TableView::setColumnVisible(QString column, bool visible) {
168  for (int i = 0; i < m_columns->size(); i++) {
169  TableColumn *col = (*m_columns)[i];
170  if (col->getTitle() == column)
171  col->setVisible(visible);
172  }
173  }
174 
175 
182  return m_content->getModel();
183  }
184 
185 
192  return m_content;
193  }
194 
195 
196  // void TableView::setModel(AbstractTableModel * newModel)
197  // {
198  // ASSERT(m_content);
199  //
200  // if (newModel)
201  // {
202  // ASSERT(m_header);
203  // connect(newModel, SIGNAL(filterProgressChanged(int)),
204  // m_header, SLOT(updateFilterProgress(int)));
205  // connect(newModel, SIGNAL(rebuildProgressChanged(int)),
206  // m_header, SLOT(updateRebuildProgress(int)));
207  // connect(newModel, SIGNAL(filterProgressRangeChanged(int, int)),
208  // m_header, SLOT(updateFilterProgressRange(int, int)));
209  // connect(newModel, SIGNAL(rebuildProgressRangeChanged(int, int)),
210  // m_header, SLOT(updateRebuildProgressRange(int, int)));
211  // connect(newModel, SIGNAL(filterCountsChanged(int, int)),
212  // m_header, SLOT(handleFilterCountsChanged(int, int)));
213  // connect(m_header, SIGNAL(requestedGlobalSelection(bool)),
214  // newModel, SLOT(setGlobalSelection(bool)));
215  // connect(m_header, SIGNAL(requestedGlobalSelection(bool)),
216  // this, SLOT(onModelSelectionChanged()));
217  // connect(m_header, SIGNAL(requestedGlobalSelection(bool)),
218  // this, SIGNAL(selectionChanged()));
219  //
220  // m_columns = newModel->getColumns();
221  //
222  // // Add a column for row numbers and global selection.
223  // m_columns->prepend(new TableColumn("", true, false));
224  //
225  // for (int i = 0; i < m_columns->size(); i++)
226  // {
227  // TableColumn * column = (*m_columns)[i];
228  //
229  // column->setWidth(QFontMetrics(font()).width(column->getTitle()) + 25);
230  // connect(column, SIGNAL(visibilityChanged()), m_header, SLOT(update()));
231  // connect(column, SIGNAL(visibilityChanged()), m_content, SLOT(refresh()));
232  // connect(column, SIGNAL(visibilityChanged()),
233  // m_content, SLOT(updateHorizontalScrollBar()));
234  // connect(column, SIGNAL(widthChanged()), m_content, SLOT(refresh()));
235  // }
236  //
237  // m_header->setColumns(m_columns);
238  // m_content->setModel(newModel);
239  // m_header->update();
240  // }
241  // }
242 
243 
249  void TableView::displayWarning(AbstractTableModel::Warning warning) {
250  switch (warning) {
251  case AbstractTableModel::SortingDisabled:
252  m_warningLabel->setText(tr("<font color='red'>Sorting disabled</font>"));
253  m_warningLabel->setVisible(true);
254  break;
255 
256  case AbstractTableModel::SortingTableSizeLimitReached:
257  m_warningLabel->setText(
258  tr("<font color='red'>Sorting disabled - table row count (%L1) > table size limit"
259  " (%L2)</font>")
260  .arg(m_content->getModel()->getVisibleRowCount())
261  .arg(m_content->getModel()->sortLimit()));
262  m_warningLabel->setVisible(true);
263  break;
264 
265  case AbstractTableModel::None:
266  m_warningLabel->setText(tr(""));
267  m_warningLabel->setVisible(false);
268  break;
269  }
270  }
271 
276  m_content->refresh();
277  }
278 
279 
281  QList< AbstractTreeItem * > newlySelectedItems) {
282  m_content->refresh();
283  m_content->scrollTo(newlySelectedItems);
284  }
285 
290  m_header = NULL;
291  m_content = NULL;
292  m_columns = NULL;
293  m_model = NULL;
294  m_settingsPath = NULL;
295  }
296 }
void scrollTo(QList< AbstractTreeItem * >)
Scrolls to the selected items.
TableViewContent * m_content
The content of the header.
Definition: TableView.h:84
TableViewContent * content()
Returns the content of the table.
Definition: TableView.cpp:191
TableColumnList * m_columns
The columns of the table.
Definition: TableView.h:85
void refresh()
Refreshes the table and viewport.
AbstractTableModel * getModel()
Returns the model.
Definition: TableView.cpp:181
AbstractTableModel * m_model
The model of the table.
Definition: TableView.h:86
TableViewHeader * getHorizontalHeader()
Returns the horizontal header.
Definition: TableView.cpp:156
Translates the tree model into a table model.
A single control point.
Definition: ControlPoint.h:369
void setColumnVisible(QString, bool)
Sets the specified column visible or invisible.
Definition: TableView.cpp:167
void nullify()
Sets all member variables to NULL.
Definition: TableView.cpp:289
AbstractTableModel * getModel()
Returns the model.
virtual ~TableView()
Destructor.
Definition: TableView.cpp:119
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
QString * m_settingsPath
Path of where to read/write the settings.
Definition: TableView.h:87
void handleModelSelectionChanged()
Handles refreshing the content when the model selection is changed.
Definition: TableView.cpp:275
TableView(AbstractTableModel *someModel, QString pathForSettigs, QString objName)
Constructor.
Definition: TableView.cpp:28
TableViewHeader * m_header
The table header.
Definition: TableView.h:83
void displayWarning(AbstractTableModel::Warning)
Displays warnings for a table.
Definition: TableView.cpp:249
QLabel * m_warningLabel
Label of any warnings.
Definition: TableView.h:88