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(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 == "setsource"_L1)
69 handleSetSourceCommand(arg);
70 else if (cmd == "synccontents"_L1)
71 handleSyncContentsCommand();
72 else if (cmd == "activatekeyword"_L1)
73 handleActivateKeywordCommand(arg);
74 else if (cmd == "activateidentifier"_L1)
75 handleActivateIdentifierCommand(arg);
76 else if (cmd == "expandtoc"_L1)
77 handleExpandTocCommand(arg);
78 else if (cmd == "setcurrentfilter"_L1)
79 handleSetCurrentFilterCommand(arg);
80 else if (cmd == "register"_L1)
81 handleRegisterCommand(arg);
82 else if (cmd == "unregister"_L1)
83 handleUnregisterCommand(arg);
84 else
85 break;
86 }
87 m_mainWindow->raise();
88 m_mainWindow->activateWindow();
89}
90
91void RemoteControl::splitInputString(const QString &input, QString &cmd,
92 QString &arg)
93{
95 QString cmdLine = input.trimmed();
96 int i = cmdLine.indexOf(u' ');
97 cmd = cmdLine.left(i);
98 arg = cmdLine.mid(i + 1);
99 cmd = cmd.toLower();
100}
101
102void RemoteControl::handleDebugCommand(const QString &arg)
103{
105 m_debug = arg == "on"_L1;
106}
107
108void RemoteControl::handleShowOrHideCommand(const QString &arg, bool show)
109{
111 if (arg.toLower() == "contents"_L1)
112 m_mainWindow->setContentsVisible(show);
113 else if (arg.toLower() == "index"_L1)
114 m_mainWindow->setIndexVisible(show);
115 else if (arg.toLower() == "bookmarks"_L1)
116 m_mainWindow->setBookmarksVisible(show);
117 else if (arg.toLower() == "search"_L1)
118 m_mainWindow->setSearchVisible(show);
119}
120
121void RemoteControl::handleSetSourceCommand(const QString &arg)
122{
124 QUrl url(arg);
125 if (url.isValid()) {
126 if (url.isRelative())
127 url = CentralWidget::instance()->currentSource().resolved(url);
128 if (m_caching) {
129 clearCache();
130 m_setSource = url;
131 } else {
132 CentralWidget::instance()->setSource(url);
133 }
134 }
135}
136
137void RemoteControl::handleSyncContentsCommand()
138{
140 if (m_caching)
141 m_syncContents = true;
142 else
143 m_mainWindow->syncContents();
144}
145
146void RemoteControl::handleActivateKeywordCommand(const QString &arg)
147{
149 if (m_caching) {
150 clearCache();
151 m_activateKeyword = arg;
152 } else {
153 m_mainWindow->setIndexString(arg);
154 if (!arg.isEmpty()) {
155 if (!helpEngine.indexWidget()->currentIndex().isValid()
157 if (QHelpSearchEngine *se = helpEngine.searchEngine()) {
158 m_mainWindow->setSearchVisible(true);
159 if (QHelpSearchQueryWidget *w = se->queryWidget()) {
160 w->collapseExtendedSearch();
161 w->setSearchInput(arg);
162 se->search(arg);
163 }
164 }
165 } else {
166 m_mainWindow->setIndexVisible(true);
167 helpEngine.indexWidget()->activateCurrentItem();
168 }
169 }
170 }
171}
172
173void RemoteControl::handleActivateIdentifierCommand(const QString &arg)
174{
176 if (m_caching) {
177 clearCache();
178 m_activateIdentifier = arg;
179 } else {
180 const auto docs = helpEngine.documentsForIdentifier(arg);
181 if (!docs.isEmpty())
182 CentralWidget::instance()->setSource(docs.first().url);
183 }
184}
185
186void RemoteControl::handleExpandTocCommand(const QString &arg)
187{
189 bool ok = false;
190 int depth = -2;
191 if (!arg.isEmpty())
192 depth = arg.toInt(&ok);
193 if (!ok || depth < -2)
194 depth = -2;
195
196 if (m_caching)
197 m_expandTOC = depth;
198 else if (depth != -2)
199 m_mainWindow->expandTOC(depth);
200}
201
202void RemoteControl::handleSetCurrentFilterCommand(const QString &arg)
203{
205 if (helpEngine.filterEngine()->filters().contains(arg)) {
206 if (m_caching) {
207 clearCache();
208 m_currentFilter = arg;
209 } else {
210 helpEngine.filterEngine()->setActiveFilter(arg);
211 }
212 }
213}
214
215void RemoteControl::handleRegisterCommand(const QString &arg)
216{
218 const QString &absFileName = QFileInfo(arg).absoluteFilePath();
219 if (helpEngine.registeredDocumentations().
220 contains(QHelpEngineCore::namespaceName(absFileName)))
221 return;
222 if (helpEngine.registerDocumentation(absFileName))
223 helpEngine.setupData();
224}
225
226void RemoteControl::handleUnregisterCommand(const QString &arg)
227{
229 const QString &absFileName = QFileInfo(arg).absoluteFilePath();
230 const QString &ns = QHelpEngineCore::namespaceName(absFileName);
231 if (helpEngine.registeredDocumentations().contains(ns)) {
232 OpenPagesManager::instance()->closePages(ns);
233 if (helpEngine.unregisterDocumentation(ns))
234 helpEngine.setupData();
235 }
236}
237
238void RemoteControl::applyCache()
239{
241 if (m_setSource.isValid()) {
242 CentralWidget::instance()->setSource(m_setSource);
243 } else if (!m_activateKeyword.isEmpty()) {
244 m_mainWindow->setIndexString(m_activateKeyword);
245 helpEngine.indexWidget()->activateCurrentItem();
246 } else if (!m_activateIdentifier.isEmpty()) {
247 const auto docs =
248 helpEngine.documentsForIdentifier(m_activateIdentifier);
249 if (!docs.isEmpty())
250 CentralWidget::instance()->setSource(docs.first().url);
251 } else if (!m_currentFilter.isEmpty()) {
252 helpEngine.filterEngine()->setActiveFilter(m_currentFilter);
253 }
254
255 if (m_syncContents)
256 m_mainWindow->syncContents();
257
258 Q_ASSERT(m_expandTOC >= -2);
259 if (m_expandTOC != -2)
260 m_mainWindow->expandTOC(m_expandTOC);
261
262 m_caching = false;
263}
264
265void RemoteControl::clearCache()
266{
268 m_currentFilter.clear();
269 m_setSource.clear();
270 m_syncContents = false;
271 m_activateKeyword.clear();
272 m_activateIdentifier.clear();
273}
274
275QT_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