LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
CliCommand Class Reference

Command line widget. More...

#include <clicommand.h>

Inheritance diagram for CliCommand:
Collaboration diagram for CliCommand:

Public Slots

void onReturnPressed ()
 Parse entered text. Check if the text is a number, a coordinate or a command and emit the right signal. This is a slot to allow getting key press events from other widgets. More...
 
void onKeyPressed (QKeyEvent *event)
 Process key events. Browse history if up or down key is pressed. This is a slot to allow getting key press events from other widgets. More...
 

Signals

void commandEntered (QString command)
 
void coordinateEntered (lc::geo::Coordinate coordinate)
 
void relativeCoordinateEntered (lc::geo::Coordinate coordinate)
 
void numberEntered (double number)
 
void textEntered (QString text)
 

Public Member Functions

 CliCommand (QWidget *parent=0)
 Create widget. More...
 
 ~CliCommand ()
 
void keyPressEvent (QKeyEvent *event)
 
bool addCommand (std::string name)
 Add a new command. More...
 
void write (QString message)
 Write a message in the logs. More...
 
void setText (QString text)
 Write text in input. More...
 
void returnText (bool returnText)
 Return raw text to Lua. More...
 

Private Member Functions

bool checkParam (QString command)
 
void enterCommand (QString command)
 
void enterCoordinate (QString coordinate)
 
void enterNumber (double number)
 

Private Attributes

Ui::CliCommand * ui
 
std::shared_ptr< QCompleter > _completer
 
std::shared_ptr< QStringListModel > _commands
 
bool _returnText
 
QStringList _history
 
int _historySize
 
int _historyIndex
 

Detailed Description

Command line widget.

Definition at line 20 of file clicommand.h.

Constructor & Destructor Documentation

CliCommand::CliCommand ( QWidget *  parent = 0)
explicit

Create widget.

Parameters
parentPointer to parent widget

Definition at line 8 of file clicommand.cpp.

8  :
9  QDockWidget(parent),
10  ui(new Ui::CliCommand),
11  _returnText(false),
12  _historySize(10),
13  _historyIndex(-1)
14 {
15  ui->setupUi(this);
16 
17  connect(ui->command, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
18 
19  _commands = std::make_shared<QStringListModel>();
20  _completer = std::make_shared<QCompleter>();
21 
22  _completer->setCaseSensitivity(Qt::CaseInsensitive);
23  _completer->setCompletionMode(QCompleter::InlineCompletion);
24  _completer->setModel(_commands.get());
25 
26  ui->command->setCompleter(_completer.get());
27 }
std::shared_ptr< QCompleter > _completer
Definition: clicommand.h:87
int _historyIndex
Definition: clicommand.h:93
std::shared_ptr< QStringListModel > _commands
Definition: clicommand.h:88
int _historySize
Definition: clicommand.h:92
Ui::CliCommand * ui
Definition: clicommand.h:86
void onReturnPressed()
Parse entered text. Check if the text is a number, a coordinate or a command and emit the right signa...
Definition: clicommand.cpp:52
bool _returnText
Definition: clicommand.h:89
CliCommand::~CliCommand ( )

Definition at line 29 of file clicommand.cpp.

29  {
30  delete ui;
31 }
Ui::CliCommand * ui
Definition: clicommand.h:86

Member Function Documentation

bool CliCommand::addCommand ( std::string  name)

Add a new command.

Definition at line 33 of file clicommand.cpp.

33  {
34  if(_commands->stringList().indexOf(name.c_str()) == -1) {
35  auto newList = _commands->stringList();
36  newList << QString(name.c_str());
37  _commands->setStringList(newList);
38  return true;
39  }
40  else {
41  return false;
42  }
43 }
std::shared_ptr< QStringListModel > _commands
Definition: clicommand.h:88
bool CliCommand::checkParam ( QString  command)
private

Definition at line 122 of file clicommand.cpp.

122  {
123  return Settings::exists(command.toStdString());
124 }
void CliCommand::commandEntered ( QString  command)
signal
void CliCommand::coordinateEntered ( lc::geo::Coordinate  coordinate)
signal
void CliCommand::enterCommand ( QString  command)
private

Definition at line 103 of file clicommand.cpp.

103  {
104  auto completion = _completer->currentCompletion();
105 
106  if(command.compare(completion, Qt::CaseInsensitive) == 0) {
107  write("Command: " + completion);
108  emit commandEntered(completion);
109  }
110  else {
111  if(checkParam(command)) {
112  write(QString("Value of %1=%2").arg(command).arg(Settings::val(command.toStdString())));
113  }
114  else
115  {
116  write("Command " + command + " not found");
117  ui->history->item(ui->history->count() - 1)->setForeground(Qt::red);
118  }
119  }
120 }
std::shared_ptr< QCompleter > _completer
Definition: clicommand.h:87
Ui::CliCommand * ui
Definition: clicommand.h:86
void write(QString message)
Write a message in the logs.
Definition: clicommand.cpp:45
bool checkParam(QString command)
Definition: clicommand.cpp:122
void commandEntered(QString command)
void CliCommand::enterCoordinate ( QString  coordinate)
private

Definition at line 126 of file clicommand.cpp.

126  {
127  lc::geo::Coordinate point;
128  QStringList numbers;
129  bool isRelative = false;
130 
131  if(coordinate.indexOf("@") != -1) {
132  isRelative = true;
133  coordinate.remove("@");
134  }
135 
136  if(coordinate.indexOf(";") != -1) {
137  numbers = coordinate.split(";");
138  }
139  else {
140  numbers = coordinate.split(",");
141  }
142 
143 
144  if(numbers.size() > 2) {
145  point = lc::geo::Coordinate(numbers[0].toFloat(), numbers[1].toFloat(), numbers[2].toFloat());
146  }
147  else {
148  point = lc::geo::Coordinate(numbers[0].toFloat(), numbers[1].toFloat());
149  }
150 
151  auto message = QString("Coordinate: x=%1; y=%2; z=%3").arg(point.x()).arg(point.y()).arg(point.z());
152  write(message);
153 
154  if(isRelative) {
155  emit relativeCoordinateEntered(point);
156  }
157  else {
158  emit coordinateEntered(point);
159  }
160 }
void coordinateEntered(lc::geo::Coordinate coordinate)
void write(QString message)
Write a message in the logs.
Definition: clicommand.cpp:45
void relativeCoordinateEntered(lc::geo::Coordinate coordinate)
void CliCommand::enterNumber ( double  number)
private

Definition at line 162 of file clicommand.cpp.

162  {
163  write(QString("Number: %1").arg(number));
164  emit numberEntered(number);
165 }
void write(QString message)
Write a message in the logs.
Definition: clicommand.cpp:45
void numberEntered(double number)
void CliCommand::keyPressEvent ( QKeyEvent *  event)

Definition at line 99 of file clicommand.cpp.

99  {
100  onKeyPressed(event);
101 }
void onKeyPressed(QKeyEvent *event)
Process key events. Browse history if up or down key is pressed. This is a slot to allow getting key ...
Definition: clicommand.cpp:167
void CliCommand::numberEntered ( double  number)
signal
void CliCommand::onKeyPressed ( QKeyEvent *  event)
slot

Process key events. Browse history if up or down key is pressed. This is a slot to allow getting key press events from other widgets.

Definition at line 167 of file clicommand.cpp.

167  {
168  switch(event->key()) {
169  case Qt::Key_Up:
170 
171  if(_historyIndex + 1 < _history.size()) {
172  _historyIndex++;
173  ui->command->setText(_history[_historyIndex]);
174  }
175  break;
176 
177  case Qt::Key_Down:
178  if(_historyIndex > 0) {
179  _historyIndex--;
180  ui->command->setText(_history[_historyIndex]);
181  }
182  else {
183  _historyIndex = -1;
184  ui->command->clear();
185  }
186  break;
187 
188  default:
189  ui->command->event(event);
190  break;
191  }
192 }
int _historyIndex
Definition: clicommand.h:93
Ui::CliCommand * ui
Definition: clicommand.h:86
QStringList _history
Definition: clicommand.h:91
void CliCommand::onReturnPressed ( )
slot

Parse entered text. Check if the text is a number, a coordinate or a command and emit the right signal. This is a slot to allow getting key press events from other widgets.

Definition at line 52 of file clicommand.cpp.

52  {
53  auto text = ui->command->text();
54  bool isNumber;
55  QStringList varFind;
56  QRegularExpression re("^([a-zA-Z]{1,10}+)=([0-9]{1,50}.[0-9]{1,50})|([0-9]{1,50})$");
57  QRegularExpressionMatch match = re.match(text, 0, QRegularExpression::PartialPreferCompleteMatch);
58  bool hasMatch = match.hasMatch();
59 
60  if(_returnText) {
61  emit textEntered(text);
62  }
63  else if(text != "") {
64  _history.push_front(text);
65 
66  if (_history.size() > _historySize) {
67  _history.pop_back();
68  }
69 
70  auto number = text.toDouble(&isNumber);
71  if (isNumber) {
72  enterNumber(number);
73  }
74  else if (text.indexOf(";") != -1 || text.indexOf(",") != -1) {
75  enterCoordinate(text);
76  }
77  else if(hasMatch) {
78  varFind = text.split("=");
79  Settings::inst = Settings::instance();
80  std::unordered_map<std::string, double>::iterator it1;
81 
82  if(checkParam(varFind[0])) {
83  write(QString("Value of %1 = %2").arg(varFind[0]).arg(varFind[1].toFloat()));
84  Settings::setVal(varFind[0].toStdString(),varFind[1].toFloat());
85  }
86  else {
87  write(QString("No such variable."));
88  }
89  }
90  else {
91  enterCommand(text);
92  }
93  }
94 
95  _historyIndex = -1;
96  ui->command->clear();
97 }
void enterCoordinate(QString coordinate)
Definition: clicommand.cpp:126
void textEntered(QString text)
int _historyIndex
Definition: clicommand.h:93
int _historySize
Definition: clicommand.h:92
Ui::CliCommand * ui
Definition: clicommand.h:86
void write(QString message)
Write a message in the logs.
Definition: clicommand.cpp:45
QStringList _history
Definition: clicommand.h:91
bool checkParam(QString command)
Definition: clicommand.cpp:122
void enterCommand(QString command)
Definition: clicommand.cpp:103
bool _returnText
Definition: clicommand.h:89
void enterNumber(double number)
Definition: clicommand.cpp:162
void CliCommand::relativeCoordinateEntered ( lc::geo::Coordinate  coordinate)
signal
void CliCommand::returnText ( bool  returnText)

Return raw text to Lua.

Parameters
returnTexttrue to return raw text, false to parse text Disables text parsing.

Definition at line 198 of file clicommand.cpp.

198  {
200 }
void returnText(bool returnText)
Return raw text to Lua.
Definition: clicommand.cpp:198
bool _returnText
Definition: clicommand.h:89
void CliCommand::setText ( QString  text)

Write text in input.

Parameters
textQString It's only used in unit tests for the moment. Maybe that can be moved to a new test class.

Definition at line 194 of file clicommand.cpp.

194  {
195  ui->command->setText(text);
196 }
Ui::CliCommand * ui
Definition: clicommand.h:86
void CliCommand::textEntered ( QString  text)
signal
void CliCommand::write ( QString  message)

Write a message in the logs.

Parameters
messageQString

Definition at line 45 of file clicommand.cpp.

45  {
46  ui->history->addItem(message);
47  if(ui->history->count() > _historySize) {
48  delete ui->history->takeItem(0);
49  }
50 }
int _historySize
Definition: clicommand.h:92
Ui::CliCommand * ui
Definition: clicommand.h:86

Member Data Documentation

std::shared_ptr<QStringListModel> CliCommand::_commands
private

Definition at line 88 of file clicommand.h.

std::shared_ptr<QCompleter> CliCommand::_completer
private

Definition at line 87 of file clicommand.h.

QStringList CliCommand::_history
private

Definition at line 91 of file clicommand.h.

int CliCommand::_historyIndex
private

Definition at line 93 of file clicommand.h.

int CliCommand::_historySize
private

Definition at line 92 of file clicommand.h.

bool CliCommand::_returnText
private

Definition at line 89 of file clicommand.h.

Ui::CliCommand* CliCommand::ui
private

Definition at line 86 of file clicommand.h.


The documentation for this class was generated from the following files: