Commit 1f69baf4 authored by Adam Goins's avatar Adam Goins Committed by Makayla Shepherd
Browse files

Added autoresize behavior to TableMainWindow.

parent 6d6d8aad
Loading
Loading
Loading
Loading
+81 −5
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include <QStatusBar>
#include <QDockWidget>
#include <QFileDialog>
#include <QHeaderView>
#include <QMenuBar>
#include <QMessageBox>
#include <QSettings>
@@ -28,14 +29,15 @@ namespace Isis {
    p_visibleColumns = -1;
    p_currentRow = 0;
    p_currentIndex = 0;
    createTable();

    setObjectName(title);
    createTable();
    readSettings(QSize(500, 300));
    readColumnSettings();
  }


  TableMainWindow::~TableMainWindow() {
    writeSettings();
  }


@@ -57,6 +59,23 @@ namespace Isis {
    return result;
  }

  void TableMainWindow::resizeColumn(int columnIndex) {
    QHeaderView* header = p_table->horizontalHeader();

    QString columnName(p_table->model()->headerData(columnIndex, Qt::Horizontal).toString());

    if (columnName.isEmpty())
    {
      return;
    }

    if (header->sectionResizeMode(columnIndex) == QHeaderView:: ResizeToContents) {
      header->setSectionResizeMode(columnIndex, QHeaderView::Interactive);
    }
    else {
      header->setSectionResizeMode(columnIndex, QHeaderView::ResizeToContents);
    }
  }

  /**
   * This creates the table main window.  The table and docking
@@ -77,6 +96,12 @@ namespace Isis {
    // Create the table widget
    p_table = new QTableWidget(this);
    p_table->setAlternatingRowColors(true);
    //
    QHeaderView* columnHeader = p_table->horizontalHeader();
    columnHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
    connect ( columnHeader, SIGNAL( sectionPressed(int) ),
              this, SLOT( resizeColumn(int) ) );

    setCentralWidget(p_table);

    // Create the dock area
@@ -151,7 +176,6 @@ namespace Isis {
    cols->setText("Columns");
    connect(cols, SIGNAL(triggered()), p_dock, SLOT(show()));
    viewMenu->addAction(cols);

    this->setMenuBar(menuBar);
    installEventFilter(this);
  }
@@ -255,6 +279,7 @@ namespace Isis {

      readItemSettings(heading, item, setOn);
    }
    readColumnSettings();
  }


@@ -550,6 +575,41 @@ namespace Isis {
    item->setCheckState(state);
  }

  /**
   * This method reads the columns in the table and sets their size
   * to the appropriate size, or the size to auto based on what they were
   * stored as.
   *
   * @param heading
   * @param item
   */
  void TableMainWindow::readColumnSettings() {

    QHeaderView* header = p_table->horizontalHeader();
    QSettings settings(settingsFileName(), QSettings::NativeFormat);

    for(int columnIndex = 0; columnIndex < p_table->model()->columnCount(); columnIndex++)
    {
       QString headerName = p_table->model()->headerData(columnIndex, Qt::Horizontal).toString();

       QString settingName = "column-" + headerName;

       QString value = settings.value(settingName, "auto").toString();

       if (value == "auto" || value == "0")
       {
          header->setSectionResizeMode(columnIndex, QHeaderView::ResizeToContents);
       }
       else
       {
         int width = value.toInt();

         header->setSectionResizeMode(columnIndex, QHeaderView::Interactive);
         p_table->setColumnWidth(columnIndex, width);
       }
    }
  }


  /**
   * This overriden method is called when the Tablemainwindow
@@ -566,6 +626,22 @@ namespace Isis {
        QString itemTitle = "item-" + item->text();
        settings.setValue(itemTitle, item->checkState());
      }

      QHeaderView *header = p_table->horizontalHeader();
      for(int columnIndex = 0; columnIndex < p_table->model()->columnCount(); columnIndex++)
      {
         QString headerName = p_table->model()->headerData(columnIndex, Qt::Horizontal).toString();
         QString settingName = "column-" + headerName;

         if (header->sectionResizeMode(columnIndex) == QHeaderView::ResizeToContents)
         {
           settings.setValue(settingName, "auto");
         }
         else
         {
           settings.setValue( settingName, header->sectionSize(columnIndex) );
         }
      }
    }
  }

+8 −0
Original line number Diff line number Diff line
@@ -44,6 +44,11 @@ namespace Isis {
  *   @history 2017-10-06 Adam Goins - showTable() now calls syncColumns() after it calls
  *                          this->show() so that it hides the unselected columns appropriately.
  *                          Fixes #5141.
  *   @history 2017-11-13 Adam Goins - modified createTable() to set the resize property of the
  *                          columns to resize automatically based on the content inside of the
  *                          column. Added resizeColumn() slot and readColumnSettings().
  *                          modifie writeSettings() to write updated settings on destroy.
  *                          Added Fixes #5142.
  */
  class TableMainWindow : public MainWindow {
      Q_OBJECT
@@ -129,6 +134,7 @@ namespace Isis {
      bool trackListItems();
      void loadTable();
      void writeSettings() const;
      void resizeColumn(int columnIndex);

    signals:
      /**
@@ -143,6 +149,8 @@ namespace Isis {
      void readItemSettings(QString heading, QListWidgetItem *item,
                            bool defaultChecked);

      void readColumnSettings();

    private:
      std::string p_appName; //!< The application name
      QWidget *p_parent; //!< The parent widget