示例#1
0
    def initUI(self):

        #Window settings

        self.setGeometry(100, 50, 1024, 600)
        
        self.icon = QtGui.QIcon('icons/cards.png')
        self.setWindowIcon(self.icon)

        #Menu settings
        self.menubar = self.menuBar()
        self.fileMenu = self.menubar.addMenu('')
        
        self.languageAction = QtGui.QAction(self)
        self.languageAction.triggered.connect(self.chooseLanguage)
        self.fileMenu.addAction(self.languageAction)
        
        self.exitAction = QtGui.QAction(self)
        self.exitAction.triggered.connect(self.close)
        self.fileMenu.addAction(self.exitAction)
        
        self.helpMenu = self.menubar.addMenu('')
        self.aboutAction = QtGui.QAction(self)
        self.aboutAction.triggered.connect(self.about)
        self.helpMenu.addAction(self.aboutAction)
        
        
        #Central stuff!!
        self.centralwidget = QtGui.QWidget(None)
        self.setCentralWidget(self.centralwidget)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)

        # Tab widget
#        self.tabWidget = QtGui.QTabWidget(self.centralwidget)
#        self.verticalLayout.addWidget(self.tabWidget)

        #New game tab
        self.newGameTab = NewGameWidget(self)
        self.verticalLayout.addWidget(self.newGameTab)
#        self.tabWidget.addTab(self.newGameTab, "")
#        self.tabWidget.setCurrentIndex(0)

        self.retranslateUi()

        #And finally, show it!
        
        self.setStyleSheet("font-size: 20px;")
        
        self.show()
示例#2
0
class MainWindow(QMainWindow):

    # Dialog translations
    QtCore.QT_TRANSLATE_NOOP("QDialogButtonBox", "&Yes")
    QtCore.QT_TRANSLATE_NOOP("QDialogButtonBox", "&No")
    QtCore.QT_TRANSLATE_NOOP("QDialogButtonBox", "OK")
    QtCore.QT_TRANSLATE_NOOP("QDialogButtonBox", "Cancel")

    def __init__(self, translator=None, qt_translator=None, parent=None):
        super(MainWindow, self).__init__(parent)
        db.connectDB()
        self.openedGames = []
        self.translator = translator
        self.qt_translator = qt_translator
        self.initUI()

    def initUI(self):

        # Window settings

        self.setGeometry(100, 50, 1024, 600)

        self.icon = QtGui.QIcon('icons/cards.png')
        self.setWindowIcon(self.icon)

        # Menu settings
        self.menubar = self.menuBar()
        self.fileMenu = self.menubar.addMenu('')

        self.languageAction = QAction(self)
        self.languageAction.triggered.connect(self.chooseLanguage)
        self.fileMenu.addAction(self.languageAction)

        self.exitAction = QAction(self)
        self.exitAction.triggered.connect(self.close)
        self.fileMenu.addAction(self.exitAction)

        self.helpMenu = self.menubar.addMenu('')
        self.aboutAction = QAction(self)
        self.aboutAction.triggered.connect(self.about)
        self.helpMenu.addAction(self.aboutAction)

        # Central stuff!!
        self.centralwidget = QWidget(None)
        self.setCentralWidget(self.centralwidget)
        self.verticalLayout = QVBoxLayout(self.centralwidget)

        # Tab widget
#        self.tabWidget = QTabWidget(self.centralwidget)
#        self.verticalLayout.addWidget(self.tabWidget)

        # New game tab
        self.newGameTab = NewGameWidget(self)
        self.verticalLayout.addWidget(self.newGameTab)
#        self.tabWidget.addTab(self.newGameTab, "")
#        self.tabWidget.setCurrentIndex(0)

        self.retranslateUi()

        # And finally, show it!

        self.setStyleSheet("font-size: 20px;")

        self.show()

    def retranslateUi(self):
        self.setWindowTitle(
            i18n("MainWindow", 'GameLog'))
        self.statusBar().showMessage(
            i18n("MainWindow", 'GameLog'))
        self.fileMenu.setTitle(
            i18n("MainWindow", '&File'))
        self.languageAction.setText(
            i18n("MainWindow", '&Language...'))
        self.exitAction.setText(
            i18n("MainWindow", '&Quit'))
        self.exitAction.setShortcut(
            i18n("MainWindow", 'Ctrl+Q'))
        self.exitAction.setStatusTip(
            i18n("MainWindow", 'Quit GameLog'))

        self.helpMenu.setTitle(
            i18n("MainWindow", '&Help'))
        self.aboutAction.setText(i18n(
            "MainWindow", '&About Gamelog...'))

        self.newGameTab.retranslateUI()
        for game in self.openedGames:
            game.retranslateUI()

    def closeEvent(self, event):
        if self.ensureClose():
            event.accept()
        else:
            event.ignore()

    def ensureClose(self):
        realopened = [x for x in self.openedGames if not x.isFinished()]
        numgames = len(realopened)
        if numgames > 0:
            tit = i18n("MainWindow", 'Exit')
            if (numgames == 1):
                msg = i18n(
                    "MainWindow", "You have an opened {} match. \
                    Do you want to save it before exiting?")
                msg = msg.format(realopened[0].getGameName())
                reply = QMessageBox.question(self, tit, msg,
                                             QMessageBox.Yes |
                                             QMessageBox.No |
                                             QMessageBox.Cancel,
                                             QMessageBox.Cancel)
            else:
                tit = i18n("MainWindow", 'Exit')
                msg = i18n("MainWindow", "You have {} opened matches. \
                            Do you want to save them before exiting?")
                msg = msg.format(numgames)
                reply = QMessageBox.question(self, tit, msg,
                                             QMessageBox.Yes |
                                             QMessageBox.No |
                                             QMessageBox.Cancel,
                                             QMessageBox.Cancel)

            if reply == QMessageBox.Cancel:
                return False

            for game in realopened:
                if reply == QMessageBox.No:
                    game.closeMatch()
                else:
                    game.saveMatch()
        else:
            tit = i18n("MainWindow", 'Exit')
            msg = i18n("MainWindow", "Are you sure you want to exit GameLog?")

            reply = QMessageBox.question(self, tit, msg,
                                         QMessageBox.Yes | QMessageBox.No,
                                         QMessageBox.No)

            if reply == QMessageBox.No:
                return False

        if db:
            db.disconnectDB()
        return True

    def newTab(self, matchTab, title):
        self.newGameTab.hide()
        self.verticalLayout.addWidget(matchTab)
        self.setWindowTitle("Gamelog - {}".format(title))
        matchTab.show()
        matchTab.setFocus()
        self.openedGames.append(matchTab)
#        idx = self.tabWidget.addTab(matchTab, title)
#        self.tabWidget.setCurrentIndex(idx)

    def removeTab(self, tab):
        tab.close()
        self.openedGames.remove(tab)
        self.setWindowTitle("Gamelog")
        self.newGameTab.show()
#        self.tabWidget.removeTab(self.tabWidget.indexOf(tab))

    def chooseLanguage(self):
        lc = LanguageChooser(self)
        lc.newQM.connect(self.loadTranslator)
        lc.exec_()

    def about(self):
        self.abdialog = AboutDialog(self)
        self.abdialog.exec_()

    def loadTranslator(self, lang):
        translator = QtCore.QTranslator()
        ret = translator.load(lang, 'i18n/')
        qt_translator = QtCore.QTranslator()
        qt_qm = 'qtbase_' + lang.split('_')[0]
        qt_translator.load(qt_qm, 'i18n/')
        if ret:
            if self.translator:
                qApp.removeTranslator(self.translator)
            if self.qt_translator:
                qApp.removeTranslator(self.qt_translator)
            self.qt_translator = qt_translator
            self.translator = translator
            qApp.installTranslator(self.qt_translator)
            qApp.installTranslator(self.translator)

    def changeEvent(self, event):
        if event.type() == QtCore.QEvent.LanguageChange:
            self.retranslateUi()

        return super(MainWindow, self).changeEvent(event)