Skip to content

Commit a607388

Browse files
committed
🚧 km-window: add sorting for the columns
fixes #47
1 parent f58c077 commit a607388

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

‎src/km-window.cpp‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
#include <algorithm> // for any_of, find_if
2727
#include <filesystem> // for exists
2828
#include <future>
29-
#include <ranges> // for ranges::*
30-
#include <span> // for span
31-
#include <thread> // for this_thread
29+
#include <ranges> // for ranges::*
30+
#include <span> // for span
31+
#include <string_view> // for string_view
32+
#include <thread> // for this_thread
3233

3334
#include <fmt/core.h>
3435

@@ -87,7 +88,7 @@ bool is_kernels_change_state(alpm_handle_t* handle, std::span<std::string_view>
8788

8889
void init_kernels_tree_widget(QTreeWidget* tree_kernels, std::span<Kernel> kernels) noexcept {
8990
for (auto& kernel : kernels) {
90-
auto* widget_item = new QTreeWidgetItem(tree_kernels);
91+
auto* widget_item = new KernelTreeWidgetItem(tree_kernels);
9192
widget_item->setCheckState(TreeCol::Check, Qt::Unchecked);
9293
widget_item->setText(TreeCol::PkgName, kernel.get_raw());
9394
widget_item->setText(TreeCol::Version, QString::fromStdString(kernel.version()));
@@ -386,4 +387,27 @@ void MainWindow::on_schedext_config() noexcept {
386387
m_sched_window->show();
387388
}
388389

390+
bool KernelTreeWidgetItem::operator<(const QTreeWidgetItem& other) const {
391+
const auto sort_col = treeWidget()->sortColumn();
392+
if (sort_col != TreeCol::Version) {
393+
return QTreeWidgetItem::operator<(other);
394+
}
395+
396+
auto get_comparable_version = [](QString&& version_string) {
397+
using namespace std::string_view_literals;
398+
auto std_str = std::move(version_string).toStdString();
399+
for (auto&& prefix : {"∨"sv, "∧"sv}) {
400+
if (std_str.starts_with(prefix)) {
401+
return std_str.substr(prefix.size());
402+
}
403+
}
404+
return std_str;
405+
};
406+
407+
const auto& version_a = get_comparable_version(text(sort_col));
408+
const auto& version_b = get_comparable_version(other.text(sort_col));
409+
410+
return alpm_pkg_vercmp(version_a.c_str(), version_b.c_str()) < 0;
411+
}
412+
389413
// NOLINTEND(bugprone-unhandled-exception-at-new)

‎src/km-window.hpp‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ enum { Check,
9393
Immutable };
9494
}
9595

96+
class KernelTreeWidgetItem : public QTreeWidgetItem {
97+
public:
98+
using QTreeWidgetItem::QTreeWidgetItem;
99+
100+
bool operator<(const QTreeWidgetItem& other) const override;
101+
};
102+
96103
class MainWindow final : public QMainWindow {
97104
Q_OBJECT
98105
Q_DISABLE_COPY_MOVE(MainWindow)
@@ -130,10 +137,10 @@ class MainWindow final : public QMainWindow {
130137
Work* m_worker{nullptr};
131138

132139
alpm_errno_t m_err{};
133-
alpm_handle_t* m_handle = utils::parse_alpm("/", "/var/lib/pacman/", &m_err);
134-
std::vector<Kernel> m_kernels = Kernel::get_kernels(m_handle);
135-
std::unique_ptr<Ui::MainWindow> m_ui = std::make_unique<Ui::MainWindow>();
136-
std::unique_ptr<ConfWindow> m_conf_window = std::make_unique<ConfWindow>();
140+
alpm_handle_t* m_handle = utils::parse_alpm("/", "/var/lib/pacman/", &m_err);
141+
std::vector<Kernel> m_kernels = Kernel::get_kernels(m_handle);
142+
std::unique_ptr<Ui::MainWindow> m_ui = std::make_unique<Ui::MainWindow>();
143+
std::unique_ptr<ConfWindow> m_conf_window = std::make_unique<ConfWindow>();
137144
std::unique_ptr<scxctl::SchedExtWindow> m_sched_window = std::make_unique<scxctl::SchedExtWindow>();
138145

139146
void build_change_list(QTreeWidgetItem* item) noexcept;

‎src/km-window.ui‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<class>MainWindow</class>
44
<widget class="QMainWindow" name="MainWindow">
55
<property name="windowModality">
6-
<enum>Qt::NonModal</enum>
6+
<enum>Qt::WindowModality::NonModal</enum>
77
</property>
88
<property name="geometry">
99
<rect>
@@ -17,8 +17,7 @@
1717
<string>CachyOS Kernel Manager</string>
1818
</property>
1919
<property name="windowIcon">
20-
<iconset theme="cachyos-kernel-manager">
21-
<normaloff>.</normaloff>.</iconset>
20+
<iconset theme="cachyos-kernel-manager"/>
2221
</property>
2322
<widget class="QWidget" name="centralwidget">
2423
<layout class="QVBoxLayout" name="verticalLayout">
@@ -38,7 +37,7 @@
3837
<item>
3938
<spacer name="horizontalSpacer_3">
4039
<property name="orientation">
41-
<enum>Qt::Horizontal</enum>
40+
<enum>Qt::Orientation::Horizontal</enum>
4241
</property>
4342
<property name="sizeHint" stdset="0">
4443
<size>
@@ -51,13 +50,16 @@
5150
<item>
5251
<widget class="QTreeWidget" name="treeKernels">
5352
<property name="frameShadow">
54-
<enum>QFrame::Raised</enum>
53+
<enum>QFrame::Shadow::Raised</enum>
5554
</property>
5655
<property name="tabKeyNavigation">
5756
<bool>true</bool>
5857
</property>
5958
<property name="selectionMode">
60-
<enum>QAbstractItemView::SingleSelection</enum>
59+
<enum>QAbstractItemView::SelectionMode::SingleSelection</enum>
60+
</property>
61+
<property name="sortingEnabled">
62+
<bool>true</bool>
6163
</property>
6264
<column>
6365
<property name="text">
@@ -87,7 +89,7 @@
8789
<item>
8890
<spacer name="horizontalSpacer_2">
8991
<property name="orientation">
90-
<enum>Qt::Horizontal</enum>
92+
<enum>Qt::Orientation::Horizontal</enum>
9193
</property>
9294
<property name="sizeHint" stdset="0">
9395
<size>
@@ -100,7 +102,7 @@
100102
<item>
101103
<spacer name="horizontalSpacer">
102104
<property name="orientation">
103-
<enum>Qt::Horizontal</enum>
105+
<enum>Qt::Orientation::Horizontal</enum>
104106
</property>
105107
<property name="sizeHint" stdset="0">
106108
<size>

0 commit comments

Comments
 (0)