1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qhelpengine.h" |
5 | #include "qhelpcontentwidget.h" |
6 | #include "qhelpfilterengine.h" |
7 | #include "qhelpindexwidget.h" |
8 | #include "qhelpsearchengine.h" |
9 | |
10 | #include <QtCore/qtimer.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | class QHelpEnginePrivate |
15 | { |
16 | public: |
17 | QHelpEnginePrivate(QHelpEngineCore *helpEngineCore); |
18 | |
19 | QHelpContentModel *contentModel = nullptr; |
20 | QHelpContentWidget *contentWidget = nullptr; |
21 | |
22 | QHelpIndexModel *indexModel = nullptr; |
23 | QHelpIndexWidget *indexWidget = nullptr; |
24 | |
25 | QHelpSearchEngine *searchEngine = nullptr; |
26 | |
27 | bool m_isApplyCurrentFilterScheduled = false; |
28 | QHelpEngineCore *m_helpEngineCore = nullptr; |
29 | }; |
30 | |
31 | QHelpEnginePrivate::QHelpEnginePrivate(QHelpEngineCore *helpEngineCore) |
32 | : m_helpEngineCore(helpEngineCore) |
33 | { |
34 | if (!contentModel) |
35 | contentModel = new QHelpContentModel(helpEngineCore); |
36 | if (!indexModel) |
37 | indexModel = new QHelpIndexModel(m_helpEngineCore); |
38 | |
39 | const auto applyCurrentFilter = [this] { |
40 | m_isApplyCurrentFilterScheduled = false; |
41 | contentModel->createContentsForCurrentFilter(); |
42 | indexModel->createIndexForCurrentFilter(); |
43 | }; |
44 | |
45 | const auto scheduleApplyCurrentFilter = [this, applyCurrentFilter] { |
46 | if (!m_helpEngineCore->error().isEmpty()) |
47 | return; |
48 | |
49 | if (m_isApplyCurrentFilterScheduled) |
50 | return; |
51 | |
52 | m_isApplyCurrentFilterScheduled = true; |
53 | QTimer::singleShot(interval: 0, receiver: m_helpEngineCore, slot: applyCurrentFilter); |
54 | }; |
55 | |
56 | QObject::connect(sender: m_helpEngineCore, signal: &QHelpEngineCore::setupFinished, |
57 | context: m_helpEngineCore, slot: scheduleApplyCurrentFilter); |
58 | QObject::connect(sender: m_helpEngineCore, signal: &QHelpEngineCore::currentFilterChanged, |
59 | context: m_helpEngineCore, slot: scheduleApplyCurrentFilter); |
60 | QObject::connect(sender: m_helpEngineCore->filterEngine(), signal: &QHelpFilterEngine::filterActivated, |
61 | context: m_helpEngineCore, slot: scheduleApplyCurrentFilter); |
62 | } |
63 | |
64 | /*! |
65 | \class QHelpEngine |
66 | \since 4.4 |
67 | \inmodule QtHelp |
68 | \brief The QHelpEngine class provides access to contents and |
69 | indices of the help engine. |
70 | */ |
71 | |
72 | /*! |
73 | Constructs a new help engine with the given \a parent. The help |
74 | engine uses the information stored in the \a collectionFile for |
75 | providing help. If the collection file does not already exist, |
76 | it will be created. |
77 | */ |
78 | QHelpEngine::QHelpEngine(const QString &collectionFile, QObject *parent) |
79 | : QHelpEngineCore(collectionFile, parent) |
80 | , d(new QHelpEnginePrivate(this)) |
81 | {} |
82 | |
83 | /*! |
84 | Destroys the help engine object. |
85 | */ |
86 | QHelpEngine::~QHelpEngine() |
87 | { |
88 | delete d; |
89 | } |
90 | |
91 | /*! |
92 | Returns the content model. |
93 | */ |
94 | QHelpContentModel *QHelpEngine::contentModel() const |
95 | { |
96 | return d->contentModel; |
97 | } |
98 | |
99 | /*! |
100 | Returns the index model. |
101 | */ |
102 | QHelpIndexModel *QHelpEngine::indexModel() const |
103 | { |
104 | return d->indexModel; |
105 | } |
106 | |
107 | /*! |
108 | Returns the content widget. |
109 | */ |
110 | QHelpContentWidget *QHelpEngine::contentWidget() |
111 | { |
112 | if (!d->contentWidget) { |
113 | d->contentWidget = new QHelpContentWidget; |
114 | d->contentWidget->setModel(d->contentModel); |
115 | #if QT_CONFIG(cursor) |
116 | connect(sender: d->contentModel, signal: &QHelpContentModel::contentsCreationStarted, context: this, slot: [this] { |
117 | d->contentWidget->setCursor(Qt::WaitCursor); |
118 | }); |
119 | connect(sender: d->contentModel, signal: &QHelpContentModel::contentsCreated, context: this, slot: [this] { |
120 | d->contentWidget->unsetCursor(); |
121 | }); |
122 | #endif |
123 | } |
124 | return d->contentWidget; |
125 | } |
126 | |
127 | /*! |
128 | Returns the index widget. |
129 | */ |
130 | QHelpIndexWidget *QHelpEngine::indexWidget() |
131 | { |
132 | if (!d->indexWidget) { |
133 | d->indexWidget = new QHelpIndexWidget; |
134 | d->indexWidget->setModel(d->indexModel); |
135 | #if QT_CONFIG(cursor) |
136 | connect(sender: d->indexModel, signal: &QHelpIndexModel::indexCreationStarted, context: this, slot: [this] { |
137 | d->indexWidget->setCursor(Qt::WaitCursor); |
138 | }); |
139 | connect(sender: d->indexModel, signal: &QHelpIndexModel::indexCreated, context: this, slot: [this] { |
140 | d->indexWidget->unsetCursor(); |
141 | }); |
142 | #endif |
143 | } |
144 | return d->indexWidget; |
145 | } |
146 | |
147 | /*! |
148 | Returns the default search engine. |
149 | */ |
150 | QHelpSearchEngine* QHelpEngine::searchEngine() |
151 | { |
152 | if (!d->searchEngine) |
153 | d->searchEngine = new QHelpSearchEngine(this, this); |
154 | return d->searchEngine; |
155 | } |
156 | |
157 | QT_END_NAMESPACE |
158 | |