1 #include "TableMainWindow.h" 10 #include <QMessageBox> 13 #include <QTableWidget> 39 TableMainWindow::~TableMainWindow() {
44 void TableMainWindow::clear() {
57 result =
p_listWidget->findItems(QString(
"*"), Qt::MatchWrap | Qt::MatchWildcard);
62 void TableMainWindow::resizeColumn(
int columnIndex) {
63 QHeaderView* header =
p_table->horizontalHeader();
65 QString columnName(
p_table->model()->headerData(columnIndex, Qt::Horizontal).toString());
67 if (columnName.isEmpty())
72 if (header->sectionResizeMode(columnIndex) == QHeaderView:: ResizeToContents) {
73 header->setSectionResizeMode(columnIndex, QHeaderView::Interactive);
76 header->setSectionResizeMode(columnIndex, QHeaderView::ResizeToContents);
87 #if defined(__APPLE__) 88 setWindowFlags(Qt::Tool);
92 setWindowFlags(Qt::Dialog);
95 statusBar()->setSizeGripEnabled(
true);
97 p_table =
new QTableWidget(
this);
98 p_table->setAlternatingRowColors(
true);
100 QHeaderView* columnHeader =
p_table->horizontalHeader();
101 columnHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
102 connect ( columnHeader, SIGNAL( sectionPressed(
int) ),
103 this, SLOT( resizeColumn(
int) ) );
109 p_dock->setObjectName(
"dock");
110 p_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
111 p_dock->setMinimumWidth(190);
114 addDockWidget(Qt::LeftDockWidgetArea,
p_dock, Qt::Vertical);
115 connect(
p_listWidget, SIGNAL(itemChanged(QListWidgetItem *)),
119 QMenuBar *menuBar = this->menuBar();
120 QMenu *fileMenu = menuBar->addMenu(
"&File");
123 p_save->setText(
"Save...");
124 p_save->setShortcut(Qt::CTRL + Qt::Key_S);
126 p_save->setDisabled(
true);
129 saveas->setText(
"Save As...");
130 connect(saveas, SIGNAL(triggered()),
this, SLOT(
saveAsTable()));
133 load->setText(
"Load...");
134 connect(load, SIGNAL(triggered()),
this, SLOT(
loadTable()));
137 del->setText(
"Delete Selected Row(s)");
138 del->setShortcut(Qt::Key_Delete);
139 connect(del, SIGNAL(triggered()),
this, SLOT(
deleteRows()));
142 clear->setText(
"Clear table");
143 clear->setShortcut(Qt::CTRL + Qt::Key_Delete);
144 connect(clear, SIGNAL(triggered()),
this, SLOT(
clearTable()));
147 close->setText(
"Close");
148 connect(close, SIGNAL(triggered()),
this, SLOT(hide()));
150 fileMenu->addAction(
p_save);
151 fileMenu->addAction(saveas);
152 fileMenu->addAction(load);
153 fileMenu->addAction(del);
154 fileMenu->addAction(clear);
155 fileMenu->addAction(close);
162 #if defined(__APPLE__) 164 toolBar->setObjectName(
"ToolBar");
165 toolBar->addAction(
p_save);
166 toolBar->addAction(saveas);
167 toolBar->addAction(load);
168 toolBar->addAction(del);
169 toolBar->addAction(clear);
170 toolBar->addAction(close);
171 this->addToolBar(toolBar);
175 QMenu *viewMenu = menuBar->addMenu(
"&View");
177 cols->setText(
"Columns");
178 connect(cols, SIGNAL(triggered()),
p_dock, SLOT(show()));
179 viewMenu->addAction(cols);
180 this->setMenuBar(menuBar);
181 installEventFilter(
this);
192 this->statusBar()->showMessage(message);
209 const QString &menuText,
int insertAt,
210 Qt::Orientation o, QString toolTip) {
212 int startCol =
p_table->columnCount();
214 for(
int i = 0; !heading.section(
":", i, i).isEmpty(); i++) {
215 QString htext = heading.section(
":", i, i);
217 int destinationColumn = insertAt;
220 p_table->insertColumn(insertAt);
223 destinationColumn = startCol + i;
224 p_table->insertColumn(startCol + i);
226 QTableWidgetItem *header =
new QTableWidgetItem(htext);
229 if (o == Qt::Horizontal) {
230 p_table->setHorizontalHeaderItem(insertAt, header);
233 p_table->setVerticalHeaderItem(insertAt, header);
238 if (o == Qt::Horizontal) {
239 p_table->setHorizontalHeaderItem(startCol + i, header);
242 p_table->setVerticalHeaderItem(startCol + i, header);
246 p_table->setColumnWidth(destinationColumn,
247 QFontMetrics(header->font()).width(header->text()) + 20);
252 int endCol =
p_table->columnCount() - 1;
255 if (!menuText.isEmpty()) {
256 QListWidgetItem *item =
new QListWidgetItem();
258 item->setText(menuText);
259 if (toolTip.isEmpty()) {
260 item->setToolTip(heading);
263 item->setToolTip(toolTip);
273 item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
292 if (this->isHidden()) {
299 int index = itemList().indexOf(item);
304 if (item->checkState() == Qt::Checked) {
305 p_table->setColumnHidden(col,
false);
309 p_table->setColumnHidden(col,
true);
328 int index = itemList().indexOf(item);
336 if (item->checkState() == Qt::Checked) {
337 p_table->setRowHidden(col,
false);
341 p_table->setRowHidden(col,
true);
360 bool vis =
p_table->isVisible();
395 if (
p_table->rowCount() == 0)
return;
396 for(
int r = 0; r <
p_table->rowCount(); r++) {
397 for(
int c = 0; c <
p_table->columnCount(); c++) {
398 p_table->item(r, c)->setText(
"");
402 p_table->scrollToItem(
p_table->item(0, 0), QAbstractItemView::PositionAtTop);
418 for(
int i = 0; i < list.size(); i++) {
434 for(
int r = 0; r <
p_table->rowCount(); r++) {
435 for(
int c = 0; c <
p_table->columnCount(); c++) {
453 if (!this->isVisible())
return;
455 for(
int c = 0; c <
p_table->columnCount(); c++) {
456 p_table->item(row, c)->setText(
"");
466 QString fn = QFileDialog::getSaveFileName((
QWidget *)parent(),
467 "Choose filename to save under",
469 "Text Files (*.txt)");
474 if (!fn.endsWith(
".txt")) {
475 filename = fn +
".txt";
502 QMessageBox::critical((
QWidget *)parent(),
503 "Error",
"Cannot open file, please check permissions");
505 p_save->setDisabled(
true);
515 for(
int i = 0; i <
p_table->columnCount(); i++) {
516 if (!
p_table->isColumnHidden(i)) {
517 QTableWidgetItem *header =
p_table->horizontalHeaderItem(i);
518 QString temp = header->text();
519 temp.replace(
"\n",
" ");
521 line = line +
"\"" + temp +
"\"";
525 line = line +
",\"" + temp +
"\"";
533 for(
int i = 0; i <
p_table->rowCount(); i++) {
538 for(
int j = 0; j <
p_table->columnCount(); j++) {
540 if (!
p_table->isColumnHidden(j)) {
541 if (
p_table->item(i, j) == 0)
break;
542 currentText =
p_table->item(i, j)->text();
545 line = line + currentText;
549 line = line +
"," + currentText;
554 if (line.split(
",", QString::SkipEmptyParts).count() != 0)
572 QListWidgetItem *item,
bool defaultChecked) {
573 QSettings settings(settingsFileName(), QSettings::NativeFormat);
575 QString itemTitle =
"item-" + item->text();
576 Qt::CheckState defaultState = defaultChecked? Qt::Checked : Qt::Unchecked;
577 Qt::CheckState state = (Qt::CheckState)
578 settings.value(itemTitle, defaultState).toInt();
579 item->setCheckState(state);
590 QHeaderView *header =
p_table->horizontalHeader();
591 QSettings settings(settingsFileName(), QSettings::NativeFormat);
593 for (
int columnIndex = 0; columnIndex <
p_table->model()->columnCount(); columnIndex++) {
595 QString headerName =
p_table->model()->headerData(columnIndex, Qt::Horizontal).toString();
596 QString settingName =
"column-" + headerName;
597 QString value = settings.value(settingName,
"auto").toString();
599 if (value ==
"auto" || value ==
"0") {
600 header->setSectionResizeMode(columnIndex, QHeaderView::ResizeToContents);
603 int width = value.toInt();
605 header->setSectionResizeMode(columnIndex, QHeaderView::Interactive);
606 p_table->setColumnWidth(columnIndex, width);
626 QSettings settings(settingsFileName(), QSettings::NativeFormat);
628 foreach (QListWidgetItem *item, itemList()) {
629 QString itemTitle =
"item-" + item->text();
630 settings.setValue(itemTitle, item->checkState());
633 QHeaderView *header =
p_table->horizontalHeader();
634 for(
int columnIndex = 0; columnIndex <
p_table->model()->columnCount(); columnIndex++)
636 QString headerName =
p_table->model()->headerData(columnIndex, Qt::Horizontal).toString();
637 QString settingName =
"column-" + headerName;
639 if (header->sectionResizeMode(columnIndex) == QHeaderView::ResizeToContents)
641 settings.setValue(settingName,
"auto");
645 settings.setValue( settingName, header->sectionSize(columnIndex) );
657 QString fn = QFileDialog::getOpenFileName((
QWidget *)parent(),
658 "Select file to load",
660 "Text Files (*.txt)");
663 if (fn.isEmpty())
return;
670 QMessageBox::critical((
QWidget *)parent(),
671 "Error",
"Cannot open file, please check permissions");
673 p_save->setDisabled(
true);
688 temp.remove(
"Positive ");
694 for(
int i = 0; i < list.count(); i++) {
695 for(
int j = 0; j < itemList().size(); j++) {
696 QListWidgetItem *item = itemList()[j];
699 if (item->text() ==
"Ground Range" && (list[i] ==
"Start Latitude" || list[i] ==
"Start Longitude" ||
700 list[i] ==
"End Latitude" || list[i] ==
"End Longitude")) {
701 item->setCheckState(Qt::Checked);
704 if (item->text() ==
"Pixel Range" && (list[i] ==
"Start Sample" || list[i] ==
"Start Line" ||
705 list[i] ==
"End Sample" || list[i] ==
"End Line")) {
706 item->setCheckState(Qt::Checked);
709 if (item->text() ==
"Pixel Range" && (list[i] ==
"Sample" || list[i] ==
"Line")) {
710 item->setCheckState(Qt::Checked);
713 if (item->text() ==
"Sample:Line" && (list[i] ==
"Sample" || list[i] ==
"Line")) {
714 item->setCheckState(Qt::Checked);
717 if (item->text() ==
"Planetocentric Lat" && list[i] ==
"Planetocentric Latitude") {
718 item->setCheckState(Qt::Checked);
721 if (item->text() ==
"Planetographic Lat" && list[i] ==
"Planetographic Latitude") {
722 item->setCheckState(Qt::Checked);
725 if (item->text() ==
"Projected X:Projected Y" && (list[i] ==
"Projected X" || list[i] ==
"Projected Y")) {
726 item->setCheckState(Qt::Checked);
729 if (item->text() ==
"Radius" && list[i] ==
"Local Radius") {
730 item->setCheckState(Qt::Checked);
733 if (item->text() ==
"XYZ" && (list[i] ==
"Point X" || list[i] ==
"Point Y" || list[i] ==
"Point Z")) {
734 item->setCheckState(Qt::Checked);
737 if (item->text() ==
"Ra:Dec" && (list[i] ==
"Right Ascension" || list[i] ==
"Declination")) {
738 item->setCheckState(Qt::Checked);
741 if (item->text() ==
"Spacecraft Position" && (list[i] ==
"Spacecraft X" || list[i] ==
"Spacecraft Y" || list[i] ==
"Spacecraft Z")) {
742 item->setCheckState(Qt::Checked);
745 if (item->text() ==
"Ephemeris Time" && list[i] ==
"Ephemeris Time") {
746 item->setCheckState(Qt::Checked);
749 if (item->text() ==
"Local Solar Time" && list[i] ==
"Local Solar Time") {
750 item->setCheckState(Qt::Checked);
753 if (item->text() ==
"Segments Sum" && list[i] ==
"Segments Sum km") {
754 item->setCheckState(Qt::Checked);
760 if (item->text() == list[i]) {
761 item->setCheckState(Qt::Checked);
768 for(
int cols = 0; cols <
p_table->columnCount(); cols++) {
769 QString header =
p_table->horizontalHeaderItem(cols)->text();
771 header.remove(
"Positive");
777 if (header == list[i]) {
778 column.push_back(cols);
784 if (!match) column.push_back(-1);
790 while(str.count() != 0) {
794 for(
int c = 0; c <
p_table->columnCount(); c++) {
795 QTableWidgetItem *item =
new QTableWidgetItem(
"");
797 if (c == 0)
p_table->scrollToItem(item);
805 for(
int i = 0; i < column.size(); i++) {
806 QTableWidgetItem *newItem =
new QTableWidgetItem(list[i]);
807 if (column[i] != -1) {
819 this->setWindowTitle(
p_title +
" : " + fn);
833 case QEvent::Close: {
void readColumnSettings()
This method reads the columns in the table and sets their size to the appropriate size...
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.
void writeSettings() const
This overriden method is called when the Tablemainwindow is closed or hidden to write the size and lo...
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.
int selectedRows() const
Returns the selected rows.
void hideEvent(QHideEvent *event)
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 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.
Namespace for ISIS/Bullet specific routines.
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.
int currentIndex() const
Returns the current index.
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...