From 1fa6f3123ba73ee9ae6e885438a78ec67e11d3ab Mon Sep 17 00:00:00 2001 From: thalieht Date: Sun, 7 Feb 2016 19:46:56 +0200 Subject: [PATCH] add toggle columns for peerlist --- src/gui/properties/peerlistwidget.cpp | 54 ++++++++++++++++++++++++--- src/gui/properties/peerlistwidget.h | 1 + 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index 705f513c5..85f1b1c42 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -67,6 +67,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent) setItemsExpandable(false); setAllColumnsShowFocus(true); setSelectionMode(QAbstractItemView::ExtendedSelection); + header()->setStretchLastSection(false); // List Model m_listModel = new QStandardItemModel(0, PeerListDelegate::COL_COUNT); m_listModel->setHeaderData(PeerListDelegate::COUNTRY, Qt::Horizontal, QVariant()); // Country flag column @@ -87,15 +88,20 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent) m_proxyModel->setSourceModel(m_listModel); m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); setModel(m_proxyModel); - //Explicitly set the column visibility. When columns are added/removed - //between versions this prevents some of them being hidden due to - //incorrect restoreState() being used. - for (unsigned int i = 0; i < PeerListDelegate::IP_HIDDEN; i++) - showColumn(i); hideColumn(PeerListDelegate::IP_HIDDEN); hideColumn(PeerListDelegate::COL_COUNT); if (!Preferences::instance()->resolvePeerCountries()) hideColumn(PeerListDelegate::COUNTRY); + //Ensure that at least one column is visible at all times + bool atLeastOne = false; + for (unsigned int i = 0; i < PeerListDelegate::IP_HIDDEN; i++) { + if (!isColumnHidden(i)) { + atLeastOne = true; + break; + } + } + if (!atLeastOne) + setColumnHidden(PeerListDelegate::IP, 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. @@ -113,6 +119,8 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent) // IP to Hostname resolver updatePeerHostNameResolutionState(); // SIGNAL/SLOT + header()->setContextMenuPolicy(Qt::CustomContextMenu); + connect(header(), SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(displayToggleColumnsMenu(const QPoint &))); connect(header(), SIGNAL(sectionClicked(int)), SLOT(handleSortColumnChanged(int))); handleSortColumnChanged(header()->sortIndicatorSection()); m_copyHotkey = new QShortcut(QKeySequence(Qt::ControlModifier + Qt::Key_C), this, SLOT(copySelectedPeers()), 0, Qt::WidgetShortcut); @@ -138,6 +146,42 @@ PeerListWidget::~PeerListWidget() delete m_copyHotkey; } +void PeerListWidget::displayToggleColumnsMenu(const QPoint&) +{ + QMenu hideshowColumn(this); + hideshowColumn.setTitle(tr("Column visibility")); + QList actions; + for (int i = 0; i < PeerListDelegate::IP_HIDDEN; ++i) { + QAction *myAct = hideshowColumn.addAction(m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); + myAct->setCheckable(true); + myAct->setChecked(!isColumnHidden(i)); + actions.append(myAct); + } + int visibleCols = 0; + for (unsigned int i = 0; i < PeerListDelegate::IP_HIDDEN; i++) { + if (!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 (!isColumnHidden(col) && (visibleCols == 1)) + return; + qDebug("Toggling column %d visibility", col); + setColumnHidden(col, !isColumnHidden(col)); + if (!isColumnHidden(col) && (columnWidth(col) <= 5)) + setColumnWidth(col, 100); + saveSettings(); + } +} + void PeerListWidget::updatePeerHostNameResolutionState() { if (Preferences::instance()->resolvePeerHostNames()) { diff --git a/src/gui/properties/peerlistwidget.h b/src/gui/properties/peerlistwidget.h index 7ec0669fe..053ba517a 100644 --- a/src/gui/properties/peerlistwidget.h +++ b/src/gui/properties/peerlistwidget.h @@ -77,6 +77,7 @@ public: private slots: void loadSettings(); void saveSettings() const; + void displayToggleColumnsMenu(const QPoint&); void showPeerListMenu(const QPoint&); void banSelectedPeers(); void copySelectedPeers();