2009-02-05 14:21:47 +00:00
|
|
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#ifndef __ARTICLE_MAKER_HH_INCLUDED__
|
|
|
|
#define __ARTICLE_MAKER_HH_INCLUDED__
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
#include <QObject>
|
2009-05-29 19:48:50 +00:00
|
|
|
#include <QMap>
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <set>
|
|
|
|
#include <list>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include "dictionary.hh"
|
|
|
|
#include "instances.hh"
|
2009-04-17 13:51:50 +00:00
|
|
|
#include "wordfinder.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// This class generates the article's body for the given lookup request
|
2009-02-01 00:08:08 +00:00
|
|
|
class ArticleMaker: public QObject
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-02-01 00:08:08 +00:00
|
|
|
Q_OBJECT // We make it QObject to use tr() conveniently
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & dictionaries;
|
|
|
|
std::vector< Instances::Group > const & groups;
|
|
|
|
|
2009-05-11 11:03:36 +00:00
|
|
|
QString displayStyle;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/// On construction, a reference to all dictionaries and a reference all
|
|
|
|
/// groups' instances are to be passed. Those references are kept stored as
|
|
|
|
/// references, and as such, any changes to them would reflect on the results
|
2010-03-30 13:41:14 +00:00
|
|
|
/// of the inquiries, although those changes are perfectly legal.
|
2009-01-28 20:55:45 +00:00
|
|
|
ArticleMaker( std::vector< sptr< Dictionary::Class > > const & dictionaries,
|
2009-05-11 11:03:36 +00:00
|
|
|
std::vector< Instances::Group > const & groups,
|
|
|
|
QString const & displayStyle );
|
|
|
|
|
|
|
|
/// Sets the display style to use for any new requests. This affects the
|
|
|
|
/// choice of the stylesheet file.
|
|
|
|
void setDisplayStyle( QString const & );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Looks up the given word within the given group, and creates a full html
|
|
|
|
/// page text containing its definition.
|
2009-03-26 19:00:08 +00:00
|
|
|
/// The result is returned as Dictionary::DataRequest just like dictionaries
|
|
|
|
/// themselves do. The difference is that the result is a complete html page
|
|
|
|
/// with all definitions from all the relevant dictionaries.
|
2009-05-29 19:48:50 +00:00
|
|
|
/// Contexts is a map of context values to be passed to each dictionary, where
|
|
|
|
/// the keys are dictionary ids.
|
2009-09-23 18:44:38 +00:00
|
|
|
/// If mutedDicts is not empty, the search would be limited only to those
|
|
|
|
/// dictionaries in group which aren't listed there.
|
2009-05-29 19:48:50 +00:00
|
|
|
sptr< Dictionary::DataRequest > makeDefinitionFor( QString const & word, unsigned groupId,
|
2009-09-23 18:44:38 +00:00
|
|
|
QMap< QString, QString > const & contexts,
|
|
|
|
QSet< QString > const & mutedDicts =
|
|
|
|
QSet< QString >() ) const;
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
/// Makes up a text which states that no translation for the given word
|
|
|
|
/// was found. Sometimes it's better to call this directly when it's already
|
|
|
|
/// known that there's no translation.
|
2009-03-26 19:00:08 +00:00
|
|
|
sptr< Dictionary::DataRequest > makeNotFoundTextFor( QString const & word, QString const & group ) const;
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2009-05-11 15:33:57 +00:00
|
|
|
/// Creates an 'untitled' page. The result is guaranteed to be instant.
|
|
|
|
sptr< Dictionary::DataRequest > makeEmptyPage() const;
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Makes everything up to and including the opening body tag.
|
2009-05-11 11:03:36 +00:00
|
|
|
std::string makeHtmlHeader( QString const & word, QString const & icon ) const;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
/// Makes the html body for makeNotFoundTextFor()
|
|
|
|
static std::string makeNotFoundBody( QString const & word, QString const & group );
|
|
|
|
|
|
|
|
friend class ArticleRequest; // Allow it calling makeNotFoundBody()
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
/// The request specific to article maker. This should really be private,
|
|
|
|
/// but we need it to be handled by moc.
|
|
|
|
class ArticleRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
QString word, group;
|
2009-05-29 19:48:50 +00:00
|
|
|
QMap< QString, QString > contexts;
|
2009-09-23 18:44:38 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > activeDicts;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
std::set< gd::wstring > alts; // Accumulated main forms
|
2009-03-26 19:00:08 +00:00
|
|
|
std::list< sptr< Dictionary::WordSearchRequest > > altSearches;
|
|
|
|
bool altsDone, bodyDone;
|
|
|
|
std::list< sptr< Dictionary::DataRequest > > bodyRequests;
|
|
|
|
bool foundAnyDefinitions;
|
2009-04-12 16:22:42 +00:00
|
|
|
bool closePrevSpan; // Indicates whether the last opened article span is to
|
|
|
|
// be closed after the article ends.
|
2009-04-17 13:51:50 +00:00
|
|
|
sptr< WordFinder > stemmedWordFinder; // Used when there're no results
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
/// A sequence of words and spacings between them, including the initial
|
|
|
|
/// spacing before the first word and the final spacing after the last word.
|
|
|
|
typedef QList< QString > Words;
|
|
|
|
typedef QList< QString > Spacings;
|
|
|
|
|
|
|
|
/// Splits the given string into words and spacings between them.
|
|
|
|
QPair< Words, Spacings > splitIntoWords( QString const & );
|
|
|
|
|
|
|
|
QPair< Words, Spacings > splittedWords;
|
|
|
|
int currentSplittedWordStart;
|
|
|
|
int currentSplittedWordEnd;
|
|
|
|
QString currentSplittedWordCompound;
|
|
|
|
bool firstCompoundWasFound;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
ArticleRequest( QString const & word, QString const & group,
|
2009-05-29 19:48:50 +00:00
|
|
|
QMap< QString, QString > const & contexts,
|
2009-03-26 19:00:08 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & activeDicts,
|
|
|
|
std::string const & header );
|
|
|
|
|
|
|
|
virtual void cancel()
|
|
|
|
{ finish(); } // Add our own requests cancellation here
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
|
|
void altSearchFinished();
|
|
|
|
void bodyFinished();
|
2009-04-17 13:51:50 +00:00
|
|
|
void stemmedSearchFinished();
|
2010-03-30 13:41:14 +00:00
|
|
|
void individualWordFinished();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// Appends the given string to 'data', with locking its mutex.
|
|
|
|
void appendToData( std::string const & );
|
|
|
|
|
|
|
|
/// Uses stemmedWordFinder to perform the next step of looking up word
|
|
|
|
/// combinations.
|
|
|
|
void compoundSearchNextStep( bool lastSearchSucceeded );
|
|
|
|
|
|
|
|
/// Creates a single word out of the [currentSplittedWordStart..End] range.
|
|
|
|
QString makeSplittedWordCompound();
|
|
|
|
|
|
|
|
/// Makes an html link to the given word.
|
|
|
|
std::string linkWord( QString const & );
|
|
|
|
|
|
|
|
/// Escapes the spacing between the words to include in html.
|
|
|
|
std::string escapeSpacing( QString const & );
|
2009-03-26 19:00:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#endif
|