mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-01-06 15:04:34 +08:00
Merge pull request #4767 from thalieht/toggle_peer_columns
Add toggle columns menu for peerlist. Closes #3301
This commit is contained in:
commit
9a91ed501e
@ -67,6 +67,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent)
|
|||||||
setItemsExpandable(false);
|
setItemsExpandable(false);
|
||||||
setAllColumnsShowFocus(true);
|
setAllColumnsShowFocus(true);
|
||||||
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||||
|
header()->setStretchLastSection(false);
|
||||||
// List Model
|
// List Model
|
||||||
m_listModel = new QStandardItemModel(0, PeerListDelegate::COL_COUNT);
|
m_listModel = new QStandardItemModel(0, PeerListDelegate::COL_COUNT);
|
||||||
m_listModel->setHeaderData(PeerListDelegate::COUNTRY, Qt::Horizontal, QVariant()); // Country flag column
|
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->setSourceModel(m_listModel);
|
||||||
m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
||||||
setModel(m_proxyModel);
|
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::IP_HIDDEN);
|
||||||
hideColumn(PeerListDelegate::COL_COUNT);
|
hideColumn(PeerListDelegate::COL_COUNT);
|
||||||
if (!Preferences::instance()->resolvePeerCountries())
|
if (!Preferences::instance()->resolvePeerCountries())
|
||||||
hideColumn(PeerListDelegate::COUNTRY);
|
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
|
//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
|
//its size is 0, because explicitly 'showing' the column isn't enough
|
||||||
//in the above scenario.
|
//in the above scenario.
|
||||||
@ -113,6 +119,8 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent)
|
|||||||
// IP to Hostname resolver
|
// IP to Hostname resolver
|
||||||
updatePeerHostNameResolutionState();
|
updatePeerHostNameResolutionState();
|
||||||
// SIGNAL/SLOT
|
// 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)));
|
connect(header(), SIGNAL(sectionClicked(int)), SLOT(handleSortColumnChanged(int)));
|
||||||
handleSortColumnChanged(header()->sortIndicatorSection());
|
handleSortColumnChanged(header()->sortIndicatorSection());
|
||||||
m_copyHotkey = new QShortcut(QKeySequence(Qt::ControlModifier + Qt::Key_C), this, SLOT(copySelectedPeers()), 0, Qt::WidgetShortcut);
|
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;
|
delete m_copyHotkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PeerListWidget::displayToggleColumnsMenu(const QPoint&)
|
||||||
|
{
|
||||||
|
QMenu hideshowColumn(this);
|
||||||
|
hideshowColumn.setTitle(tr("Column visibility"));
|
||||||
|
QList<QAction*> 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()
|
void PeerListWidget::updatePeerHostNameResolutionState()
|
||||||
{
|
{
|
||||||
if (Preferences::instance()->resolvePeerHostNames()) {
|
if (Preferences::instance()->resolvePeerHostNames()) {
|
||||||
|
@ -77,6 +77,7 @@ public:
|
|||||||
private slots:
|
private slots:
|
||||||
void loadSettings();
|
void loadSettings();
|
||||||
void saveSettings() const;
|
void saveSettings() const;
|
||||||
|
void displayToggleColumnsMenu(const QPoint&);
|
||||||
void showPeerListMenu(const QPoint&);
|
void showPeerListMenu(const QPoint&);
|
||||||
void banSelectedPeers();
|
void banSelectedPeers();
|
||||||
void copySelectedPeers();
|
void copySelectedPeers();
|
||||||
|
Loading…
Reference in New Issue
Block a user