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