Example #1
0
    def __init__(self, initial_nation=None):
        super().__init__()

        widget_layout = QtWidgets.QVBoxLayout(self)

        # toolbar
        toolbar = QtWidgets.QToolBar()
        a = qt.create_action(tools.load_ui_icon('icon.add.png'), 'Add nation', toolbar, self.add_nation)
        toolbar.addAction(a)
        a = qt.create_action(tools.load_ui_icon('icon.delete.png'), 'Remove nation', toolbar, self.remove_nation)
        toolbar.addAction(a)
        widget_layout.addLayout(qt.wrap_in_boxlayout(toolbar))

        # nation selection combo box
        label = QtWidgets.QLabel('Choose')
        self.nation_combobox = QtWidgets.QComboBox()
        self.nation_combobox.setFixedWidth(200)
        self.nation_combobox.currentIndexChanged.connect(self.nation_selected)
        widget_layout.addWidget(qt.wrap_in_groupbox(qt.wrap_in_boxlayout((label, self.nation_combobox)), 'Nations'))

        # nation info panel
        layout = QtWidgets.QVBoxLayout()

        # description
        self.description_edit = QtWidgets.QLineEdit()
        self.description_edit.setFixedWidth(300)
        self.description_edit.setText('Test')
        layout.addLayout(qt.wrap_in_boxlayout((QtWidgets.QLabel('Description'), self.description_edit)))

        # color
        self.color_picker = QtWidgets.QPushButton()
        self.color_picker.setFixedSize(24, 24)
        self.color_picker.clicked.connect(self.show_color_picker)
        layout.addLayout(qt.wrap_in_boxlayout((QtWidgets.QLabel('Color'), self.color_picker)))

        # capital province
        self.capital_province_edit = QtWidgets.QLineEdit()
        self.capital_province_edit.setFixedWidth(300)
        layout.addLayout(qt.wrap_in_boxlayout((QtWidgets.QLabel('Capital'), self.capital_province_edit)))

        # all provinces
        self.provinces_combobox = QtWidgets.QComboBox()
        self.provinces_combobox.setFixedWidth(300)
        self.number_provinces_label = QtWidgets.QLabel()
        layout.addLayout(qt.wrap_in_boxlayout((self.number_provinces_label, self.provinces_combobox)))

        widget_layout.addWidget(qt.wrap_in_groupbox(layout, 'Info'))

        # vertical stretch
        widget_layout.addStretch()

        # reset content
        self.reset_content()

        # select initial nation if given
        if initial_nation:
            index = utils.index_of_element(self.nations, initial_nation)
            self.nation_combobox.setCurrentIndex(index)
Example #2
0
    def __init__(self, scenario, initial_province=None):
        super().__init__()

        self.scenario = scenario

        widget_layout = QtWidgets.QVBoxLayout(self)

        # toolbar
        toolbar = QtWidgets.QToolBar()
        a = qt.create_action(tools.load_ui_icon('icon.add.png'),
                             'Add province', toolbar, self.add_province)
        toolbar.addAction(a)
        a = qt.create_action(tools.load_ui_icon('icon.delete.png'),
                             'Remove province', toolbar, self.remove_province)
        toolbar.addAction(a)
        widget_layout.addLayout(qt.wrap_in_boxlayout(toolbar))

        # provinces selection combo box
        label = QtWidgets.QLabel('Choose')
        self.provinces_combobox = QtWidgets.QComboBox()
        self.provinces_combobox.setFixedWidth(200)
        self.provinces_combobox.currentIndexChanged.connect(
            self.province_combobox_index_changed)
        widget_layout.addWidget(
            qt.wrap_in_groupbox(
                qt.wrap_in_boxlayout((label, self.provinces_combobox)),
                constants.SCENARIO_FILE_PROVINCES))

        # province info panel
        layout = QtWidgets.QVBoxLayout()

        # nation
        self.nation_label = QtWidgets.QLabel('Nation')
        layout.addWidget(self.nation_label)

        widget_layout.addWidget(qt.wrap_in_groupbox(layout, 'Info'))

        # vertical stretch
        widget_layout.addStretch()

        # reset content
        self.reset_content()

        # if province is given, select it
        if initial_province:
            index = utils.index_of_element(self.provinces, initial_province)
            self.provinces_combobox.setCurrentIndex(index)
Example #3
0
    def __init__(self, initial_province=None):
        super().__init__()

        widget_layout = QtWidgets.QVBoxLayout(self)

        # toolbar
        toolbar = QtWidgets.QToolBar()
        a = qt.create_action(tools.load_ui_icon('icon.add.png'), 'Add province', toolbar, self.add_province)
        toolbar.addAction(a)
        a = qt.create_action(tools.load_ui_icon('icon.delete.png'), 'Remove province', toolbar, self.remove_province)
        toolbar.addAction(a)
        widget_layout.addLayout(qt.wrap_in_boxlayout(toolbar))

        # provinces selection combo box
        label = QtWidgets.QLabel('Choose')
        self.provinces_combobox = QtWidgets.QComboBox()
        self.provinces_combobox.setFixedWidth(200)
        self.provinces_combobox.currentIndexChanged.connect(self.province_combobox_index_changed)
        widget_layout.addWidget(qt.wrap_in_groupbox(qt.wrap_in_boxlayout((label, self.provinces_combobox)), 'provinces'))

        # province info panel
        layout = QtWidgets.QVBoxLayout()

        # nation
        self.nation_label = QtWidgets.QLabel('Nation')
        layout.addWidget(self.nation_label)

        widget_layout.addWidget(qt.wrap_in_groupbox(layout, 'Info'))

        # vertical stretch
        widget_layout.addStretch()

        # reset content
        self.reset_content()

        # if province is given, select it
        if initial_province:
            index = utils.index_of_element(self.provinces, initial_province)
            self.provinces_combobox.setCurrentIndex(index)
Example #4
0
    def received_preview(self, client, channel, action, message):
        """
        Populates the widget after the network reply comes from the server with the preview.
        """

        # immediately unsubscribe, we need it only once
        local_network_client.disconnect_from_channel(constants.C.LOBBY,
                                                     self.received_preview)

        # unpack message
        nations = [(message['nations'][key][constants.NationProperty.NAME],
                    key) for key in message['nations']]
        nations = sorted(nations)  # by first element, which is the name
        nation_names, self.nation_ids = zip(*nations)

        # fill the widget with useful stuff
        layout = QtWidgets.QGridLayout(self)

        # selection list for nations
        self.nations_list = QtWidgets.QListWidget()
        # self.nations_list.setFixedWidth(200)
        self.nations_list.setSelectionMode(
            QtWidgets.QAbstractItemView.SingleSelection)
        self.nations_list.itemSelectionChanged.connect(
            self.nations_list_selection_changed)
        self.nations_list.addItems(nation_names)
        self.nations_list.setHorizontalScrollBarPolicy(
            QtCore.Qt.ScrollBarAlwaysOff)
        # 10px extra
        self.nations_list.setFixedWidth(
            self.nations_list.sizeHintForColumn(0) +
            2 * self.nations_list.frameWidth() + 17 + 10)
        # TODO use app.style().pixelMetric(QtWidgets.QStyle.PM_ScrollBarExtent)
        layout.addWidget(qt.wrap_in_groupbox(self.nations_list, 'Nations'), 0,
                         0)

        # map view (no scroll bars)
        self.map_scene = QtWidgets.QGraphicsScene()
        self.map_view = qt.FitSceneInViewGraphicsView(self.map_scene)
        self.map_view.setHorizontalScrollBarPolicy(
            QtCore.Qt.ScrollBarAlwaysOff)
        self.map_view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        #       self.map_view.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding)
        #       self.map_view.setFixedSize(100, 100)
        layout.addWidget(qt.wrap_in_groupbox(self.map_view, 'Map'), 0, 1)

        # scenario description
        self.description = QtWidgets.QPlainTextEdit()
        self.description.setHorizontalScrollBarPolicy(
            QtCore.Qt.ScrollBarAlwaysOff)
        self.description.setVerticalScrollBarPolicy(
            QtCore.Qt.ScrollBarAsNeeded)
        self.description.setReadOnly(True)
        self.description.setPlainText(
            message[constants.ScenarioProperty.DESCRIPTION])
        height = self.description.fontMetrics().lineSpacing(
        ) * 4  # 4 lines high
        self.description.setFixedHeight(height)
        layout.addWidget(qt.wrap_in_groupbox(self.description, 'Description'),
                         1, 0, 1, 2)  # goes over two columns

        # nation description
        self.nation_info = QtWidgets.QPlainTextEdit()
        self.nation_info.setHorizontalScrollBarPolicy(
            QtCore.Qt.ScrollBarAlwaysOff)
        self.nation_info.setVerticalScrollBarPolicy(
            QtCore.Qt.ScrollBarAsNeeded)
        self.nation_info.setReadOnly(True)
        height = self.nation_info.fontMetrics().lineSpacing(
        ) * 6  # 6 lines high
        self.nation_info.setFixedHeight(height)
        layout.addWidget(qt.wrap_in_groupbox(self.nation_info, 'Nation Info'),
                         2, 0, 1, 2)

        # stretching of the elements
        layout.setRowStretch(
            0, 1)  # nation list and map get all the available height
        layout.setColumnStretch(1, 1)  # map gets all the available width

        # add the start button
        toolbar = QtWidgets.QToolBar()
        toolbar.addAction(
            qt.create_action(tools.load_ui_icon('icon.confirm.png'),
                             'Start selected scenario',
                             toolbar,
                             trigger_connection=self.start_scenario_clicked))
        layout.addWidget(toolbar, 3, 0, 1, 2, alignment=QtCore.Qt.AlignRight)

        # draw the map
        columns = message[constants.ScenarioProperty.MAP_COLUMNS]
        rows = message[constants.ScenarioProperty.MAP_ROWS]
        self.map_scene.setSceneRect(0, 0, columns, rows)

        # fill the ground layer with a neutral color
        item = self.map_scene.addRect(0, 0, columns, rows)
        item.setBrush(QtCore.Qt.lightGray)
        item.setPen(qt.TRANSPARENT_PEN)
        item.setZValue(0)

        # for all nations
        for nation_id, nation in message['nations'].items():

            # get nation color
            color_string = nation[constants.NationProperty.COLOR]
            color = QtGui.QColor()
            color.setNamedColor(color_string)

            # get nation name
            nation_name = nation[constants.NationProperty.NAME]

            # get nation outline
            path = QtGui.QPainterPath()
            # TODO traversing everything is quite slow go only once and add to paths
            for column in range(0, columns):
                for row in range(0, rows):
                    if nation_id == message['map'][column + row * columns]:
                        path.addRect(column, row, 1, 1)
            path = path.simplified()

            item = graphics.MiniMapNationItem(path)
            item.signaller.clicked.connect(
                partial(self.map_selected_nation,
                        utils.index_of_element(nation_names, nation_name)))
            #           item.signaller.entered.connect(partial(self.change_map_name, nation_name))
            #           item.signaller.left.connect(partial(self.change_map_name, ''))
            brush = QtGui.QBrush(color)
            item.setBrush(brush)

            item.setToolTip(nation_name)

            pen = QtGui.QPen()
            pen.setWidth(2)
            pen.setCosmetic(True)
            item.setPen(pen)

            self.map_scene.addItem(item)
#           item = self.map_scene.addPath(path, brush=brush) # will use the default pen for outline

        self.preview = message
Example #5
0
    def received_preview(self, client, channel, action, message):
        """
        Populates the widget after the network reply comes from the server with the preview.
        """

        # immediately unsubscribe, we need it only once
        local_network_client.disconnect_from_channel(constants.C.LOBBY, self.received_preview)

        # unpack message
        nations = [(message['nations'][key][constants.NationProperty.NAME], key) for key in message['nations']]
        nations = sorted(nations)  # by first element, which is the name
        nation_names, self.nation_ids = zip(*nations)

        # fill the widget with useful stuff
        layout = QtWidgets.QGridLayout(self)

        # selection list for nations
        self.nations_list = QtWidgets.QListWidget()
        # self.nations_list.setFixedWidth(200)
        self.nations_list.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
        self.nations_list.itemSelectionChanged.connect(self.nations_list_selection_changed)
        self.nations_list.addItems(nation_names)
        self.nations_list.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        # 10px extra
        self.nations_list.setFixedWidth(self.nations_list.sizeHintForColumn(0) +
                                        2 * self.nations_list.frameWidth() + 17 + 10)
        # TODO use app.style().pixelMetric(QtWidgets.QStyle.PM_ScrollBarExtent)
        layout.addWidget(qt.wrap_in_groupbox(self.nations_list, 'Nations'), 0, 0)

        # map view (no scroll bars)
        self.map_scene = QtWidgets.QGraphicsScene()
        self.map_view = qt.FitSceneInViewGraphicsView(self.map_scene)
        self.map_view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.map_view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
#       self.map_view.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding)
#       self.map_view.setFixedSize(100, 100)
        layout.addWidget(qt.wrap_in_groupbox(self.map_view, 'Map'), 0, 1)

        # scenario description
        self.description = QtWidgets.QPlainTextEdit()
        self.description.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.description.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.description.setReadOnly(True)
        self.description.setPlainText(message[constants.ScenarioProperty.DESCRIPTION])
        height = self.description.fontMetrics().lineSpacing() * 4  # 4 lines high
        self.description.setFixedHeight(height)
        layout.addWidget(qt.wrap_in_groupbox(self.description, 'Description'), 1, 0, 1, 2)  # goes over two columns

        # nation description
        self.nation_info = QtWidgets.QPlainTextEdit()
        self.nation_info.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.nation_info.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.nation_info.setReadOnly(True)
        height = self.nation_info.fontMetrics().lineSpacing() * 6  # 6 lines high
        self.nation_info.setFixedHeight(height)
        layout.addWidget(qt.wrap_in_groupbox(self.nation_info, 'Nation Info'), 2, 0, 1, 2)

        # stretching of the elements
        layout.setRowStretch(0, 1)  # nation list and map get all the available height
        layout.setColumnStretch(1, 1)  # map gets all the available width

        # add the start button
        toolbar = QtWidgets.QToolBar()
        toolbar.addAction(qt.create_action(tools.load_ui_icon('icon.confirm.png'), 'Start selected scenario', toolbar,
                                           trigger_connection=self.start_scenario_clicked))
        layout.addWidget(toolbar, 3, 0, 1, 2, alignment=QtCore.Qt.AlignRight)

        # draw the map
        columns = message[constants.ScenarioProperty.MAP_COLUMNS]
        rows = message[constants.ScenarioProperty.MAP_ROWS]
        self.map_scene.setSceneRect(0, 0, columns, rows)

        # fill the ground layer with a neutral color
        item = self.map_scene.addRect(0, 0, columns, rows)
        item.setBrush(QtCore.Qt.lightGray)
        item.setPen(qt.TRANSPARENT_PEN)
        item.setZValue(0)

        # for all nations
        for nation_id, nation in message['nations'].items():

            # get nation color
            color_string = nation[constants.NationProperty.COLOR]
            color = QtGui.QColor()
            color.setNamedColor(color_string)

            # get nation name
            nation_name = nation[constants.NationProperty.NAME]

            # get nation outline
            path = QtGui.QPainterPath()
            # TODO traversing everything is quite slow go only once and add to paths
            for column in range(0, columns):
                for row in range(0, rows):
                    if nation_id == message['map'][column + row * columns]:
                        path.addRect(column, row, 1, 1)
            path = path.simplified()

            item = graphics.MiniMapNationItem(path)
            item.signaller.clicked.connect(
                partial(self.map_selected_nation, utils.index_of_element(nation_names, nation_name)))
#           item.signaller.entered.connect(partial(self.change_map_name, nation_name))
#           item.signaller.left.connect(partial(self.change_map_name, ''))
            brush = QtGui.QBrush(color)
            item.setBrush(brush)

            item.setToolTip(nation_name)

            pen = QtGui.QPen()
            pen.setWidth(2)
            pen.setCosmetic(True)
            item.setPen(pen)

            self.map_scene.addItem(item)
#           item = self.map_scene.addPath(path, brush=brush) # will use the default pen for outline

        self.preview = message