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