Repository: godotengine/godot
File: editor/scene/connections_dialog.cpp (C++)

Code before:
		return TREE_ITEM_TYPE_ROOT;
	} else if (p_item.get_parent() == tree->get_root()) {
		return TREE_ITEM_TYPE_CLASS;
	} else if (p_item.get_parent()->get_parent() == tree->get_root()) {
		return TREE_ITEM_TYPE_SIGNAL;
	} else {
		return TREE_ITEM_TYPE_CONNECTION;
	}
}

bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
	return bool(p_connection.flags & CONNECT_INHERITED);
}

/*
 * Open connection dialog with TreeItem data to CREATE a brand-new connection.
 */
void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
	const Dictionary sinfo = p_item.get_metadata(0);
	const StringName signal_name = sinfo["name"];
	const PackedStringArray signal_args = sinfo["args"];

	if (!is_editing_resource) {
		ConnectDialog::ConnectionData cd;

		Node *dst_node = Object::cast_to<Node>(selected_object)->get_owner() ? cast_to<Node>(selected_object)->get_owner() : cast_to<Node>(selected_object);
		if (!dst_node || dst_node->get_script().is_null()) {
			dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
		}
		cd.source = selected_object;
		cd.target = dst_node;
		cd.signal = signal_name;
		cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);
		connect_dialog->init(cd, signal_args);
		connect_dialog->set_title(TTR("Connect a Signal to a Method"));
		connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");
	}
}

/*
 * Open connection dialog with Connection data to EDIT an existing connection.
 */
void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {
	TreeItem *signal_item = p_item.get_parent();
	ERR_FAIL_NULL(signal_item);

	Connection connection = p_item.get_metadata(0);
	ConnectDialog::ConnectionData cd = connection;

	Object *src = cd.source;
	Object *dst = cd.target;

Review comment (suggestion):
```suggestion
		Node *selected_node = Object::cast_to<Node>(selected_object);
		Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
```
Avoid excessive casts.

Code after:
	} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
		_go_to_method(*item);
	}
}

ConnectionsDock::TreeItemType ConnectionsDock::_get_item_type(const TreeItem &p_item) const {
	if (&p_item == tree->get_root()) {
		return TREE_ITEM_TYPE_ROOT;
	} else if (p_item.get_parent() == tree->get_root()) {
		return TREE_ITEM_TYPE_CLASS;
	} else if (p_item.get_parent()->get_parent() == tree->get_root()) {
		return TREE_ITEM_TYPE_SIGNAL;
	} else {
		return TREE_ITEM_TYPE_CONNECTION;
	}
}

bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
	return bool(p_connection.flags & CONNECT_INHERITED);
}

/*
 * Open connection dialog with TreeItem data to CREATE a brand-new connection.
 */
void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
	if (is_editing_resource) {
		return;
	}

	const Dictionary sinfo = p_item.get_metadata(0);
	const StringName signal_name = sinfo["name"];
	const PackedStringArray signal_args = sinfo["args"];

	ConnectDialog::ConnectionData cd;

	Node *selected_node = Object::cast_to<Node>(selected_object);
	Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
	if (!dst_node || dst_node->get_script().is_null()) {
		dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
	}
	cd.source = selected_object;
	cd.target = dst_node;
	cd.signal = signal_name;
	cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);
	connect_dialog->init(cd, signal_args);
	connect_dialog->set_title(TTR("Connect a Signal to a Method"));
	connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");
}

/*
 * Open connection dialog with Connection data to EDIT an existing connection.