83 QWidget *w = ConnectionEdit::widgetAt(pos);
85 while (w !=
nullptr && !m_formWindow->isManaged(w))
86 w = w->parentWidget();
90 if (state() == Editing) {
91 QLabel *label = qobject_cast<QLabel*>(w);
94 const int cnt = connectionCount();
95 for (
int i = 0; i < cnt; ++i) {
96 Connection *con = connection(i);
97 if (con->widget(EndPoint::Source) == w)
101 if (!canBeBuddy(w, m_formWindow))
120 if (m_updating || background() ==
nullptr)
122 ConnectionEdit::updateBackground();
125 QList<Connection *> newList;
126 const auto label_list = background()->findChildren<QLabel*>();
127 for (QLabel *label : label_list) {
128 const QString buddy_name = buddy(label, m_formWindow->core());
129 if (buddy_name.isEmpty())
132 const QWidgetList targets = background()->findChildren<QWidget*>(buddy_name);
133 if (targets.isEmpty())
136 const auto wit = std::find_if(targets.cbegin(), targets.cend(),
137 [] (
const QWidget *w) {
return !w->isHidden(); });
138 if (wit == targets.cend())
141 Connection *con =
new Connection(
this);
142 con->setEndPoint(EndPoint::Source, label, widgetRect(label).center());
143 con->setEndPoint(EndPoint::Target, *wit, widgetRect(*wit).center());
147 QList<Connection *> toRemove;
149 const int c = connectionCount();
150 for (
int i = 0; i < c; i++) {
151 Connection *con = connection(i);
152 QObject *source = con->object(EndPoint::Source);
153 QObject *target = con->object(EndPoint::Target);
155 std::any_of(newList.cbegin(), newList.cend(),
156 [source, target] (
const Connection *nc)
157 {
return nc->object(EndPoint::Source) == source && nc->object(EndPoint::Target) == target; });
159 toRemove.append(con);
161 if (!toRemove.isEmpty()) {
164 for (Connection *con : std::as_const(toRemove))
165 delete takeConnection(con);
168 for (Connection *newConn : std::as_const(newList)) {
170 const int c = connectionCount();
171 for (
int i = 0; i < c; i++) {
172 Connection *con = connection(i);
173 if (con->object(EndPoint::Source) == newConn->object(EndPoint::Source) &&
174 con->object(EndPoint::Target) == newConn->object(EndPoint::Target)) {
182 AddConnectionCommand command(
this, newConn);
192 ConnectionEdit::setBackground(background);
193 if (background ==
nullptr)
196 const auto label_list = background->findChildren<QLabel*>();
197 for (QLabel *label : label_list) {
198 const QString buddy_name = buddy(label, m_formWindow->core());
199 if (buddy_name.isEmpty())
201 QWidget *target = background->findChild<QWidget*>(buddy_name);
202 if (target ==
nullptr)
205 Connection *con =
new Connection(
this);
206 con->setEndPoint(EndPoint::Source, label, widgetRect(label).center());
207 con->setEndPoint(EndPoint::Target, target, widgetRect(target).center());
214 SetPropertyCommand *command =
new SetPropertyCommand(fw);
215 command->init(label, buddyPropertyC, buddy->objectName());
216 command->setText(BuddyEditor::tr(
"Add buddy"));
222 Connection *tmp_con = newlyAddedConnection();
223 Q_ASSERT(tmp_con !=
nullptr);
225 tmp_con->setEndPoint(EndPoint::Target, target, pos);
227 QWidget *source = tmp_con->widget(EndPoint::Source);
228 Q_ASSERT(source !=
nullptr);
229 Q_ASSERT(target !=
nullptr);
231 Connection *new_con = createConnection(source, target);
233 if (new_con !=
nullptr) {
234 new_con->setEndPoint(EndPoint::Source, source, tmp_con->endPointPos(EndPoint::Source));
235 new_con->setEndPoint(EndPoint::Target, target, tmp_con->endPointPos(EndPoint::Target));
238 addConnection(new_con);
239 QLabel *source = qobject_cast<QLabel*>(new_con->widget(EndPoint::Source));
240 QWidget *target = new_con->widget(EndPoint::Target);
242 undoStack()->push(createBuddyCommand(m_formWindow, source, target));
244 qDebug(
"BuddyEditor::endConnection(): not a label");
246 setSelected(new_con,
true);
249 clearNewlyAddedConnection();
250 findObjectsUnderMouse(mapFromGlobal(QCursor::pos()));
255 QWidgetList child_list = widget->findChildren<
QWidget*>();
256 child_list.prepend(widget);
258 ConnectionSet remove_set;
259 for (QWidget *w : std::as_const(child_list)) {
260 const ConnectionList &cl = connectionList();
261 for (Connection *con : cl) {
262 if (con->widget(EndPoint::Source) == w || con->widget(EndPoint::Target) == w)
263 remove_set.insert(con, con);
267 if (!remove_set.isEmpty()) {
268 undoStack()->beginMacro(tr(
"Remove buddies"));
269 for (Connection *con : std::as_const(remove_set)) {
270 setSelected(con,
false);
272 QWidget *source = con->widget(EndPoint::Source);
273 if (qobject_cast<QLabel*>(source) == 0) {
274 qDebug(
"BuddyConnection::widgetRemoved(): not a label");
276 ResetPropertyCommand *command =
new ResetPropertyCommand(formWindow());
277 command->init(source, buddyPropertyC);
278 undoStack()->push(command);
280 delete takeConnection(con);
282 undoStack()->endMacro();
288 const ConnectionSet selectedConnections = selection();
289 if (selectedConnections.isEmpty())
292 undoStack()->beginMacro(tr(
"Remove %n buddies",
nullptr, selectedConnections.size()));
293 for (Connection *con : selectedConnections) {
294 setSelected(con,
false);
296 QWidget *source = con->widget(EndPoint::Source);
297 if (qobject_cast<QLabel*>(source) == 0) {
298 qDebug(
"BuddyConnection::deleteSelected(): not a label");
300 ResetPropertyCommand *command =
new ResetPropertyCommand(formWindow());
301 command->init(source, buddyPropertyC);
302 undoStack()->push(command);
304 delete takeConnection(con);
306 undoStack()->endMacro();
312 auto labelList = background()->findChildren<QLabel*>();
313 if (labelList.isEmpty())
316 QWidgetList usedBuddies;
317 const ConnectionList &beforeConnections = connectionList();
318 for (
const Connection *c : beforeConnections)
319 usedBuddies.push_back(c->widget(EndPoint::Target));
322 for (
auto it = labelList.begin(); it != labelList.end(); ) {
325 if (m_formWindow->isManaged(label)) {
326 const QString buddy_name = buddy(label, m_formWindow->core());
327 if (buddy_name.isEmpty())
328 newBuddy = findBuddy(label, usedBuddies);
331 buddies.push_back(newBuddy);
332 usedBuddies.push_back(newBuddy);
335 it = labelList.erase(it);
339 if (labelList.isEmpty())
341 const auto count = labelList.size();
342 Q_ASSERT(count == buddies.size());
343 undoStack()->beginMacro(tr(
"Add %n buddies",
nullptr, count));
344 for (qsizetype i = 0; i < count; ++i)
345 undoStack()->push(createBuddyCommand(m_formWindow, labelList.at(i), buddies.at(i)));
346 undoStack()->endMacro();
348 const ConnectionList &connections = connectionList();
349 for (Connection *con : connections)
350 setSelected(con, buddies.contains(con->widget(EndPoint::Target)));