mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-09 00:52:30 -06:00
Clean up code related to show/hide columns
This commit is contained in:
@@ -155,7 +155,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent)
|
||||
updatePeerHostNameResolutionState();
|
||||
// SIGNAL/SLOT
|
||||
header()->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(header(), &QWidget::customContextMenuRequested, this, &PeerListWidget::displayToggleColumnsMenu);
|
||||
connect(header(), &QWidget::customContextMenuRequested, this, &PeerListWidget::displayColumnHeaderMenu);
|
||||
connect(header(), &QHeaderView::sectionClicked, this, &PeerListWidget::handleSortColumnChanged);
|
||||
connect(header(), &QHeaderView::sectionMoved, this, &PeerListWidget::saveSettings);
|
||||
connect(header(), &QHeaderView::sectionResized, this, &PeerListWidget::saveSettings);
|
||||
@@ -177,7 +177,7 @@ PeerListWidget::~PeerListWidget()
|
||||
saveSettings();
|
||||
}
|
||||
|
||||
void PeerListWidget::displayToggleColumnsMenu(const QPoint &)
|
||||
void PeerListWidget::displayColumnHeaderMenu()
|
||||
{
|
||||
QMenu *menu = new QMenu(this);
|
||||
menu->setAttribute(Qt::WA_DeleteOnClose);
|
||||
@@ -188,36 +188,22 @@ void PeerListWidget::displayToggleColumnsMenu(const QPoint &)
|
||||
if ((i == PeerListColumns::COUNTRY) && !Preferences::instance()->resolvePeerCountries())
|
||||
continue;
|
||||
|
||||
QAction *myAct = menu->addAction(m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
|
||||
myAct->setCheckable(true);
|
||||
myAct->setChecked(!isColumnHidden(i));
|
||||
myAct->setData(i);
|
||||
}
|
||||
|
||||
connect(menu, &QMenu::triggered, this, [this](const QAction *action)
|
||||
{
|
||||
int visibleCols = 0;
|
||||
for (int i = 0; i < PeerListColumns::IP_HIDDEN; ++i)
|
||||
const auto columnName = m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
|
||||
QAction *action = menu->addAction(columnName, this, [this, i](const bool checked)
|
||||
{
|
||||
if (!isColumnHidden(i))
|
||||
++visibleCols;
|
||||
if (!checked && (visibleColumnsCount() <= 1))
|
||||
return;
|
||||
|
||||
if (visibleCols > 1)
|
||||
break;
|
||||
}
|
||||
setColumnHidden(i, !checked);
|
||||
|
||||
const int col = action->data().toInt();
|
||||
if (checked && (columnWidth(i) <= 5))
|
||||
resizeColumnToContents(i);
|
||||
|
||||
if (!isColumnHidden(col) && (visibleCols == 1))
|
||||
return;
|
||||
|
||||
setColumnHidden(col, !isColumnHidden(col));
|
||||
|
||||
if (!isColumnHidden(col) && (columnWidth(col) <= 5))
|
||||
resizeColumnToContents(col);
|
||||
|
||||
saveSettings();
|
||||
});
|
||||
saveSettings();
|
||||
});
|
||||
action->setCheckable(true);
|
||||
action->setChecked(!isColumnHidden(i));
|
||||
}
|
||||
|
||||
menu->popup(QCursor::pos());
|
||||
}
|
||||
@@ -492,6 +478,18 @@ void PeerListWidget::updatePeer(const BitTorrent::Torrent *torrent, const BitTor
|
||||
}
|
||||
}
|
||||
|
||||
int PeerListWidget::visibleColumnsCount() const
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0, iMax = header()->count(); i < iMax; ++i)
|
||||
{
|
||||
if (!isColumnHidden(i))
|
||||
++count;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
void PeerListWidget::handleResolved(const QHostAddress &ip, const QString &hostname) const
|
||||
{
|
||||
if (hostname.isEmpty())
|
||||
|
||||
@@ -88,7 +88,7 @@ public:
|
||||
private slots:
|
||||
void loadSettings();
|
||||
void saveSettings() const;
|
||||
void displayToggleColumnsMenu(const QPoint &);
|
||||
void displayColumnHeaderMenu();
|
||||
void showPeerListMenu(const QPoint &);
|
||||
void banSelectedPeers();
|
||||
void copySelectedPeers();
|
||||
@@ -97,6 +97,7 @@ private slots:
|
||||
|
||||
private:
|
||||
void updatePeer(const BitTorrent::Torrent *torrent, const BitTorrent::PeerInfo &peer, bool &isNewPeer);
|
||||
int visibleColumnsCount() const;
|
||||
|
||||
void wheelEvent(QWheelEvent *event) override;
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ PropertiesWidget::PropertiesWidget(QWidget *parent)
|
||||
, m_ui->filesList, qOverload<const QModelIndex &>(&QAbstractItemView::edit));
|
||||
connect(m_ui->filesList, &QWidget::customContextMenuRequested, this, &PropertiesWidget::displayFilesListMenu);
|
||||
connect(m_ui->filesList, &QAbstractItemView::doubleClicked, this, &PropertiesWidget::openItem);
|
||||
connect(m_ui->filesList->header(), &QWidget::customContextMenuRequested, this, &PropertiesWidget::displayFileListHeaderMenu);
|
||||
connect(m_ui->filesList->header(), &QWidget::customContextMenuRequested, this, &PropertiesWidget::displayColumnHeaderMenu);
|
||||
connect(m_ui->filesList->header(), &QHeaderView::sectionMoved, this, &PropertiesWidget::saveSettings);
|
||||
connect(m_ui->filesList->header(), &QHeaderView::sectionResized, this, &PropertiesWidget::saveSettings);
|
||||
connect(m_ui->filesList->header(), &QHeaderView::sortIndicatorChanged, this, &PropertiesWidget::saveSettings);
|
||||
@@ -177,28 +177,29 @@ PropertiesWidget::~PropertiesWidget()
|
||||
delete m_ui;
|
||||
}
|
||||
|
||||
void PropertiesWidget::displayFileListHeaderMenu()
|
||||
void PropertiesWidget::displayColumnHeaderMenu()
|
||||
{
|
||||
QMenu *menu = new QMenu(this);
|
||||
menu->setAttribute(Qt::WA_DeleteOnClose);
|
||||
menu->setTitle(tr("Column visibility"));
|
||||
|
||||
for (int i = 0; i < TorrentContentModelItem::TreeItemColumns::NB_COL; ++i)
|
||||
{
|
||||
QAction *myAct = menu->addAction(m_propListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
|
||||
myAct->setCheckable(true);
|
||||
myAct->setChecked(!m_ui->filesList->isColumnHidden(i));
|
||||
if (i == TorrentContentModelItem::TreeItemColumns::COL_NAME)
|
||||
myAct->setEnabled(false);
|
||||
|
||||
connect(myAct, &QAction::toggled, this, [this, i](const bool checked)
|
||||
const auto columnName = m_propListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
|
||||
QAction *action = menu->addAction(columnName, this, [this, i](const bool checked)
|
||||
{
|
||||
m_ui->filesList->setColumnHidden(i, !checked);
|
||||
|
||||
if (!m_ui->filesList->isColumnHidden(i) && (m_ui->filesList->columnWidth(i) <= 5))
|
||||
if (checked && (m_ui->filesList->columnWidth(i) <= 5))
|
||||
m_ui->filesList->resizeColumnToContents(i);
|
||||
|
||||
saveSettings();
|
||||
});
|
||||
action->setCheckable(true);
|
||||
action->setChecked(!m_ui->filesList->isColumnHidden(i));
|
||||
|
||||
if (i == TorrentContentModelItem::TreeItemColumns::COL_NAME)
|
||||
action->setEnabled(false);
|
||||
}
|
||||
|
||||
menu->popup(QCursor::pos());
|
||||
|
||||
@@ -81,7 +81,6 @@ public slots:
|
||||
void readSettings();
|
||||
void saveSettings();
|
||||
void reloadPreferences();
|
||||
void displayFileListHeaderMenu();
|
||||
void openItem(const QModelIndex &index) const;
|
||||
void loadTrackers(BitTorrent::Torrent *const torrent);
|
||||
|
||||
@@ -101,6 +100,7 @@ protected slots:
|
||||
|
||||
private slots:
|
||||
void configure();
|
||||
void displayColumnHeaderMenu();
|
||||
void filterText(const QString &filter);
|
||||
void updateSavePath(BitTorrent::Torrent *const torrent);
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ TrackerListWidget::TrackerListWidget(PropertiesWidget *properties)
|
||||
connect(this, &QWidget::customContextMenuRequested, this, &TrackerListWidget::showTrackerListMenu);
|
||||
// Header
|
||||
header()->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(header(), &QWidget::customContextMenuRequested, this, &TrackerListWidget::displayToggleColumnsMenu);
|
||||
connect(header(), &QWidget::customContextMenuRequested, this, &TrackerListWidget::displayColumnHeaderMenu);
|
||||
connect(header(), &QHeaderView::sectionMoved, this, &TrackerListWidget::saveSettings);
|
||||
connect(header(), &QHeaderView::sectionResized, this, &TrackerListWidget::saveSettings);
|
||||
connect(header(), &QHeaderView::sortIndicatorChanged, this, &TrackerListWidget::saveSettings);
|
||||
@@ -641,17 +641,17 @@ QStringList TrackerListWidget::headerLabels()
|
||||
|
||||
int TrackerListWidget::visibleColumnsCount() const
|
||||
{
|
||||
int visibleCols = 0;
|
||||
for (int i = 0; i < COL_COUNT; ++i)
|
||||
int count = 0;
|
||||
for (int i = 0, iMax = header()->count(); i < iMax; ++i)
|
||||
{
|
||||
if (!isColumnHidden(i))
|
||||
++visibleCols;
|
||||
++count;
|
||||
}
|
||||
|
||||
return visibleCols;
|
||||
return count;
|
||||
}
|
||||
|
||||
void TrackerListWidget::displayToggleColumnsMenu(const QPoint &)
|
||||
void TrackerListWidget::displayColumnHeaderMenu()
|
||||
{
|
||||
QMenu *menu = new QMenu(this);
|
||||
menu->setAttribute(Qt::WA_DeleteOnClose);
|
||||
@@ -659,27 +659,21 @@ void TrackerListWidget::displayToggleColumnsMenu(const QPoint &)
|
||||
|
||||
for (int i = 0; i < COL_COUNT; ++i)
|
||||
{
|
||||
QAction *myAct = menu->addAction(headerLabels().at(i));
|
||||
myAct->setCheckable(true);
|
||||
myAct->setChecked(!isColumnHidden(i));
|
||||
myAct->setData(i);
|
||||
QAction *action = menu->addAction(headerLabels().at(i), this, [this, i](const bool checked)
|
||||
{
|
||||
if (!checked && (visibleColumnsCount() <= 1))
|
||||
return;
|
||||
|
||||
setColumnHidden(i, !checked);
|
||||
|
||||
if (checked && (columnWidth(i) <= 5))
|
||||
resizeColumnToContents(i);
|
||||
|
||||
saveSettings();
|
||||
});
|
||||
action->setCheckable(true);
|
||||
action->setChecked(!isColumnHidden(i));
|
||||
}
|
||||
|
||||
connect(menu, &QMenu::triggered, this, [this](const QAction *action)
|
||||
{
|
||||
const int col = action->data().toInt();
|
||||
Q_ASSERT(visibleColumnsCount() > 0);
|
||||
|
||||
if (!isColumnHidden(col) && (visibleColumnsCount() == 1))
|
||||
return;
|
||||
|
||||
setColumnHidden(col, !isColumnHidden(col));
|
||||
|
||||
if (!isColumnHidden(col) && (columnWidth(col) <= 5))
|
||||
resizeColumnToContents(col);
|
||||
|
||||
saveSettings();
|
||||
});
|
||||
|
||||
menu->popup(QCursor::pos());
|
||||
}
|
||||
|
||||
@@ -61,8 +61,6 @@ public:
|
||||
explicit TrackerListWidget(PropertiesWidget *properties);
|
||||
~TrackerListWidget();
|
||||
|
||||
int visibleColumnsCount() const;
|
||||
|
||||
public slots:
|
||||
void setRowColor(int row, const QColor &color);
|
||||
|
||||
@@ -78,14 +76,18 @@ public slots:
|
||||
void deleteSelectedTrackers();
|
||||
void editSelectedTracker();
|
||||
void showTrackerListMenu(const QPoint &);
|
||||
void displayToggleColumnsMenu(const QPoint &);
|
||||
void loadSettings();
|
||||
void saveSettings() const;
|
||||
|
||||
protected:
|
||||
QVector<QTreeWidgetItem *> getSelectedTrackerItems() const;
|
||||
|
||||
private slots:
|
||||
void displayColumnHeaderMenu();
|
||||
|
||||
private:
|
||||
int visibleColumnsCount() const;
|
||||
|
||||
static QStringList headerLabels();
|
||||
|
||||
PropertiesWidget *m_properties;
|
||||
|
||||
Reference in New Issue
Block a user