Skip to content

Commit c18dcd8

Browse files
committed
Scroll to top of table after resorting
Closes #195
1 parent 911a50e commit c18dcd8

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

src/comp_tables/composite_table.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ void CompositeTable::sort(int columnIndex, Qt::SortOrder order)
928928
currentSorting = {column, order};
929929

930930
performSort(previousSort, true);
931+
Q_EMIT wasResorted();
931932
}
932933

933934
/**

src/comp_tables/composite_table.h

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ struct SortingPass {
8484
* @see CompositeColumn
8585
*/
8686
class CompositeTable : public QAbstractTableModel {
87+
Q_OBJECT
88+
8789
/** The project database. */
8890
const Database* const db;
8991
/** The database table this table is based on. */
@@ -202,6 +204,12 @@ class CompositeTable : public QAbstractTableModel {
202204

203205
public:
204206
ProjectSettings* getProjectSettings() const;
207+
208+
signals:
209+
/**
210+
* Emitted after the table was resorted.
211+
*/
212+
void wasResorted();
205213
};
206214

207215

src/main/main_window.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,12 @@ void MainWindow::connectUI()
197197
connect(mainAreaTabs, &QTabWidget::currentChanged, this, &MainWindow::handle_tabChanged);
198198
// Double clicks on table
199199
for (const ItemTypeMapper* const mapper : typesHandler->getAllMappers()) {
200-
auto openFunction = [this, mapper] (const QModelIndex& index) {
201-
if (mapper->type == ItemTypeAscent) {
202-
viewItem(mapper, ViewRowIndex(index.row()));
203-
} else {
204-
editItem(mapper, index);
205-
}
206-
};
207-
connect(mapper->tableView, &QTableView::doubleClicked, this, openFunction);
200+
auto handlerFunction = &MainWindow::handle_editSelectedItem;
201+
if (mapper->type == ItemTypeAscent) {
202+
handlerFunction = &MainWindow::handle_viewSelectedItem;
203+
}
204+
connect(mapper->tableView, &QTableView::doubleClicked, this, handlerFunction);
205+
connect(mapper->compTable, &CompositeTable::wasResorted, this, &MainWindow::scrollToTopAfterSorting);
208206
}
209207
}
210208

@@ -826,6 +824,16 @@ void MainWindow::performUpdatesAfterUserAction(const ItemTypeMapper* const mappe
826824
updateFilters();
827825
}
828826

827+
/**
828+
* Scrolls the active table to the top.
829+
*
830+
* To be called after the table was sorted.
831+
*/
832+
void MainWindow::scrollToTopAfterSorting()
833+
{
834+
getActiveMapper()->tableView->scrollToTop();
835+
}
836+
829837
/**
830838
* Updates elements of the ascent filter bar which depend on contents of the composite tables.
831839
*

src/main/main_window.h

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class MainWindow : public QMainWindow, public Ui_MainWindow
126126
void deleteItems(const ItemTypeMapper* const mapper, QSet<ViewRowIndex> viewRowIndices);
127127
// Helpers
128128
void performUpdatesAfterUserAction(const ItemTypeMapper* const mapper, bool numberOfEntriesChanged, BufferRowIndex bufferRowToSelectIndex = BufferRowIndex());
129+
void scrollToTopAfterSorting();
129130
void updateFilters(const ItemTypeMapper* mapper = nullptr);
130131
public:
131132
void updateSelectionAfterUserAction(const ItemTypeMapper* const mapper, ViewRowIndex viewRowIndex);

0 commit comments

Comments
 (0)