Skip to content

Commit

Permalink
New setting for sorting numeric columns descending by default
Browse files Browse the repository at this point in the history
Simultaneously removed setting for showing the project settings window after creating a new database (now defaults to yes)

Closes #222
  • Loading branch information
svetter committed Sep 22, 2024
1 parent 657a921 commit be6506e
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 70 deletions.
2 changes: 2 additions & 0 deletions PAL.pro
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ HEADERS += \
src/main/filter_bar.h \
src/main/filter_wizard.h \
src/main/helpers.h \
src/main/inverted_sort_header_view.h \
src/main/item_types_handler.h \
src/main/main_window.h \
src/main/main_window_tab_content.h \
Expand Down Expand Up @@ -225,6 +226,7 @@ SOURCES += \
src/main/filter_bar.cpp \
src/main/filter_wizard.cpp \
src/main/helpers.cpp \
src/main/inverted_sort_header_view.cpp \
src/main/main_window.cpp \
src/main/main_window_tab_content.cpp \
src/settings/project_settings.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/comp_tables/composite_column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @param table The CompositeTable that this column belongs to.
* @param name The internal name for this column.
* @param uiName The name of this column as it should be displayed in the UI.
* @param contentType The type of data the column contents.
* @param contentType The type of data the column contains.
* @param cellsAreInterdependent Whether the contents of the cells in this column depend on each other.
* @param isStatistical Whether the column is a statistical column.
* @param suffix A suffix to append to the content of each cell.
Expand Down
3 changes: 1 addition & 2 deletions src/comp_tables/composite_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ QVariant CompositeTable::data(const QModelIndex& index, int role) const
* For the QAbstractItemModel implementation, sorts the table by the given column and order.
*
* This function is called by the view when the user clicks on a column header to sort by that
* column. It looks up the column which was clicked and delegates the sorting to
* performSortByColumn().
* column. It looks up the column which was clicked and delegates the sorting to performSort().
*
* @param columnIndex The index of the visible column to sort by.
* @param order The order to sort by (ascending or descending).
Expand Down
61 changes: 61 additions & 0 deletions src/main/inverted_sort_header_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023-2024 Simon Vetter
*
* This file is part of PeakAscentLogger.
*
* PeakAscentLogger is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* PeakAscentLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PeakAscentLogger.
* If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @file inverted_sort_header_view.cpp
*
* This file defines the InvertedSortHeaderView class.
*/

#include "inverted_sort_header_view.h"

#include "src/settings/settings.h"

#include <QMouseEvent>



InvertedSortHeaderView::InvertedSortHeaderView(QWidget* parent, const CompositeTable& table) :
QHeaderView(Qt::Orientation::Horizontal, parent),
table(table)
{}

InvertedSortHeaderView::~InvertedSortHeaderView()
{}



void InvertedSortHeaderView::mousePressEvent(QMouseEvent* event)
{
const int logicalIndex = logicalIndexAt(event->pos());

const CompositeColumn& column = table.getColumnAt(logicalIndex);
const bool isNumericColumn = column.contentType == Integer || column.contentType == Date || column.contentType == Time;

const bool applyDefaultOrder = logicalIndex != sortIndicatorSection();
const bool invertDefaultOrder = isNumericColumn && Settings::sortNumericColumnsDescendingByDefault.get();
const bool applyInvertedDefaultOrder = applyDefaultOrder && invertDefaultOrder;

if (applyInvertedDefaultOrder) {
// Default to decending
const bool oldBlockState = this->blockSignals(true);
setSortIndicator(logicalIndex, Qt::AscendingOrder);
this->blockSignals(oldBlockState);
}

QHeaderView::mousePressEvent(event);
}
49 changes: 49 additions & 0 deletions src/main/inverted_sort_header_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023-2024 Simon Vetter
*
* This file is part of PeakAscentLogger.
*
* PeakAscentLogger is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* PeakAscentLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PeakAscentLogger.
* If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @file inverted_sort_header_view.h
*
* This file declares the InvertedSortHeaderView class.
*/

#ifndef INVERTED_SORT_HEADER_VIEW_H
#define INVERTED_SORT_HEADER_VIEW_H

#include "src/comp_tables/composite_table.h"

#include <QHeaderView>



class InvertedSortHeaderView : public QHeaderView
{
Q_OBJECT

const CompositeTable& table;

public:
InvertedSortHeaderView(QWidget* parent, const CompositeTable& table);
virtual ~InvertedSortHeaderView();

protected:
void mousePressEvent(QMouseEvent* event) override;
};



#endif // INVERTED_SORT_HEADER_VIEW_H
8 changes: 3 additions & 5 deletions src/main/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,9 @@ void MainWindow::handle_newDatabase()
activeMapper.openingTab();
activeMapper.statsEngine.setCurrentlyVisible(true);

if (Settings::openProjectSettingsOnNewDatabase.get()) {
ProjectSettingsWindow* dialog = new ProjectSettingsWindow(*this, *this, db, true);
connect(dialog, &ProjectSettingsWindow::finished, [=]() { delete dialog; });
dialog->open();
}
ProjectSettingsWindow* dialog = new ProjectSettingsWindow(*this, *this, db, true);
connect(dialog, &ProjectSettingsWindow::finished, [=]() { delete dialog; });
dialog->open();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/main_window_tab_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ MainWindowTabContent::MainWindowTabContent(QWidget* parent) :
compTable(nullptr),
isViewable(false),
isDuplicable(false),
headerView(nullptr),
columnContextMenu(QMenu(this)),
columnContextMenuHideColumnAction(nullptr),
columnContextMenuRestoreColumnMenu(nullptr),
Expand All @@ -50,6 +51,7 @@ MainWindowTabContent::MainWindowTabContent(QWidget* parent) :

MainWindowTabContent::~MainWindowTabContent()
{
delete headerView;
qDeleteAll(shortcuts);
delete columnContextMenuRestoreColumnMenu;
}
Expand All @@ -69,6 +71,11 @@ void MainWindowTabContent::init(MainWindow* mainWindow, const ItemTypesHandler*
this->isDuplicable = duplicable;


// Set horizontal header
this->headerView = new InvertedSortHeaderView(tableView, *compTable);
headerView->setSectionsClickable(true);
tableView->setHorizontalHeader(headerView);

// Set model
tableView->setModel(compTable);
compTable->setUpdateImmediately(mapper->type == mainWindow->getCurrentTabIndex());
Expand Down
8 changes: 6 additions & 2 deletions src/main/main_window_tab_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define MAIN_WINDOW_TAB_CONTENT_H

#include "src/data/item_types.h"
#include "src/main/inverted_sort_header_view.h"
#include "ui_main_window_tab.h"

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

private:
/** The context menu for the column header area of all UI tables. */
/** The custom horizontal header view for the UI table. */
InvertedSortHeaderView* headerView;

/** The context menu for the column header area of the UI table. */
QMenu columnContextMenu;
/** The column context menu entry for hiding the selected column. */
QAction* columnContextMenuHideColumnAction;
Expand All @@ -57,7 +61,7 @@ class MainWindowTabContent : public QWidget, private Ui_MainWindowTabContent
/** The column context menu entry for creating a new custom column. */
QAction* columnContextMenuAddCustomColumnAction;

/** The context menu for the cell are of all UI tables. */
/** The context menu for the cells area of the UI table. */
QMenu tableContextMenu;
/** The context menu entry for opening the selected item. */
QAction* tableContextMenuOpenAction;
Expand Down
6 changes: 6 additions & 0 deletions src/settings/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ void Settings::checkForVersionChange()
qSettings.remove("allowEmptyNames");
}

// 1.7.0 or older
if (settingsVersionUpTo("1.7.0")) {
// Setting for opening project settings after creating new database was removed => clean up
qSettings.remove("openProjectSettingsOnNewDatabase");
}

// Update settings version
const QString currentAppVersion = getAppVersion();
const QString currentSettingsVersion = appVersion.get();
Expand Down
6 changes: 3 additions & 3 deletions src/settings/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ class Settings {
/** Warn user if an item with the same name already exists. */
inline static const Setting<bool> warnAboutDuplicateNames = Setting<bool> ("warnAboutDuplicateNames", true);

/** For any integer, date or time column in any table, default to sorting them in descending order first. */
inline static const Setting<bool> sortNumericColumnsDescendingByDefault = Setting<bool> ("sortNumericColumnsDescendingByDefault", false);

/** Only prepare the composite table corresponding to the open tab on startup, and defer preparing the other tables until they are opened. */
inline static const Setting<bool> onlyPrepareActiveTableOnStartup = Setting<bool> ("onlyPrepareActiveTableOnStartup", true);

/** Open the project settings dialog when creating a new database. */
inline static const Setting<bool> openProjectSettingsOnNewDatabase = Setting<bool> ("openProjectSettingsOnNewDatabase", true);

// Remember UI
/** Remember the window positions of the main window and all dialogs. */
inline static const Setting<bool> rememberWindowPositions = Setting<bool> ("rememberWindowPositions", true);
Expand Down
6 changes: 3 additions & 3 deletions src/settings/settings_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void SettingsWindow::loadSettings()
confirmDeleteCheckbox ->setChecked (confirmDelete .get());
confirmCancelCheckbox ->setChecked (confirmCancel .get());
warnAboutDuplicateNamesCheckbox ->setChecked (warnAboutDuplicateNames .get());
defaultNumericColumnsToDescendingCheckbox ->setChecked (sortNumericColumnsDescendingByDefault .get());
onlyPrepareActiveTableCheckbox ->setChecked (onlyPrepareActiveTableOnStartup .get());
openProjectSettingsOnNewDatabaseCheckbox ->setChecked (openProjectSettingsOnNewDatabase .get());
rememberWindowGeometryCheckbox ->setChecked (rememberWindowPositions .get());
rememberWindowPositionsRelativeCheckbox ->setChecked (rememberWindowPositionsRelative .get());
rememberTableCheckbox ->setChecked (rememberTab .get());
Expand Down Expand Up @@ -142,8 +142,8 @@ void SettingsWindow::loadDefaults()
confirmDeleteCheckbox ->setChecked (confirmDelete .getDefault());
confirmCancelCheckbox ->setChecked (confirmCancel .getDefault());
warnAboutDuplicateNamesCheckbox ->setChecked (warnAboutDuplicateNames .getDefault());
defaultNumericColumnsToDescendingCheckbox ->setChecked (sortNumericColumnsDescendingByDefault .getDefault());
onlyPrepareActiveTableCheckbox ->setChecked (onlyPrepareActiveTableOnStartup .getDefault());
openProjectSettingsOnNewDatabaseCheckbox ->setChecked (openProjectSettingsOnNewDatabase .getDefault());
rememberWindowGeometryCheckbox ->setChecked (rememberWindowPositions .getDefault());
rememberWindowPositionsRelativeCheckbox ->setChecked (rememberWindowPositionsRelative .getDefault());
rememberTableCheckbox ->setChecked (rememberTab .getDefault());
Expand Down Expand Up @@ -193,8 +193,8 @@ void SettingsWindow::saveSettings()
confirmDelete .set(confirmDeleteCheckbox ->isChecked());
confirmCancel .set(confirmCancelCheckbox ->isChecked());
warnAboutDuplicateNames .set(warnAboutDuplicateNamesCheckbox ->isChecked());
sortNumericColumnsDescendingByDefault .set(defaultNumericColumnsToDescendingCheckbox ->isChecked());
onlyPrepareActiveTableOnStartup .set(onlyPrepareActiveTableCheckbox ->isChecked());
openProjectSettingsOnNewDatabase .set(openProjectSettingsOnNewDatabaseCheckbox ->isChecked());
rememberWindowPositions .set(rememberWindowGeometryCheckbox ->isChecked());
rememberWindowPositionsRelative .set(rememberWindowPositionsRelativeCheckbox ->isChecked());
rememberTab .set(rememberTableCheckbox ->isChecked());
Expand Down
Loading

0 comments on commit be6506e

Please sign in to comment.