<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_tree_widget_items(self, definitions, to_collapse=None):
 """ Converts the list of top level definitions to a list of top level tree items. """

        def flatten(definitions):
            """
            Flattens the document structure tree as a simple sequential list.
            """
            ret_val = []
            for de in definitions:
                ret_val.append(de)
                for sub_d in de.children:
                    ret_val.append(sub_d)
                    ret_val += flatten(sub_d.children)
            return ret_val

        def convert(name, editor, to_collapse):
            ti = QtWidgets.QTreeWidgetItem()
            ti.setText(0, name.name)
            if isinstance(name.icon, list):
                icon = QtGui.QIcon.fromTheme(
                    name.icon[0], QtGui.QIcon(name.icon[1]))
            else:
                icon = QtGui.QIcon(name.icon)
            ti.setIcon(0, icon)
            name.block = editor.document().findBlockByNumber(name.line)
            ti.setData(0, QtCore.Qt.UserRole, name)
            ti.setToolTip(0, name.description)
            name.tree_item = ti
            block_data = name.block.userData()
            if block_data is None:
                block_data = TextBlockUserData()
                name.block.setUserData(block_data)
            block_data.tree_item = ti

            if to_collapse is not None and \
                    TextBlockHelper.is_collapsed(name.block):
                to_collapse.append(ti)

            for ch in name.children:
                ti_ch, to_collapse = convert(ch, editor, to_collapse)
                if ti_ch:
                    ti.addChild(ti_ch)
            return ti, to_collapse

        self._definitions = definitions
        self._flattened_defs = flatten(self._definitions)

        items = []
        for d in definitions:
            value, to_collapse = convert(d, self._editor, to_collapse)
            items.append(value)
        if to_collapse is not None:
            return items, to_collapse

        return items