From c90794878fb7ff08cb060c069774681e61c5152f Mon Sep 17 00:00:00 2001 From: thalieht Date: Wed, 22 Jun 2016 18:25:53 +0300 Subject: [PATCH] SearchTab: Allow to toggle columns in searchtab --- src/gui/search/searchtab.cpp | 56 ++++++++++++++++++++++++++++++++++++ src/gui/search/searchtab.h | 1 + 2 files changed, 57 insertions(+) diff --git a/src/gui/search/searchtab.cpp b/src/gui/search/searchtab.cpp index e7db4eceb..84e575fb4 100644 --- a/src/gui/search/searchtab.cpp +++ b/src/gui/search/searchtab.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -105,9 +106,28 @@ SearchTab::SearchTab(SearchWidget *parent) m_ui->resultsBrowser->setAllColumnsShowFocus(true); m_ui->resultsBrowser->setSortingEnabled(true); + //Ensure that at least one column is visible at all times + bool atLeastOne = false; + for (unsigned int i = 0; i < SearchSortModel::DL_LINK; i++) { + if (!m_ui->resultsBrowser->isColumnHidden(i)) { + atLeastOne = true; + break; + } + } + if (!atLeastOne) + m_ui->resultsBrowser->setColumnHidden(SearchSortModel::NAME, false); + //To also mitigate the above issue, we have to resize each column when + //its size is 0, because explicitly 'showing' the column isn't enough + //in the above scenario. + for (unsigned int i = 0; i < SearchSortModel::DL_LINK; i++) + if ((m_ui->resultsBrowser->columnWidth(i) <= 0) && !m_ui->resultsBrowser->isColumnHidden(i)) + m_ui->resultsBrowser->resizeColumnToContents(i); + // Connect signals to slots (search part) connect(m_ui->resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(downloadItem(const QModelIndex&))); + header()->setContextMenuPolicy(Qt::CustomContextMenu); + connect(header(), SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(displayToggleColumnsMenu(const QPoint &))); connect(header(), SIGNAL(sectionResized(int, int, int)), this, SLOT(saveSettings())); connect(header(), SIGNAL(sectionMoved(int, int, int)), this, SLOT(saveSettings())); @@ -304,3 +324,39 @@ void SearchTab::saveSettings() const { Preferences::instance()->setSearchTabHeaderState(header()->saveState()); } + +void SearchTab::displayToggleColumnsMenu(const QPoint&) +{ + QMenu hideshowColumn(this); + hideshowColumn.setTitle(tr("Column visibility")); + QList actions; + for (int i = 0; i < SearchSortModel::DL_LINK; ++i) { + QAction *myAct = hideshowColumn.addAction(m_searchListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); + myAct->setCheckable(true); + myAct->setChecked(!m_ui->resultsBrowser->isColumnHidden(i)); + actions.append(myAct); + } + int visibleCols = 0; + for (unsigned int i = 0; i < SearchSortModel::DL_LINK; i++) { + if (!m_ui->resultsBrowser->isColumnHidden(i)) + visibleCols++; + + if (visibleCols > 1) + break; + } + + // Call menu + QAction *act = hideshowColumn.exec(QCursor::pos()); + if (act) { + int col = actions.indexOf(act); + Q_ASSERT(col >= 0); + Q_ASSERT(visibleCols > 0); + if ((!m_ui->resultsBrowser->isColumnHidden(col)) && (visibleCols == 1)) + return; + qDebug("Toggling column %d visibility", col); + m_ui->resultsBrowser->setColumnHidden(col, !m_ui->resultsBrowser->isColumnHidden(col)); + if ((!m_ui->resultsBrowser->isColumnHidden(col)) && (m_ui->resultsBrowser->columnWidth(col) <= 5)) + m_ui->resultsBrowser->setColumnWidth(col, 100); + saveSettings(); + } +} diff --git a/src/gui/search/searchtab.h b/src/gui/search/searchtab.h index f74c9cfeb..2322f82da 100644 --- a/src/gui/search/searchtab.h +++ b/src/gui/search/searchtab.h @@ -98,6 +98,7 @@ private slots: void loadSettings(); void saveSettings() const; void updateFilter(); + void displayToggleColumnsMenu(const QPoint&); private: void fillFilterComboBoxes();