示例#1
0
class TableWindow(QMainWindow):
    def __init__(self, title):
        super(TableWindow, self).__init__()

        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle(title)

        self.layout = QHBoxLayout(self.ui.centralwidget)
        self.ui.windowTitle = title
        self.ui.centralwidget.setLayout(self.layout)
        self.tableView = QTableView()
        self.layout.addWidget(self.tableView)
        self.tableView.setHorizontalHeader(MyHeaderView(Qt.Horizontal))
        self.tableView.setVerticalHeader(MyHeaderView(Qt.Vertical))

    def setModel(self, model):
        self.tableView.setModel(model)
        margins = self.layout.contentsMargins()
        self.resize((margins.left() + margins.right() +
                     self.tableView.frameWidth() * 2 +
                     self.tableView.verticalHeader().width() +
                     self.tableView.horizontalHeader().length()),
                    self.height())
    def __init__(self, window, settings, *args, **kwargs):
        super(GeneralTab, self).__init__(*args, **kwargs)

        self.win = window
        self.settings = settings

        # Create inputs for each general option.
        self.websiteFileField = QLineEdit()
        self.websiteFileField.textChanged.connect(self.websiteFileFieldChanged)

        # The index of each launch option should correspond to the correct LaunchMode.
        self.launchOpt = QComboBox()
        self.launchOpt.addItems(
            ["Webiste", "Open file", "Restore last session"])
        self.launchOpt.currentIndexChanged.connect(self.launchModeValueChanged)

        self.intervalSpinner = QSpinBox()
        self.intervalSpinner.setMinimum(0)
        self.intervalSpinner.valueChanged.connect(
            self.saveIntervalValueChanged)

        self.splashScreenCheckBox = QCheckBox()
        self.splashScreenCheckBox.stateChanged.connect(
            self.enableSplashScreenStateChanged)

        splashScreenContainer = QWidget()
        splashScreenLayout = QHBoxLayout(splashScreenContainer)
        margin = splashScreenLayout.contentsMargins()
        splashScreenLayout.setContentsMargins(0, margin.top(), 0, 0)
        splashScreenLayout.setAlignment(Qt.AlignLeft)
        splashScreenLayout.addWidget(self.splashScreenCheckBox)

        label = QLabel("Enable splash screen")
        label.setToolTip(
            "When the splash screen is disabled, it is not possible to interrupt the startup anymore."
        )
        splashScreenLayout.addWidget(label)

        # Add all elements to the tab.
        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignTop)

        layout.addWidget(QLabel("Launch mode:"))
        layout.addWidget(self.launchOpt)

        self.websiteFileLabel = QLabel("Website URL:")
        layout.addWidget(self.websiteFileLabel)
        layout.addWidget(self.websiteFileField)

        label = QLabel("Save interval:")
        label.setToolTip(
            "Save the application each n seconds. Choose 0 to disable saving.")
        layout.addWidget(label)
        layout.addWidget(self.intervalSpinner)

        layout.addWidget(splashScreenContainer)

        # Fill all the text fields.
        self.loadSettings()
示例#3
0
class HorizontalImageScrollArea(QScrollArea):
    def __init__(self, parent=None):
        super(HorizontalImageScrollArea, self).__init__(parent)
        self.setWidgetResizable(True)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setFrameShape(QFrame.NoFrame)

        self.layout = QHBoxLayout()

        scroll = QWidget()
        scroll.setLayout(self.layout)
        self.setWidget(scroll)

    def eventFilter(self, obj, event):
        if obj == self.widget() and event.type() == QEvent.Resize:
            self.widget().resize(self.calcNewSize())
            return True

        return super(HorizontalImageScrollArea, self).eventFilter(obj, event)

    def calcNewSize(self):
        height = self.viewport().height()

        layoutMargins = self.layout.contentsMargins()
        heightForCalc = height - layoutMargins.top() - layoutMargins.bottom()

        width = self.calcWidthForHeight(heightForCalc)
        return QSize(width, height)

    def calcWidthForHeight(self, height):
        width = 0
        for wgt in range(self.layout.count()):
            width += self.layout.itemAt(wgt).widget().widthForHeight(height)

        if self.layout.count() > 1:
            width += self.layout.spacing() * (self.layout.count() - 1)
        return width
示例#4
0
class TableWindow(QMainWindow):
    def __init__(self, title):
        super(TableWindow, self).__init__()

        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle(title)

        self.layout = QHBoxLayout(self.ui.centralwidget)
        self.ui.windowTitle = title
        self.ui.centralwidget.setLayout(self.layout)
        self.tableView = QTableView()
        self.layout.addWidget(self.tableView)
        self.tableView.setHorizontalHeader(MyHeaderView(Qt.Horizontal))
        self.tableView.setVerticalHeader(MyHeaderView(Qt.Vertical))

    def setModel(self, model):
        self.tableView.setModel(model)
        margins = self.layout.contentsMargins()
        self.resize((
            margins.left() + margins.right() + self.tableView.frameWidth() * 2 + self.tableView.verticalHeader().width() + self.tableView.horizontalHeader().length()),
            self.height())
示例#5
0
class Planet_BasicInfoPanel(QFrame):

    requestOpenGalaxy = pyqtSignal(XNCoords)
    requestRefreshPlanet = pyqtSignal()
    requestRenamePlanet = pyqtSignal(int, str)  # planet_id, new_planet_name

    def __init__(self, parent: QWidget):
        super(Planet_BasicInfoPanel, self).__init__(parent)
        #
        self._planet_pic_url = ''
        self._pixmap = QPixmap()
        self._planet = XNPlanet()
        # setup frame
        self.setFrameShape(QFrame.StyledPanel)
        self.setFrameShadow(QFrame.Raised)
        # bold font
        font = self.font()
        font.setWeight(QFont.Bold)
        # resource pictures
        self._pix_met = QPixmap(':/i/s_metall.png')
        self._pix_cry = QPixmap(':/i/s_kristall.png')
        self._pix_deit = QPixmap(':/i/s_deuterium.png')
        self._pix_energy = QPixmap(':/i/s_energy.png')
        # layout
        self._layout = QHBoxLayout()
        self._layout.setContentsMargins(5, 5, 5, 5)
        self._layout.setSpacing(5)
        self.setLayout(self._layout)
        self._vlayout = QVBoxLayout()
        self._hlayout_name_coords = QHBoxLayout()
        self._hlayout_fields = QHBoxLayout()
        self._hlayout_res = QHBoxLayout()
        self._hlayout_resmax = QHBoxLayout()
        self._hlayout_energy = QHBoxLayout()
        # labels
        self._lbl_img = QLabel(self)
        self._lbl_name = QLabel(self)
        self._lbl_coords = QLabel(self)
        self._lbl_coords.linkActivated.connect(self.on_coords_link_activated)
        self._lbl_fields = QLabel()
        # resource labels
        self._lbl_res_on_planet = QLabel(self.tr('Resources:'), self)
        self._lbl_metal = QLabel(self)
        self._lbl_crystal = QLabel(self)
        self._lbl_deit = QLabel(self)
        self._lbl_cur_met = QLabel(self)
        self._lbl_cur_cry = QLabel(self)
        self._lbl_cur_deit = QLabel(self)
        self._lbl_cur_met.setFont(font)
        self._lbl_cur_cry.setFont(font)
        self._lbl_cur_deit.setFont(font)
        self._lbl_metal.setPixmap(self._pix_met)
        self._lbl_crystal.setPixmap(self._pix_cry)
        self._lbl_deit.setPixmap(self._pix_deit)
        # resource max
        self._lbl_res_max = QLabel(self.tr('Capacity:'), self)
        self._lbl_metal2 = QLabel(self)
        self._lbl_crystal2 = QLabel(self)
        self._lbl_deit2 = QLabel(self)
        self._lbl_max_met = QLabel(self)
        self._lbl_max_cry = QLabel(self)
        self._lbl_max_deit = QLabel(self)
        self._lbl_max_met.setFont(font)
        self._lbl_max_cry.setFont(font)
        self._lbl_max_deit.setFont(font)
        self._lbl_metal2.setPixmap(self._pix_met)
        self._lbl_crystal2.setPixmap(self._pix_cry)
        self._lbl_deit2.setPixmap(self._pix_deit)
        # energy labels
        self._lbl_energy = QLabel(self.tr('Energy, charge:'), self)
        self._lbl_energy_pix = QLabel(self)
        self._lbl_energy_pix.setPixmap(self._pix_energy)
        self._lbl_energy_stats = QLabel(self)
        self._lbl_energy_stats.setFont(font)
        # button
        self._btn_refresh = QPushButton(self.tr('Refresh planet'), self)
        self._btn_refresh.setIcon(QIcon(':/i/reload.png'))
        self._btn_refresh.clicked.connect(self.on_btn_refresh_clicked)
        self._btn_refresh.setMinimumHeight(25)
        self._btn_tools = QToolButton(self)
        self._btn_tools.setIcon(QIcon(':/i/tools_32.png'))
        self._btn_tools.setText(self.tr('Actions...'))
        self._btn_tools.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
        self._btn_tools.setPopupMode(QToolButton.InstantPopup)
        self._btn_tools.setMinimumHeight(25)
        self._actions_menu = QMenu(self)
        self._action_renameplanet = QAction(self.tr('Rename planet'), self)
        self._action_leaveplanet = QAction(self.tr('Leave planet'), self)
        self._actions_menu.addAction(self._action_renameplanet)
        self._actions_menu.addAction(self._action_leaveplanet)
        self._btn_tools.setMenu(self._actions_menu)
        self._action_renameplanet.triggered.connect(
            self.on_action_renameplanet)
        self._action_leaveplanet.triggered.connect(self.on_action_leaveplanet)
        # finalize layout
        self._hlayout_name_coords.addWidget(self._lbl_name)
        self._hlayout_name_coords.addWidget(self._lbl_coords)
        self._hlayout_name_coords.addWidget(self._btn_refresh)
        self._hlayout_name_coords.addWidget(self._btn_tools)
        self._hlayout_name_coords.addStretch()
        #
        self._hlayout_fields.addWidget(self._lbl_fields)
        self._hlayout_fields.addStretch()
        #
        self._hlayout_res.addWidget(self._lbl_res_on_planet)
        self._hlayout_res.addWidget(self._lbl_metal)
        self._hlayout_res.addWidget(self._lbl_cur_met)
        self._hlayout_res.addWidget(self._lbl_crystal)
        self._hlayout_res.addWidget(self._lbl_cur_cry)
        self._hlayout_res.addWidget(self._lbl_deit)
        self._hlayout_res.addWidget(self._lbl_cur_deit)
        self._hlayout_res.addStretch()
        #
        self._hlayout_resmax.addWidget(self._lbl_res_max)
        self._hlayout_resmax.addWidget(self._lbl_metal2)
        self._hlayout_resmax.addWidget(self._lbl_max_met)
        self._hlayout_resmax.addWidget(self._lbl_crystal2)
        self._hlayout_resmax.addWidget(self._lbl_max_cry)
        self._hlayout_resmax.addWidget(self._lbl_deit2)
        self._hlayout_resmax.addWidget(self._lbl_max_deit)
        self._hlayout_resmax.addStretch()
        # minimum widths for res labels
        lbl_width = 100
        res_width = 120
        self._lbl_res_on_planet.setMinimumWidth(lbl_width)
        self._lbl_res_max.setMinimumWidth(lbl_width)
        self._lbl_energy.setMinimumWidth(lbl_width)
        self._lbl_cur_met.setMinimumWidth(res_width)
        self._lbl_cur_cry.setMinimumWidth(res_width)
        self._lbl_cur_deit.setMinimumWidth(res_width)
        self._lbl_max_met.setMinimumWidth(res_width)
        self._lbl_max_cry.setMinimumWidth(res_width)
        self._lbl_max_deit.setMinimumWidth(res_width)
        #
        self._hlayout_energy.addWidget(self._lbl_energy)
        self._hlayout_energy.addWidget(self._lbl_energy_pix)
        self._hlayout_energy.addWidget(self._lbl_energy_stats)
        self._hlayout_energy.addStretch()
        #
        self._vlayout.addLayout(self._hlayout_name_coords)
        self._vlayout.addLayout(self._hlayout_fields)
        self._vlayout.addLayout(self._hlayout_res)
        self._vlayout.addLayout(self._hlayout_resmax)
        self._vlayout.addLayout(self._hlayout_energy)
        self._vlayout.addStretch()
        #
        self._layout.addWidget(self._lbl_img, 0, Qt.AlignTop | Qt.AlignHCenter)
        self._layout.addLayout(self._vlayout)
        self._layout.addStretch()

    def setup_from_planet(self, planet: XNPlanet):
        # store references
        self._planet = planet
        self._planet_pic_url = planet.pic_url
        # deal with planet pic
        file_name = './cache/img/{0}'.format(
            self._planet_pic_url.replace('/', '_'))
        self._pixmap = QPixmap(file_name)
        self._lbl_img.setPixmap(self._pixmap)
        # setup widget max height based on picture size and layout's margins
        margins = self._layout.contentsMargins()
        top_margin = margins.top()
        bottom_margin = margins.bottom()
        max_height = self._pixmap.height() + top_margin + bottom_margin
        if max_height < 130:
            max_height = 130
        self.setMaximumHeight(max_height)
        # planet name, corods, fields
        self._lbl_name.setText(planet.name)
        self._lbl_coords.setText('<a href="{0}">{0}</a>'.format(
            planet.coords.coords_str()))
        fields_left_str = '{0}: {1}'.format(
            self.tr('left'), planet.fields_total - planet.fields_busy)
        self._lbl_fields.setText(
            self.tr('Fields:') + ' {0} / {1} ({2})'.format(
                planet.fields_busy, planet.fields_total, fields_left_str))
        # resources
        self.update_resources()

    def update_resources(self):
        # update planet resources
        color_enough = '#008800'
        color_exceed = '#AA0000'
        # cur metal
        color = color_enough
        if self._planet.res_current.met > self._planet.res_max_silos.met:
            color = color_exceed
        self._lbl_cur_met.setText('<font color="{0}">{1}</font>'.format(
            color, number_format(int(self._planet.res_current.met))))
        # cur crystal
        color = color_enough
        if self._planet.res_current.cry > self._planet.res_max_silos.cry:
            color = color_exceed
        self._lbl_cur_cry.setText('<font color="{0}">{1}</font>'.format(
            color, number_format(int(self._planet.res_current.cry))))
        # cur deit
        color = color_enough
        if self._planet.res_current.deit > self._planet.res_max_silos.deit:
            color = color_exceed
        self._lbl_cur_deit.setText('<font color="{0}">{1}</font>'.format(
            color, number_format(int(self._planet.res_current.deit))))
        # update res max
        self._lbl_max_met.setText(
            number_format(int(self._planet.res_max_silos.met)))
        self._lbl_max_cry.setText(
            number_format(int(self._planet.res_max_silos.cry)))
        self._lbl_max_deit.setText(
            number_format(int(self._planet.res_max_silos.deit)))
        # energy
        self._lbl_energy_stats.setText('{0} / {1}    ({2}%)'.format(
            self._planet.energy.energy_left, self._planet.energy.energy_total,
            self._planet.energy.charge_percent))

    @pyqtSlot(str)
    def on_coords_link_activated(self, link: str):
        coords = XNCoords()
        coords.parse_str(link, raise_on_error=True)
        self.requestOpenGalaxy.emit(coords)

    @pyqtSlot()
    def on_btn_refresh_clicked(self):
        self.requestRefreshPlanet.emit()

    @pyqtSlot()
    def on_action_renameplanet(self):
        new_name = input_string_dialog(self, self.tr('Rename planet'),
                                       self.tr('Enter new planet name:'),
                                       self._planet.name)
        if (new_name is not None) and (new_name != self._planet.name):
            self.requestRenamePlanet.emit(self._planet.planet_id, new_name)

    @pyqtSlot()
    def on_action_leaveplanet(self):
        QMessageBox.warning(self, self.tr('Not done'),
                            self.tr('Leaving planet is not done!'))
示例#6
0
class BaseLineEdit(QLineEdit):
    editSig = pyqtSignal()

    def __init__(self, *args, leftWidget=None, rightWidget=None, **kwargs):
        super(BaseLineEdit, self).__init__(*args, **kwargs)

        self.textSelectionEnabled = True
        self.leftRightLayout = None
        self.leftWidget = leftWidget
        self.rightWidget = rightWidget

        if self.leftWidget or self.rightWidget:
            # 对行编辑框内的部件进行布局
            self.leftRightLayout = QHBoxLayout(self)
            self.leftRightLayout.setContentsMargins(2, 2, 2, 2)
            self.leftRightLayout.setSpacing(0)
            if self.leftWidget:
                self.leftRightLayout.addWidget(leftWidget)
            self.leftRightLayout.addStretch()
            if self.rightWidget:
                self.leftRightLayout.addWidget(rightWidget)

    def emitEditSig(self):
        '''
        @brief:   主动发射编辑信号editSig()
        因为Qt4中signals是protected权限,无法类外调用,所以这里设置一个public权限接口,实现在类外
        调用主动发射编辑信号
        @author:  缪庆瑞
        @date:    2020.05.26
        '''
        self.setFocus()
        self.editSig.emit()

    def setLeftRightLayoutMargin(self, left=0, right=0, top=0, bottom=0):
        '''
        *@brief:   设置行编辑框左右布局margin,主要为了方便调整左右侧部件的位置
        *@author:  缪庆瑞
        *@date:    2020.04.11
        *@param:   left:左边间距 right:右边间距
        *@param:   top:上边间距 bottom:下边间距 默认0,一般不需要调整
        '''
        if self.leftRightLayout:
            self.leftRightLayout.setContentsMargins(left, top, right, bottom)
            '''
            在编辑框显示之前左右部件的大小如果没有fixed,那么默认将采用部件自身的推荐大小。所以
            这里只在部件显示(内部已完成布局)的情况下才自动调整文本的margin,避免按照部件推荐大小
            设置margin导致编辑框的大小策略受到不良影响。
            '''
            if self.isVisible():
                self.autoAdjustTextMargins()

    def mouseMoveEvent(self, event):
        '''
        *@brief:   鼠标移动事件处理
        *@author:  缪庆瑞
        *@date:    2020.05.20
        *@param:   event:鼠标事件
        '''
        if self.textSelectionEnabled:
            QLineEdit.mouseMoveEvent(self, event)

    def mouseReleaseEvent(self, event):
        '''
        *@brief:   鼠标释放事件处理
        *@author:  缪庆瑞
        *@date:    2020.04.11
        *@param:   event:鼠标事件
        '''
        # 左键释放并且非只读模式
        if event.button() == Qt.LeftButton and not self.isReadOnly():
            self.editSig.emit()  # 发射编辑信号
        QLineEdit.mouseReleaseEvent(self, event)

    def mouseDoubleClickEvent(self, event):
        '''
        *@brief:   鼠标双击事件处理
        *@author:  缪庆瑞
        *@date:    2020.05.20
        *@param:   event:鼠标事件
        '''
        if self.textSelectionEnabled:
            QLineEdit.mouseDoubleClickEvent(self, event)

    def resizeEvent(self, event):
        '''
        *@brief:   部件大小调整事件处理 (用来自动调整编辑框文本的margins)
        *@author:  缪庆瑞
        *@date:    2020.04.11
        *@param:   event:大小事件
        '''
        QLineEdit.resizeEvent(self, event)
        self.autoAdjustTextMargins()

    def autoAdjustTextMargins(self):
        '''
        *@brief:   根据布局的边距和部件的宽度,自动调整编辑框文本的margins
        *@author:  缪庆瑞
        *@date:    2020.04.11
        '''
        if self.leftRightLayout:
            leftMargin = self.leftWidget.width() if self.leftWidget else 0
            rightMargin = self.rightWidget.width() if self.rightWidget else 0
            leftMargin += self.leftRightLayout.contentsMargins().left()
            rightMargin += self.leftRightLayout.contentsMargins().right()
            # 设置编辑框的margin 确保文本不会被部件遮挡
            self.setTextMargins(leftMargin, 0, rightMargin, 0)
示例#7
0
class Planet_BasicInfoPanel(QFrame):

    requestOpenGalaxy = pyqtSignal(XNCoords)
    requestRefreshPlanet = pyqtSignal()
    requestRenamePlanet = pyqtSignal(int, str)   # planet_id, new_planet_name

    def __init__(self, parent: QWidget):
        super(Planet_BasicInfoPanel, self).__init__(parent)
        #
        self._planet_pic_url = ''
        self._pixmap = QPixmap()
        self._planet = XNPlanet()
        # setup frame
        self.setFrameShape(QFrame.StyledPanel)
        self.setFrameShadow(QFrame.Raised)
        # bold font
        font = self.font()
        font.setWeight(QFont.Bold)
        # resource pictures
        self._pix_met = QPixmap(':/i/s_metall.png')
        self._pix_cry = QPixmap(':/i/s_kristall.png')
        self._pix_deit = QPixmap(':/i/s_deuterium.png')
        self._pix_energy = QPixmap(':/i/s_energy.png')
        # layout
        self._layout = QHBoxLayout()
        self._layout.setContentsMargins(5, 5, 5, 5)
        self._layout.setSpacing(5)
        self.setLayout(self._layout)
        self._vlayout = QVBoxLayout()
        self._hlayout_name_coords = QHBoxLayout()
        self._hlayout_fields = QHBoxLayout()
        self._hlayout_res = QHBoxLayout()
        self._hlayout_resmax = QHBoxLayout()
        self._hlayout_energy = QHBoxLayout()
        # labels
        self._lbl_img = QLabel(self)
        self._lbl_name = QLabel(self)
        self._lbl_coords = QLabel(self)
        self._lbl_coords.linkActivated.connect(self.on_coords_link_activated)
        self._lbl_fields = QLabel()
        # resource labels
        self._lbl_res_on_planet = QLabel(self.tr('Resources:'), self)
        self._lbl_metal = QLabel(self)
        self._lbl_crystal = QLabel(self)
        self._lbl_deit = QLabel(self)
        self._lbl_cur_met = QLabel(self)
        self._lbl_cur_cry = QLabel(self)
        self._lbl_cur_deit = QLabel(self)
        self._lbl_cur_met.setFont(font)
        self._lbl_cur_cry.setFont(font)
        self._lbl_cur_deit.setFont(font)
        self._lbl_metal.setPixmap(self._pix_met)
        self._lbl_crystal.setPixmap(self._pix_cry)
        self._lbl_deit.setPixmap(self._pix_deit)
        # resource max
        self._lbl_res_max = QLabel(self.tr('Capacity:'), self)
        self._lbl_metal2 = QLabel(self)
        self._lbl_crystal2 = QLabel(self)
        self._lbl_deit2 = QLabel(self)
        self._lbl_max_met = QLabel(self)
        self._lbl_max_cry = QLabel(self)
        self._lbl_max_deit = QLabel(self)
        self._lbl_max_met.setFont(font)
        self._lbl_max_cry.setFont(font)
        self._lbl_max_deit.setFont(font)
        self._lbl_metal2.setPixmap(self._pix_met)
        self._lbl_crystal2.setPixmap(self._pix_cry)
        self._lbl_deit2.setPixmap(self._pix_deit)
        # energy labels
        self._lbl_energy = QLabel(self.tr('Energy, charge:'), self)
        self._lbl_energy_pix = QLabel(self)
        self._lbl_energy_pix.setPixmap(self._pix_energy)
        self._lbl_energy_stats = QLabel(self)
        self._lbl_energy_stats.setFont(font)
        # button
        self._btn_refresh = QPushButton(self.tr('Refresh planet'), self)
        self._btn_refresh.setIcon(QIcon(':/i/reload.png'))
        self._btn_refresh.clicked.connect(self.on_btn_refresh_clicked)
        self._btn_refresh.setMinimumHeight(25)
        self._btn_tools = QToolButton(self)
        self._btn_tools.setIcon(QIcon(':/i/tools_32.png'))
        self._btn_tools.setText(self.tr('Actions...'))
        self._btn_tools.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
        self._btn_tools.setPopupMode(QToolButton.InstantPopup)
        self._btn_tools.setMinimumHeight(25)
        self._actions_menu = QMenu(self)
        self._action_renameplanet = QAction(self.tr('Rename planet'), self)
        self._action_leaveplanet = QAction(self.tr('Leave planet'), self)
        self._actions_menu.addAction(self._action_renameplanet)
        self._actions_menu.addAction(self._action_leaveplanet)
        self._btn_tools.setMenu(self._actions_menu)
        self._action_renameplanet.triggered.connect(self.on_action_renameplanet)
        self._action_leaveplanet.triggered.connect(self.on_action_leaveplanet)
        # finalize layout
        self._hlayout_name_coords.addWidget(self._lbl_name)
        self._hlayout_name_coords.addWidget(self._lbl_coords)
        self._hlayout_name_coords.addWidget(self._btn_refresh)
        self._hlayout_name_coords.addWidget(self._btn_tools)
        self._hlayout_name_coords.addStretch()
        #
        self._hlayout_fields.addWidget(self._lbl_fields)
        self._hlayout_fields.addStretch()
        #
        self._hlayout_res.addWidget(self._lbl_res_on_planet)
        self._hlayout_res.addWidget(self._lbl_metal)
        self._hlayout_res.addWidget(self._lbl_cur_met)
        self._hlayout_res.addWidget(self._lbl_crystal)
        self._hlayout_res.addWidget(self._lbl_cur_cry)
        self._hlayout_res.addWidget(self._lbl_deit)
        self._hlayout_res.addWidget(self._lbl_cur_deit)
        self._hlayout_res.addStretch()
        #
        self._hlayout_resmax.addWidget(self._lbl_res_max)
        self._hlayout_resmax.addWidget(self._lbl_metal2)
        self._hlayout_resmax.addWidget(self._lbl_max_met)
        self._hlayout_resmax.addWidget(self._lbl_crystal2)
        self._hlayout_resmax.addWidget(self._lbl_max_cry)
        self._hlayout_resmax.addWidget(self._lbl_deit2)
        self._hlayout_resmax.addWidget(self._lbl_max_deit)
        self._hlayout_resmax.addStretch()
        # minimum widths for res labels
        lbl_width = 100
        res_width = 120
        self._lbl_res_on_planet.setMinimumWidth(lbl_width)
        self._lbl_res_max.setMinimumWidth(lbl_width)
        self._lbl_energy.setMinimumWidth(lbl_width)
        self._lbl_cur_met.setMinimumWidth(res_width)
        self._lbl_cur_cry.setMinimumWidth(res_width)
        self._lbl_cur_deit.setMinimumWidth(res_width)
        self._lbl_max_met.setMinimumWidth(res_width)
        self._lbl_max_cry.setMinimumWidth(res_width)
        self._lbl_max_deit.setMinimumWidth(res_width)
        #
        self._hlayout_energy.addWidget(self._lbl_energy)
        self._hlayout_energy.addWidget(self._lbl_energy_pix)
        self._hlayout_energy.addWidget(self._lbl_energy_stats)
        self._hlayout_energy.addStretch()
        #
        self._vlayout.addLayout(self._hlayout_name_coords)
        self._vlayout.addLayout(self._hlayout_fields)
        self._vlayout.addLayout(self._hlayout_res)
        self._vlayout.addLayout(self._hlayout_resmax)
        self._vlayout.addLayout(self._hlayout_energy)
        self._vlayout.addStretch()
        #
        self._layout.addWidget(self._lbl_img, 0, Qt.AlignTop | Qt.AlignHCenter)
        self._layout.addLayout(self._vlayout)
        self._layout.addStretch()

    def setup_from_planet(self, planet: XNPlanet):
        # store references
        self._planet = planet
        self._planet_pic_url = planet.pic_url
        # deal with planet pic
        file_name = './cache/img/{0}'.format(
                    self._planet_pic_url.replace('/', '_'))
        self._pixmap = QPixmap(file_name)
        self._lbl_img.setPixmap(self._pixmap)
        # setup widget max height based on picture size and layout's margins
        margins = self._layout.contentsMargins()
        top_margin = margins.top()
        bottom_margin = margins.bottom()
        max_height = self._pixmap.height() + top_margin + bottom_margin
        if max_height < 130:
            max_height = 130
        self.setMaximumHeight(max_height)
        # planet name, corods, fields
        self._lbl_name.setText(planet.name)
        self._lbl_coords.setText('<a href="{0}">{0}</a>'.format(
                planet.coords.coords_str()))
        fields_left_str = '{0}: {1}'.format(
                self.tr('left'),
                planet.fields_total - planet.fields_busy)
        self._lbl_fields.setText(
                self.tr('Fields:') +
                ' {0} / {1} ({2})'.format(planet.fields_busy,
                                          planet.fields_total,
                                          fields_left_str))
        # resources
        self.update_resources()

    def update_resources(self):
        # update planet resources
        color_enough = '#008800'
        color_exceed = '#AA0000'
        # cur metal
        color = color_enough
        if self._planet.res_current.met > self._planet.res_max_silos.met:
            color = color_exceed
        self._lbl_cur_met.setText('<font color="{0}">{1}</font>'.format(
                color, number_format(int(self._planet.res_current.met))))
        # cur crystal
        color = color_enough
        if self._planet.res_current.cry > self._planet.res_max_silos.cry:
            color = color_exceed
        self._lbl_cur_cry.setText('<font color="{0}">{1}</font>'.format(
                color, number_format(int(self._planet.res_current.cry))))
        # cur deit
        color = color_enough
        if self._planet.res_current.deit > self._planet.res_max_silos.deit:
            color = color_exceed
        self._lbl_cur_deit.setText('<font color="{0}">{1}</font>'.format(
                color, number_format(int(self._planet.res_current.deit))))
        # update res max
        self._lbl_max_met.setText(number_format(
                int(self._planet.res_max_silos.met)))
        self._lbl_max_cry.setText(number_format(
                int(self._planet.res_max_silos.cry)))
        self._lbl_max_deit.setText(number_format(
                int(self._planet.res_max_silos.deit)))
        # energy
        self._lbl_energy_stats.setText('{0} / {1}    ({2}%)'.format(
                self._planet.energy.energy_left,
                self._planet.energy.energy_total,
                self._planet.energy.charge_percent))

    @pyqtSlot(str)
    def on_coords_link_activated(self, link: str):
        coords = XNCoords()
        coords.parse_str(link, raise_on_error=True)
        self.requestOpenGalaxy.emit(coords)

    @pyqtSlot()
    def on_btn_refresh_clicked(self):
        self.requestRefreshPlanet.emit()

    @pyqtSlot()
    def on_action_renameplanet(self):
        new_name = input_string_dialog(
                self,
                self.tr('Rename planet'),
                self.tr('Enter new planet name:'),
                self._planet.name)
        if (new_name is not None) and (new_name != self._planet.name):
            self.requestRenamePlanet.emit(self._planet.planet_id, new_name)

    @pyqtSlot()
    def on_action_leaveplanet(self):
        QMessageBox.warning(self, self.tr('Not done'),
                            self.tr('Leaving planet is not done!'))
示例#8
0
    def __init__(self, font, parent=None):
        super().__init__(parent, Qt.MSWindowsFixedSizeDialogHint)
        self.setWindowModality(Qt.WindowModal)
        self.setWindowTitle(self.tr("Export…"))

        self._exportDirectory = QDir.toNativeSeparators(
            QStandardPaths.standardLocations(QStandardPaths.DocumentsLocation)[0]
        )
        self.baseName = getAttrWithFallback(font.info, "postscriptFontName")

        self.formatBtnSet = ButtonSet(self)
        self.formatBtnSet.setOptions(["OTF", "TTF"])
        self.formatBtnSet.setSelectionMode(ButtonSet.OneOrMoreSelection)
        self.compressionBtnSet = ButtonSet(self)
        self.compressionBtnSet.setOptions(["None", "WOFF", "WOFF2"])
        self.compressionBtnSet.setSelectionMode(ButtonSet.OneOrMoreSelection)
        self.numberLabel = QLabel(self)
        self.formatBtnSet.clicked.connect(self.updateNumbers)
        self.compressionBtnSet.clicked.connect(self.updateNumbers)

        self.removeOverlapBox = QCheckBox(self.tr("Remove Overlap"), self)
        # self.removeOverlapBox.setChecked(True)  # XXX: implement
        self.removeOverlapBox.setEnabled(False)
        self.autohintBox = QCheckBox(self.tr("Autohint"), self)
        # self.autohintBox.setChecked(True)  # XXX: implement
        self.autohintBox.setEnabled(False)

        self.exportBox = QCheckBox(self)
        boxSize = self.exportBox.sizeHint()
        self.exportBox.setText(self.tr("Use Export Directory"))
        self.exportBox.setChecked(True)
        self.exportIcon = QLabel(self)
        icon = self.style().standardIcon(QStyle.SP_DirClosedIcon)
        iconSize = QSize(24, 24)
        self.exportIcon.setPixmap(icon.pixmap(icon.actualSize(iconSize)))
        self.exportIcon.setBaseSize(iconSize)
        self.exportDirLabel = QLabel(self)
        self.exportDirLabel.setText(self.exportDirectory)
        self.exportDirButton = QPushButton(self)
        self.exportDirButton.setText(self.tr("Choose…"))
        self.exportDirButton.clicked.connect(
            lambda: self.chooseExportDir(self.exportDirectory)
        )

        # if files are to be overwritten, put up a warning
        # + use a file system watcher to avoid TOCTOU
        self.warningIcon = QLabel(self)
        icon = icons.i_warning()
        iconSize = QSize(20, 20)
        self.warningIcon.setPixmap(icon.pixmap(icon.actualSize(iconSize)))
        self.warningIcon.setBaseSize(iconSize)
        # XXX: not sure why this is needed
        self.warningIcon.setFixedWidth(iconSize.width())
        sp = self.warningIcon.sizePolicy()
        sp.setRetainSizeWhenHidden(True)
        self.warningIcon.setSizePolicy(sp)
        self.warningLabel = QLabel(self)
        palette = self.warningLabel.palette()
        role, color = self.warningLabel.foregroundRole(), QColor(230, 20, 20)
        palette.setColor(palette.Active, role, color)
        palette.setColor(palette.Inactive, role, color)
        self.warningLabel.setPalette(palette)
        sp = self.warningLabel.sizePolicy()
        sp.setRetainSizeWhenHidden(True)
        self.warningLabel.setSizePolicy(sp)

        self.updateExportStatus()
        self.exportBox.toggled.connect(self.updateExportStatus)

        self.watcher = QFileSystemWatcher(self)
        self.watcher.addPath(self.exportDirectory)
        self.updateNumbers()
        self.watcher.directoryChanged.connect(self.updateNumbers)

        buttonBox = QDialogButtonBox(QDialogButtonBox.Cancel)
        buttonBox.addButton(self.tr("Generate…"), QDialogButtonBox.AcceptRole)
        buttonBox.accepted.connect(self.finish)
        buttonBox.rejected.connect(self.reject)

        layout = QVBoxLayout(self)
        formLayout = QFormLayout()
        formLayout.addRow(self.tr("Format"), self.formatBtnSet)
        formLayout.addRow(self.tr("Compression"), self.compressionBtnSet)
        formLayout.setHorizontalSpacing(16)
        formLayout.setContentsMargins(0, 0, 0, 4)
        layout.addLayout(formLayout)
        layout.addWidget(self.numberLabel)
        layout.addWidget(self.removeOverlapBox)
        layout.addWidget(self.autohintBox)
        layout.addWidget(self.exportBox)
        exportLayout = QHBoxLayout()
        exportLayout.addWidget(self.exportIcon)
        exportLayout.addWidget(self.exportDirLabel)
        exportLayout.addWidget(self.exportDirButton)
        exportLayout.addWidget(QWidget())
        margins = exportLayout.contentsMargins()
        margins.setLeft(margins.left() + boxSize.width() + 4)
        exportLayout.setContentsMargins(margins)
        exportLayout.setStretch(3, 1)
        layout.addLayout(exportLayout)
        warningLayout = QHBoxLayout()
        warningLayout.addWidget(self.warningIcon)
        warningLayout.addWidget(self.warningLabel)
        warningLayout.addWidget(QWidget())
        margins.setBottom(margins.bottom() + 4)
        warningLayout.setContentsMargins(margins)
        warningLayout.setStretch(3, 1)
        layout.addLayout(warningLayout)
        layout.addWidget(buttonBox)
        # XXX: check this on non-Windows platforms
        layout.setContentsMargins(16, 16, 16, 16)

        self.readSettings()