forked from qt-creator/qt-creator
Implement our own search for keywords using QSql.
Currently we can only retrieve a list of keywords from the help index model, which is slow and needs a fully setup gui help engine. This needs to be implmeneted in the help lib though.
This commit is contained in:
@@ -28,43 +28,134 @@
|
||||
**************************************************************************/
|
||||
|
||||
#include "helpindexfilter.h"
|
||||
#include "helpplugin.h"
|
||||
#include "helpmanager.h"
|
||||
|
||||
#include <extensionsystem/pluginmanager.h>
|
||||
#include <coreplugin/icore.h>
|
||||
#include <coreplugin/modemanager.h>
|
||||
|
||||
#include <QtGui/QIcon>
|
||||
|
||||
#include <QtHelp/QHelpEngine>
|
||||
#include <QtHelp/QHelpIndexModel>
|
||||
|
||||
#include <QtSql/QSqlDatabase>
|
||||
#include <QtSql/QSqlDriver>
|
||||
#include <QtSql/QSqlError>
|
||||
#include <QtSql/QSqlQuery>
|
||||
|
||||
using namespace Locator;
|
||||
using namespace Help;
|
||||
using namespace Help::Internal;
|
||||
|
||||
Q_DECLARE_METATYPE(ILocatorFilter*);
|
||||
|
||||
HelpIndexFilter::HelpIndexFilter(HelpPlugin *plugin, QHelpEngine *helpEngine):
|
||||
m_plugin(plugin),
|
||||
m_helpEngine(helpEngine),
|
||||
m_icon(QIcon()) // TODO: Put an icon next to the results
|
||||
// -- HelpIndexFilter::HelpFileReader
|
||||
|
||||
class HelpIndexFilter::HelpFileReader
|
||||
{
|
||||
public:
|
||||
HelpFileReader();
|
||||
~HelpFileReader();
|
||||
|
||||
public:
|
||||
void updateHelpFiles();
|
||||
QList<FilterEntry> matchesFor(const QString &entry, ILocatorFilter *locator);
|
||||
|
||||
private:
|
||||
bool m_initialized;
|
||||
QStringList m_helpFiles;
|
||||
};
|
||||
|
||||
HelpIndexFilter::HelpFileReader::HelpFileReader()
|
||||
: m_initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
HelpIndexFilter::HelpFileReader::~HelpFileReader()
|
||||
{
|
||||
}
|
||||
|
||||
void HelpIndexFilter::HelpFileReader::updateHelpFiles()
|
||||
{
|
||||
m_helpFiles.clear();
|
||||
const QLatin1String id("HelpIndexFilter::HelpFileReader::helpFiles");
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), id);
|
||||
if (db.driver()
|
||||
&& db.driver()->lastError().type() == QSqlError::NoError) {
|
||||
db.setDatabaseName(HelpManager::collectionFilePath());
|
||||
if (db.open()) {
|
||||
QSqlQuery query = QSqlQuery(db);
|
||||
query.exec(QLatin1String("SELECT a.FilePath FROM NamespaceTable a"));
|
||||
while (query.next())
|
||||
m_helpFiles.append(query.value(0).toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
QSqlDatabase::removeDatabase(id);
|
||||
}
|
||||
|
||||
QList<FilterEntry> HelpIndexFilter::HelpFileReader::matchesFor(const QString &id,
|
||||
ILocatorFilter *locator)
|
||||
{
|
||||
if (!m_initialized) {
|
||||
updateHelpFiles();
|
||||
m_initialized = true;
|
||||
}
|
||||
|
||||
QList<FilterEntry> entries;
|
||||
const QLatin1String sqlite("QSQLITE");
|
||||
const QLatin1String name("HelpIndexFilter::HelpFileReader::matchesFor");
|
||||
foreach(const QString &file, m_helpFiles) {
|
||||
if (!QFile::exists(file))
|
||||
continue;
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase(sqlite, name);
|
||||
if (db.driver()
|
||||
&& db.driver()->lastError().type() == QSqlError::NoError) {
|
||||
db.setDatabaseName(file);
|
||||
if (db.open()) {
|
||||
QSqlQuery query = QSqlQuery(db);
|
||||
query.setForwardOnly(true);
|
||||
query.exec(QString::fromLatin1("SELECT DISTINCT Name FROM "
|
||||
"IndexTable WHERE Name LIKE '%%1%'").arg(id));
|
||||
while (query.next()) {
|
||||
const QString &key = query.value(0).toString();
|
||||
if (!key.isEmpty()) {
|
||||
// NOTE: do not use an icon since it is really slow
|
||||
entries.append(FilterEntry(locator, key, QVariant(),
|
||||
QIcon()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QSqlDatabase::removeDatabase(name);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
// -- HelpIndexFilter
|
||||
|
||||
HelpIndexFilter::HelpIndexFilter()
|
||||
: m_fileReader(new HelpFileReader)
|
||||
{
|
||||
setIncludedByDefault(false);
|
||||
setShortcutString(QString(QLatin1Char('?')));
|
||||
|
||||
connect(m_helpEngine->indexModel(), SIGNAL(indexCreated()),
|
||||
this, SLOT(updateIndices()));
|
||||
connect(&HelpManager::helpEngineCore(), SIGNAL(setupFinished()), this,
|
||||
SLOT(updateHelpFiles()));
|
||||
}
|
||||
|
||||
void HelpIndexFilter::updateIndices()
|
||||
HelpIndexFilter::~HelpIndexFilter()
|
||||
{
|
||||
const QString currentFilter = m_plugin->indexFilter();
|
||||
if (!currentFilter.isEmpty())
|
||||
m_plugin->setIndexFilter(QString());
|
||||
delete m_fileReader;
|
||||
}
|
||||
|
||||
m_helpIndex = m_helpEngine->indexModel()->stringList();
|
||||
|
||||
if (!currentFilter.isEmpty())
|
||||
m_plugin->setIndexFilter(currentFilter);
|
||||
void HelpIndexFilter::updateHelpFiles()
|
||||
{
|
||||
m_fileReader->updateHelpFiles();
|
||||
}
|
||||
|
||||
QString HelpIndexFilter::displayName() const
|
||||
@@ -84,19 +175,15 @@ ILocatorFilter::Priority HelpIndexFilter::priority() const
|
||||
|
||||
QList<FilterEntry> HelpIndexFilter::matchesFor(const QString &entry)
|
||||
{
|
||||
QList<FilterEntry> entries;
|
||||
foreach (const QString &string, m_helpIndex) {
|
||||
if (string.contains(entry, Qt::CaseInsensitive)) {
|
||||
FilterEntry entry(this, string, QVariant(), m_icon);
|
||||
entries.append(entry);
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
if (entry.length() < 2)
|
||||
return QList<FilterEntry>();
|
||||
return m_fileReader->matchesFor(entry, this);
|
||||
}
|
||||
|
||||
void HelpIndexFilter::accept(FilterEntry selection) const
|
||||
{
|
||||
QMap<QString, QUrl> links = m_helpEngine->indexModel()->linksForKeyword(selection.displayName);
|
||||
const QHelpEngineCore &engine = HelpManager::helpEngineCore();
|
||||
QMap<QString, QUrl> links = engine.linksForIdentifier(selection.displayName);
|
||||
if (links.size() == 1) {
|
||||
emit linkActivated(links.begin().value());
|
||||
} else if (!links.isEmpty()) {
|
||||
|
||||
@@ -32,13 +32,6 @@
|
||||
|
||||
#include <locator/ilocatorfilter.h>
|
||||
|
||||
#include <QtGui/QIcon>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QHelpEngine;
|
||||
class QUrl;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Help {
|
||||
namespace Internal {
|
||||
|
||||
@@ -47,9 +40,11 @@ class HelpPlugin;
|
||||
class HelpIndexFilter : public Locator::ILocatorFilter
|
||||
{
|
||||
Q_OBJECT
|
||||
class HelpFileReader;
|
||||
|
||||
public:
|
||||
HelpIndexFilter(HelpPlugin *plugin, QHelpEngine *helpEngine);
|
||||
HelpIndexFilter();
|
||||
~HelpIndexFilter();
|
||||
|
||||
// ILocatorFilter
|
||||
QString displayName() const;
|
||||
@@ -64,13 +59,10 @@ signals:
|
||||
void linksActivated(const QMap<QString, QUrl> &urls, const QString &keyword) const;
|
||||
|
||||
private slots:
|
||||
void updateIndices();
|
||||
void updateHelpFiles();
|
||||
|
||||
private:
|
||||
HelpPlugin *m_plugin;
|
||||
QHelpEngine *m_helpEngine;
|
||||
QStringList m_helpIndex;
|
||||
QIcon m_icon;
|
||||
HelpFileReader *m_fileReader;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
@@ -354,13 +354,6 @@ bool HelpPlugin::initialize(const QStringList &arguments, QString *error)
|
||||
connect(m_indexWidget, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
|
||||
m_centralWidget, SLOT(showTopicChooser(QMap<QString, QUrl>, QString)));
|
||||
|
||||
HelpIndexFilter *helpIndexFilter = new HelpIndexFilter(this, m_helpEngine);
|
||||
addAutoReleasedObject(helpIndexFilter);
|
||||
connect(helpIndexFilter, SIGNAL(linkActivated(QUrl)), this,
|
||||
SLOT(switchToHelpMode(QUrl)));
|
||||
connect(helpIndexFilter, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
|
||||
this, SLOT(switchToHelpMode(QMap<QString, QUrl>, QString)));
|
||||
|
||||
previousAction->setEnabled(m_centralWidget->isBackwardAvailable());
|
||||
nextAction->setEnabled(m_centralWidget->isForwardAvailable());
|
||||
|
||||
@@ -396,6 +389,13 @@ bool HelpPlugin::initialize(const QStringList &arguments, QString *error)
|
||||
connect(generalSettingsPage, SIGNAL(fontChanged()), this, SLOT(fontChanged()));
|
||||
connect(generalSettingsPage, SIGNAL(dialogAccepted()), this,
|
||||
SLOT(checkForGeneralChanges()));
|
||||
HelpIndexFilter *helpIndexFilter = new HelpIndexFilter();
|
||||
addAutoReleasedObject(helpIndexFilter);
|
||||
connect(helpIndexFilter, SIGNAL(linkActivated(QUrl)), this,
|
||||
SLOT(switchToHelpMode(QUrl)));
|
||||
connect(helpIndexFilter, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
|
||||
this, SLOT(switchToHelpMode(QMap<QString, QUrl>, QString)));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user