hivemind 1.0.0
Loading...
Searching...
No Matches
agent_controls.cpp
Go to the documentation of this file.
2
3#include <QColorDialog>
4#include <QLabel>
5
6namespace Gui
7{
9 : QFrame(parent), m_Layout{ new QGridLayout(this) },
10 m_ActiveAgentComboBox{ new QComboBox(this) },
11 m_ActiveAgentColorBox{ new ColorBox(this) },
12 m_NewAgentButton{ new QPushButton(this) }, m_ActiveAgentIndex{}
13 {
14 setObjectName("AgentControls");
15
16 setFrameStyle(QFrame::Panel | QFrame::Raised);
17
18 QLabel* heading{ new QLabel(this) };
19 heading->setText("Agent Controls");
20 m_Layout->addWidget(heading, 0, 0, 1, 3, Qt::AlignHCenter);
21
22 QFrame* hLine{ new QFrame(this) };
23 hLine->setFrameStyle(QFrame::HLine | QFrame::Sunken);
24 m_Layout->addWidget(hLine, 1, 0, 1, 3);
25
26 QFrame* activeAgentFrame{ new QFrame(this) };
27 activeAgentFrame->setFrameStyle(QFrame::Panel | QFrame::Raised);
28 QGridLayout* activeAgentFrameLayout{ new QGridLayout(
29 activeAgentFrame) };
30 QLabel* activeAgentHeading{ new QLabel(activeAgentFrame) };
31 activeAgentHeading->setText("Active agent");
32 activeAgentFrameLayout->addWidget(activeAgentHeading, 0, 0, 1, 3,
33 Qt::AlignLeft);
34 m_ActiveAgentComboBox->setCursor(Qt::PointingHandCursor);
35 activeAgentFrameLayout->addWidget(m_ActiveAgentComboBox, 1, 0, 1, 2);
36 activeAgentFrameLayout->addWidget(m_ActiveAgentColorBox, 1, 2, 1, 1);
37 m_Layout->addWidget(activeAgentFrame, 2, 0, 1, 3);
38
39 m_NewAgentButton->setText("New agent");
40 m_NewAgentButton->setCursor(Qt::PointingHandCursor);
41 m_Layout->addWidget(m_NewAgentButton, 3, 0, 1, 3);
42
43 connect(m_ActiveAgentComboBox, SIGNAL(currentIndexChanged(int)), this,
44 SLOT(SetActiveAgentIndex(int)));
45
46 connect(m_NewAgentButton, &QPushButton::clicked,
47 [this]() { emit AddAgent(); });
48
49 connect(m_ActiveAgentColorBox, SIGNAL(ColorUpdated(QColor)), this,
50 SLOT(SetAgentColor(QColor)));
51 }
52
53 void
55 {
56 auto agent =
57 std::find_if(m_Agents.first, m_Agents.second,
58 [&](const Core::Agent& agent) { return agent.Id == m_ActiveAgentIndex; });
59 if (agent != m_Agents.second) {
60 agent->Color = color.name().toStdString();
61 }
62
64 }
65
66 void
68 std::pair<std::vector<Core::Agent>::iterator, std::vector<Core::Agent>::iterator>
69 agents)
70 {
71 m_Agents = agents;
72 m_ActiveAgentComboBox->clear();
73 for (auto iter{ agents.first }; iter != agents.second; ++iter) {
74 QString newAgentText = "Agent " + QString::number(iter->Id);
75 m_ActiveAgentComboBox->blockSignals(true);
76 m_ActiveAgentComboBox->insertItem(iter->Id, newAgentText);
77 m_ActiveAgentComboBox->setCurrentIndex(iter->Id);
78 m_ActiveAgentComboBox->blockSignals(false);
79 }
81
82 m_ActiveAgentColorBox->update();
85 }
86
87 void
89 {
90 if (index == -1) {
91 return;
92 }
93
94 m_ActiveAgentIndex = index;
95 auto agent =
96 std::find_if(m_Agents.first, m_Agents.second,
97 [&](const Core::Agent& agent) { return agent.Id == index; });
98 if (agent != m_Agents.second) {
100 QColor(QString::fromStdString(agent->Color)));
101 }
102
103 m_ActiveAgentColorBox->update();
105 }
106
107 void
109 {
110 auto agent =
111 std::find_if(m_Agents.first, m_Agents.second,
112 [&](const Core::Agent& agent) { return agent.Id == m_ActiveAgentIndex; });
113 if (agent != m_Agents.second) {
115 QColor(QString::fromStdString(agent->Color)));
116 }
117
118 m_ActiveAgentColorBox->update();
119 }
120
121} // namespace Gui
QComboBox * m_ActiveAgentComboBox
QPushButton * m_NewAgentButton
AgentControls(QWidget *parent=nullptr)
void UpdateAgents(std::pair< std::vector< Core::Agent >::iterator, std::vector< Core::Agent >::iterator >)
void ActiveAgentChanged(int)
void SetAgentColor(QColor color)
void AgentChanged(std::pair< std::vector< Core::Agent >::iterator, std::vector< Core::Agent >::iterator >)
std::pair< std::vector< Core::Agent >::iterator, std::vector< Core::Agent >::iterator > m_Agents
QGridLayout * m_Layout
ColorBox * m_ActiveAgentColorBox
void SetActiveAgentIndex(int index)
void UpdateColor(QColor color)
Definition: color_box.cpp:45
Definition: action.h:6
std::string Color
Definition: types.h:94