1 #include "TableMainWindow.h"
10 #include <QMessageBox>
12 #include <QTableWidget>
38 TableMainWindow::~TableMainWindow() {
42 void TableMainWindow::clear() {
55 result =
p_listWidget->findItems(QString(
"*"), Qt::MatchWrap | Qt::MatchWildcard);
68 #if defined(__APPLE__)
69 setWindowFlags(Qt::Tool);
73 setWindowFlags(Qt::Dialog);
76 statusBar()->setSizeGripEnabled(
true);
78 p_table =
new QTableWidget(
this);
79 p_table->setAlternatingRowColors(
true);
83 p_dock =
new QDockWidget(
"Columns",
this);
84 p_dock->setObjectName(
"dock");
85 p_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
86 p_dock->setMinimumWidth(190);
89 addDockWidget(Qt::LeftDockWidgetArea,
p_dock, Qt::Vertical);
90 connect(
p_listWidget, SIGNAL(itemChanged(QListWidgetItem *)),
94 QMenuBar *menuBar = this->menuBar();
95 QMenu *fileMenu = menuBar->addMenu(
"&File");
98 p_save->setText(
"Save...");
99 p_save->setShortcut(Qt::CTRL + Qt::Key_S);
101 p_save->setDisabled(
true);
104 saveas->setText(
"Save As...");
105 connect(saveas, SIGNAL(triggered()),
this, SLOT(
saveAsTable()));
108 load->setText(
"Load...");
109 connect(load, SIGNAL(triggered()),
this, SLOT(
loadTable()));
112 del->setText(
"Delete Selected Row(s)");
113 del->setShortcut(Qt::Key_Delete);
114 connect(del, SIGNAL(triggered()),
this, SLOT(
deleteRows()));
117 clear->setText(
"Clear table");
118 connect(clear, SIGNAL(triggered()),
this, SLOT(
clearTable()));
121 close->setText(
"Close");
122 connect(close, SIGNAL(triggered()),
this, SLOT(hide()));
124 fileMenu->addAction(
p_save);
125 fileMenu->addAction(saveas);
126 fileMenu->addAction(load);
127 fileMenu->addAction(del);
128 fileMenu->addAction(clear);
129 fileMenu->addAction(close);
136 #if defined(__APPLE__)
138 toolBar->setObjectName(
"ToolBar");
139 toolBar->addAction(
p_save);
140 toolBar->addAction(saveas);
141 toolBar->addAction(load);
142 toolBar->addAction(del);
143 toolBar->addAction(clear);
144 toolBar->addAction(close);
145 this->addToolBar(toolBar);
149 QMenu *viewMenu = menuBar->addMenu(
"&View");
151 cols->setText(
"Columns");
152 connect(cols, SIGNAL(triggered()),
p_dock, SLOT(show()));
153 viewMenu->addAction(cols);
155 this->setMenuBar(menuBar);
156 installEventFilter(
this);
167 this->statusBar()->showMessage(message);
184 const QString &menuText,
int insertAt,
185 Qt::Orientation o, QString toolTip) {
187 int startCol =
p_table->columnCount();
189 for(
int i = 0; !heading.section(
":", i, i).isEmpty(); i++) {
190 QString htext = heading.section(
":", i, i);
192 int destinationColumn = insertAt;
195 p_table->insertColumn(insertAt);
198 destinationColumn = startCol + i;
199 p_table->insertColumn(startCol + i);
201 QTableWidgetItem *header =
new QTableWidgetItem(htext);
204 if (o == Qt::Horizontal) {
205 p_table->setHorizontalHeaderItem(insertAt, header);
208 p_table->setVerticalHeaderItem(insertAt, header);
213 if (o == Qt::Horizontal) {
214 p_table->setHorizontalHeaderItem(startCol + i, header);
217 p_table->setVerticalHeaderItem(startCol + i, header);
221 p_table->setColumnWidth(destinationColumn,
222 QFontMetrics(header->font()).width(header->text()) + 20);
227 int endCol =
p_table->columnCount() - 1;
230 if (!menuText.isEmpty()) {
231 QListWidgetItem *item =
new QListWidgetItem();
233 item->setText(menuText);
234 if (toolTip.isEmpty()) {
235 item->setToolTip(heading);
238 item->setToolTip(toolTip);
248 item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
266 if (this->isHidden()) {
273 int index = itemList().indexOf(item);
278 if (item->checkState() == Qt::Checked) {
279 p_table->setColumnHidden(col,
false);
283 p_table->setColumnHidden(col,
true);
302 int index = itemList().indexOf(item);
310 if (item->checkState() == Qt::Checked) {
311 p_table->setRowHidden(col,
false);
315 p_table->setRowHidden(col,
true);
334 bool vis =
p_table->isVisible();
361 if (
p_table->rowCount() == 0)
return;
362 for(
int r = 0; r <
p_table->rowCount(); r++) {
363 for(
int c = 0; c <
p_table->columnCount(); c++) {
364 p_table->item(r, c)->setText(
"");
368 p_table->scrollToItem(
p_table->item(0, 0), QAbstractItemView::PositionAtTop);
384 for(
int i = 0; i < list.size(); i++) {
385 if (selectedRows.size() == 0) {
386 selectedRows.push_back(
p_table->row(list[i]));
388 else if (!selectedRows.contains(
p_table->row(list[i]))) {
389 selectedRows.push_back(
p_table->row(list[i]));
393 qSort(selectedRows.begin(), selectedRows.end());
394 for(
int d = selectedRows.size(); d > 0; d--) {
395 p_table->removeRow(selectedRows[d-1]);
400 for(
int r = 0; r <
p_table->rowCount(); r++) {
401 for(
int c = 0; c <
p_table->columnCount(); c++) {
419 if (!this->isVisible())
return;
421 for(
int c = 0; c <
p_table->columnCount(); c++) {
422 p_table->item(row, c)->setText(
"");
432 QString fn = QFileDialog::getSaveFileName((
QWidget *)parent(),
433 "Choose filename to save under",
435 "Text Files (*.txt)");
440 if (!fn.endsWith(
".txt")) {
441 filename = fn +
".txt";
468 QMessageBox::critical((
QWidget *)parent(),
469 "Error",
"Cannot open file, please check permissions");
471 p_save->setDisabled(
true);
481 for(
int i = 0; i <
p_table->columnCount(); i++) {
482 if (!
p_table->isColumnHidden(i)) {
483 QTableWidgetItem *header =
p_table->horizontalHeaderItem(i);
484 QString temp = header->text();
485 temp.replace(
"\n",
" ");
487 line = line +
"\"" + temp +
"\"";
491 line = line +
",\"" + temp +
"\"";
499 for(
int i = 0; i <
p_table->rowCount(); i++) {
504 for(
int j = 0; j <
p_table->columnCount(); j++) {
506 if (!
p_table->isColumnHidden(j)) {
507 if (
p_table->item(i, j) == 0)
break;
508 currentText =
p_table->item(i, j)->text();
511 line = line + currentText;
515 line = line +
"," + currentText;
520 if (line.split(
",", QString::SkipEmptyParts).count() != 0)
538 QListWidgetItem *item,
bool defaultChecked) {
539 QSettings settings(settingsFileName(), QSettings::NativeFormat);
541 QString itemTitle =
"item-" + item->text();
542 Qt::CheckState defaultState = defaultChecked? Qt::Checked : Qt::Unchecked;
543 Qt::CheckState state = (Qt::CheckState)
544 settings.value(itemTitle, defaultState).toInt();
545 item->setCheckState(state);
558 QSettings settings(settingsFileName(), QSettings::NativeFormat);
560 foreach (QListWidgetItem *item, itemList()) {
561 QString itemTitle =
"item-" + item->text();
562 settings.setValue(itemTitle, item->checkState());
573 QString fn = QFileDialog::getOpenFileName((
QWidget *)parent(),
574 "Select file to load",
576 "Text Files (*.txt)");
579 if (fn.isEmpty())
return;
586 QMessageBox::critical((
QWidget *)parent(),
587 "Error",
"Cannot open file, please check permissions");
589 p_save->setDisabled(
true);
604 temp.remove(
"Positive ");
610 for(
int i = 0; i < list.count(); i++) {
611 for(
int j = 0; j < itemList().size(); j++) {
612 QListWidgetItem *item = itemList()[j];
615 if (item->text() ==
"Ground Range" && (list[i] ==
"Start Latitude" || list[i] ==
"Start Longitude" ||
616 list[i] ==
"End Latitude" || list[i] ==
"End Longitude")) {
617 item->setCheckState(Qt::Checked);
620 if (item->text() ==
"Pixel Range" && (list[i] ==
"Start Sample" || list[i] ==
"Start Line" ||
621 list[i] ==
"End Sample" || list[i] ==
"End Line")) {
622 item->setCheckState(Qt::Checked);
625 if (item->text() ==
"Pixel Range" && (list[i] ==
"Sample" || list[i] ==
"Line")) {
626 item->setCheckState(Qt::Checked);
629 if (item->text() ==
"Sample:Line" && (list[i] ==
"Sample" || list[i] ==
"Line")) {
630 item->setCheckState(Qt::Checked);
633 if (item->text() ==
"Planetocentric Lat" && list[i] ==
"Planetocentric Latitude") {
634 item->setCheckState(Qt::Checked);
637 if (item->text() ==
"Planetographic Lat" && list[i] ==
"Planetographic Latitude") {
638 item->setCheckState(Qt::Checked);
641 if (item->text() ==
"Projected X:Projected Y" && (list[i] ==
"Projected X" || list[i] ==
"Projected Y")) {
642 item->setCheckState(Qt::Checked);
645 if (item->text() ==
"Radius" && list[i] ==
"Local Radius") {
646 item->setCheckState(Qt::Checked);
649 if (item->text() ==
"XYZ" && (list[i] ==
"Point X" || list[i] ==
"Point Y" || list[i] ==
"Point Z")) {
650 item->setCheckState(Qt::Checked);
653 if (item->text() ==
"Ra:Dec" && (list[i] ==
"Right Ascension" || list[i] ==
"Declination")) {
654 item->setCheckState(Qt::Checked);
657 if (item->text() ==
"Spacecraft Position" && (list[i] ==
"Spacecraft X" || list[i] ==
"Spacecraft Y" || list[i] ==
"Spacecraft Z")) {
658 item->setCheckState(Qt::Checked);
661 if (item->text() ==
"Ephemeris Time" && list[i] ==
"Ephemeris Time") {
662 item->setCheckState(Qt::Checked);
665 if (item->text() ==
"Local Solar Time" && list[i] ==
"Local Solar Time") {
666 item->setCheckState(Qt::Checked);
669 if (item->text() ==
"Segments Sum" && list[i] ==
"Segments Sum km") {
670 item->setCheckState(Qt::Checked);
676 if (item->text() == list[i]) {
677 item->setCheckState(Qt::Checked);
684 for(
int cols = 0; cols <
p_table->columnCount(); cols++) {
685 QString header =
p_table->horizontalHeaderItem(cols)->text();
687 header.remove(
"Positive");
693 if (header == list[i]) {
694 column.push_back(cols);
700 if (!match) column.push_back(-1);
706 while(str.count() != 0) {
710 for(
int c = 0; c <
p_table->columnCount(); c++) {
711 QTableWidgetItem *item =
new QTableWidgetItem(
"");
713 if (c == 0)
p_table->scrollToItem(item);
721 for(
int i = 0; i < column.size(); i++) {
722 QTableWidgetItem *newItem =
new QTableWidgetItem(list[i]);
723 if (column[i] != -1) {
735 this->setWindowTitle(
p_title +
" : " + fn);
749 case QEvent::Close: {
virtual void readSettings(QSize defaultSize=QSize())
This method ensure that the settings get written even if the Main window was only hidden...
QAction * p_save
Action to save the table to the current file.
void setStatusMessage(QString message)
sets the status message in the lower lefthand corner of the window.
bool eventFilter(QObject *o, QEvent *e)
This event filter is installed in the constructor.
void saveAsTable()
This method will select a file, set it as the current file and save the table.
int p_currentIndex
Current index.
QFile p_currentFile
The current file.
void clearRow(int row)
This method clears the text of the given row.
void deleteColumn(int item)
This method deletes a column from the table.
void hideEvent(QHideEvent *event)
int selectedRows() const
Returns the selected rows.
bool trackListItems()
Returns whether or not we should track items.
void createTable()
This creates the table main window.
void deleteRows()
This method is called when the user selects a row or rows uses the delete button or selects the delet...
bool p_trackItems
Boolean to track items.
void saveTable()
This method allows the user to save the data from the table to the current file.
void setTrackListItems(bool track=false)
If this property is true, the class will keep track of the checked/unchecked items in the dock area w...
void readItemSettings(QString heading, QListWidgetItem *item, bool defaultChecked)
This method reads the 'checked' settings on the items listed in the dock area which determine which t...
QList< int > p_endColumn
List of end columns.
int p_currentRow
Current row.
QPointer< QListWidget > p_listWidget
List widget.
virtual void closeEvent(QCloseEvent *event)
This method is overridden so that we can be sure to write the current settings of the Main window...
void closeEvent(QCloseEvent *event)
void writeSettings() const
This overriden method is called when the Tablemainwindow is closed or hidden to write the size and lo...
void syncColumns()
This method hides and shows the columns according to which items the user has selected to be view-abl...
int p_visibleColumns
Number of visible columns.
void setCurrentRow(int row)
Sets the current row to row.
TableMainWindow(QString title, QWidget *parent=0)
Constructs a new TableMainWindow object.
QString p_title
The title string.
void clearTable()
This method clears all items from each row and column.
void loadTable()
This method loads a text file into the table.
int currentIndex() const
Returns the current index.
Base class for the Qisis main windows.
void fileLoaded()
Signal emitted when a file has loaded.
void addToTable(bool setOn, const QString &heading, const QString &menuText="", int insertAt=-1, Qt::Orientation o=Qt::Horizontal, QString toolTip="")
Adds a new column to the table when a new curve is added to the plot.
QList< int > p_startColumn
List of start columns.
void showTable()
This method checks to see if the table has been created.
QTableWidget * p_table
The table.
QWidget * p_parent
The parent widget.
QDockWidget * p_dock
The dock widget.
void setCurrentIndex(int currentIndex)
Sets the current index to currentIndex.
void syncRows()
Use this method to sync the table with the dock widget list if the table orientation is horizontal...