• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kiten/lib

  • sources
  • kde-4.14
  • kdeedu
  • kiten
  • lib
entrylist.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * This file is part of Kiten, a KDE Japanese Reference Tool *
3  * Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com> *
4  * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> *
5  * Copyright (C) 2006 Eric Kjeldergaard <kjelderg@gmail.com> *
6  * Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com> *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Library General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Library General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Library General Public License *
19  * along with this library; see the file COPYING.LIB. If not, write to *
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
21  * Boston, MA 02110-1301, USA. *
22  *****************************************************************************/
23 
24 #include "entrylist.h"
25 
26 #include <KDebug>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KStandardDirs>
30 
31 #include <QFileInfo>
32 #include <QRegExp>
33 #include <QTextCodec>
34 
35 #include <sys/mman.h>
36 
37 #include "DictEdict/dictfileedict.h"
38 #include "DictEdict/entryedict.h"
39 #include "kitenmacros.h"
40 
41 class EntryList::Private
42 {
43  public:
44  Private() : storedScrollValue( 0 )
45  , sorted( false )
46  , sortedByDictionary( false ) {}
47  Private( const Private &old ) : storedScrollValue( old.storedScrollValue )
48  , sorted( old.sorted )
49  , sortedByDictionary( old.sortedByDictionary )
50  , query( old.query ) {}
51 
52  int storedScrollValue;
53  bool sorted;
54  bool sortedByDictionary;
55  DictQuery query;
56 };
57 
58 /* sorts the EntryList in a C++ish, thread-safe manner. */
59 class SortFunctor
60 {
61  public:
62  QStringList *dictionary_order;
63  QStringList *sort_order;
64 
65  bool operator()( const Entry *n1, const Entry *n2 ) const
66  {
67  return n1->sort( *n2, *dictionary_order, *sort_order );
68  }
69 };
70 
71 EntryList::EntryList()
72 : QList<Entry*>()
73 , d( new Private )
74 {
75 }
76 
77 EntryList::EntryList( const EntryList &old )
78 : QList<Entry*> ( old )
79 , d( new Private( *( old.d ) ) )
80 {
81 }
82 
83 EntryList::~EntryList()
84 {
85  delete d;
86 // kdDebug() << "A copy of EntryList is being deleted... watch your memory!" << endl;
87 }
88 
89 int EntryList::scrollValue() const
90 {
91  return d->storedScrollValue;
92 }
93 
94 void EntryList::setScrollValue( int val )
95 {
96  d->storedScrollValue = val;
97 }
98 
99 void EntryList::deleteAll()
100 {
101  while( ! this->isEmpty() )
102  {
103  delete this->takeFirst();
104  }
105 
106  d->sorted = false;
107 }
108 
109 /* Returns the EntryList as HTML */
110 //TODO: Some intelligent decision making regarding when to print what when AutoPrinting is on
111 QString EntryList::toHTML( unsigned int start, unsigned int length ) const
112 {
113  unsigned int max = count();
114  if( start > max )
115  {
116  return QString();
117  }
118  if( start + length > max )
119  {
120  length = max-start;
121  }
122 
123  QString result;
124  QString temp;
125  QString &lastDictionary = temp;
126  const QString fromDictionary = i18n( "From Dictionary:" );
127  QString query( getQuery() );
128 
129  bool firstTimeDeinflection = true;
130  bool firstTimeCommon = true;
131  bool firstTimeUncommon = true;
132  for( unsigned int i = 0; i < max; ++i )
133  {
134  Entry *entry = at( i );
135  if( d->sortedByDictionary )
136  {
137  const QString &newDictionary = entry->getDictName();
138  if( firstTimeDeinflection && newDictionary == EDICT
139  && DictFileEdict::deinflectionLabel )
140  {
141  const QString &label = *DictFileEdict::deinflectionLabel;
142  const QString &type = *DictFileEdict::wordType;
143  const QString &message = i18nc( "%1 is a word type (verb or adjective)."
144  " %2 is a verb or adjective tense."
145  " Example: 'Entered verb in past tense'."
146  , "Entered %1 in %2 form"
147  , type
148  , label );
149 
150  result += QString( "<div style=\"font-style:italic\">%1</div>" ).arg( message );
151 
152  firstTimeDeinflection = false;
153  }
154 
155  if( lastDictionary != newDictionary )
156  {
157  lastDictionary = newDictionary;
158  result += QString( "<div class=\"DictionaryHeader\">%1 %2</div>" )
159  .arg( fromDictionary )
160  .arg( newDictionary );
161  firstTimeCommon = true;
162  firstTimeUncommon = true;
163  }
164  }
165 
166  if( getQuery().getFilterType() == DictQuery::CommonUncommon )
167  {
168  if( entry->getDictionaryType() == EDICT )
169  {
170  EntryEdict *entryEdict = static_cast<EntryEdict*>( entry );
171  if( entryEdict->isCommon() && firstTimeCommon )
172  {
173  result += QString( "<div class=\"CommonHeader\">%1</div>" ).arg( i18n( "Common" ) );
174  firstTimeCommon = false;
175  }
176  else if( ! entryEdict->isCommon() && firstTimeUncommon )
177  {
178  result += QString( "<div class=\"UncommonHeader\">%1</div>" ).arg( i18n( "Uncommon" ) );
179  firstTimeUncommon = false;
180  }
181  }
182  }
183 
184  if( length-- > 0 )
185  {
186  result += QString( "<div class=\"Entry\" index=\"%1\" dict=\"%2\">%3</div>" )
187  .arg( QString::number( i ) )
188  .arg( entry->getDictName() )
189  .arg( entry->toHTML() );
190  }
191  else
192  {
193  break;
194  }
195  }
196  //result.replace( query, "<query>" + query + "</query>" );
197  return result;
198 }
199 
200 QString EntryList::toKVTML( unsigned int start, unsigned int length ) const
201 {
202  unsigned int max = count();
203  if( start > max )
204  {
205  return QString();
206  }
207  if( start + length > max )
208  {
209  length = max - start;
210  }
211 
212  QString result = "<?xml version=\"1.0\"?>\n<!DOCTYPE kvtml SYSTEM \"kvoctrain.dtd\">\n"
213  "<kvtml encoding=\"UTF-8\" "
214  " generator=\"kiten v42.0\""
215  " title=\"To be determined\">\n";
216  foreach( Entry *it, *this )
217  {
218  if( length-- > 0 )
219  {
220  result = result + it->toKVTML() + '\n';
221  }
222  else
223  {
224  break;
225  }
226  }
227  return result + "</kvtml>\n";
228 }
229 
230 QString EntryList::toHTML() const
231 {
232  return toHTML( 0, count() );
233 }
234 
235 /* Returns the EntryList as HTML */
236 //TODO: Some intelligent decision making... regarding the output format (differ for
237 //different types of searches?
238 QString EntryList::toString( unsigned int start, unsigned int length ) const
239 {
240  unsigned int max = count();
241  if( start > max )
242  {
243  return QString();
244  }
245  if( start + length > max )
246  {
247  length = max-start;
248  }
249 
250  QString result;
251  foreach( Entry *it, *this )
252  {
253  if( length-- > 0 )
254  {
255  result = result + it->toString();
256  }
257  else
258  {
259  break;
260  }
261  }
262 
263  return result;
264 }
265 
266 QString EntryList::toString() const
267 {
268  return toString( 0, count() );
269 }
270 
271 void EntryList::sort( QStringList &sortOrder, QStringList &dictionaryOrder )
272 {
273  //Don't shortcut sorting, unless we start to keep track of the last sorting order,
274  //Otherwise we won't respond when the user changes the sorting order
275  SortFunctor sorter;
276  sorter.dictionary_order = &dictionaryOrder;
277  sorter.sort_order = &sortOrder;
278 
279  qStableSort( this->begin(), this->end(), sorter );
280  d->sorted = true;
281  d->sortedByDictionary = dictionaryOrder.size() > 0;
282 }
283 
284 const EntryList& EntryList::operator+=( const EntryList &other )
285 {
286  foreach( Entry *it, other )
287  {
288  this->append( it );
289  }
290  if( other.size() > 0 )
291  {
292  d->sorted = false;
293  }
294 
295  return *this;
296 }
297 
298 const EntryList& EntryList::operator=( const EntryList &other )
299 {
300  QList<Entry*>::operator=( other );
301  *d = *( other.d );
302 
303  return *this;
304 }
305 
306 void EntryList::appendList( const EntryList *other )
307 {
308  foreach( Entry *it, *other )
309  {
310  append( it );
311  }
312 
313  if( other->size() > 0 )
314  {
315  d->sorted = false;
316  }
317 }
318 
323 DictQuery EntryList::getQuery() const
324 {
325  return d->query;
326 }
327 
331 void EntryList::setQuery( const DictQuery &newQuery )
332 {
333  d->query = newQuery;
334 }
QList::operator=
QList< T > & operator=(const QList< T > &other)
EntryList::setQuery
void setQuery(const DictQuery &newQuery)
Set the query for this list.
Definition: entrylist.cpp:331
DictQuery::CommonUncommon
Definition: dictquery.h:329
Entry
The Entry class is a generic base class for each particular entry in a given dictionary.
Definition: entry.h:44
QList< Entry * >::count
int count() const
EntryList::toHTML
QString toHTML() const
Convert every element of the EntryList to a QString in HTML form and return it.
Definition: entrylist.cpp:230
QList< Entry * >::at
const T & at(int i) const
EDICT
#define EDICT
Definition: kitenmacros.h:24
EntryList::sort
void sort(QStringList &sortOrder, QStringList &dictionaryOrder)
Sort the list according to the given fields in sortOrder, if dictionaryOrder is blank, don't order the list by dictionary, otherwise items are sorted by dictionary then by sortOrder aspects.
Definition: entrylist.cpp:271
DictFileEdict::wordType
static QString * wordType
Definition: dictfileedict.h:69
EntryList::EntryList
EntryList()
Basic constructor, create an empty EntryList.
Definition: entrylist.cpp:71
EntryList::toString
QString toString() const
Convert every element of the EntryList to a QString and return it.
Definition: entrylist.cpp:266
EntryList::setScrollValue
void setScrollValue(int val)
Definition: entrylist.cpp:94
DictFileEdict::deinflectionLabel
static QString * deinflectionLabel
Definition: dictfileedict.h:68
QList::size
int size() const
entryedict.h
QString::number
QString number(int n, int base)
QList< Entry * >::append
void append(const T &value)
Entry::toKVTML
virtual QString toKVTML() const
KVTML format for exporting.
Definition: entry.cpp:392
EntryEdict
Definition: entryedict.h:60
QList< Entry * >::isEmpty
bool isEmpty() const
Entry::toHTML
virtual QString toHTML() const
An entry should be able to generate a representation of itself in (valid) HTML.
Definition: entry.cpp:384
EntryList::operator+=
const EntryList & operator+=(const EntryList &other)
Append another EntryList onto this one.
Definition: entrylist.cpp:284
QString
EntryList::appendList
void appendList(const EntryList *other)
Append another EntryList onto this one.
Definition: entrylist.cpp:306
QList
EntryList::scrollValue
int scrollValue() const
Definition: entrylist.cpp:89
QStringList
EntryList::~EntryList
virtual ~EntryList()
Basic Destructor, does not delete Entry* objects.
Definition: entrylist.cpp:83
QList< Entry * >::end
iterator end()
EntryList::deleteAll
void deleteAll()
Delete all Entry objects in our list.
Definition: entrylist.cpp:99
Entry::getDictionaryType
virtual QString getDictionaryType() const =0
Get the dictionary type (e.g.
QList< Entry * >::takeFirst
T takeFirst()
EntryEdict::isCommon
bool isCommon() const
Definition: entryedict.cpp:102
Entry::sort
virtual bool sort(const Entry &that, const QStringList &dictionaryList, const QStringList &fieldList) const
An overrideable sorting function, similer to operator< in most contexts The default version will sort...
Definition: entry.cpp:425
DictQuery
A class to allow users of libkiten to properly setup a database query.
Definition: dictquery.h:89
EntryList::operator=
const EntryList & operator=(const EntryList &other)
Copy an entry list.
Definition: entrylist.cpp:298
kitenmacros.h
EntryList::toKVTML
QString toKVTML(unsigned int start, unsigned int length) const
Convert the entire list to KVTML for export to a flashcard app.
Definition: entrylist.cpp:200
Entry::toString
virtual QString toString() const
This will return a pure text interpretation of the Entry.
Definition: entry.cpp:413
dictfileedict.h
Entry::getDictName
const QString & getDictName() const
Get the dictionary name that generated this Entry.
Definition: entry.cpp:91
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QList< Entry * >::begin
iterator begin()
EntryList
EntryList is a simple container for Entry objects, and is-a QList A few simple overrides allo...
Definition: entrylist.h:38
entrylist.h
EntryList::getQuery
DictQuery getQuery() const
Get the query that generated this list, note that if you have appended EntryLists from two different ...
Definition: entrylist.cpp:323
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kiten/lib

Skip menu "kiten/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal