File indexing completed on 2024-12-01 03:29:24
0001 /* 0002 This file is part of Kiten, a KDE Japanese Reference Tool 0003 SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com> 0004 SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "historyptrlist.h" 0010 0011 #include "entrylist.h" 0012 0013 #include <QList> 0014 0015 class HistoryPtrList::Private 0016 { 0017 public: 0018 Private() 0019 : index(-1) 0020 { 0021 } 0022 0023 static const int max_size = 20; 0024 int index; 0025 QList<EntryList *> list; 0026 }; 0027 0028 HistoryPtrList::HistoryPtrList() 0029 : d(new Private) 0030 { 0031 } 0032 0033 HistoryPtrList::~HistoryPtrList() 0034 { 0035 for (int i = d->list.size() - 1; i >= 0; i--) { 0036 d->list.at(i)->deleteAll(); 0037 delete d->list.at(i); 0038 } 0039 0040 delete d; 0041 } 0042 0043 void HistoryPtrList::addItem(EntryList *newItem) 0044 { 0045 if (!newItem) 0046 return; 0047 // If we're currently looking at something prior to the end of the list 0048 // Remove everything in the list up to this point. 0049 int currentPosition = d->index + 1; 0050 EntryList *temp; 0051 while (currentPosition < count()) { 0052 temp = d->list.takeLast(); 0053 temp->deleteAll(); 0054 delete temp; 0055 } 0056 0057 // Now... check to make sure our history isn't 'fat' 0058 while (count() >= d->max_size) { 0059 temp = d->list.takeFirst(); 0060 temp->deleteAll(); 0061 delete temp; 0062 } 0063 d->index = count() - 1; // Since we have trimmed down to the current position 0064 0065 // One other odd case... if this query is a repeat of the last query 0066 // replace the current one with the new one 0067 if (!d->list.empty()) { 0068 if (current()->getQuery() == newItem->getQuery()) { 0069 temp = d->list.takeLast(); 0070 temp->deleteAll(); 0071 delete temp; 0072 } 0073 } 0074 // Now add the item 0075 d->list.append(newItem); 0076 d->index = count() - 1; 0077 } 0078 0079 int HistoryPtrList::count() 0080 { 0081 return d->list.size(); 0082 } 0083 0084 EntryList *HistoryPtrList::current() 0085 { 0086 if (d->index == -1) { 0087 return nullptr; 0088 } 0089 0090 return d->list.at(d->index); 0091 } 0092 0093 int HistoryPtrList::index() 0094 { 0095 return d->index; 0096 } 0097 0098 void HistoryPtrList::next(int distance) 0099 { 0100 if (distance + d->index > count() - 1) { 0101 d->index = count() - 1; 0102 } else { 0103 d->index += distance; 0104 } 0105 } 0106 0107 void HistoryPtrList::prev(int distance) 0108 { 0109 if (d->index - distance < 0) { 0110 d->index = 0; 0111 } else { 0112 d->index -= distance; 0113 } 0114 } 0115 0116 void HistoryPtrList::setCurrent(int i) 0117 { 0118 if (i < count() && i >= 0) { 0119 d->index = i; 0120 } 0121 } 0122 0123 // Get a StringList of the History Items 0124 QStringList HistoryPtrList::toStringList() 0125 { 0126 QStringList result; 0127 0128 for (const EntryList *p : d->list) { 0129 result.append(p->getQuery().toString()); 0130 } 0131 0132 return result; 0133 } 0134 0135 QStringList HistoryPtrList::toStringListNext() 0136 { 0137 HistoryPtrList localCopy(*this); 0138 0139 int currentPosition = d->index + 1; 0140 while (currentPosition--) { 0141 localCopy.d->list.removeFirst(); 0142 } 0143 0144 return localCopy.toStringList(); 0145 } 0146 0147 QStringList HistoryPtrList::toStringListPrev() 0148 { 0149 QStringList result; 0150 0151 for (int i = 0; i < d->index; i++) { 0152 result.append(d->list.at(i)->getQuery().toString()); 0153 } 0154 0155 return result; 0156 }