Isis 3 Programmer Reference
TableColumnList.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "IsisDebug.h"
10 
11 #include "TableColumnList.h"
12 
13 #include <iostream>
14 
15 #include <QList>
16 #include <QPair>
17 #include <QString>
18 
19 #include "IException.h"
20 #include "TableColumn.h"
21 
22 
23 using std::cerr;
24 using std::swap;
25 
26 
27 namespace Isis {
28  TableColumnList::TableColumnList() {
29  nullify();
30 
31  m_cols = new QList< TableColumn * >;
32  m_sortingOrder = new QList< TableColumn * >;
33  }
34 
35 
36  TableColumnList::TableColumnList(TableColumnList const &other) {
37  nullify();
38 
39  m_cols = new QList< TableColumn * >(*other.m_cols);
40  m_sortingOrder = new QList< TableColumn * >(*other.m_sortingOrder);
41  }
42 
43 
44  TableColumnList::~TableColumnList() {
45  delete m_cols;
46  m_cols = NULL;
47 
48  delete m_sortingOrder;
49  m_sortingOrder = NULL;
50  }
51 
52 
53  TableColumn *&TableColumnList::operator[](int index) {
54  checkIndexRange(index);
55 
56  return (*m_cols)[index];
57  }
58 
59 
60  TableColumn *&TableColumnList::operator[](QString title) {
61  for (int i = 0; i < m_cols->size(); i++)
62  if (m_cols->at(i)->getTitle() == title)
63  return (*m_cols)[i];
64 
65  QString msg = "There is no column with a title of [";
66  msg += title;
67  msg += "] inside this column list";
68  throw IException(IException::Programmer, msg, _FILEINFO_);
69  }
70 
71 
72  void TableColumnList::append(TableColumn *newCol) {
73  if (!newCol) {
74  QString msg = "Attempted to add NULL column to the columnlist";
75  throw IException(IException::Programmer, msg, _FILEINFO_);
76  }
77 
78  m_cols->append(newCol);
79  m_sortingOrder->append(newCol);
80  connect(newCol, SIGNAL(sortOutDated()), this, SIGNAL(sortOutDated()));
81  }
82 
83 
84  void TableColumnList::prepend(TableColumn *newCol) {
85  m_cols->prepend(newCol);
86  m_sortingOrder->append(newCol);
87  }
88 
89 
90  int TableColumnList::indexOf(TableColumn const *someCol) const {
91  int index = -1;
92  for (int i = 0; index < 0 && i < m_cols->size(); i++)
93  if ((*m_cols)[i] == someCol)
94  index = i;
95 
96  return index;
97  }
98 
99 
100  bool TableColumnList::contains(TableColumn const *someCol) const {
101  return indexOf(someCol) != -1;
102  }
103 
104 
105  bool TableColumnList::contains(QString columnTitle) const {
106  bool foundTitle = false;
107  for (int i = 0; i < m_cols->size() && !foundTitle; i++)
108  foundTitle = (m_cols->at(i)->getTitle() == columnTitle);
109  return foundTitle;
110  }
111 
112 
113  void TableColumnList::lower(TableColumn *col, bool emitSortOutDated) {
114  int oldIndex = m_sortingOrder->indexOf(col);
115  checkIndexRange(oldIndex);
116 
117  // if not already lowest priority
118  if (oldIndex < m_sortingOrder->size() - 1) {
119  m_sortingOrder->removeAt(oldIndex);
120  m_sortingOrder->insert(oldIndex + 1, col);
121  }
122 
123  if (emitSortOutDated)
124  emit sortOutDated();
125  }
126 
127 
128  //void TableColumnList::lower(int visibleColumnIndex, bool emitSortOutDated) {
129  // lower(indexOf(getVisibleColumns()[visibleColumnIndex]), emitSortOutDated);
130  //}
131 
132 
133  void TableColumnList::raise(TableColumn *col, bool emitSortOutDated) {
134  int oldIndex = m_sortingOrder->indexOf(col);
135  checkIndexRange(oldIndex);
136 
137  // if not already highest priority
138  if (oldIndex > 0) {
139  m_sortingOrder->removeAt(oldIndex);
140  m_sortingOrder->insert(oldIndex - 1, col);
141  }
142 
143  if (emitSortOutDated)
144  emit sortOutDated();
145  }
146 
147 
148  //void TableColumnList::raise(int visibleColumnIndex, bool emitSortOutDated) {
149  // raise(indexOf(getVisibleColumns()[visibleColumnIndex]), emitSortOutDated);
150  //}
151 
152 
153  void TableColumnList::raiseToTop(TableColumn *col) {
154  while (m_sortingOrder->at(0) != col)
155  raise(col, false);
156 
157  emit sortOutDated();
158  }
159 
160 
161  //void TableColumnList::raiseToTop(int visibleColumnIndex) {
162  // raiseToTop(indexOf(getVisibleColumns()[visibleColumnIndex]));
163  //}
164 
165 
166  int TableColumnList::size() const {
167  ASSERT(m_cols);
168  return m_cols->size();
169  }
170 
171 
172  TableColumnList &TableColumnList::operator=(
173  TableColumnList other) {
174  swap(*m_cols, *other.m_cols);
175  swap(*m_sortingOrder, *other.m_sortingOrder);
176 
177  return *this;
178  }
179 
180 
185  int minX = 0;
186  int maxX = 0;
187 
188  TableColumnList visibleCols = getVisibleColumns();
189 
190  if (visibleColumn < visibleCols.size() && visibleColumn >= 0) {
191  int indent = 0;
192 
193  for (int i = 0; i < visibleColumn; i++)
194  indent += visibleCols[i]->getWidth() - 1;
195 
196  minX = indent;
197  maxX = minX + visibleCols[visibleColumn]->getWidth() - 1;
198  }
199 
200  return QPair<int, int>(minX, maxX);
201  }
202 
203 
204  TableColumnList TableColumnList::getVisibleColumns() {
205  TableColumnList visibleColumns;
206 
207  // cerr << "TableColumnList::getVisibleColumns() this: " << this << "\n";
208  // cerr << "TableColumnList::getVisibleColumns() m_cols size: " << m_cols->size() << "\n";
209 
210  for (int i = 0; i < size(); i++)
211  if (m_cols->at(i)->isVisible())
212  visibleColumns.append(m_cols->at(i));
213 
214  // cerr << "TableColumnList::getVisibleColumns() visibleColumns: " << visibleColumns.size() << "\n";
215 
216  // fix sorting order
217  *visibleColumns.m_sortingOrder = *m_sortingOrder;
218  for (int i = m_sortingOrder->size() - 1; i >= 0; i--)
219  if (!visibleColumns.contains((*visibleColumns.m_sortingOrder)[i]))
220  visibleColumns.m_sortingOrder->removeAt(i);
221 
222  // cerr << "TableColumnList::getVisibleColumns() visibleColumns: " << visibleColumns.size() << "\n";
223 
224  return visibleColumns;
225  }
226 
227 
228  int TableColumnList::getVisibleWidth() const {
229  int width = 0;
230 
231  for (int i = 0; i < size(); i++)
232  if (m_cols->at(i)->isVisible())
233  width += m_cols->at(i)->getWidth() - 1;
234  // For the border...
235  width -= 2;
236 
237  return width;
238  }
239 
240 
241  QList< TableColumn * > TableColumnList::getSortingOrder() {
242  ASSERT(m_sortingOrder);
243 
244  QList< TableColumn * > validSortingOrder;
245  for (int i = 0; i < m_sortingOrder->size(); i++)
246  if (m_sortingOrder->at(i)->getTitle().size())
247  validSortingOrder.append(m_sortingOrder->at(i));
248 
249  return validSortingOrder;
250  }
251 
252 
253  QStringList TableColumnList::getSortingOrderAsStrings() const {
254  QStringList m_sortingOrderStrings;
255  for (int i = 0; i < m_sortingOrder->size(); i++)
256  if (m_sortingOrder->at(i)->getTitle().size())
257  m_sortingOrderStrings.append(m_sortingOrder->at(i)->getTitle());
258 
259  return m_sortingOrderStrings;
260  }
261 
262 
263  void TableColumnList::setSortingOrder(QStringList newOrder) {
264  for (int i = newOrder.size() - 1; i >= 0; i--)
265  if (contains(newOrder[i]))
266  raiseToTop(operator[](newOrder[i]));
267  }
268 
269 
270  void TableColumnList::checkIndexRange(int index) {
271  ASSERT(m_cols);
272 
273  if (index < 0 || index >= m_cols->size()) {
274  QString msg = "index [";
275  msg += index;
276  msg += "] is out of range. Size of list is: ";
277  msg += m_cols->size();
278  throw IException(IException::Programmer, msg, _FILEINFO_);
279  }
280  }
281 
282 
283  void TableColumnList::nullify() {
284  m_cols = NULL;
285  m_sortingOrder = NULL;
286  }
287 }
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
QStringList
Isis::TableColumnList
Definition: TableColumnList.h:30
Isis::TableColumnList::getVisibleXRange
QPair< int, int > getVisibleXRange(int visibleColumn)
Definition: TableColumnList.cpp:184
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
QPair
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:23
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16