Пример #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)
Пример #2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        layout = QtWidgets.QHBoxLayout(self)

        self.client_list_widget = QtWidgets.QListWidget()
        #       list.itemSelectionChanged.connect(self.selection_changed)
        #       list.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.client_list_widget.setHorizontalScrollBarPolicy(
            QtCore.Qt.ScrollBarAlwaysOff)
        self.client_list_widget.setVerticalScrollBarPolicy(
            QtCore.Qt.ScrollBarAsNeeded)
        self.client_list_widget.setFixedWidth(200)
        layout.addWidget(
            qt.wrap_in_groupbox(self.client_list_widget, 'Players'))

        self.chat_log_text_edit = QtWidgets.QTextEdit()
        self.chat_log_text_edit.setEnabled(False)
        chat_log_group = qt.wrap_in_groupbox(self.chat_log_text_edit,
                                             'Chat log')

        self.chat_input_edit = QtWidgets.QLineEdit()
        self.chat_input_edit.returnPressed.connect(self.send_chat_message)
        chat_input_group = qt.wrap_in_groupbox(self.chat_input_edit,
                                               'Chat input')
        layout.addLayout(qt.wrap_in_boxlayout(
            (chat_log_group, chat_input_group),
            horizontal=False,
            add_stretch=False),
                         stretch=1)

        # connection to server

        # chat messages
        local_network_client.connect_to_channel(constants.C.CHAT,
                                                self.receive_chat_messages)
        local_network_client.send(constants.C.CHAT, constants.M.CHAT_SUBSCRIBE)

        # LOBBY
        local_network_client.connect_to_channel(constants.C.LOBBY,
                                                self.receive_lobby_messages)
        self.request_updated_client_list()

        # set timer for connected client updates
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.request_updated_client_list)
        self.timer.setInterval(5000)
        self.timer.start()
Пример #3
0
    def _layout_widget_preferences_music(self):
        """
        Create music options widget.
        """
        tab = QtWidgets.QWidget()
        tab_layout = QtWidgets.QVBoxLayout(tab)

        # soundtrack section
        layout = QtWidgets.QVBoxLayout()

        # mute checkbox
        checkbox = QtWidgets.QCheckBox('Mute soundtrack')
        self._register_check_box(checkbox, constants.Option.SOUNDTRACK_MUTE)
        layout.addWidget(checkbox)

        # volume slide
        layout.addWidget(QtWidgets.QLabel('Volume'))
        slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        slider.setTickInterval(25)
        slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
        slider.setMaximumWidth(100)
        self._register_slider(slider, constants.Option.SOUNDTRACK_VOLUME)
        layout.addWidget(slider)

        # wrap in group box and add to tab
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Soundtrack'))

        # vertical stretch
        tab_layout.addStretch()

        # add tab
        self.tab_music = tab
        self.stacked_layout.addWidget(tab)
Пример #4
0
    def _layout_widget_preferences_music(self):
        """
        Create music options widget.
        """
        tab = QtWidgets.QWidget()
        tab_layout = QtWidgets.QVBoxLayout(tab)

        # soundtrack section
        layout = QtWidgets.QVBoxLayout()

        # mute checkbox
        checkbox = QtWidgets.QCheckBox('Mute soundtrack')
        self._register_check_box(checkbox, constants.Option.SOUNDTRACK_MUTE)
        layout.addWidget(checkbox)

        # volume slide
        layout.addWidget(QtWidgets.QLabel('Volume'))
        slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        slider.setTickInterval(25)
        slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
        slider.setMaximumWidth(100)
        self._register_slider(slider, constants.Option.SOUNDTRACK_VOLUME)
        layout.addWidget(slider)

        # wrap in group box and add to tab
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Soundtrack'))

        # vertical stretch
        tab_layout.addStretch()

        # add tab
        self.tab_music = tab
        self.stacked_layout.addWidget(tab)
Пример #5
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)
Пример #6
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)
Пример #7
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        layout = QtWidgets.QHBoxLayout(self)

        self.client_list_widget = QtWidgets.QListWidget()
#       list.itemSelectionChanged.connect(self.selection_changed)
#       list.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.client_list_widget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.client_list_widget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.client_list_widget.setFixedWidth(200)
        layout.addWidget(qt.wrap_in_groupbox(self.client_list_widget, 'Players'))

        self.chat_log_text_edit = QtWidgets.QTextEdit()
        self.chat_log_text_edit.setEnabled(False)
        chat_log_group = qt.wrap_in_groupbox(self.chat_log_text_edit, 'Chat log')

        self.chat_input_edit = QtWidgets.QLineEdit()
        self.chat_input_edit.returnPressed.connect(self.send_chat_message)
        chat_input_group = qt.wrap_in_groupbox(self.chat_input_edit, 'Chat input')
        layout.addLayout(qt.wrap_in_boxlayout((chat_log_group, chat_input_group), horizontal=False, add_stretch=False),
                         stretch=1)

        # connection to server

        # chat messages
        local_network_client.connect_to_channel(constants.C.CHAT, self.receive_chat_messages)
        local_network_client.send(constants.C.CHAT, constants.M.CHAT_SUBSCRIBE)

        # LOBBY
        local_network_client.connect_to_channel(constants.C.LOBBY, self.receive_lobby_messages)
        self.request_updated_client_list()

        # set timer for connected client updates
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.request_updated_client_list)
        self.timer.setInterval(5000)
        self.timer.start()
Пример #8
0
    def _layout_widget_preferences_general(self):
        """
        Create general options widget.
        """
        tab = QtWidgets.QWidget()
        tab_layout = QtWidgets.QVBoxLayout(tab)

        # language
        label = QtWidgets.QLabel('Choose')
        combobox = QtWidgets.QComboBox()
        tab_layout.addWidget(qt.wrap_in_groupbox(qt.wrap_in_boxlayout((label, combobox)), 'User Interface Language'))

        # vertical stretch
        tab_layout.addStretch()

        # add tab
        self.tab_general = tab
        self.stacked_layout.addWidget(tab)
Пример #9
0
    def _layout_widget_preferences_general(self):
        """
        Create general options widget.
        """
        tab = QtWidgets.QWidget()
        tab_layout = QtWidgets.QVBoxLayout(tab)

        # language
        label = QtWidgets.QLabel('Choose')
        combobox = QtWidgets.QComboBox()
        tab_layout.addWidget(
            qt.wrap_in_groupbox(qt.wrap_in_boxlayout((label, combobox)),
                                'User Interface Language'))

        # vertical stretch
        tab_layout.addStretch()

        # add tab
        self.tab_general = tab
        self.stacked_layout.addWidget(tab)
Пример #10
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
Пример #11
0
    def _layout_widget_preferences_network(self):
        """
        Create network options widget.
        """
        tab = QtWidgets.QWidget()
        tab_layout = QtWidgets.QVBoxLayout(tab)

        # client
        layout = QtWidgets.QVBoxLayout()
        if local_network_client.is_connected():
            peer_address, peer_port = local_network_client.peer_address()
            status = 'Connected to {}:{}'.format(peer_address.toString(), peer_port)
        else:
            status = 'Disconnected'
        layout.addWidget(QtWidgets.QLabel(status))
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Client'))
        # alias name edit box
        label = QtWidgets.QLabel('Alias')
        edit = QtWidgets.QLineEdit()
        edit.setFixedWidth(300)
        self._register_line_edit(edit, constants.Option.LOCALCLIENT_NAME)
        layout.addLayout(qt.wrap_in_boxlayout((label, edit)))

        # local server group box
        layout = QtWidgets.QVBoxLayout()
        import ipgetter
        local_ip = ipgetter.myip()
        layout.addWidget(QtWidgets.QLabel('Local public IP address: {}'.format(local_ip)))
        # accepts incoming connections checkbox
        checkbox = QtWidgets.QCheckBox('Accepts incoming connections')
        self._register_check_box(checkbox, constants.Option.LOCALSERVER_OPEN)
        layout.addWidget(checkbox)
        # alias name edit box
        label = QtWidgets.QLabel('Alias')
        edit = QtWidgets.QLineEdit()
        edit.setFixedWidth(300)
        self._register_line_edit(edit, constants.Option.LOCALSERVER_NAME)
        layout.addLayout(qt.wrap_in_boxlayout((label, edit)))
        # actions toolbar
        toolbar = QtWidgets.QToolBar()
        toolbar.setIconSize(QtCore.QSize(24, 24))
        # show local server monitor
        toolbar.addAction(
            qt.create_action(tools.load_ui_icon('icon.preferences.network.png'), 'Show local server monitor', toolbar))
        layout.addLayout(qt.wrap_in_boxlayout(toolbar))
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Local Server'))

        # remote server group box
        layout = QtWidgets.QVBoxLayout()
        # remote server address
        label = QtWidgets.QLabel('Remote IP address')
        edit = QtWidgets.QLineEdit()
        edit.setFixedWidth(300)
        layout.addLayout(qt.wrap_in_boxlayout((label, edit)))
        # actions toolbar
        toolbar = QtWidgets.QToolBar()
        toolbar.setIconSize(QtCore.QSize(24, 24))
        # connect to remote server
        a = qt.create_action(tools.load_ui_icon('icon.preferences.network.png'), 'Connect/Disconnect to remote server',
                             toolbar, checkable=True)
        toolbar.addAction(a)
        layout.addLayout(qt.wrap_in_boxlayout(toolbar))
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Remote Server'))

        # vertical stretch
        tab_layout.addStretch()

        # add tab
        self.tab_network = tab
        self.stacked_layout.addWidget(tab)
Пример #12
0
    def _layout_widget_preferences_network(self):
        """
        Create network options widget.
        """
        tab = QtWidgets.QWidget()
        tab_layout = QtWidgets.QVBoxLayout(tab)

        # client
        layout = QtWidgets.QVBoxLayout()
        if local_network_client.is_connected():
            peer_address, peer_port = local_network_client.peer_address()
            status = 'Connected to {}:{}'.format(peer_address.toString(),
                                                 peer_port)
        else:
            status = 'Disconnected'
        layout.addWidget(QtWidgets.QLabel(status))
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Client'))
        # alias name edit box
        label = QtWidgets.QLabel('Alias')
        edit = QtWidgets.QLineEdit()
        edit.setFixedWidth(300)
        self._register_line_edit(edit, constants.Option.LOCALCLIENT_NAME)
        layout.addLayout(qt.wrap_in_boxlayout((label, edit)))

        # local server group box
        layout = QtWidgets.QVBoxLayout()
        import ipgetter
        local_ip = ipgetter.myip()
        layout.addWidget(
            QtWidgets.QLabel('Local public IP address: {}'.format(local_ip)))
        # accepts incoming connections checkbox
        checkbox = QtWidgets.QCheckBox('Accepts incoming connections')
        self._register_check_box(checkbox, constants.Option.LOCALSERVER_OPEN)
        layout.addWidget(checkbox)
        # alias name edit box
        label = QtWidgets.QLabel('Alias')
        edit = QtWidgets.QLineEdit()
        edit.setFixedWidth(300)
        self._register_line_edit(edit, constants.Option.LOCALSERVER_NAME)
        layout.addLayout(qt.wrap_in_boxlayout((label, edit)))
        # actions toolbar
        toolbar = QtWidgets.QToolBar()
        toolbar.setIconSize(QtCore.QSize(24, 24))
        # show local server monitor
        toolbar.addAction(
            qt.create_action(
                tools.load_ui_icon('icon.preferences.network.png'),
                'Show local server monitor', toolbar))
        layout.addLayout(qt.wrap_in_boxlayout(toolbar))
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Local Server'))

        # remote server group box
        layout = QtWidgets.QVBoxLayout()
        # remote server address
        label = QtWidgets.QLabel('Remote IP address')
        edit = QtWidgets.QLineEdit()
        edit.setFixedWidth(300)
        layout.addLayout(qt.wrap_in_boxlayout((label, edit)))
        # actions toolbar
        toolbar = QtWidgets.QToolBar()
        toolbar.setIconSize(QtCore.QSize(24, 24))
        # connect to remote server
        a = qt.create_action(
            tools.load_ui_icon('icon.preferences.network.png'),
            'Connect/Disconnect to remote server',
            toolbar,
            checkable=True)
        toolbar.addAction(a)
        layout.addLayout(qt.wrap_in_boxlayout(toolbar))
        tab_layout.addWidget(qt.wrap_in_groupbox(layout, 'Remote Server'))

        # vertical stretch
        tab_layout.addStretch()

        # add tab
        self.tab_network = tab
        self.stacked_layout.addWidget(tab)
Пример #13
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