Skip to content

Commit 1f3c749

Browse files
author
Thomas Witte
committed
added highlighting when clicking an interactive marker
1 parent 76483e8 commit 1f3c749

8 files changed

Lines changed: 112 additions & 34 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ find_package(catkin REQUIRED COMPONENTS
1717
hector_uav_msgs
1818
tf2
1919
tf2_ros
20+
tf2_geometry_msgs
2021
)
2122

2223
## System dependencies are found with CMake's conventions

include/interactive_script/builtins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct SignalObject : QObject {
3232
void applySourceChanges(SourceChangeMessage, QTextCharFormat);
3333
void clearTerminal();
3434
void appendTerminal(QString);
35-
void highlightTokens(TokenMessage);
35+
void highlightTokens(TokenMessage, QTextCharFormat);
3636
void removeFormatting();
3737
void setBlockValue(QString, QString, QString);
3838
};

include/interactive_script/interactive_script_plugin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class InteractiveScriptGui
3939
// void triggerConfiguration();
4040
Ui::InteractiveScriptWidget ui_;
4141
private:
42-
bool setting_print_performance_statistics = true;
42+
bool setting_print_performance_statistics = false;
4343

4444
QWidget* widget_ = nullptr;
4545

@@ -51,7 +51,7 @@ class InteractiveScriptGui
5151
public slots:
5252
void onChangeEditorText(QString);
5353
void onApplySourceChanges(SourceChangeMessage msg, QTextCharFormat fmt);
54-
void onHighlightTokens(TokenMessage);
54+
void onHighlightTokens(TokenMessage, QTextCharFormat);
5555
void onRemoveFormatting();
5656
void onClearTerminal();
5757
void onAppendTerminal(QString);

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<depend>MiniLua</depend>
6060
<depend>tf2</depend>
6161
<depend>tf2_ros</depend>
62+
<depend>tf2_geometry_msgs</depend>
6263

6364
<!-- The export tag contains other, unspecified, tags -->
6465
<export>

src/interactive_script_plugin/blocklywidget.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#include "interactive_script/blocklywidget.h"
2+
#include <ros/package.h>
23

34
BlocklyWidget::BlocklyWidget(QWidget *parent) : QWebEngineView(parent)
45
{
5-
load(QUrl("file:///home/thomas/catkin_ws/src/blockly/index.html"));
6+
QString path = "file://"
7+
+ QString::fromStdString(ros::package::getPath("interactive_script_blockly"))
8+
+ "/../quadcopter/index.html";
9+
10+
std::cout << "Loading blockly URL: " << path.toStdString() << std::endl;
11+
12+
load(QUrl(path));
613

714
bridge = new BlocklyBridge();
815
channel = new QWebChannel(this);

src/interactive_script_plugin/builtins.cpp

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ std::pair<Interpreter::ExecResult, eval_result_t> Interpreter::dostring(const st
3030
}
3131
}
3232

33+
QTextCharFormat format(Qt::GlobalColor bg_color, Qt::GlobalColor fg_color = Qt::white) {
34+
QTextCharFormat fmt;
35+
fmt.setBackground(bg_color);
36+
fmt.setForeground(fg_color);
37+
return fmt;
38+
}
39+
3340
void VisualizationInterpreter::run_script(std::string& script) {
3441
if (is_running.exchange(true)) { // avoid running scripts in parallel
3542
return;
@@ -114,7 +121,27 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
114121
args[2].source.get(),
115122
args[3].source.get(),
116123
[x_source = args[0].source, y_source = args[1].source, z_source = args[2].source, psi_source = args[3].source, this, tokens = parser.tokens, original_psi = get<double>(args[3])](const auto& feedback) mutable {
117-
if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP) {
124+
if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::BUTTON_CLICK) {
125+
vector<LuaToken> tokens;
126+
signal.removeFormatting();
127+
128+
if(x_source) {
129+
signal.highlightTokens(x_source->get_all_tokens(), format(Qt::red));
130+
}
131+
132+
if(y_source) {
133+
signal.highlightTokens(y_source->get_all_tokens(), format(Qt::darkGreen));
134+
}
135+
136+
if(z_source) {
137+
signal.highlightTokens(z_source->get_all_tokens(), format(Qt::blue));
138+
}
139+
140+
if(psi_source) {
141+
signal.highlightTokens(psi_source->get_all_tokens(), format(Qt::darkCyan));
142+
}
143+
144+
} else if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP) {
118145
auto changes = make_shared<lua::rt::SourceChangeAnd>();
119146
if (x_source && feedback->control_name == "move_x") {
120147
if (const auto& change = x_source->forceValue(feedback->pose.position.x))
@@ -134,11 +161,13 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
134161
}
135162

136163
// apply and highlight changes
137-
signal.removeFormatting();
138-
QTextCharFormat fmt;
139-
fmt.setBackground(Qt::red);
140-
fmt.setForeground(Qt::white);
141-
signal.applySourceChanges(changes, fmt);
164+
if (feedback->control_name != "clicked") {
165+
signal.removeFormatting();
166+
QTextCharFormat fmt;
167+
fmt.setBackground(Qt::red);
168+
fmt.setForeground(Qt::white);
169+
signal.applySourceChanges(changes, fmt);
170+
}
142171
}
143172
});
144173

@@ -286,6 +315,10 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
286315
return false;
287316
}
288317

318+
vector<LuaToken> get_all_tokens() const override {
319+
return {};
320+
}
321+
289322
SignalObject& signal;
290323
string id;
291324
string field;
@@ -309,7 +342,23 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
309342
marker.addPoint(get<double>(args[0]), get<double>(args[1]), get<double>(args[2]),
310343
args[0].source.get(), args[1].source.get(), args[2].source.get(),
311344
[x_source = args[0].source, y_source = args[1].source, z_source = args[2].source, this, tokens = parser.tokens](const auto& feedback) mutable {
312-
if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP) {
345+
if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::BUTTON_CLICK) {
346+
vector<LuaToken> tokens;
347+
signal.removeFormatting();
348+
349+
if(x_source) {
350+
signal.highlightTokens(x_source->get_all_tokens(), format(Qt::red));
351+
}
352+
353+
if(y_source) {
354+
signal.highlightTokens(y_source->get_all_tokens(), format(Qt::darkGreen));
355+
}
356+
357+
if(z_source) {
358+
signal.highlightTokens(z_source->get_all_tokens(), format(Qt::blue));
359+
}
360+
361+
} else if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP) {
313362
auto changes = make_shared<lua::rt::SourceChangeAnd>();
314363
if (x_source && feedback->control_name == "move_x") {
315364
if (const auto& change = x_source->forceValue(feedback->pose.position.x))
@@ -325,11 +374,13 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
325374
}
326375

327376
// apply and highlight changes
328-
signal.removeFormatting();
329-
QTextCharFormat fmt;
330-
fmt.setBackground(Qt::red);
331-
fmt.setForeground(Qt::white);
332-
signal.applySourceChanges(changes, fmt);
377+
if (feedback->control_name != "clicked") {
378+
signal.removeFormatting();
379+
QTextCharFormat fmt;
380+
fmt.setBackground(Qt::red);
381+
fmt.setForeground(Qt::white);
382+
signal.applySourceChanges(changes, fmt);
383+
}
333384
}
334385
});
335386
return {};
@@ -355,7 +406,27 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
355406
marker.addPose(get<double>(args[0]), get<double>(args[1]), get<double>(args[2]), get<double>(args[3]),
356407
args[0].source.get(), args[1].source.get(), args[2].source.get(), args[3].source.get(),
357408
[x_source = args[0].source, y_source = args[1].source, z_source = args[2].source, psi_source = args[3].source, this, tokens = parser.tokens, original_psi = get<double>(args[3])](const auto& feedback) mutable {
358-
if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP) {
409+
if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::BUTTON_CLICK) {
410+
vector<LuaToken> tokens;
411+
signal.removeFormatting();
412+
413+
if(x_source) {
414+
signal.highlightTokens(x_source->get_all_tokens(), format(Qt::red));
415+
}
416+
417+
if(y_source) {
418+
signal.highlightTokens(y_source->get_all_tokens(), format(Qt::darkGreen));
419+
}
420+
421+
if(z_source) {
422+
signal.highlightTokens(z_source->get_all_tokens(), format(Qt::blue));
423+
}
424+
425+
if(psi_source) {
426+
signal.highlightTokens(psi_source->get_all_tokens(), format(Qt::darkCyan));
427+
}
428+
429+
} else if (feedback->event_type == visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP) {
359430
auto changes = make_shared<lua::rt::SourceChangeAnd>();
360431
if (x_source && feedback->control_name == "move_x") {
361432
if (const auto& change = x_source->forceValue(feedback->pose.position.x))
@@ -375,11 +446,13 @@ void VisualizationInterpreter::populate_visualization_env(Environment& env, LuaP
375446
}
376447

377448
// apply and highlight changes
378-
signal.removeFormatting();
379-
QTextCharFormat fmt;
380-
fmt.setBackground(Qt::red);
381-
fmt.setForeground(Qt::white);
382-
signal.applySourceChanges(changes, fmt);
449+
if (feedback->control_name != "clicked") {
450+
signal.removeFormatting();
451+
QTextCharFormat fmt;
452+
fmt.setBackground(Qt::red);
453+
fmt.setForeground(Qt::white);
454+
signal.applySourceChanges(changes, fmt);
455+
}
383456
}
384457
});
385458
return {};
@@ -432,7 +505,7 @@ void LiveScriptInterpreter::populate_live_env(lua::rt::Environment &env, const A
432505
return {nil()};
433506
}
434507

435-
signal.highlightTokens(stmt.tokens);
508+
signal.highlightTokens(stmt.tokens, format(Qt::magenta));
436509

437510
std::this_thread::sleep_for(chrono::milliseconds(static_cast<long>(get<double>(args[0])*1000)));
438511

@@ -448,7 +521,7 @@ void LiveScriptInterpreter::populate_live_env(lua::rt::Environment &env, const A
448521
return {nil()};
449522
}
450523

451-
signal.highlightTokens(stmt.tokens);
524+
signal.highlightTokens(stmt.tokens, format(Qt::magenta));
452525

453526
while (!quad.is_at_target()) {
454527
if (*cancelled)

src/interactive_script_plugin/interactive_script_plugin.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,13 @@ void InteractiveScriptGui::removeFormatting() {
123123
cursor.setCharFormat(QTextCharFormat());
124124
}
125125

126-
void InteractiveScriptGui::onHighlightTokens(TokenMessage tokens) {
127-
bool old_eval_paused = eval_paused;
126+
void InteractiveScriptGui::onHighlightTokens(TokenMessage tokens, QTextCharFormat fmt) {
128127
eval_paused = true;
129128

130129
QTextCursor cursor(ui_.editor->document());
131130

132-
// remove any formatting
133-
removeFormatting();
134-
135131
for (const auto& t : tokens) {
136-
//cout << "highlight: " << t << endl;
137-
138-
QTextCharFormat fmt;
139-
fmt.setBackground(Qt::red);
140-
fmt.setForeground(Qt::white);
132+
cout << "highlight: " << t << endl;
141133

142134
cursor.setPosition(t.pos, QTextCursor::MoveAnchor);
143135
cursor.setPosition(t.pos+t.length, QTextCursor::KeepAnchor);

src/interactive_script_plugin/marker_interface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void MarkerInterface::addPoint(double x, double y, double z,
4545
visualization_msgs::InteractiveMarkerControl box_control;
4646
box_control.always_visible = true;
4747
box_control.markers.push_back( box_marker );
48+
box_control.name = "clicked";
49+
box_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
4850

4951
// add the control to the interactive marker
5052
int_marker.controls.push_back( box_control );
@@ -217,6 +219,8 @@ void MarkerInterface::addPose(double x, double y, double z, double psi,
217219
box_control.always_visible = true;
218220
box_control.markers.push_back( box_marker );
219221
box_control.markers.push_back( arrow_marker );
222+
box_control.name = "clicked";
223+
box_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
220224
//box_control.orientation = geometry_msgs::quaternion(0,0,psi);
221225

222226
// add the control to the interactive marker

0 commit comments

Comments
 (0)