KCompletion

kcompletionmatches.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999, 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kcompletionmatches.h"
9
10#include <kcompletion.h>
11#include <kcompletion_p.h> // for KCompletionMatchesWrapper
12
13class KCompletionMatchesPrivate
14{
15public:
16 KCompletionMatchesPrivate(bool sort, KCompletionMatches *parent)
17 : sorting(sort)
18 , q_ptr(parent)
19 {
20 }
21
22 bool sorting;
23 KCompletionMatches *const q_ptr;
24
25 Q_DECLARE_PUBLIC(KCompletionMatches)
26};
27
29 : KSortableList<QString, int>()
30 , d_ptr(new KCompletionMatchesPrivate(o.sorting(), this))
31{
33}
34
36{
38 if (*this == o) {
39 return *this;
40 }
42 d->sorting = o.sorting();
43
44 return *this;
45}
46
48 : d_ptr(new KCompletionMatchesPrivate(sort_P, this))
49{
50}
51
52KCompletionMatches::KCompletionMatches(const KCompletionMatchesWrapper &matches)
53 : d_ptr(new KCompletionMatchesPrivate(matches.sorting(), this))
54{
55 if (matches.m_sortedListPtr) {
56 KCompletionMatchesList::operator=(*matches.m_sortedListPtr);
57 } else {
58 const QStringList list = matches.list();
59 reserve(list.size());
60 std::transform(list.crbegin(), list.crend(), std::back_inserter(*this), [](const QString &str) {
61 return KSortableItem<QString, int>(1, str);
62 });
63 }
64}
65
69
71{
73 if (d->sorting && sort_P) {
74 const_cast<KCompletionMatches *>(this)->sort();
75 }
76 QStringList stringList;
77 stringList.reserve(size());
78 // high weight == sorted last -> reverse the sorting here
79 std::transform(crbegin(), crend(), std::back_inserter(stringList), [](const KSortableItem<QString> &item) {
80 return item.value();
81 });
82 return stringList;
83}
84
86{
88 return d->sorting;
89}
90
92{
93 for (auto it1 = begin(); it1 != end(); ++it1) {
94 auto it2 = it1;
95 ++it2;
96 while (it2 != end()) {
97 if ((*it1).value() == (*it2).value()) {
98 // Use the max weight
99 (*it1).first = std::max((*it1).key(), (*it2).key());
100 it2 = erase(it2);
101 continue;
102 }
103 ++it2;
104 }
105 }
106}
This structure is returned by KCompletion::allWeightedMatches().
KCompletionMatches(bool sort)
Default constructor.
QStringList list(bool sort=true) const
Returns the matches as a QStringList.
void removeDuplicates()
Removes duplicate matches.
bool sorting() const
If sorting() returns false, the matches aren't sorted by their weight, even if true is passed to list...
~KCompletionMatches()
default destructor.
KCompletionMatches & operator=(const KCompletionMatches &)
assignment operator.
KSortableItem is a QPair that provides several operators for sorting.
KSortableList is a QList which associates a key with each item in the list.
void sort()
Sorts the KSortableItems.
QList::iterator begin()
QList::const_reverse_iterator crbegin() const const
QList::const_reverse_iterator crend() const const
QList::iterator end()
QList::iterator erase(QList::iterator pos)
QList< T > & operator=(const QList< T > &other)
void reserve(int alloc)
int size() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:42:46 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.