10template <
typename Container>
11std::vector<int> sortedRows(
const Container& table)
16 const auto selectedRows = table->selectionModel()->selectedRows();
17 if (selectedRows.empty())
20 std::vector<int> rows;
21 rows.reserve(selectedRows.size());
22 std::transform(selectedRows.cbegin(), selectedRows.cend(), std::back_inserter(rows), [](
const QModelIndex& index) { return index.row(); });
23 std::sort(rows.begin(), rows.end(), std::greater<int>());
26 rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
31template <
typename Container,
typename Func>
32bool applyOnSortedTable(
const Container& table, Func&& action)
34 std::vector<int> rows = sortedRows(table);
38 for (
const int row : rows)
40 if (row < 0 || row >= table->rowCount())
43 std::invoke(action, row);
49template <
typename Container,
typename Func>
50auto findIf(Container& container, Func&& predicate) -> std::optional<typename Container::value_type>
52 const auto it = std::find_if(std::begin(container), std::end(container), std::forward<Func>(predicate));
53 if (it == std::cend(container))
59template <
typename Container,
typename Func>
60auto findIf(
const Container& container, Func&& predicate) -> std::optional<typename Container::value_type>
62 const auto it = std::find_if(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
63 if (it == std::cend(container))
69template <
typename Container,
typename Func>
70auto findIfRef(Container& container, Func&& predicate) ->
typename Container::value_type*
72 const auto it = std::find_if(std::begin(container), std::end(container), std::forward<Func>(predicate));
73 return it != std::end(container) ? std::addressof(*it) :
nullptr;
76template <
typename Container,
typename Func>
77auto findIfPtr(
const Container& container, Func&& predicate) ->
typename Container::value_type
79 static_assert(std::is_pointer_v<typename Container::value_type>,
"findIfPtr requires a container of pointers");
80 const auto it = std::find_if(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
81 return it != std::cend(container) ? *it :
nullptr;
84template <
typename Container,
typename Projection,
typename Value>
85auto findBy(
const Container& container, Projection&& projection,
const Value& value) -> std::optional<typename Container::value_type>
87 return findIf(container, [&](
const auto& item) {
return std::invoke(projection, item) == value; });
90template <
typename Container,
typename Value,
typename Projection>
91auto findByRef(Container& container, Projection&& projection,
const Value& value) ->
typename Container::value_type*
93 auto it = std::find_if(std::begin(container), std::end(container), [&](
const auto& item) {
return std::invoke(projection, item) == value; });
95 return it != std::end(container) ? std::addressof(*it) :
nullptr;
98template <
class T,
typename Container,
typename Func>
99T countIf(
const Container& container, Func&& predicate)
101 return std::count_if(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
104template <
class T,
typename Container,
typename Func>
105T accumulate(
const Container& container, Func&& predicate)
107 return std::accumulate(std::cbegin(container), std::cend(container), T{0}, std::forward<Func>(predicate));
110template <
typename Container,
typename Func>
111bool anyOf(
const Container& container, Func&& predicate)
113 return std::any_of(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
116template <
typename Container1,
typename Container2,
typename Func>
117void transform(
const Container1& source, Container2& destination, Func&& predicate)
119 std::transform(cbegin(source), cend(source), std::back_inserter(destination), std::forward<Func>(predicate));
132template <
typename Container,
typename Func>
133auto takeIf(Container& container, Func&& predicate) ->
typename Container::value_type
135 using Value =
typename Container::value_type;
137 auto it = std::find_if(container.begin(), container.end(), std::forward<Func>(predicate));
138 if (it == container.end())
142 auto result = std::move(*it);
150template <
typename Container,
typename Func>
151void removeIf(Container& container, Func&& predicate)
153 container.erase(std::remove_if(container.begin(), container.end(), std::forward<Func>(predicate)));
156inline bool isInt(
const QString& value)
159 UNUSED_RETURN(value.toInt(&ok));
163inline bool isUInt(
const QString& value)
166 UNUSED_RETURN(value.toUInt(&ok));
170inline bool isDouble(
const QString& value)
173 UNUSED_RETURN(value.toDouble(&ok));