def __init__(self, *args, **kwargs): """ Sets up the graphics view. """ super().__init__(*args, **kwargs) self.setObjectName('mini-map-widget') layout = QtWidgets.QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) # the content is a scene self.scene = QtWidgets.QGraphicsScene() # tracker rectangle that tracks the view of the map, initially hidden self.tracker = QtWidgets.QGraphicsRectItem() self.tracker.setCursor(QtCore.Qt.PointingHandCursor) self.tracker.setZValue(1000) self.tracker.hide() self.scene.addItem(self.tracker) # the view on the scene (no scroll bars) self.view = QtWidgets.QGraphicsView(self.scene) self.view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) layout.addWidget(self.view) # the width and height (fixed width throughout the game) # TODO make this adjustable self.view.setFixedWidth(self.VIEW_WIDTH) view_height = math.floor(0.6 * self.VIEW_WIDTH) self.view.setFixedHeight(view_height) # tool bar below the mini map self.toolbar = QtWidgets.QToolBar() self.toolbar.setIconSize(QtCore.QSize(20, 20)) # action group (only one of them can be checked at each time) action_group = QtWidgets.QActionGroup(self.toolbar) # political view in the beginning a = qt.create_action(tools.load_ui_icon('icon.mini.political.png'), 'Show political view', action_group, toggle_connection=self.switch_to_political_view, checkable=True) self.toolbar.addAction(a) # geographical view a = qt.create_action(tools.load_ui_icon('icon.mini.geographical.png'), 'Show geographical view', action_group, toggle_connection=self.switch_to_geographical_view, checkable=True) self.toolbar.addAction(a) self.mode = constants.OverviewMapMode.POLITICAL # wrap tool bar into horizontal layout with stretch l = QtWidgets.QHBoxLayout() l.setContentsMargins(0, 0, 0, 0) l.addWidget(self.toolbar) l.addStretch() # add layout containing tool bar layout.addLayout(l) # graphics items in scene (except the tracker) self.scene_items = []
def contextMenuEvent(self, event): """ Right click (context click) on a tile. Shows the context menu, depending on the tile position """ # get mouse position in scene coordinates scene_position = self.mapToScene(event.pos()) / self.TILE_SIZE column, row = editor_scenario.scenario.map_position(scene_position.x(), scene_position.y()) # create context menu menu = QtWidgets.QMenu(self) # change terrain a = qt.create_action(tools.load_ui_icon('icon.editor.change_terrain.png'), 'Set terrain', self, partial(self.change_terrain.emit, column, row)) menu.addAction(a) # is there a province province = editor_scenario.scenario.province_at(column, row) if province: a = qt.create_action(tools.load_ui_icon('icon.editor.province_info.png'), 'Province info', self, partial(self.province_info.emit, province)) menu.addAction(a) # is there also nation nation = editor_scenario.scenario.province_property(province, constants.ProvinceProperty.NATION) if nation: a = qt.create_action(tools.load_ui_icon('icon.editor.nation_info.png'), 'Nation info', self, partial(self.nation_info.emit, nation)) menu.addAction(a) menu.exec(event.globalPos())
def __init__(self, *args, **kwargs): """ Create toolbar. """ super().__init__(*args, **kwargs) self.layout = QtWidgets.QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) # create tool bar toolbar = QtWidgets.QToolBar() action_group = QtWidgets.QActionGroup(toolbar) # actions single player new/load a = qt.create_action(tools.load_ui_icon('icon.lobby.single.new.png'), 'Start new single player scenario', action_group, toggle_connection=self.toggled_single_player_scenario_selection, checkable=True) toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.lobby.single.load.png'), 'Continue saved single player scenario', action_group, toggle_connection=self.toggled_single_player_load_scenario, checkable=True) toolbar.addAction(a) toolbar.addSeparator() # actions multi player a = qt.create_action(tools.load_ui_icon('icon.lobby.network.png'), 'Show server lobby', action_group, toggle_connection=self.toggled_server_lobby, checkable=True) toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.lobby.multiplayer-game.png'), 'Start or continue multiplayer scenario', action_group, toggle_connection=self.toggled_multiplayer_scenario_selection, checkable=True) toolbar.addAction(a) self.layout.addWidget(toolbar, alignment=QtCore.Qt.AlignTop) self.content = None
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)
def __init__(self, *args, **kwargs): """ Sets up the graphics view, the toolbar and the tracker rectangle. """ super().__init__(*args, **kwargs) self.setObjectName('mini-map-widget') layout = QtWidgets.QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) # the content is a scene self.scene = QtWidgets.QGraphicsScene() # tracker rectangle that tracks the view of the map, initially hidden self.tracker = QtWidgets.QGraphicsRectItem() self.tracker.setCursor(QtCore.Qt.PointingHandCursor) self.tracker.setZValue(1000) self.tracker.hide() self.scene.addItem(self.tracker) # the view on the scene (no scroll bars) self.view = QtWidgets.QGraphicsView(self.scene) self.view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) layout.addWidget(self.view) # the width and height (fixed width throughout the game) # TODO make this adjustable self.view.setFixedWidth(self.VIEW_WIDTH) view_height = math.floor(0.6 * self.VIEW_WIDTH) self.view.setFixedHeight(view_height) # tool bar below the mini map self.toolbar = QtWidgets.QToolBar() self.toolbar.setIconSize(QtCore.QSize(20, 20)) # action group (only one of them can be checked at each time) action_group = QtWidgets.QActionGroup(self.toolbar) # political view in the beginning a = qt.create_action(tools.load_ui_icon('icon.mini.political.png'), 'Show political view', action_group, toggle_connection=self.switch_to_political_view, checkable=True) self.toolbar.addAction(a) # geographical view a = qt.create_action(tools.load_ui_icon('icon.mini.geographical.png'), 'Show geographical view', action_group, toggle_connection=self.switch_to_geographical_view, checkable=True) self.toolbar.addAction(a) self.mode = constants.OverviewMapMode.POLITICAL # wrap tool bar into horizontal layout with stretch l = QtWidgets.QHBoxLayout() l.setContentsMargins(0, 0, 0, 0) l.addWidget(self.toolbar) l.addStretch() # add layout containing tool bar layout.addLayout(l) # graphics items in scene (except the tracker) self.scene_items = []
def __init__(self, *args, **kwargs): """ Create and add all tab """ super().__init__(*args, **kwargs) toolbar = QtWidgets.QToolBar() toolbar.setIconSize(QtCore.QSize(32, 32)) action_group = QtWidgets.QActionGroup(toolbar) # general preferences action_general = qt.create_action(tools.load_ui_icon('icon.preferences.general.png'), 'Show general preferences', action_group, toggle_connection=self._toggled_action_preferences_general, checkable=True) toolbar.addAction(action_general) # network preferences a = qt.create_action(tools.load_ui_icon('icon.preferences.network.png'), 'Show network preferences', action_group, toggle_connection=self._toggled_action_preferences_network, checkable=True) toolbar.addAction(a) # graphics preferences a = qt.create_action(tools.load_ui_icon('icon.preferences.graphics.png'), 'Show graphics preferences', action_group, toggle_connection=self._toggled_action_preferences_graphics, checkable=True) toolbar.addAction(a) # music preferences a = qt.create_action(tools.load_ui_icon('icon.preferences.music.png'), 'Show music preferences', action_group, toggle_connection=self._toggled_action_preferences_music, checkable=True) toolbar.addAction(a) # spacer spacer = QtWidgets.QWidget() spacer.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) toolbar.addWidget(spacer) # reset preferences a = QtWidgets.QAction(tools.load_ui_icon('icon.preferences.reset.png'), 'Reset preferences', self) a.triggered.connect(self.reset_preferences) toolbar.addAction(a) self.stacked_layout = QtWidgets.QStackedLayout() layout = QtWidgets.QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(toolbar) layout.addLayout(self.stacked_layout) # empty lists self._check_boxes = [] self._line_edits = [] self._sliders = [] # add tabs self._layout_widget_preferences_general() self._layout_widget_preferences_graphics() self._layout_widget_preferences_music() self._layout_widget_preferences_network() # initially show general preferences action_general.setChecked(True)
def contextMenuEvent(self, event): # noqa: N802 """ Right click (context click) on a tile. Shows the context menu, depending on the tile position """ logger.debug('contextMenuEvent event.pos:%s', event.pos()) # if there is no scenario existing, don't process the context click if not self.scenario.server_scenario: return # get mouse position in scene coordinates scene_position = self.mapToScene(event.pos()) / constants.TILE_SIZE column, row = self.scenario.server_scenario.map_position( scene_position.x(), scene_position.y()) # create context menu menu = QtWidgets.QMenu(self) # change terrain a = qt.create_action( tools.load_ui_icon('icon.editor.change_terrain.png'), 'Set terrain', self, partial(self.change_terrain.emit, column, row)) menu.addAction(a) terrain_type = self.scenario.server_scenario.terrain_at(column, row) if terrain_type != TerrainType.SEA.value: a = qt.create_action( tools.load_ui_icon('icon.editor.change_terrain_resource.png'), 'Set resource', self, partial(self.change_terrain_resource.emit, column, row)) menu.addAction(a) # is there a province province = self.scenario.server_scenario.province_at(column, row) if province: a = qt.create_action( tools.load_ui_icon('icon.editor.province_info.png'), 'Province info', self, partial(self.province_info.emit, province)) menu.addAction(a) # is there also nation nation = self.scenario.server_scenario.province_property( province, constants.ProvinceProperty.NATION) if nation: a = qt.create_action( tools.load_ui_icon('icon.editor.nation_info.png'), 'Nation info', self, partial(self.nation_info.emit, nation)) menu.addAction(a) menu.exec(event.globalPos())
def __init__(self, *args, **kwargs): """ Create toolbar. """ super().__init__(*args, **kwargs) self.layout = QtWidgets.QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) # create tool bar toolbar = QtWidgets.QToolBar() action_group = QtWidgets.QActionGroup(toolbar) # actions single player new/load a = qt.create_action( tools.load_ui_icon('icon.lobby.single.new.png'), 'Start new single player scenario', action_group, toggle_connection=self.toggled_single_player_scenario_selection, checkable=True) toolbar.addAction(a) a = qt.create_action( tools.load_ui_icon('icon.lobby.single.load.png'), 'Continue saved single player scenario', action_group, toggle_connection=self.toggled_single_player_load_scenario, checkable=True) toolbar.addAction(a) toolbar.addSeparator() # actions multi player a = qt.create_action(tools.load_ui_icon('icon.lobby.network.png'), 'Show server lobby', action_group, toggle_connection=self.toggled_server_lobby, checkable=True) toolbar.addAction(a) a = qt.create_action( tools.load_ui_icon('icon.lobby.multiplayer-game.png'), 'Start or continue multiplayer scenario', action_group, toggle_connection=self.toggled_multiplayer_scenario_selection, checkable=True) toolbar.addAction(a) self.layout.addWidget(toolbar, alignment=QtCore.Qt.AlignTop) self.content = None
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)
def __init__(self, *args, **kwargs): """ Sets up all the input elements of the create new scenario dialog. """ super().__init__(*args, **kwargs) self.parameters = {} widget_layout = QtWidgets.QVBoxLayout(self) # title box box = QtWidgets.QGroupBox('Title') layout = QtWidgets.QVBoxLayout(box) edit = QtWidgets.QLineEdit() edit.setFixedWidth(300) edit.setPlaceholderText('Unnamed') self.parameters[constants.ScenarioProperty.TITLE] = edit layout.addWidget(edit) widget_layout.addWidget(box) # map size box = QtWidgets.QGroupBox('Map size') layout = QtWidgets.QHBoxLayout(box) layout.addWidget(QtWidgets.QLabel('Width')) edit = QtWidgets.QLineEdit() edit.setFixedWidth(50) edit.setValidator(QtGui.QIntValidator(1, 1000)) edit.setPlaceholderText('100') self.parameters[constants.ScenarioProperty.MAP_COLUMNS] = edit layout.addWidget(edit) layout.addWidget(QtWidgets.QLabel('Height')) edit = QtWidgets.QLineEdit() edit.setFixedWidth(50) edit.setValidator(QtGui.QIntValidator(1, 1000)) edit.setPlaceholderText('60') self.parameters[constants.ScenarioProperty.MAP_ROWS] = edit layout.addWidget(edit) layout.addStretch() widget_layout.addWidget(box) # vertical stretch widget_layout.addStretch() # add confirmation button layout = QtWidgets.QHBoxLayout() toolbar = QtWidgets.QToolBar() a = qt.create_action(tools.load_ui_icon('icon.confirm.png'), 'Create new scenario', toolbar, self.on_ok) toolbar.addAction(a) layout.addStretch() layout.addWidget(toolbar) widget_layout.addLayout(layout)
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)
def __init__(self, client): """ Create and setup all the elements. """ self.scenario = EditorScenario() super().__init__(client, self.scenario, EditorMainMap(self.scenario, None)) # new, load, save scenario actions a = qt.create_action(tools.load_ui_icon('icon.scenario.new.png'), 'Create new scenario', self, self.new_scenario_dialog) self._toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.scenario.load.png'), 'Load scenario', self, self.load_scenario_dialog) self._toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.scenario.save.png'), 'Save scenario', self, self.save_scenario_dialog) self._toolbar.addAction(a) # main map self.main_map.change_terrain.connect(self.map_change_terrain) self.main_map.change_terrain_resource.connect( self.map_change_terrain_resource) self.main_map.province_info.connect(self.provinces_dialog) self.main_map.nation_info.connect(self.nations_dialog) # edit properties (general, nations, provinces) actions a = qt.create_action(tools.load_ui_icon('icon.editor.general.png'), 'Edit general properties', self, self.general_properties_dialog) self._toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.editor.nations.png'), 'Edit nations', self, self.nations_dialog) self._toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.editor.provinces.png'), 'Edit provinces', self, self.provinces_dialog) self._toolbar.addAction(a) self._layout.addWidget(self._toolbar, 0, 0, 1, 2) self._layout.addWidget(self._mini_map, 1, 0) self._layout.addWidget(self.main_map, 1, 1, 2, 1) self._layout.addWidget(self._info_panel, 2, 0) self._layout.setRowStretch( 2, 1) # the info box will take all vertical space left self._layout.setColumnStretch( 1, 1) # the main map will take all horizontal space left self._add_help_and_exit_buttons(client)
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
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)
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)
def __init__(self, client, server_base_scenario, selected_nation): logger.debug('__init__ server_base_scenario:%s, selected_nation:%s', server_base_scenario, selected_nation) self.scenario = GameScenario() self._main_map = MainMap(self.scenario, selected_nation) super().__init__(client, self.scenario, self._main_map) self._layout.addWidget(self._toolbar, 0, 0, 1, 2) self._layout.addWidget(self._mini_map, 1, 0) self.scenario.init(server_base_scenario) self._selected_object = GameSelectedObject(self._info_panel) self._default_cursor = self.main_map.viewport().cursor() self._main_map.mouse_press_event.connect( self._main_map_mouse_press_event) self._main_map.mouse_move_event.connect( self._main_map_mouse_move_event) self._turn_manager = TurnManager(self.scenario, selected_nation) self._turn_manager.event_turn_completed.connect( self._event_turn_completed) turn_end_widget = TurnEndWidget(self._turn_manager) order_buttons_widget = OrderButtonsWidget() self._layout.addWidget(order_buttons_widget, 2, 0) self._layout.addWidget(self._info_panel, 3, 0) self._layout.addWidget(turn_end_widget, 4, 0) self._layout.addWidget(self.main_map, 1, 1, 3, 1) self._layout.setRowStretch( 3, 1) # the info box will take all vertical space left self._layout.setColumnStretch( 1, 1) # the main map will take all horizontal space left self._workforce_widgets = {} # !!! TODO this is just to test, remove me a little bit later!!! self._create_workforce_widget(13, 29, WorkforceType.ENGINEER) self._create_workforce_widget(11, 27, WorkforceType.PROSPECTOR) self._create_workforce_widget(12, 30, WorkforceType.FARMER) self._create_workforce_widget(15, 29, WorkforceType.FORESTER) self._create_workforce_widget(14, 29, WorkforceType.MINER) self._create_workforce_widget(13, 29, WorkforceType.RANCHER) # !!! TODO remove above a = qt.create_action(tools.load_ui_icon('icon.scenario.load.png'), 'Load scenario', self, self.load_scenario_dialog) self._toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.scenario.save.png'), 'Save scenario', self, self.save_scenario_dialog) self._toolbar.addAction(a) self._add_help_and_exit_buttons(client) self._add_workforces() self._info_panel.refresh_nation_asset_info() logger.debug('__init__ finished')
def __init__(self, *args, **kwargs): """ Create and add all tab """ super().__init__(*args, **kwargs) toolbar = QtWidgets.QToolBar() toolbar.setIconSize(QtCore.QSize(32, 32)) action_group = QtWidgets.QActionGroup(toolbar) # general preferences action_general = qt.create_action( tools.load_ui_icon('icon.preferences.general.png'), 'Show general preferences', action_group, toggle_connection=self._toggled_action_preferences_general, checkable=True) toolbar.addAction(action_general) # network preferences a = qt.create_action( tools.load_ui_icon('icon.preferences.network.png'), 'Show network preferences', action_group, toggle_connection=self._toggled_action_preferences_network, checkable=True) toolbar.addAction(a) # graphics preferences a = qt.create_action( tools.load_ui_icon('icon.preferences.graphics.png'), 'Show graphics preferences', action_group, toggle_connection=self._toggled_action_preferences_graphics, checkable=True) toolbar.addAction(a) # music preferences a = qt.create_action( tools.load_ui_icon('icon.preferences.music.png'), 'Show music preferences', action_group, toggle_connection=self._toggled_action_preferences_music, checkable=True) toolbar.addAction(a) # spacer spacer = QtWidgets.QWidget() spacer.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) toolbar.addWidget(spacer) # reset preferences a = QtWidgets.QAction(tools.load_ui_icon('icon.preferences.reset.png'), 'Reset preferences', self) a.triggered.connect(self.reset_preferences) toolbar.addAction(a) self.stacked_layout = QtWidgets.QStackedLayout() layout = QtWidgets.QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(toolbar) layout.addLayout(self.stacked_layout) # empty lists self._check_boxes = [] self._line_edits = [] self._sliders = [] # add tabs self._layout_widget_preferences_general() self._layout_widget_preferences_graphics() self._layout_widget_preferences_music() self._layout_widget_preferences_network() # initially show general preferences action_general.setChecked(True)
def __init__(self, client): """ Create and setup all the elements. """ super().__init__() # store the client self.client = client # toolbar on top of the window self.toolbar = QtWidgets.QToolBar() self.toolbar.setIconSize(QtCore.QSize(32, 32)) # new, load, save scenario actions a = qt.create_action(tools.load_ui_icon('icon.scenario.new.png'), 'Create new scenario', self, self.new_scenario_dialog) self.toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.scenario.load.png'), 'Load scenario', self, self.load_scenario_dialog) self.toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.scenario.save.png'), 'Save scenario', self, self.save_scenario_dialog) self.toolbar.addAction(a) self.toolbar.addSeparator() # edit properties (general, nations, provinces) actions a = qt.create_action(tools.load_ui_icon('icon.editor.general.png'), 'Edit general properties', self, self.general_properties_dialog) self.toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.editor.nations.png'), 'Edit nations', self, self.nations_dialog) self.toolbar.addAction(a) a = qt.create_action(tools.load_ui_icon('icon.editor.provinces.png'), 'Edit provinces', self, self.provinces_dialog) self.toolbar.addAction(a) # spacer spacer = QtWidgets.QWidget() spacer.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) self.toolbar.addWidget(spacer) clock = qt.ClockLabel() self.toolbar.addWidget(clock) # help and exit action a = QtWidgets.QAction(tools.load_ui_icon('icon.help.png'), 'Show help', self) a.triggered.connect(client.show_help_browser) # TODO with partial make reference to specific page self.toolbar.addAction(a) a = QtWidgets.QAction(tools.load_ui_icon('icon.back_to_startscreen.png'), 'Exit to main menu', self) a.triggered.connect(client.switch_to_start_screen) # TODO ask if something is changed we should save.. (you might loose progress) self.toolbar.addAction(a) # info box widget self.info_panel = InfoPanel() # main map self.main_map = MainMap() self.main_map.focus_changed.connect(self.info_panel.update_tile_info) self.main_map.change_terrain.connect(self.map_change_terrain) self.main_map.province_info.connect(self.provinces_dialog) self.main_map.nation_info.connect(self.nations_dialog) # mini map self.mini_map = MiniMap() self.mini_map.roi_changed.connect(self.main_map.set_center_position) # connect to editor_scenario editor_scenario.changed.connect(self.scenario_changed) # layout of widgets and toolbar layout = QtWidgets.QGridLayout(self) layout.addWidget(self.toolbar, 0, 0, 1, 2) layout.addWidget(self.mini_map, 1, 0) layout.addWidget(self.info_panel, 2, 0) layout.addWidget(self.main_map, 1, 1, 2, 1) layout.setRowStretch(2, 1) # the info box will take all vertical space left layout.setColumnStretch(1, 1) # the main map will take all horizontal space left
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