Beispiel #1
0
class OverviewWidget(QWidget):
    def __init__(self, parent=None):
        super(OverviewWidget, self).__init__(parent)
        # objects, sub-windows
        self.ui = None
        self._btn_reload = None
        self._aswidget = None
        self.world = XNovaWorld_instance()
        # build progress widgets
        self.bp_widgets = dict()  # build progress widgets
        self.bp_widgets_sy = dict()  # shipyard build progress widgets
        self.bp_widgets_res = dict()  # researches build progress widgets

    def load_ui(self):
        # layout
        self._layout = QVBoxLayout()
        self._layout_topbuttons = QHBoxLayout()
        self.setLayout(self._layout)
        self._layout.addLayout(self._layout_topbuttons)
        # sub-windows
        # reload button
        self._btn_reload = QPushButton(self.tr('Refresh overview'), self)
        self._btn_reload.setIcon(QIcon(':i/reload.png'))
        self._btn_reload.clicked.connect(self.on_btn_refresh_overview)
        self._layout_topbuttons.addWidget(self._btn_reload)
        self._layout_topbuttons.addStretch()
        # group box to hold builds info
        self._gb_builds = CollapsibleFrame(self)
        self._gb_builds.setTitle(self.tr('Building Jobs'))
        self._layout.addWidget(self._gb_builds)
        # groupbox to hold shipyard builds info
        self._gb_shipyard = CollapsibleFrame(self)
        self._gb_shipyard.setTitle(self.tr('Shipyard Jobs'))
        self._layout.addWidget(self._gb_shipyard)
        # groupbox to hold researches in progress info
        self._gb_research = CollapsibleFrame(self)
        self._gb_research.setTitle(self.tr('Researches'))
        self._layout.addWidget(self._gb_research)
        # groupbox to hold account stats widget
        self._gb_accstats = CollapsibleFrame(self)
        self._gb_accstats.setTitle(self.tr('Statistics'))
        self._layout.addWidget(self._gb_accstats)
        # account stats widget
        self._aswidget = Overview_AccStatsWidget(self)
        self._aswidget.load_ui()
        self._aswidget.show()
        self._gb_accstats.addWidget(self._aswidget)
        self._gb_accstats.expand()  # staticstics expanded by default
        # add final spacer
        self._layout.addStretch()

    def update_account_info(self):
        a = self.world.get_account_info()
        if self._aswidget is not None:
            self._aswidget.update_account_info(a)

    def get_bpw_for_planet(self,
                           planet_id: int,
                           typ: str = '') -> BuildProgressWidget:
        if typ == '':
            if planet_id in self.bp_widgets:
                return self.bp_widgets[planet_id]
            # create BPW for planet
            bpw = BuildProgressWidget(self)
            bpw.requestCancelBuildWithPlanet.connect(
                self.on_request_build_cancel_with_planet)
            self._gb_builds.addWidget(bpw)
            self.bp_widgets[planet_id] = bpw
            bpw.hide()
            return bpw
        elif typ == BuildProgressWidget.BPW_TYPE_SHIPYARD:
            if planet_id in self.bp_widgets_sy:
                return self.bp_widgets_sy[planet_id]
            # create BPW for planet shipyard
            bpw = BuildProgressWidget(self)
            bpw.requestCancelBuildWithPlanet.connect(
                self.on_request_build_cancel_with_planet)
            self._gb_shipyard.addWidget(bpw)
            self.bp_widgets_sy[planet_id] = bpw
            bpw.hide()
            return bpw
        elif typ == BuildProgressWidget.BPW_TYPE_RESEARCH:
            if planet_id in self.bp_widgets_res:
                return self.bp_widgets_res[planet_id]
            # create BPW for planet shipyard
            bpw = BuildProgressWidget(self)
            bpw.requestCancelBuildWithPlanet.connect(
                self.on_request_build_cancel_with_planet)
            self._gb_research.addWidget(bpw)
            self.bp_widgets_res[planet_id] = bpw
            bpw.hide()
            return bpw
        else:
            logger.error(
                'get_bpw_for_planet(): unknown typre requested: {0}'.format(
                    typ))

    def update_builds(self):
        self.setUpdatesEnabled(
            False)  # big visual changes may follow, stop screen flicker
        # delete existing build progress widgets (do not do it, just hide)
        for bpw in self.bp_widgets.values():
            bpw.hide()
        for bpw in self.bp_widgets_sy.values():
            bpw.hide()
        planets = self.world.get_planets()
        for pl in planets:
            # buildings
            bpw = self.get_bpw_for_planet(pl.planet_id)
            bpw.update_from_planet(pl)
            # shipyard
            if len(pl.shipyard_progress_items) > 0:
                bpw = self.get_bpw_for_planet(
                    pl.planet_id, BuildProgressWidget.BPW_TYPE_SHIPYARD)
                bpw.show()
                bpw.update_from_planet(pl,
                                       BuildProgressWidget.BPW_TYPE_SHIPYARD)
            # researches
            bpw = self.get_bpw_for_planet(
                pl.planet_id, BuildProgressWidget.BPW_TYPE_RESEARCH)
            bpw.update_from_planet(pl, BuildProgressWidget.BPW_TYPE_RESEARCH)
        # make equal widths (this is not working, why?)
        # self._equalize_builds_widths()
        self.setUpdatesEnabled(True)

    def _equalize_builds_widths(self):
        maxwidth = -1
        for bpw in self.bp_widgets:
            w = bpw.get_els_widths()
            if w > maxwidth:
                maxwidth = w
        for bpw in self.bp_widgets:
            w = bpw.make_as_wide_as(maxwidth)
        logger.debug('got max width: {0}'.format(maxwidth))

    @pyqtSlot()
    def on_btn_refresh_overview(self):
        self.world.signal(self.world.SIGNAL_RELOAD_PAGE, page_name='overview')

    @pyqtSlot(XNPlanetBuildingItem, int)
    def on_request_build_cancel_with_planet(self, bitem: XNPlanetBuildingItem,
                                            planet_id: int):
        self.world.signal(self.world.SIGNAL_BUILD_CANCEL,
                          planet_id=planet_id,
                          bitem=bitem)
Beispiel #2
0
class OverviewWidget(QWidget):
    def __init__(self, parent=None):
        super(OverviewWidget, self).__init__(parent)
        # objects, sub-windows
        self.ui = None
        self._btn_reload = None
        self._aswidget = None
        self.world = XNovaWorld_instance()
        # build progress widgets
        self.bp_widgets = dict()  # build progress widgets
        self.bp_widgets_sy = dict()  # shipyard build progress widgets
        self.bp_widgets_res = dict()  # researches build progress widgets

    def load_ui(self):
        # layout
        self._layout = QVBoxLayout()
        self._layout_topbuttons = QHBoxLayout()
        self.setLayout(self._layout)
        self._layout.addLayout(self._layout_topbuttons)
        # sub-windows
        # reload button
        self._btn_reload = QPushButton(self.tr('Refresh overview'), self)
        self._btn_reload.setIcon(QIcon(':i/reload.png'))
        self._btn_reload.clicked.connect(self.on_btn_refresh_overview)
        self._layout_topbuttons.addWidget(self._btn_reload)
        self._layout_topbuttons.addStretch()
        # group box to hold builds info
        self._gb_builds = CollapsibleFrame(self)
        self._gb_builds.setTitle(self.tr('Building Jobs'))
        self._layout.addWidget(self._gb_builds)
        # groupbox to hold shipyard builds info
        self._gb_shipyard = CollapsibleFrame(self)
        self._gb_shipyard.setTitle(self.tr('Shipyard Jobs'))
        self._layout.addWidget(self._gb_shipyard)
        # groupbox to hold researches in progress info
        self._gb_research = CollapsibleFrame(self)
        self._gb_research.setTitle(self.tr('Researches'))
        self._layout.addWidget(self._gb_research)
        # groupbox to hold account stats widget
        self._gb_accstats = CollapsibleFrame(self)
        self._gb_accstats.setTitle(self.tr('Statistics'))
        self._layout.addWidget(self._gb_accstats)
        # account stats widget
        self._aswidget = Overview_AccStatsWidget(self)
        self._aswidget.load_ui()
        self._aswidget.show()
        self._gb_accstats.addWidget(self._aswidget)
        self._gb_accstats.expand()  # staticstics expanded by default
        # add final spacer
        self._layout.addStretch()

    def update_account_info(self):
        a = self.world.get_account_info()
        if self._aswidget is not None:
            self._aswidget.update_account_info(a)

    def get_bpw_for_planet(self, planet_id: int, typ: str='') -> BuildProgressWidget:
        if typ == '':
            if planet_id in self.bp_widgets:
                return self.bp_widgets[planet_id]
            # create BPW for planet
            bpw = BuildProgressWidget(self)
            bpw.requestCancelBuildWithPlanet.connect(self.on_request_build_cancel_with_planet)
            self._gb_builds.addWidget(bpw)
            self.bp_widgets[planet_id] = bpw
            bpw.hide()
            return bpw
        elif typ == BuildProgressWidget.BPW_TYPE_SHIPYARD:
            if planet_id in self.bp_widgets_sy:
                return self.bp_widgets_sy[planet_id]
            # create BPW for planet shipyard
            bpw = BuildProgressWidget(self)
            bpw.requestCancelBuildWithPlanet.connect(self.on_request_build_cancel_with_planet)
            self._gb_shipyard.addWidget(bpw)
            self.bp_widgets_sy[planet_id] = bpw
            bpw.hide()
            return bpw
        elif typ == BuildProgressWidget.BPW_TYPE_RESEARCH:
            if planet_id in self.bp_widgets_res:
                return self.bp_widgets_res[planet_id]
            # create BPW for planet shipyard
            bpw = BuildProgressWidget(self)
            bpw.requestCancelBuildWithPlanet.connect(self.on_request_build_cancel_with_planet)
            self._gb_research.addWidget(bpw)
            self.bp_widgets_res[planet_id] = bpw
            bpw.hide()
            return bpw
        else:
            logger.error('get_bpw_for_planet(): unknown typre requested: {0}'.format(typ))

    def update_builds(self):
        self.setUpdatesEnabled(False)  # big visual changes may follow, stop screen flicker
        # delete existing build progress widgets (do not do it, just hide)
        for bpw in self.bp_widgets.values():
            bpw.hide()
        for bpw in self.bp_widgets_sy.values():
            bpw.hide()
        planets = self.world.get_planets()
        for pl in planets:
            # buildings
            bpw = self.get_bpw_for_planet(pl.planet_id)
            bpw.update_from_planet(pl)
            # shipyard
            if len(pl.shipyard_progress_items) > 0:
                bpw = self.get_bpw_for_planet(pl.planet_id, BuildProgressWidget.BPW_TYPE_SHIPYARD)
                bpw.show()
                bpw.update_from_planet(pl, BuildProgressWidget.BPW_TYPE_SHIPYARD)
            # researches
            bpw = self.get_bpw_for_planet(pl.planet_id, BuildProgressWidget.BPW_TYPE_RESEARCH)
            bpw.update_from_planet(pl, BuildProgressWidget.BPW_TYPE_RESEARCH)
        # make equal widths (this is not working, why?)
        # self._equalize_builds_widths()
        self.setUpdatesEnabled(True)

    def _equalize_builds_widths(self):
        maxwidth = -1
        for bpw in self.bp_widgets:
            w = bpw.get_els_widths()
            if w > maxwidth:
                maxwidth = w
        for bpw in self.bp_widgets:
            w = bpw.make_as_wide_as(maxwidth)
        logger.debug('got max width: {0}'.format(maxwidth))

    @pyqtSlot()
    def on_btn_refresh_overview(self):
        self.world.signal(self.world.SIGNAL_RELOAD_PAGE, page_name='overview')

    @pyqtSlot(XNPlanetBuildingItem, int)
    def on_request_build_cancel_with_planet(self, bitem: XNPlanetBuildingItem, planet_id: int):
        self.world.signal(self.world.SIGNAL_BUILD_CANCEL,
                          planet_id=planet_id,
                          bitem=bitem)