def __init__(self, parent):
        super(EditorCompletion, self).__init__()
        self._preferences = parent
        vbox = QVBoxLayout(self)

        groupBoxClose = QGroupBox(translations.TR_PREF_EDITOR_COMPLETE)
        formClose = QGridLayout(groupBoxClose)
        formClose.setContentsMargins(5, 15, 5, 5)
        self._checkParentheses = QCheckBox(
            translations.TR_PREF_EDITOR_PARENTHESES + " ()")
        self._checkParentheses.setChecked('(' in settings.BRACES)
        self._checkKeys = QCheckBox(translations.TR_PREF_EDITOR_KEYS + " {}")
        self._checkKeys.setChecked('{' in settings.BRACES)
        self._checkBrackets = QCheckBox(translations.TR_PREF_EDITOR_BRACKETS +
                                        " []")
        self._checkBrackets.setChecked('[' in settings.BRACES)
        self._checkSimpleQuotes = QCheckBox(
            translations.TR_PREF_EDITOR_SIMPLE_QUOTES)
        self._checkSimpleQuotes.setChecked("'" in settings.QUOTES)
        self._checkDoubleQuotes = QCheckBox(
            translations.TR_PREF_EDITOR_DOUBLE_QUOTES)
        self._checkDoubleQuotes.setChecked('"' in settings.QUOTES)
        self._checkCompleteDeclarations = QCheckBox(
            translations.TR_PREF_EDITOR_COMPLETE_DECLARATIONS.format(
                resources.get_shortcut("Complete-Declarations").toString(
                    QKeySequence.NativeText)))
        self._checkCompleteDeclarations.setChecked(
            settings.COMPLETE_DECLARATIONS)
        formClose.addWidget(self._checkParentheses,
                            1,
                            1,
                            alignment=Qt.AlignTop)
        formClose.addWidget(self._checkKeys, 1, 2, alignment=Qt.AlignTop)
        formClose.addWidget(self._checkBrackets, 2, 1, alignment=Qt.AlignTop)
        formClose.addWidget(self._checkSimpleQuotes,
                            2,
                            2,
                            alignment=Qt.AlignTop)
        formClose.addWidget(self._checkDoubleQuotes,
                            3,
                            1,
                            alignment=Qt.AlignTop)
        vbox.addWidget(groupBoxClose)

        groupBoxCode = QGroupBox(translations.TR_PREF_EDITOR_CODE_COMPLETION)
        formCode = QGridLayout(groupBoxCode)
        formCode.setContentsMargins(5, 15, 5, 5)
        self._checkCodeDot = QCheckBox(
            translations.TR_PREF_EDITOR_ACTIVATE_COMPLETION)
        self._checkCodeDot.setChecked(settings.CODE_COMPLETION)
        formCode.addWidget(self._checkCompleteDeclarations,
                           5,
                           1,
                           alignment=Qt.AlignTop)

        formCode.addWidget(self._checkCodeDot, 6, 1, alignment=Qt.AlignTop)
        vbox.addWidget(groupBoxCode)
        vbox.addItem(
            QSpacerItem(0, 10, QSizePolicy.Expanding, QSizePolicy.Expanding))

        self.connect(self._preferences, SIGNAL("savePreferences()"), self.save)
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()

        self.title = "Graphs and Stuff"
        self.width = 1200
        self.height = 900

        self.setWindowTitle(self.title)
        self.setGeometry(0, 0, self.width, self.height)

        self.layout = QGridLayout()
        self.layout.setContentsMargins(10, 10, 10, 10)
        self.layout_widget = QWidget()
        self.layout_widget.setLayout(self.layout)
        self.setCentralWidget(self.layout_widget)

        self.rewards_graph = utils.LiveGraph(title="Guard vs. Hostile",
                                             subgraph_count=2,
                                             sample_efficiency=0.05,
                                             parent=self)
        self.suffer_graph = utils.LiveGraph("Performance by Suffering", 1,
                                            self)
        self.ghost_graph = utils.LiveGraph("Performance by Ghosting", 1, self)
        self.exploration_graph = utils.LiveGraph(
            "Performance by Ghost Exploration", 1, self)

        self.layout.addWidget(self.rewards_graph.widget, 0, 0)
        self.layout.addWidget(self.exploration_graph.widget, 0, 1)
        self.layout.addWidget(self.suffer_graph.widget, 1, 0)
        self.layout.addWidget(self.ghost_graph.widget, 1, 1)

        # create testing chain
        self.tester = WorldTesterChain([
            WorldTester(config.set_suffering,
                        lambda p, r: self.suffer_graph.add_point(0, p, r), 0,
                        2, 20, config.SUFFERING),
            WorldTester(config.set_ghost_exploration,
                        lambda p, r: self.exploration_graph.add_point(0, p, r),
                        0, 0.1, 1, config.GHOST_EXPLORATION),
            WorldTester(config.set_ghost_count,
                        lambda p, r: self.ghost_graph.add_point(0, p, r), 0,
                        20, 200, config.GHOST_COUNT)
        ])

        # create pygame window
        self.pg_window = PygameWindow()

        #self.pg_window.run_world(self.tester.next_world(self))
        self.pg_window.run_world(World(self))

        self.show()

        self.is_closed = False

        # setup pygame update loop
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_pygame)
        self.timer.start(0)

    def update_pygame(self):
        pg_is_running, world_is_running = self.pg_window.update()

        if not (pg_is_running and world_is_running):

            if pg_is_running:
                world = self.tester.next_world(self)
                if world is not None:
                    self.pg_window.run_world(world)
                    return

            self.close()

    def close(self):
        if not self.is_closed: self.on_close()
        self.is_closed = True
        super().close()

    def on_close(self):
        # dump graph data
        self.rewards_graph.dump("reward.gph")
        self.suffer_graph.dump("suffer.gph")
        self.ghost_graph.dump("ghost.gph")
        self.exploration_graph.dump("exploration.gph")