Skip to content

Commit be6506e

Browse files
committed
New setting for sorting numeric columns descending by default
Simultaneously removed setting for showing the project settings window after creating a new database (now defaults to yes) Closes #222
1 parent 657a921 commit be6506e

13 files changed

+196
-70
lines changed

PAL.pro

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ HEADERS += \
130130
src/main/filter_bar.h \
131131
src/main/filter_wizard.h \
132132
src/main/helpers.h \
133+
src/main/inverted_sort_header_view.h \
133134
src/main/item_types_handler.h \
134135
src/main/main_window.h \
135136
src/main/main_window_tab_content.h \
@@ -225,6 +226,7 @@ SOURCES += \
225226
src/main/filter_bar.cpp \
226227
src/main/filter_wizard.cpp \
227228
src/main/helpers.cpp \
229+
src/main/inverted_sort_header_view.cpp \
228230
src/main/main_window.cpp \
229231
src/main/main_window_tab_content.cpp \
230232
src/settings/project_settings.cpp \

src/comp_tables/composite_column.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @param table The CompositeTable that this column belongs to.
3838
* @param name The internal name for this column.
3939
* @param uiName The name of this column as it should be displayed in the UI.
40-
* @param contentType The type of data the column contents.
40+
* @param contentType The type of data the column contains.
4141
* @param cellsAreInterdependent Whether the contents of the cells in this column depend on each other.
4242
* @param isStatistical Whether the column is a statistical column.
4343
* @param suffix A suffix to append to the content of each cell.

src/comp_tables/composite_table.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,7 @@ QVariant CompositeTable::data(const QModelIndex& index, int role) const
973973
* For the QAbstractItemModel implementation, sorts the table by the given column and order.
974974
*
975975
* This function is called by the view when the user clicks on a column header to sort by that
976-
* column. It looks up the column which was clicked and delegates the sorting to
977-
* performSortByColumn().
976+
* column. It looks up the column which was clicked and delegates the sorting to performSort().
978977
*
979978
* @param columnIndex The index of the visible column to sort by.
980979
* @param order The order to sort by (ascending or descending).
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2023-2024 Simon Vetter
3+
*
4+
* This file is part of PeakAscentLogger.
5+
*
6+
* PeakAscentLogger is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* PeakAscentLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License along with PeakAscentLogger.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* @file inverted_sort_header_view.cpp
20+
*
21+
* This file defines the InvertedSortHeaderView class.
22+
*/
23+
24+
#include "inverted_sort_header_view.h"
25+
26+
#include "src/settings/settings.h"
27+
28+
#include <QMouseEvent>
29+
30+
31+
32+
InvertedSortHeaderView::InvertedSortHeaderView(QWidget* parent, const CompositeTable& table) :
33+
QHeaderView(Qt::Orientation::Horizontal, parent),
34+
table(table)
35+
{}
36+
37+
InvertedSortHeaderView::~InvertedSortHeaderView()
38+
{}
39+
40+
41+
42+
void InvertedSortHeaderView::mousePressEvent(QMouseEvent* event)
43+
{
44+
const int logicalIndex = logicalIndexAt(event->pos());
45+
46+
const CompositeColumn& column = table.getColumnAt(logicalIndex);
47+
const bool isNumericColumn = column.contentType == Integer || column.contentType == Date || column.contentType == Time;
48+
49+
const bool applyDefaultOrder = logicalIndex != sortIndicatorSection();
50+
const bool invertDefaultOrder = isNumericColumn && Settings::sortNumericColumnsDescendingByDefault.get();
51+
const bool applyInvertedDefaultOrder = applyDefaultOrder && invertDefaultOrder;
52+
53+
if (applyInvertedDefaultOrder) {
54+
// Default to decending
55+
const bool oldBlockState = this->blockSignals(true);
56+
setSortIndicator(logicalIndex, Qt::AscendingOrder);
57+
this->blockSignals(oldBlockState);
58+
}
59+
60+
QHeaderView::mousePressEvent(event);
61+
}

src/main/inverted_sort_header_view.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2023-2024 Simon Vetter
3+
*
4+
* This file is part of PeakAscentLogger.
5+
*
6+
* PeakAscentLogger is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* PeakAscentLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License along with PeakAscentLogger.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* @file inverted_sort_header_view.h
20+
*
21+
* This file declares the InvertedSortHeaderView class.
22+
*/
23+
24+
#ifndef INVERTED_SORT_HEADER_VIEW_H
25+
#define INVERTED_SORT_HEADER_VIEW_H
26+
27+
#include "src/comp_tables/composite_table.h"
28+
29+
#include <QHeaderView>
30+
31+
32+
33+
class InvertedSortHeaderView : public QHeaderView
34+
{
35+
Q_OBJECT
36+
37+
const CompositeTable& table;
38+
39+
public:
40+
InvertedSortHeaderView(QWidget* parent, const CompositeTable& table);
41+
virtual ~InvertedSortHeaderView();
42+
43+
protected:
44+
void mousePressEvent(QMouseEvent* event) override;
45+
};
46+
47+
48+
49+
#endif // INVERTED_SORT_HEADER_VIEW_H

src/main/main_window.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,9 @@ void MainWindow::handle_newDatabase()
855855
activeMapper.openingTab();
856856
activeMapper.statsEngine.setCurrentlyVisible(true);
857857

858-
if (Settings::openProjectSettingsOnNewDatabase.get()) {
859-
ProjectSettingsWindow* dialog = new ProjectSettingsWindow(*this, *this, db, true);
860-
connect(dialog, &ProjectSettingsWindow::finished, [=]() { delete dialog; });
861-
dialog->open();
862-
}
858+
ProjectSettingsWindow* dialog = new ProjectSettingsWindow(*this, *this, db, true);
859+
connect(dialog, &ProjectSettingsWindow::finished, [=]() { delete dialog; });
860+
dialog->open();
863861
}
864862

865863
/**

src/main/main_window_tab_content.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ MainWindowTabContent::MainWindowTabContent(QWidget* parent) :
3636
compTable(nullptr),
3737
isViewable(false),
3838
isDuplicable(false),
39+
headerView(nullptr),
3940
columnContextMenu(QMenu(this)),
4041
columnContextMenuHideColumnAction(nullptr),
4142
columnContextMenuRestoreColumnMenu(nullptr),
@@ -50,6 +51,7 @@ MainWindowTabContent::MainWindowTabContent(QWidget* parent) :
5051

5152
MainWindowTabContent::~MainWindowTabContent()
5253
{
54+
delete headerView;
5355
qDeleteAll(shortcuts);
5456
delete columnContextMenuRestoreColumnMenu;
5557
}
@@ -69,6 +71,11 @@ void MainWindowTabContent::init(MainWindow* mainWindow, const ItemTypesHandler*
6971
this->isDuplicable = duplicable;
7072

7173

74+
// Set horizontal header
75+
this->headerView = new InvertedSortHeaderView(tableView, *compTable);
76+
headerView->setSectionsClickable(true);
77+
tableView->setHorizontalHeader(headerView);
78+
7279
// Set model
7380
tableView->setModel(compTable);
7481
compTable->setUpdateImmediately(mapper->type == mainWindow->getCurrentTabIndex());

src/main/main_window_tab_content.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define MAIN_WINDOW_TAB_CONTENT_H
2626

2727
#include "src/data/item_types.h"
28+
#include "src/main/inverted_sort_header_view.h"
2829
#include "ui_main_window_tab.h"
2930

3031
#include <QWidget>
@@ -46,7 +47,10 @@ class MainWindowTabContent : public QWidget, private Ui_MainWindowTabContent
4647
bool isDuplicable;
4748

4849
private:
49-
/** The context menu for the column header area of all UI tables. */
50+
/** The custom horizontal header view for the UI table. */
51+
InvertedSortHeaderView* headerView;
52+
53+
/** The context menu for the column header area of the UI table. */
5054
QMenu columnContextMenu;
5155
/** The column context menu entry for hiding the selected column. */
5256
QAction* columnContextMenuHideColumnAction;
@@ -57,7 +61,7 @@ class MainWindowTabContent : public QWidget, private Ui_MainWindowTabContent
5761
/** The column context menu entry for creating a new custom column. */
5862
QAction* columnContextMenuAddCustomColumnAction;
5963

60-
/** The context menu for the cell are of all UI tables. */
64+
/** The context menu for the cells area of the UI table. */
6165
QMenu tableContextMenu;
6266
/** The context menu entry for opening the selected item. */
6367
QAction* tableContextMenuOpenAction;

src/settings/settings.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ void Settings::checkForVersionChange()
8888
qSettings.remove("allowEmptyNames");
8989
}
9090

91+
// 1.7.0 or older
92+
if (settingsVersionUpTo("1.7.0")) {
93+
// Setting for opening project settings after creating new database was removed => clean up
94+
qSettings.remove("openProjectSettingsOnNewDatabase");
95+
}
96+
9197
// Update settings version
9298
const QString currentAppVersion = getAppVersion();
9399
const QString currentSettingsVersion = appVersion.get();

src/settings/settings.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,12 @@ class Settings {
314314
/** Warn user if an item with the same name already exists. */
315315
inline static const Setting<bool> warnAboutDuplicateNames = Setting<bool> ("warnAboutDuplicateNames", true);
316316

317+
/** For any integer, date or time column in any table, default to sorting them in descending order first. */
318+
inline static const Setting<bool> sortNumericColumnsDescendingByDefault = Setting<bool> ("sortNumericColumnsDescendingByDefault", false);
319+
317320
/** Only prepare the composite table corresponding to the open tab on startup, and defer preparing the other tables until they are opened. */
318321
inline static const Setting<bool> onlyPrepareActiveTableOnStartup = Setting<bool> ("onlyPrepareActiveTableOnStartup", true);
319322

320-
/** Open the project settings dialog when creating a new database. */
321-
inline static const Setting<bool> openProjectSettingsOnNewDatabase = Setting<bool> ("openProjectSettingsOnNewDatabase", true);
322-
323323
// Remember UI
324324
/** Remember the window positions of the main window and all dialogs. */
325325
inline static const Setting<bool> rememberWindowPositions = Setting<bool> ("rememberWindowPositions", true);

src/settings/settings_window.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ void SettingsWindow::loadSettings()
9999
confirmDeleteCheckbox ->setChecked (confirmDelete .get());
100100
confirmCancelCheckbox ->setChecked (confirmCancel .get());
101101
warnAboutDuplicateNamesCheckbox ->setChecked (warnAboutDuplicateNames .get());
102+
defaultNumericColumnsToDescendingCheckbox ->setChecked (sortNumericColumnsDescendingByDefault .get());
102103
onlyPrepareActiveTableCheckbox ->setChecked (onlyPrepareActiveTableOnStartup .get());
103-
openProjectSettingsOnNewDatabaseCheckbox ->setChecked (openProjectSettingsOnNewDatabase .get());
104104
rememberWindowGeometryCheckbox ->setChecked (rememberWindowPositions .get());
105105
rememberWindowPositionsRelativeCheckbox ->setChecked (rememberWindowPositionsRelative .get());
106106
rememberTableCheckbox ->setChecked (rememberTab .get());
@@ -142,8 +142,8 @@ void SettingsWindow::loadDefaults()
142142
confirmDeleteCheckbox ->setChecked (confirmDelete .getDefault());
143143
confirmCancelCheckbox ->setChecked (confirmCancel .getDefault());
144144
warnAboutDuplicateNamesCheckbox ->setChecked (warnAboutDuplicateNames .getDefault());
145+
defaultNumericColumnsToDescendingCheckbox ->setChecked (sortNumericColumnsDescendingByDefault .getDefault());
145146
onlyPrepareActiveTableCheckbox ->setChecked (onlyPrepareActiveTableOnStartup .getDefault());
146-
openProjectSettingsOnNewDatabaseCheckbox ->setChecked (openProjectSettingsOnNewDatabase .getDefault());
147147
rememberWindowGeometryCheckbox ->setChecked (rememberWindowPositions .getDefault());
148148
rememberWindowPositionsRelativeCheckbox ->setChecked (rememberWindowPositionsRelative .getDefault());
149149
rememberTableCheckbox ->setChecked (rememberTab .getDefault());
@@ -193,8 +193,8 @@ void SettingsWindow::saveSettings()
193193
confirmDelete .set(confirmDeleteCheckbox ->isChecked());
194194
confirmCancel .set(confirmCancelCheckbox ->isChecked());
195195
warnAboutDuplicateNames .set(warnAboutDuplicateNamesCheckbox ->isChecked());
196+
sortNumericColumnsDescendingByDefault .set(defaultNumericColumnsToDescendingCheckbox ->isChecked());
196197
onlyPrepareActiveTableOnStartup .set(onlyPrepareActiveTableCheckbox ->isChecked());
197-
openProjectSettingsOnNewDatabase .set(openProjectSettingsOnNewDatabaseCheckbox ->isChecked());
198198
rememberWindowPositions .set(rememberWindowGeometryCheckbox ->isChecked());
199199
rememberWindowPositionsRelative .set(rememberWindowPositionsRelativeCheckbox ->isChecked());
200200
rememberTab .set(rememberTableCheckbox ->isChecked());

0 commit comments

Comments
 (0)