//! \return parsed raw format into html
QString toHtml() const {
+ if(!_key.size() || !_bookmarks)
+ return "";
+ qDebug() << ">toHtml";
+
QStringList list = _bookmarks->search(_key);
+ qDebug() << "toHtml" << list.size();
QString result;
foreach(QString translation, list)
- result += translation;
+ result += translation + "\n";
+ qDebug() << "<toHtml";
return result;
}
return false;
}
QSqlQuery cur(db);
- cur.exec("create table bookmarks(word text ,translation text)");
+ cur.exec("create table bookmarks(key text ,translation text)");
return true;
}
QList<Translation*> Bookmarks::searchWordList(QString word) {
- if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
+
+ if(word.indexOf("*")==-1 && word.indexOf("?")== -1)
word+="%";
word = word.replace("*", "%");
word = word.replace("?", "_");
word = removeAccents(word);
QSqlQuery cur(db);
- cur.prepare("select key from bookmarks where key=?");
+ cur.prepare("select key from bookmarks where key like ?");
cur.addBindValue(word);
cur.exec();
- QList<Translation*> res;
+ QSet<QString> res;
while(cur.next())
- res.append(new BookmarkTranslation(cur.value(0).toString(), this));
- return res;
+ res.insert(cur.value(0).toString());
+ qDebug() << "searchWordList " << res.size();
+ QList<Translation*> tr;
+ foreach(QString str, res.toList())
+ tr.append(new BookmarkTranslation(str, this));
+ return tr;
}
QStringList Bookmarks::search(QString word) {
+ qDebug() << "bookmarks::search";
QSqlQuery cur(db);
QStringList result;
- cur.prepare("select translation from bookmarks where word=?");
+ cur.prepare("select translation from bookmarks where key=?");
cur.addBindValue(word);
cur.exec();
while(cur.next())
result << cur.value(0).toString();
+
+ qDebug() << result.size() << " " << result;
return result;
}
bool Bookmarks::inBookmarks(QString word) {
QSqlQuery cur(db);
- cur.prepare("select translation from bookmarks where word=? limit 1");
+ cur.prepare("select translation from bookmarks where key like ? limit 1");
cur.addBindValue(word);
cur.exec();
if(cur.next())
SLOT(htmlTranslationReady()));
connect(&_bookmarkWatcher, SIGNAL(finished()), this,
SLOT(bookmarksListReady()));
+ connect(&_bookmarkSearchWatcher, SIGNAL(finished()), this,
+ SLOT(translationReady()));
QThreadPool::globalInstance()->setMaxThreadCount(
QThreadPool::globalInstance()->maxThreadCount()+1);
void Backbone::search(QString word) {
_result.clear();
mappedSearch = word.toLower();
- //_time.restart();
+
stopped = false;
_innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
_resultWatcher.setFuture(_innerResult);
+
+ _innerBookmarks = QtConcurrent::run(bookmarks, &Bookmarks::searchWordList,
+ word);
+ _bookmarkSearchWatcher.setFuture(_innerBookmarks);
}
void Backbone::translationReady() {
- //if(!_innerResult.isFinished())
- // return;
- QFutureIterator<QList<Translation*> > it(_innerResult);
+ if(_innerResult.isFinished()) {
+ QFutureIterator<QList<Translation*> > it(_innerResult);
+
+ while(it.hasNext()) {
+ QList<Translation* > list = it.next();
+ foreach(Translation* trans, list)
+ _result.insert(trans->key().toLower(), trans);
+ }
+ }
- while(it.hasNext()) {
- QList<Translation* > list = it.next();
+ if(_innerBookmarks.isFinished()) {
+ QList<Translation*> list = _innerBookmarks.result();
+ qDebug() << "translation bookmarks" << list.size();
foreach(Translation* trans, list)
- _result.insert(trans->key().toLower(), trans);
+ _result.insert(trans->key().toLower(), trans);
}
- //qDebug () << "time " << _time.elapsed();
- if(!stopped)
+ if(!stopped && _innerResult.isFinished() && _innerBookmarks.isFinished())
Q_EMIT ready();
}
_htmlResult.clear();
QList<TranslationPtr> dummy;
stopped = false;
- //_time.restart();
+ qDebug()<< "search html";
foreach(Translation* tr, translations)
dummy.append(TranslationPtr(tr));
+ qDebug()<< "search html mapp " << dummy.size();
_innerHtmlResult = QtConcurrent::mapped(dummy,
&TranslationPtr::toHtml);
+ qDebug()<< "search html after map";
_htmlResultWatcher.setFuture(_innerHtmlResult);
}
void Backbone::htmlTranslationReady() {
- //if(!_innerHtmlResult.isFinished())
- //return;
QFutureIterator<QString> it(_innerHtmlResult);
while(it.hasNext())
_htmlResult.append(it.next());
- //qDebug() << "time " << _time.elapsed();
if(!stopped)
Q_EMIT htmlReady();
private:
- QHash<CommonDictInterface*, bool> _dicts;
- QList<CommonDictInterface*> _plugins;
- QFuture<QList<Translation*> > _innerResult;
- QFuture<QString> _innerHtmlResult;
- QFuture<QList<Translation*> > _innerBookmarks;
- QMultiHash<QString, Translation*> _result;
- QStringList _htmlResult;
- QList<Translation*> _bookmarksResult;
- //QTime _time;
- QString _pluginPath, _defaultPluginPath;
- QString _configPath;
- QString _defaultConfigPath;
+ QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
+ QList<CommonDictInterface*> _plugins; // List of plugins
+
+
+ QFuture<QList<Translation*> > _innerResult; //Res of concurent word search
+ QFuture<QString> _innerHtmlResult; // Result of html search
+ QFuture<QList<Translation*> > _innerBookmarks; //Res of search in bookmarks
+ QFuture<QStringList> _innerHtmlBookmarks; //Html result of bookmarks search
+
+ QMultiHash<QString, Translation*> _result; //Final result of word search
+ QStringList _htmlResult; // Final result of html search
+ QList<Translation*> _bookmarksResult; // Final result of search in bookmarks
+
+
+ // Keeps track of concurent computations
QFutureWatcher<QList<Translation*> > _resultWatcher;
QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
+ QFutureWatcher<QList<Translation*> > _bookmarkSearchWatcher;
QFutureWatcher<QString> _htmlResultWatcher;
+
+
+ QString _pluginPath, _defaultPluginPath;
+ QString _configPath;
+ QString _defaultConfigPath;
int _searchLimit, _defaultSearchLimit;
int _activeSearchNum;
int _historyLen, _defaultHistoryLen;
CommonDictInterface* plugin(QString type); //< search for given type plugin
QList<CommonDictInterface*> activeDicts();
- //QList<Translation*> mapSearch(CommonDictInterface*) const;
History* _history;