YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
modestreemanager.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7#include "modestreemanager.h"
8
9#include <QHeaderView>
10#include <QScrollArea>
11#include <QGroupBox>
12#define TREEMODE_OK 1
13#define TREEMODE_WARN 2
14
15ModesListWidget::ModesListWidget(QWidget *parent) : QTreeWidget(parent)
16{
17 setHeaderLabels({"Parts", "Mode"});
18 setMaximumWidth(300);
19 setFrameShape(Shape::NoFrame);
20 setAnimated(true);
21 header()->setDefaultSectionSize(150);
22}
23
25 : QObject(parent),
26 m_okIcon(":/apply.svg"),
27 m_warningIcon(":/warning.svg")
28{
29 m_tabs = new QTabWidget();
30 m_list = new ModesListWidget();
31 m_tabs->addTab(m_list, "List");
32 m_tabs->setMaximumWidth(m_list->maximumWidth());
33
34 auto* widgetContainer = new QWidget();
35 m_widgetLayout = new QVBoxLayout();
36 m_widgetLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
37 widgetContainer->setLayout(m_widgetLayout);
38 widgetContainer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
39
40 auto* scroll = new QScrollArea();
41 scroll->setWidgetResizable(true);
42 scroll->setWidget(widgetContainer);
43
44 m_tabs->addTab(scroll, "Widgets");
45
46 layout->addWidget(m_tabs);
47
48 connect(m_tabs,SIGNAL(currentChanged(int)),this, SLOT(onTabChanged(int)));
49}
50
51void ModesTreeManager::addRobot(const std::string &robotName)
52{
53 addRobotInList(robotName);
54 addRobotInWidget(robotName);
55}
56
57void ModesTreeManager::addRobotPart(const std::string &robotName, const std::string& partName, int partIndex, PartItem* part)
58{
59 m_indexToPartMap[partIndex].partItem = part;
60 addRobotPartInList(robotName, partName, partIndex, part);
61 addRobotPartInWidget(robotName, partName, partIndex, part);
62}
63
65{
66 const QVector<JointItem::JointState>& modes = m_indexToPartMap[index].partItem->getPartModes();
67 updateRobotPartInList(index, modes);
68 updateRobotPartInWidget(index, modes);
69}
70
71void ModesTreeManager::onTabChanged(int index)
72{
73 m_tabs->setMaximumWidth(m_tabs->widget(index)->maximumWidth());
74}
75
76void ModesTreeManager::onJointClicked(int partIndex, int jointIndex)
77{
78 emit sig_jointClicked(partIndex, jointIndex);
79}
80
81void ModesTreeManager::onJointHomeFromTree(int partIndex, int jointIndex)
82{
83 m_indexToPartMap[partIndex].partItem->getJointWidget(jointIndex)->home();
84}
85
86void ModesTreeManager::onJointRunFromTree(int partIndex, int jointIndex)
87{
88 m_indexToPartMap[partIndex].partItem->getJointWidget(jointIndex)->run();
89}
90
91void ModesTreeManager::onJointIdleFromTree(int partIndex, int jointIndex)
92{
93 m_indexToPartMap[partIndex].partItem->getJointWidget(jointIndex)->idle();
94}
95
96void ModesTreeManager::onJointPIDFromTree(int partIndex, int jointIndex)
97{
98 m_indexToPartMap[partIndex].partItem->getJointWidget(jointIndex)->showPID();
99}
100
101void ModesTreeManager::onPartDoubleClicked(int partIndex)
102{
104}
105
106void ModesTreeManager::addRobotInList(const std::string &robotName)
107{
108 auto* robot_top = new QTreeWidgetItem();
109 robot_top->setText(0, robotName.c_str());
110 m_list->addTopLevelItem(robot_top);
111 robot_top->setExpanded(true);
112 m_robotMapList[robotName] = robot_top;
113}
114
115void ModesTreeManager::addRobotInWidget(const std::string &robotName)
116{
118 newRobot->setTitle(robotName.c_str());
119 m_widgetLayout->addWidget(newRobot);
120
121 newRobot->enableCollapseAllContextMenu(true);
122 newRobot->setIcons(m_okIcon, m_warningIcon);
123
124 m_robotMapWidget[robotName] = newRobot;
125
126 connect(newRobot, SIGNAL(sig_partDoubleClicked(int)), this, SLOT(onPartDoubleClicked(int)));
127}
128
129void ModesTreeManager::addRobotPartInList(const std::string &robotName, const std::string &partName, int partIndex, PartItem *part)
130{
131 auto* partItem = new QTreeWidgetItem();
132 partItem->setText(0, partName.c_str());
133 QTreeWidgetItem *tp = m_robotMapList[robotName];
134 tp->addChild(partItem);
135 partItem->setExpanded(false);
136 m_indexToPartMap[partIndex].listWidget = partItem;
137
138 for (int i = 0; i < part->getNumberOfJoints(); ++i)
139 {
140 auto* jointNode = new QTreeWidgetItem(partItem);
141 jointNode->setText(0,QString("Joint %1 (%2)").arg(i).arg(part->getJointName(i)));
144 jointNode->setBackground(0,c);
145 jointNode->setBackground(1,c);
146 jointNode->setForeground(0,Qt::black);
147 jointNode->setForeground(1,Qt::black);
148 }
149}
150
151void ModesTreeManager::addRobotPartInWidget(const std::string &robotName, const std::string &partName, int partIndex, PartItem *part)
152{
153 PartItemTree* partWidget = new PartItemTree(partIndex);
154
155 for (int i = 0; i < part->getNumberOfJoints(); ++i)
156 {
157 auto* jointWidget = partWidget->addJoint();
158 jointWidget->setJointName(QString("%1 - %2").arg(i).arg(part->getJointName(i)));
159 }
160
161 RobotWidgetTree* robotWidget = m_robotMapWidget[robotName];
162
163 robotWidget->addPart(partName, partIndex, partWidget);
164
165 connect(partWidget, SIGNAL(sig_jointClicked(int,int)), this, SLOT(onJointClicked(int,int)));
166 connect(partWidget, SIGNAL(sig_homeClicked(int,int)), this, SLOT(onJointHomeFromTree(int,int)));
167 connect(partWidget, SIGNAL(sig_runClicked(int,int)), this, SLOT(onJointRunFromTree(int,int)));
168 connect(partWidget, SIGNAL(sig_idleClicked(int,int)), this, SLOT(onJointIdleFromTree(int,int)));
169 connect(partWidget, SIGNAL(sig_PIDClicked(int,int)), this, SLOT(onJointPIDFromTree(int,int)));
170
171 m_indexToPartMap[partIndex].robotWidget = robotWidget;
172}
173
174void ModesTreeManager::updateRobotPartInList(int index, const QVector<JointItem::JointState> &modes)
175{
176 QTreeWidgetItem *parentNode = m_indexToPartMap[index].listWidget;
177
178 if (modes.size() != parentNode->childCount())
179 {
180 return;
181 }
182
183 bool foundFaultPart = false;
184 for(int i = 0; i < parentNode->childCount(); i++){
185 QTreeWidgetItem *item = parentNode->child(i);
186 QString mode;
188 mode = JointItem::GetModeString(modes.at(i));
189
190 if(modes.at(i) == JointItem::HwFault){
191 foundFaultPart = true;
192 if(item->data(0,Qt::UserRole).toInt() != TREEMODE_WARN){
193 item->setIcon(0,m_warningIcon);
194 item->setData(0,Qt::UserRole,TREEMODE_WARN);
195 }
196 }else{
197 item->setIcon(0,QIcon());
198 item->setData(0,Qt::UserRole,TREEMODE_OK);
199 }
200
201 if(parentNode->isExpanded()){
202 if(item->text(1) != mode){
203 item->setText(1,mode);
204 }
205 if(item->background(0) != c){
206 item->setBackground(0, c);
207 item->setBackground(1, c);
208 item->setForeground(0, Qt::black);
209 item->setForeground(1, Qt::black);
210 }
211 }
212 }
213
214 if(!foundFaultPart){
215 if(parentNode->data(0,Qt::UserRole).toInt() != TREEMODE_OK){
216 parentNode->setBackground(0,Qt::white);
217 parentNode->setIcon(0, m_okIcon);
218 parentNode->setData(0,Qt::UserRole,TREEMODE_OK);
219 }
220 }else{
221 if(parentNode->data(0,Qt::UserRole).toInt() != TREEMODE_WARN){
222 parentNode->setBackground(0,hwFaultColor);
223 parentNode->setIcon(0, m_warningIcon);
224 parentNode->setData(0,Qt::UserRole,TREEMODE_WARN);
225 }
226 }
227}
228
229void ModesTreeManager::updateRobotPartInWidget(int index, const QVector<JointItem::JointState> &modes)
230{
231 m_indexToPartMap[index].robotWidget->updateRobotPart(index, modes);
232}
int SIGNAL(int pid, int signum)
static QColor GetModeColor(JointState mode)
Definition jointitem.cpp:57
static QString GetModeString(JointState mode)
ModesListWidget(QWidget *parent=nullptr)
void sig_jointClicked(int partIndex, int jointIndex)
void updateRobotPart(int index)
void addRobotPart(const std::string &robotName, const std::string &partName, int partIndex, PartItem *part)
void addRobot(const std::string &robotName)
void sig_partDoubleClicked(int index)
ModesTreeManager(QHBoxLayout *layout, QWidget *parent=nullptr)
JointItemTree * addJoint()
int getNumberOfJoints()
QString getJointName(int joint)
void addPart(const std::string &partName, int partIndex, PartItemTree *partTreeWidget)
A mini-server for performing network communication in the background.
#define TREEMODE_WARN
#define TREEMODE_OK
const QColor hwFaultColor