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