Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ui/form/controls/tableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
return rows.toVector();
}

QVector<int> TableView::selectedSelRows() const
{
QSet<int> rowsSet;
const auto indexes = selectedIndexes();
for (const auto &index : indexes) {
rowsSet.insert(index.row());
}

auto rows = rowsSet.values();
std::sort(rows.begin(), rows.end());

Check warning on line 57 in src/ui/form/controls/tableview.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace with the version of "std::ranges::sort" that takes a range.

See more on https://sonarcloud.io/project/issues?id=tnodir_fort&issues=AZ2j9DjRFU0vxzQ9wQK9&open=AZ2j9DjRFU0vxzQ9wQK9&pullRequest=736
return rows.toVector();
}

QModelIndexList TableView::sortedSelectedIndexes() const
{
auto indexes = selectedIndexes();
Expand Down
1 change: 1 addition & 0 deletions src/ui/form/controls/tableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class TableView : public QTableView

int currentRow() const;
QVector<int> selectedRows() const;
QVector<int> selectedSelRows() const;
QModelIndexList sortedSelectedIndexes() const;

QString selectedText() const;
Expand Down
130 changes: 129 additions & 1 deletion src/ui/form/stat/pages/connectionspage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,135 @@
order = connListModel()->sortOrder();
}

const bool sortChanged = section == int(ConnListColumn::Time);

Check warning on line 387 in src/ui/form/stat/pages/connectionspage.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use an intermediary "std::to_underlying" to cast an enum to an integral type that is not the underlying type.

See more on https://sonarcloud.io/project/issues?id=tnodir_fort&issues=AZ2j9DlwFU0vxzQ9wQK-&open=AZ2j9DlwFU0vxzQ9wQK-&pullRequest=736

if (sortChanged) { selectedRowsSave(true); }

auto header = m_connListView->horizontalHeader();
header->setSortIndicator(int(ConnListColumn::Time), order);

connListModel()->sort(int(ConnListColumn::Time), order);

if (sortChanged) {
selectedRowsRestore(true);
updateSelectedRowsRestore(true);
}
}

void ConnectionsPage::selectedRowsSave(const bool monitorPause)
{
if (m_selectedRowsMonitorPaused) { return; }

if (monitorPause) { m_selectedRowsMonitorPaused = true; }

const auto *model = connListModel();

const auto currentRow = connListCurrentIndex();
m_selectedRowsStore.focused = (currentRow >= 0) ? model->getConnIdByIndex(currentRow) : -1;

m_selectedRowsStore.selecteds.clear();
const auto rows = m_connListView->selectedSelRows();
for (const auto &row : rows) {
m_selectedRowsStore.selecteds.insert(model->getConnIdByIndex(row));
}
}

void ConnectionsPage::selectedRowsSaveDo() {
selectedRowsSave();
}

void ConnectionsPage::selectedRowsRestore(const bool monitorUnpause)
{
if (monitorUnpause) { m_selectedRowsMonitorPaused = false; }

m_connListView->setCurrentIndex(QModelIndex());
m_connListView->clearSelection();

const auto selectedEmpty = m_selectedRowsStore.selecteds.isEmpty();
if (selectedEmpty && m_selectedRowsStore.focused == -1) { return; }

auto *selModel = m_connListView->selectionModel();
const auto *model = connListModel();

const auto originalAutoScroll = m_connListView->hasAutoScroll();
m_connListView->setAutoScroll(false);

if (m_selectedRowsStore.focused != -1) {
int row = model->getIndexByConnId(m_selectedRowsStore.focused);
if (row >= 0) {
selModel->setCurrentIndex(model->index(row, 0),
QItemSelectionModel::NoUpdate);
}
}

if (!selectedEmpty) {

QItemSelection selection;
const auto lastCol = model->columnCount() - 1;
for (const auto &connId : std::as_const(m_selectedRowsStore.selecteds)) {
const auto row = model->getIndexByConnId(connId);
if (row >= 0) {
selection.select(model->index(row, 0), model->index(row, lastCol));
}
}

if (!selection.isEmpty()) {
selModel->select(selection,
QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
}

m_connListView->setAutoScroll(originalAutoScroll);
}

void ConnectionsPage::selectedRowsRestoreDo() {
selectedRowsRestore();
}

void ConnectionsPage::updateSelectedRowsRestore(bool fromSort)
{
const bool enable = !connListModel()->isAscendingOrder();

if (enable) {
connect(connListModel(), &QAbstractItemModel::rowsAboutToBeInserted, this,
&ConnectionsPage::selectedRowsSaveDo);
connect(connListModel(), &QAbstractItemModel::rowsAboutToBeRemoved, this,
&ConnectionsPage::selectedRowsSaveDo);
connect(connListModel(), &QAbstractItemModel::layoutAboutToBeChanged, this,
&ConnectionsPage::selectedRowsSaveDo);
connect(connListModel(), &QAbstractItemModel::modelAboutToBeReset, this,
&ConnectionsPage::selectedRowsSaveDo);

connect(connListModel(), &QAbstractItemModel::rowsInserted, this,
&ConnectionsPage::selectedRowsRestoreDo);
connect(connListModel(), &QAbstractItemModel::rowsRemoved, this,
&ConnectionsPage::selectedRowsRestoreDo);
connect(connListModel(), &QAbstractItemModel::layoutChanged, this,
&ConnectionsPage::selectedRowsRestoreDo);
connect(connListModel(), &QAbstractItemModel::modelReset, this,
&ConnectionsPage::selectedRowsRestoreDo);

if (fromSort) { updateAutoScroll(); }
}
else {
disconnect(connListModel(), &QAbstractItemModel::rowsAboutToBeInserted, this,
&ConnectionsPage::selectedRowsSaveDo);
disconnect(connListModel(), &QAbstractItemModel::rowsAboutToBeRemoved, this,
&ConnectionsPage::selectedRowsSaveDo);
disconnect(connListModel(), &QAbstractItemModel::layoutAboutToBeChanged, this,
&ConnectionsPage::selectedRowsSaveDo);
disconnect(connListModel(), &QAbstractItemModel::modelAboutToBeReset, this,
&ConnectionsPage::selectedRowsSaveDo);

disconnect(connListModel(), &QAbstractItemModel::rowsInserted, this,
&ConnectionsPage::selectedRowsRestoreDo);
disconnect(connListModel(), &QAbstractItemModel::rowsRemoved, this,
&ConnectionsPage::selectedRowsRestoreDo);
disconnect(connListModel(), &QAbstractItemModel::layoutChanged, this,
&ConnectionsPage::selectedRowsRestoreDo);
disconnect(connListModel(), &QAbstractItemModel::modelReset, this,
&ConnectionsPage::selectedRowsRestoreDo);
}
}

void ConnectionsPage::doAutoScroll()
Expand All @@ -409,7 +534,10 @@

doAutoScroll();
} else {
connListModel()->disconnect(this);
disconnect(connListModel(), &QAbstractItemModel::rowsInserted, this,
&ConnectionsPage::doAutoScroll);
disconnect(connListModel(), &QAbstractItemModel::modelReset, this,
&ConnectionsPage::doAutoScroll);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/ui/form/stat/pages/connectionspage.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ protected slots:
void setupTableConnHeaderMenuColumns(QMenu *menu, QHeaderView *header);

void onTableConnSortClicked(int section, Qt::SortOrder order);

struct selectedRowsStore {
qint64 focused = -1;
QSet<qint64> selecteds;
};
void selectedRowsSave(const bool monitorPause = false);
void selectedRowsSaveDo();
void selectedRowsRestore(const bool monitorUnpause = false);
void selectedRowsRestoreDo();
void updateSelectedRowsRestore(const bool fromSort = false);

void doAutoScroll();

void updateAutoScroll();
Expand All @@ -60,6 +71,9 @@ protected slots:
private:
ConnListModel *m_connListModel = nullptr;

bool m_selectedRowsMonitorPaused = false;
selectedRowsStore m_selectedRowsStore;

QPushButton *m_btEdit = nullptr;
QAction *m_actCopyAsFilter = nullptr;
QAction *m_actCopy = nullptr;
Expand Down
16 changes: 15 additions & 1 deletion src/ui/model/connlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ qint64 ConnListModel::connIdByIndex(int row) const
return isAscendingOrder() ? (connIdMin() + row) : (connIdMax() - row);
}

int ConnListModel::indexByConnId(qint64 connId) const
{
const qint64 min = connIdMin();
const qint64 max = connIdMax();

if (connId < min || connId > max) return -1;

return isAscendingOrder() ? int(connId - min) : int(max - connId);
}

int ConnListModel::doSqlCount() const
{
return connIdMax() <= 0 ? 0 : int(connIdMax() - connIdMin()) + 1;
Expand Down Expand Up @@ -514,7 +524,11 @@ void ConnListModel::resetConnRows(qint64 idMin, qint64 idMax)

void ConnListModel::removeConnRows(qint64 idMin, int count)
{
beginRemoveRows({}, 0, count - 1);
if (isAscendingOrder()) {
beginRemoveRows({}, 0, count - 1);
} else {
beginRemoveRows({}, rowCount() - count, rowCount() - 1);
}
m_connIdMin = idMin;
invalidateRowCache();
endRemoveRows();
Expand Down
8 changes: 8 additions & 0 deletions src/ui/model/connlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class ConnListModel : public TableSqlModel

const ConnRow &connRowAt(int row) const;

qint64 getConnIdByIndex(int row) const {
return connIdByIndex(row);
}
qint64 getIndexByConnId(qint64 connId) const {
return indexByConnId(connId);
}

QString rowsAsFilter(const QVector<int> &rows) const;

static QString reasonText(FortConnReason reason);
Expand All @@ -81,6 +88,7 @@ protected slots:
qint64 oldIdMin, qint64 oldIdMax, qint64 idMin, qint64 idMax) const;

virtual qint64 connIdByIndex(int row) const;
virtual int indexByConnId(qint64 connId) const;

int doSqlCount() const override;
QString sqlBase() const override;
Expand Down