Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
remotecontrol.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
7#include "mainwindow.h"
9#include "tracer.h"
10
11#include <QtCore/QFile>
12#include <QtCore/QFileInfo>
13#include <QtCore/QFileSystemWatcher>
14#include <QtCore/QTextStream>
15
16#include <QtWidgets/QMessageBox>
17#include <QtWidgets/QApplication>
18
19#include <QtHelp/QHelpEngine>
20#include <QtHelp/QHelpFilterEngine>
21#include <QtHelp/QHelpIndexWidget>
22#include <QtHelp/QHelpLink>
23#include <QtHelp/QHelpSearchQueryWidget>
24
25#ifdef Q_OS_WIN
26# include "stdinlistener_win.h"
27#else
28# include "stdinlistener.h"
29#endif
30
32
33using namespace Qt::StringLiterals;
34
35RemoteControl::RemoteControl(MainWindow *mainWindow)
36 : QObject(mainWindow)
37 , m_mainWindow(mainWindow)
39{
41 connect(m_mainWindow, &MainWindow::initDone,
42 this, &RemoteControl::applyCache);
43
44 StdInListener *l = new StdInListener(this);
45 connect(l, &StdInListener::receivedCommand,
46 this, &RemoteControl::handleCommandString);
47 l->start();
48}
49
50void RemoteControl::handleCommandString(const QString &cmdString)
51{
53 const QStringList &commands = cmdString.split(u';');
54 for (const QString &command : commands) {
55 QString cmd, arg;
56 splitInputString(command, cmd, arg);
57
58 if (m_debug)
59 QMessageBox::information(nullptr, tr("Debugging Remote Control"),
60 tr("Received Command: %1 %2").arg(cmd, arg));
61
62 if (cmd == "debug"_L1)
63 handleDebugCommand(arg);
64 else if (cmd == "show"_L1)
65 handleShowOrHideCommand(arg, true);
66 else if (cmd == "hide"_L1)
67 handleShowOrHideCommand(arg, false);
68 else if (cmd == "quit"_L1)
69 handleQuitCommand();
70 else if (cmd == "setsource"_L1)
71 handleSetSourceCommand(arg);
72 else if (cmd == "synccontents"_L1)
73 handleSyncContentsCommand();
74 else if (cmd == "activatekeyword"_L1)
75 handleActivateKeywordCommand(arg);
76 else if (cmd == "activateidentifier"_L1)
77 handleActivateIdentifierCommand(arg);
78 else if (cmd == "expandtoc"_L1)
79 handleExpandTocCommand(arg);
80 else if (cmd == "setcurrentfilter"_L1)
81 handleSetCurrentFilterCommand(arg);
82 else if (cmd == "register"_L1)
83 handleRegisterCommand(arg);
84 else if (cmd == "unregister"_L1)
85 handleUnregisterCommand(arg);
86 else
87 break;
88 }
89 m_mainWindow->raise();
90 m_mainWindow->activateWindow();
91}
92
93void RemoteControl::splitInputString(const QString &input, QString &cmd,
94 QString &arg)
95{
97 QString cmdLine = input.trimmed();
98 int i = cmdLine.indexOf(u' ');
99 cmd = cmdLine.left(i);
100 arg = cmdLine.mid(i + 1);
101 cmd = cmd.toLower();
102}
103
104void RemoteControl::handleDebugCommand(const QString &arg)
105{
107 m_debug = arg == "on"_L1;
108}
109
110void RemoteControl::handleShowOrHideCommand(const QString &arg, bool show)
111{
113 if (arg.toLower() == "contents"_L1)
114 m_mainWindow->setContentsVisible(show);
115 else if (arg.toLower() == "index"_L1)
116 m_mainWindow->setIndexVisible(show);
117 else if (arg.toLower() == "bookmarks"_L1)
118 m_mainWindow->setBookmarksVisible(show);
119 else if (arg.toLower() == "search"_L1)
120 m_mainWindow->setSearchVisible(show);
121}
122
123void RemoteControl::handleQuitCommand()
124{
125 QCoreApplication::quit();
126}
127
128void RemoteControl::handleSetSourceCommand(const QString &arg)
129{
131 QUrl url(arg);
132 if (url.isValid()) {
133 if (url.isRelative())
134 url = CentralWidget::instance()->currentSource().resolved(url);
135 if (m_caching) {
136 clearCache();
137 m_setSource = url;
138 } else {
139 CentralWidget::instance()->setSource(url);
140 }
141 }
142}
143
144void RemoteControl::handleSyncContentsCommand()
145{
147 if (m_caching)
148 m_syncContents = true;
149 else
150 m_mainWindow->syncContents();
151}
152
153void RemoteControl::handleActivateKeywordCommand(const QString &arg)
154{
156 if (m_caching) {
157 clearCache();
158 m_activateKeyword = arg;
159 } else {
160 m_mainWindow->setIndexString(arg);
161 if (!arg.isEmpty()) {
162 if (!helpEngine.indexWidget()->currentIndex().isValid()
164 if (QHelpSearchEngine *se = helpEngine.searchEngine()) {
165 m_mainWindow->setSearchVisible(true);
166 if (QHelpSearchQueryWidget *w = se->queryWidget()) {
167 w->collapseExtendedSearch();
168 w->setSearchInput(arg);
169 se->search(arg);
170 }
171 }
172 } else {
173 m_mainWindow->setIndexVisible(true);
174 helpEngine.indexWidget()->activateCurrentItem();
175 }
176 }
177 }
178}
179
180void RemoteControl::handleActivateIdentifierCommand(const QString &arg)
181{
183 if (m_caching) {
184 clearCache();
185 m_activateIdentifier = arg;
186 } else {
187 const auto docs = helpEngine.documentsForIdentifier(arg);
188 if (!docs.isEmpty())
189 CentralWidget::instance()->setSource(docs.first().url);
190 }
191}
192
193void RemoteControl::handleExpandTocCommand(const QString &arg)
194{
196 bool ok = false;
197 int depth = -2;
198 if (!arg.isEmpty())
199 depth = arg.toInt(&ok);
200 if (!ok || depth < -2)
201 depth = -2;
202
203 if (m_caching)
204 m_expandTOC = depth;
205 else if (depth != -2)
206 m_mainWindow->expandTOC(depth);
207}
208
209void RemoteControl::handleSetCurrentFilterCommand(const QString &arg)
210{
212 if (helpEngine.filterEngine()->filters().contains(arg)) {
213 if (m_caching) {
214 clearCache();
215 m_currentFilter = arg;
216 } else {
217 helpEngine.filterEngine()->setActiveFilter(arg);
218 }
219 }
220}
221
222void RemoteControl::handleRegisterCommand(const QString &arg)
223{
225 const QString &absFileName = QFileInfo(arg).absoluteFilePath();
226 if (helpEngine.registeredDocumentations().
227 contains(QHelpEngineCore::namespaceName(absFileName)))
228 return;
229 if (helpEngine.registerDocumentation(absFileName))
230 helpEngine.setupData();
231}
232
233void RemoteControl::handleUnregisterCommand(const QString &arg)
234{
236 const QString &absFileName = QFileInfo(arg).absoluteFilePath();
237 const QString &ns = QHelpEngineCore::namespaceName(absFileName);
238 if (helpEngine.registeredDocumentations().contains(ns)) {
239 OpenPagesManager::instance()->closePages(ns);
240 if (helpEngine.unregisterDocumentation(ns))
241 helpEngine.setupData();
242 }
243}
244
245void RemoteControl::applyCache()
246{
248 if (m_setSource.isValid()) {
249 CentralWidget::instance()->setSource(m_setSource);
250 } else if (!m_activateKeyword.isEmpty()) {
251 m_mainWindow->setIndexString(m_activateKeyword);
252 helpEngine.indexWidget()->activateCurrentItem();
253 } else if (!m_activateIdentifier.isEmpty()) {
254 const auto docs =
255 helpEngine.documentsForIdentifier(m_activateIdentifier);
256 if (!docs.isEmpty())
257 CentralWidget::instance()->setSource(docs.first().url);
258 } else if (!m_currentFilter.isEmpty()) {
259 helpEngine.filterEngine()->setActiveFilter(m_currentFilter);
260 }
261
262 if (m_syncContents)
263 m_mainWindow->syncContents();
264
265 Q_ASSERT(m_expandTOC >= -2);
266 if (m_expandTOC != -2)
267 m_mainWindow->expandTOC(m_expandTOC);
268
269 m_caching = false;
270}
271
272void RemoteControl::clearCache()
273{
275 m_currentFilter.clear();
276 m_setSource.clear();
277 m_syncContents = false;
278 m_activateKeyword.clear();
279 m_activateIdentifier.clear();
280}
281
282QT_END_NAMESPACE
static CentralWidget * instance()
QHelpFilterEngine * filterEngine() const
QHelpSearchEngine * searchEngine() const
bool fullTextSearchFallbackEnabled() const
QHelpIndexWidget * indexWidget()
static HelpEngineWrapper & instance()
void syncContents()
void setBookmarksVisible(bool visible)
void expandTOC(int depth)
void setIndexVisible(bool visible)
void setSearchVisible(bool visible)
static OpenPagesManager * instance()
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34