kiten/lib
HistoryPtrList.cpp
Go to the documentation of this file.00001 /* This file is part of Kiten, a KDE Japanese Reference Tool... 00002 Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> 00003 (C) 2006 Eric Kjeldergaard <kjelderg@gmail.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 #include "HistoryPtrList.h" 00021 #include "EntryList.h" 00022 00023 #include <QtCore/QList> 00024 #include <QtCore/QMutableListIterator> 00025 00026 class HistoryPtrList::Private { 00027 public: 00028 Private() : m_index(-1) {} 00029 static const int s_max_size = 20; 00030 int m_index; 00031 QList<EntryList*> m_list; 00032 }; 00033 00034 HistoryPtrList::HistoryPtrList():d(new Private) { 00035 } 00036 00037 HistoryPtrList::~HistoryPtrList() { 00038 for(int i=d->m_list.size()-1; i>=0; i--) { 00039 d->m_list.at(i)->deleteAll(); 00040 delete d->m_list.at(i); 00041 } 00042 delete d; 00043 } 00044 00045 void 00046 HistoryPtrList::addItem(EntryList *newItem) { 00047 if(!newItem) return; 00048 //If we're currently looking at something prior to the end of the list 00049 //Remove everything in the list up to this point. 00050 int currentPosition = d->m_index+1; 00051 EntryList *temp; 00052 while(currentPosition < count()) { 00053 temp = d->m_list.takeLast(); 00054 temp->deleteAll(); 00055 delete temp; 00056 } 00057 00058 //Now... check to make sure our history isn't 'fat' 00059 while(count() >= d->s_max_size) { 00060 temp = d->m_list.takeFirst(); 00061 temp->deleteAll(); 00062 delete temp; 00063 } 00064 d->m_index = count()-1; //Since we have trimmed down to the current position 00065 00066 //One other odd case... if this query is a repeat of the last query 00067 //replace the current one with the new one 00068 if(d->m_list.size() > 0) { 00069 if(current()->getQuery() == newItem->getQuery()) { 00070 temp = d->m_list.takeLast(); 00071 temp->deleteAll(); 00072 delete temp; 00073 } 00074 } 00075 //Now add the item 00076 d->m_list.append(newItem); 00077 d->m_index = count()-1; 00078 } 00079 00080 //Get a StringList of the History Items 00081 QStringList 00082 HistoryPtrList::toStringList() { 00083 QStringList result; 00084 00085 foreach(const EntryList *p, d->m_list) 00086 result.append(p->getQuery().toString()); 00087 00088 return result; 00089 } 00090 00091 QStringList 00092 HistoryPtrList::toStringListPrev() { 00093 QStringList result; 00094 00095 for(int i=0; i<d->m_index; i++) 00096 result.append(d->m_list.at(i)->getQuery().toString()); 00097 00098 return result; 00099 } 00100 00101 QStringList 00102 HistoryPtrList::toStringListNext() { 00103 HistoryPtrList localCopy(*this); 00104 00105 int currentPosition = d->m_index + 1; 00106 while(currentPosition--) 00107 localCopy.d->m_list.removeFirst(); 00108 00109 return localCopy.toStringList(); 00110 } 00111 00112 void 00113 HistoryPtrList::next(int distance) { 00114 if(distance + d->m_index > count() - 1) 00115 d->m_index = count() - 1; 00116 else 00117 d->m_index += distance; 00118 } 00119 00120 void 00121 HistoryPtrList::prev(int distance) { 00122 if(d->m_index - distance < 0) 00123 d->m_index = 0; 00124 else 00125 d->m_index -= distance; 00126 } 00127 00128 EntryList* 00129 HistoryPtrList::current() { 00130 if(d->m_index == -1) return NULL; 00131 return d->m_list.at(d->m_index); 00132 } 00133 00134 void 00135 HistoryPtrList::setCurrent(int i) { 00136 if(i<count() && i>=0) 00137 d->m_index=i; 00138 } 00139 00140 int 00141 HistoryPtrList::index() { 00142 return d->m_index; 00143 } 00144 00145 int 00146 HistoryPtrList::count() { 00147 return d->m_list.size(); 00148 }
KDE 4.2 API Reference