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

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:30:23