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 "helpindexfilter.h"
|
||||||
#include "helpplugin.h"
|
#include "helpmanager.h"
|
||||||
|
|
||||||
#include <extensionsystem/pluginmanager.h>
|
#include <extensionsystem/pluginmanager.h>
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <coreplugin/modemanager.h>
|
#include <coreplugin/modemanager.h>
|
||||||
|
|
||||||
|
#include <QtGui/QIcon>
|
||||||
|
|
||||||
#include <QtHelp/QHelpEngine>
|
#include <QtHelp/QHelpEngine>
|
||||||
#include <QtHelp/QHelpIndexModel>
|
#include <QtHelp/QHelpIndexModel>
|
||||||
|
|
||||||
|
#include <QtSql/QSqlDatabase>
|
||||||
|
#include <QtSql/QSqlDriver>
|
||||||
|
#include <QtSql/QSqlError>
|
||||||
|
#include <QtSql/QSqlQuery>
|
||||||
|
|
||||||
using namespace Locator;
|
using namespace Locator;
|
||||||
using namespace Help;
|
using namespace Help;
|
||||||
using namespace Help::Internal;
|
using namespace Help::Internal;
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(ILocatorFilter*);
|
Q_DECLARE_METATYPE(ILocatorFilter*);
|
||||||
|
|
||||||
HelpIndexFilter::HelpIndexFilter(HelpPlugin *plugin, QHelpEngine *helpEngine):
|
// -- HelpIndexFilter::HelpFileReader
|
||||||
m_plugin(plugin),
|
|
||||||
m_helpEngine(helpEngine),
|
class HelpIndexFilter::HelpFileReader
|
||||||
m_icon(QIcon()) // TODO: Put an icon next to the results
|
{
|
||||||
|
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);
|
setIncludedByDefault(false);
|
||||||
setShortcutString(QString(QLatin1Char('?')));
|
setShortcutString(QString(QLatin1Char('?')));
|
||||||
|
|
||||||
connect(m_helpEngine->indexModel(), SIGNAL(indexCreated()),
|
connect(&HelpManager::helpEngineCore(), SIGNAL(setupFinished()), this,
|
||||||
this, SLOT(updateIndices()));
|
SLOT(updateHelpFiles()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelpIndexFilter::updateIndices()
|
HelpIndexFilter::~HelpIndexFilter()
|
||||||
{
|
{
|
||||||
const QString currentFilter = m_plugin->indexFilter();
|
delete m_fileReader;
|
||||||
if (!currentFilter.isEmpty())
|
}
|
||||||
m_plugin->setIndexFilter(QString());
|
|
||||||
|
|
||||||
m_helpIndex = m_helpEngine->indexModel()->stringList();
|
void HelpIndexFilter::updateHelpFiles()
|
||||||
|
{
|
||||||
if (!currentFilter.isEmpty())
|
m_fileReader->updateHelpFiles();
|
||||||
m_plugin->setIndexFilter(currentFilter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString HelpIndexFilter::displayName() const
|
QString HelpIndexFilter::displayName() const
|
||||||
@@ -84,19 +175,15 @@ ILocatorFilter::Priority HelpIndexFilter::priority() const
|
|||||||
|
|
||||||
QList<FilterEntry> HelpIndexFilter::matchesFor(const QString &entry)
|
QList<FilterEntry> HelpIndexFilter::matchesFor(const QString &entry)
|
||||||
{
|
{
|
||||||
QList<FilterEntry> entries;
|
if (entry.length() < 2)
|
||||||
foreach (const QString &string, m_helpIndex) {
|
return QList<FilterEntry>();
|
||||||
if (string.contains(entry, Qt::CaseInsensitive)) {
|
return m_fileReader->matchesFor(entry, this);
|
||||||
FilterEntry entry(this, string, QVariant(), m_icon);
|
|
||||||
entries.append(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return entries;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelpIndexFilter::accept(FilterEntry selection) const
|
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) {
|
if (links.size() == 1) {
|
||||||
emit linkActivated(links.begin().value());
|
emit linkActivated(links.begin().value());
|
||||||
} else if (!links.isEmpty()) {
|
} else if (!links.isEmpty()) {
|
||||||
|
|||||||
@@ -32,13 +32,6 @@
|
|||||||
|
|
||||||
#include <locator/ilocatorfilter.h>
|
#include <locator/ilocatorfilter.h>
|
||||||
|
|
||||||
#include <QtGui/QIcon>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
|
||||||
class QHelpEngine;
|
|
||||||
class QUrl;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
namespace Help {
|
namespace Help {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
@@ -47,9 +40,11 @@ class HelpPlugin;
|
|||||||
class HelpIndexFilter : public Locator::ILocatorFilter
|
class HelpIndexFilter : public Locator::ILocatorFilter
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
class HelpFileReader;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HelpIndexFilter(HelpPlugin *plugin, QHelpEngine *helpEngine);
|
HelpIndexFilter();
|
||||||
|
~HelpIndexFilter();
|
||||||
|
|
||||||
// ILocatorFilter
|
// ILocatorFilter
|
||||||
QString displayName() const;
|
QString displayName() const;
|
||||||
@@ -64,13 +59,10 @@ signals:
|
|||||||
void linksActivated(const QMap<QString, QUrl> &urls, const QString &keyword) const;
|
void linksActivated(const QMap<QString, QUrl> &urls, const QString &keyword) const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void updateIndices();
|
void updateHelpFiles();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HelpPlugin *m_plugin;
|
HelpFileReader *m_fileReader;
|
||||||
QHelpEngine *m_helpEngine;
|
|
||||||
QStringList m_helpIndex;
|
|
||||||
QIcon m_icon;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
@@ -354,13 +354,6 @@ bool HelpPlugin::initialize(const QStringList &arguments, QString *error)
|
|||||||
connect(m_indexWidget, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
|
connect(m_indexWidget, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
|
||||||
m_centralWidget, SLOT(showTopicChooser(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());
|
previousAction->setEnabled(m_centralWidget->isBackwardAvailable());
|
||||||
nextAction->setEnabled(m_centralWidget->isForwardAvailable());
|
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(fontChanged()), this, SLOT(fontChanged()));
|
||||||
connect(generalSettingsPage, SIGNAL(dialogAccepted()), this,
|
connect(generalSettingsPage, SIGNAL(dialogAccepted()), this,
|
||||||
SLOT(checkForGeneralChanges()));
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user