82 QWidget *w = ConnectionEdit::widgetAt(pos);
84 while (w !=
nullptr && !m_formWindow->isManaged(w))
85 w = w->parentWidget();
89 if (state() == Editing) {
90 QLabel *label = qobject_cast<QLabel*>(w);
93 const int cnt = connectionCount();
94 for (
int i = 0; i < cnt; ++i) {
95 Connection *con = connection(i);
96 if (con->widget(EndPoint::Source) == w)
100 if (!canBeBuddy(w, m_formWindow))
119 if (m_updating || background() ==
nullptr)
121 ConnectionEdit::updateBackground();
124 QList<Connection *> newList;
125 const auto label_list = background()->findChildren<QLabel*>();
126 for (QLabel *label : label_list) {
127 const QString buddy_name = buddy(label, m_formWindow->core());
128 if (buddy_name.isEmpty())
131 const QWidgetList targets = background()->findChildren<QWidget*>(buddy_name);
132 if (targets.isEmpty())
135 const auto wit = std::find_if(targets.cbegin(), targets.cend(),
136 [] (
const QWidget *w) {
return !w->isHidden(); });
137 if (wit == targets.cend())
140 Connection *con =
new Connection(
this);
141 con->setEndPoint(EndPoint::Source, label, widgetRect(label).center());
142 con->setEndPoint(EndPoint::Target, *wit, widgetRect(*wit).center());
146 QList<Connection *> toRemove;
148 const int c = connectionCount();
149 for (
int i = 0; i < c; i++) {
150 Connection *con = connection(i);
151 QObject *source = con->object(EndPoint::Source);
152 QObject *target = con->object(EndPoint::Target);
154 std::any_of(newList.cbegin(), newList.cend(),
155 [source, target] (
const Connection *nc)
156 {
return nc->object(EndPoint::Source) == source && nc->object(EndPoint::Target) == target; });
158 toRemove.append(con);
160 if (!toRemove.isEmpty()) {
163 for (Connection *con : std::as_const(toRemove))
164 delete takeConnection(con);
167 for (Connection *newConn : std::as_const(newList)) {
169 const int c = connectionCount();
170 for (
int i = 0; i < c; i++) {
171 Connection *con = connection(i);
172 if (con->object(EndPoint::Source) == newConn->object(EndPoint::Source) &&
173 con->object(EndPoint::Target) == newConn->object(EndPoint::Target)) {
181 AddConnectionCommand command(
this, newConn);
191 ConnectionEdit::setBackground(background);
192 if (background ==
nullptr)
195 const auto label_list = background->findChildren<QLabel*>();
196 for (QLabel *label : label_list) {
197 const QString buddy_name = buddy(label, m_formWindow->core());
198 if (buddy_name.isEmpty())
200 QWidget *target = background->findChild<QWidget*>(buddy_name);
201 if (target ==
nullptr)
204 Connection *con =
new Connection(
this);
205 con->setEndPoint(EndPoint::Source, label, widgetRect(label).center());
206 con->setEndPoint(EndPoint::Target, target, widgetRect(target).center());
213 SetPropertyCommand *command =
new SetPropertyCommand(fw);
214 command->init(label, buddyPropertyC, buddy->objectName());
215 command->setText(BuddyEditor::tr(
"Add buddy"));
221 Connection *tmp_con = newlyAddedConnection();
222 Q_ASSERT(tmp_con !=
nullptr);
224 tmp_con->setEndPoint(EndPoint::Target, target, pos);
226 QWidget *source = tmp_con->widget(EndPoint::Source);
227 Q_ASSERT(source !=
nullptr);
228 Q_ASSERT(target !=
nullptr);
230 Connection *new_con = createConnection(source, target);
232 if (new_con !=
nullptr) {
233 new_con->setEndPoint(EndPoint::Source, source, tmp_con->endPointPos(EndPoint::Source));
234 new_con->setEndPoint(EndPoint::Target, target, tmp_con->endPointPos(EndPoint::Target));
237 addConnection(new_con);
238 QLabel *source = qobject_cast<QLabel*>(new_con->widget(EndPoint::Source));
239 QWidget *target = new_con->widget(EndPoint::Target);
241 undoStack()->push(createBuddyCommand(m_formWindow, source, target));
243 qDebug(
"BuddyEditor::endConnection(): not a label");
245 setSelected(new_con,
true);
248 clearNewlyAddedConnection();
249 findObjectsUnderMouse(mapFromGlobal(QCursor::pos()));
254 QWidgetList child_list = widget->findChildren<QWidget*>();
255 child_list.prepend(widget);
257 ConnectionSet remove_set;
258 for (QWidget *w : std::as_const(child_list)) {
259 const ConnectionList &cl = connectionList();
260 for (Connection *con : cl) {
261 if (con->widget(EndPoint::Source) == w || con->widget(EndPoint::Target) == w)
262 remove_set.insert(con, con);
266 if (!remove_set.isEmpty()) {
267 undoStack()->beginMacro(tr(
"Remove buddies"));
268 for (Connection *con : std::as_const(remove_set)) {
269 setSelected(con,
false);
271 QWidget *source = con->widget(EndPoint::Source);
272 if (qobject_cast<QLabel*>(source) == 0) {
273 qDebug(
"BuddyConnection::widgetRemoved(): not a label");
275 ResetPropertyCommand *command =
new ResetPropertyCommand(formWindow());
276 command->init(source, buddyPropertyC);
277 undoStack()->push(command);
279 delete takeConnection(con);
281 undoStack()->endMacro();
287 const ConnectionSet selectedConnections = selection();
288 if (selectedConnections.isEmpty())
291 undoStack()->beginMacro(tr(
"Remove %n buddies",
nullptr, selectedConnections.size()));
292 for (Connection *con : selectedConnections) {
293 setSelected(con,
false);
295 QWidget *source = con->widget(EndPoint::Source);
296 if (qobject_cast<QLabel*>(source) == 0) {
297 qDebug(
"BuddyConnection::deleteSelected(): not a label");
299 ResetPropertyCommand *command =
new ResetPropertyCommand(formWindow());
300 command->init(source, buddyPropertyC);
301 undoStack()->push(command);
303 delete takeConnection(con);
305 undoStack()->endMacro();
311 auto labelList = background()->findChildren<QLabel*>();
312 if (labelList.isEmpty())
315 QWidgetList usedBuddies;
316 const ConnectionList &beforeConnections = connectionList();
317 for (
const Connection *c : beforeConnections)
318 usedBuddies.push_back(c->widget(EndPoint::Target));
321 for (
auto it = labelList.begin(); it != labelList.end(); ) {
324 if (m_formWindow->isManaged(label)) {
325 const QString buddy_name = buddy(label, m_formWindow->core());
326 if (buddy_name.isEmpty())
327 newBuddy = findBuddy(label, usedBuddies);
330 buddies.push_back(newBuddy);
331 usedBuddies.push_back(newBuddy);
334 it = labelList.erase(it);
338 if (labelList.isEmpty())
340 const auto count = labelList.size();
341 Q_ASSERT(count == buddies.size());
342 undoStack()->beginMacro(tr(
"Add %n buddies",
nullptr, count));
343 for (qsizetype i = 0; i < count; ++i)
344 undoStack()->push(createBuddyCommand(m_formWindow, labelList.at(i), buddies.at(i)));
345 undoStack()->endMacro();
347 const ConnectionList &connections = connectionList();
348 for (Connection *con : connections)
349 setSelected(con, buddies.contains(con->widget(EndPoint::Target)));