swift
commandinput.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
9 #include "gui/guiapplication.h"
11 
12 using namespace swift::misc;
13 using namespace swift::misc::network;
14 using namespace swift::misc::simulation;
15 using namespace swift::core::context;
16 
17 namespace swift::gui::components
18 {
19  CCommandInput::CCommandInput(QWidget *parent) : CLineEditHistory(parent), CIdentifiable(this)
20  {
21  if (!CSimpleCommandParser::registered("swift::gui::components::CCommandInput"))
22  {
23  CSimpleCommandParser::registerCommand({ ".tooltip", "toggle dot command tooltip" });
24  CSimpleCommandParser::registerCommand({ ".help", "show help" });
25  }
26 
27  if (this->placeholderText().isEmpty()) { this->setPlaceholderText(".dot commands"); }
28 
29  const QPointer<CCommandInput> myself(this);
30  QTimer::singleShot(5000, this, [=] {
31  if (!myself) { return; }
32  m_dsCommandTooltip.inputSignal();
33  });
34 
35  if (sGui && sGui->supportsContexts())
36  {
38  {
39  connect(sGui->getIContextSimulator(), &IContextSimulator::simulatorPluginChanged, this,
40  &CCommandInput::onSimulatorPluginChanged, Qt::QueuedConnection);
41  }
42  if (sGui->getIContextNetwork())
43  {
44  connect(sGui->getIContextNetwork(), &IContextNetwork::connectedServerChanged, this,
45  &CCommandInput::onConnectedServerChanged, Qt::QueuedConnection);
46  }
47  }
48  connect(this, &CCommandInput::returnPressedUnemptyLine, this, &CCommandInput::validateCommand,
49  Qt::QueuedConnection);
50  }
51 
53  {
54  m_showToolTip = show;
55  this->setCommandToolTip();
56  }
57 
58  void CCommandInput::validateCommand()
59  {
60  const QString c(this->getLastEnteredLineFormatted());
61  if (c.isEmpty()) { return; }
62  if (c.startsWith('.'))
63  {
64  if (c.contains("help", Qt::CaseInsensitive))
65  {
66  this->setCommandToolTip();
67  return;
68  }
69  if (c.contains("tooltip", Qt::CaseInsensitive))
70  {
71  this->showToolTip(!m_showToolTip);
72  return;
73  }
74  emit this->commandEntered(c, this->identifier());
75  }
76  else { emit this->textEntered(c, this->identifier()); }
77  }
78 
79  void CCommandInput::setCommandToolTip()
80  {
81  if (m_showToolTip) { this->setToolTip(CSimpleCommandParser::commandsHtmlHelp()); }
82  else { this->setToolTip(""); }
83  }
84 
85  void CCommandInput::onSimulatorPluginChanged(const CSimulatorPluginInfo &info)
86  {
87  Q_UNUSED(info)
88 
89  // different simulators have different commands
90  m_dsCommandTooltip.inputSignal();
91  }
92 
93  void CCommandInput::onConnectedServerChanged(const network::CServer &server)
94  {
95  Q_UNUSED(server)
96 
97  // commands of network
98  m_dsCommandTooltip.inputSignal();
99  }
100 } // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
const context::IContextSimulator * getIContextSimulator() const
Direct access to contexts if a CCoreFacade has been initialized.
bool supportsContexts(bool ignoreShutdownTest=false) const
Supports contexts.
Line edit with history.
QString getLastEnteredLineFormatted() const
Get the last entered line but simplified and trimmed.
void returnPressedUnemptyLine()
Return has been pressed, line is NOT empty (spaces are trimmed)
void commandEntered(const QString &command, const swift::misc::CIdentifier &originator)
Command was entered.
void textEntered(const QString &command, const swift::misc::CIdentifier &originator)
Text entered (which is not a command)
void showToolTip(bool show)
Show tooltip.
void inputSignal()
Received input signal, or manually trigger.
Base class with a member CIdentifier to be inherited by a class which has an identity in the environm...
Definition: identifiable.h:24
const CIdentifier & identifier() const
Get identifier.
Definition: identifiable.h:27
Value object encapsulating information of a server.
Definition: server.h:28
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition: threadutils.h:30