File: historyptrlist.cpp

package info (click to toggle)
kiten 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,156 kB
  • sloc: cpp: 6,079; xml: 239; makefile: 8; sh: 2
file content (156 lines) | stat: -rw-r--r-- 3,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
    This file is part of Kiten, a KDE Japanese Reference Tool
    SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
    SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "historyptrlist.h"

#include "entrylist.h"

#include <QList>

class HistoryPtrList::Private
{
public:
    Private()
        : index(-1)
    {
    }

    static const int max_size = 20;
    int index;
    QList<EntryList *> list;
};

HistoryPtrList::HistoryPtrList()
    : d(new Private)
{
}

HistoryPtrList::~HistoryPtrList()
{
    for (int i = d->list.size() - 1; i >= 0; i--) {
        d->list.at(i)->deleteAll();
        delete d->list.at(i);
    }

    delete d;
}

void HistoryPtrList::addItem(EntryList *newItem)
{
    if (!newItem)
        return;
    // If we're currently looking at something prior to the end of the list
    // Remove everything in the list up to this point.
    int currentPosition = d->index + 1;
    EntryList *temp;
    while (currentPosition < count()) {
        temp = d->list.takeLast();
        temp->deleteAll();
        delete temp;
    }

    // Now... check to make sure our history isn't 'fat'
    while (count() >= d->max_size) {
        temp = d->list.takeFirst();
        temp->deleteAll();
        delete temp;
    }
    d->index = count() - 1; // Since we have trimmed down to the current position

    // One other odd case... if this query is a repeat of the last query
    // replace the current one with the new one
    if (!d->list.empty()) {
        if (current()->getQuery() == newItem->getQuery()) {
            temp = d->list.takeLast();
            temp->deleteAll();
            delete temp;
        }
    }
    // Now add the item
    d->list.append(newItem);
    d->index = count() - 1;
}

int HistoryPtrList::count()
{
    return d->list.size();
}

EntryList *HistoryPtrList::current()
{
    if (d->index == -1) {
        return nullptr;
    }

    return d->list.at(d->index);
}

int HistoryPtrList::index()
{
    return d->index;
}

void HistoryPtrList::next(int distance)
{
    if (distance + d->index > count() - 1) {
        d->index = count() - 1;
    } else {
        d->index += distance;
    }
}

void HistoryPtrList::prev(int distance)
{
    if (d->index - distance < 0) {
        d->index = 0;
    } else {
        d->index -= distance;
    }
}

void HistoryPtrList::setCurrent(int i)
{
    if (i < count() && i >= 0) {
        d->index = i;
    }
}

// Get a StringList of the History Items
QStringList HistoryPtrList::toStringList()
{
    QStringList result;

    for (const EntryList *p : d->list) {
        result.append(p->getQuery().toString());
    }

    return result;
}

QStringList HistoryPtrList::toStringListNext()
{
    HistoryPtrList localCopy(*this);

    int currentPosition = d->index + 1;
    while (currentPosition--) {
        localCopy.d->list.removeFirst();
    }

    return localCopy.toStringList();
}

QStringList HistoryPtrList::toStringListPrev()
{
    QStringList result;

    for (int i = 0; i < d->index; i++) {
        result.append(d->list.at(i)->getQuery().toString());
    }

    return result;
}