Esempio n. 1
0
class MainDialog(QDialog):
    def __init__(self, login_dialog):
        super(MainDialog, self).__init__()
        # recovering UI from main_ui.py file
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        # got to save user name and login dialog
        self._user = str()
        self.lg = login_dialog

        # connecting logout press to logout action
        self.ui.pushButton_4.clicked.connect(self.logout)

        # loading list of queries
        self.queries = list()
        fname = path.join('.profile', 'queries')
        if path.isfile(fname):
            for line in open(path.join('.profile', 'queries')).read().split(':;:'):
                if '::' not in line:
                    continue
                query = line.split(':::')
                self.queries.append(query)
        # sorting them in alphabetic order
        self.queries.sort(key=lambda x: x[0])
        # adding queries to combo boxes
        for query in self.queries:
            self.ui.comboBox.addItem(query[0])

        # connecting buttons to their respective actions
        self.ui.comboBox.currentIndexChanged.connect(self.query_changed)
        self.ui.pushButton_2.clicked.connect(self.new_query)
        self.ui.pushButton_3.clicked.connect(self.remove_query)
        self.ui.pushButton.clicked.connect(self.run_query)

        # connecting to the database
        self.db = dataset.connect(f'postgresql://{DBUSERNAME}:{DBPASSWORD}@localhost:5432/{DBNAME}')

        # creating plot and legend objects
        self.cv = Canvas(self.ui.frame_3)
        self.legend_labels = [(self.ui.label_2, self.ui.label_3)]

    # this is triggered when admin pressed "Remove query"
    @pyqtSlot()
    def remove_query(self):
        # removing cquery from combox box and list of queries
        index = self.ui.comboBox.currentIndex()
        self.queries.pop(index)
        self.ui.comboBox.removeItem(index)
        self.update()

    # this is triggered when user/admin presses 'Run Query' button
    @pyqtSlot()
    def run_query(self):
        # we get text of the current query from text field
        query = self.ui.plainTextEdit.toPlainText()
        # removing all rows from table widget
        self.ui.tableWidget.setRowCount(0)
        # creating vars for setting up headers
        columns_set = False
        header = list()
        try:
            for i, row in enumerate(self.db.query(query)):
                # adding new row to the table widget
                self.ui.tableWidget.insertRow(i)
                # setting up headers
                if not columns_set:
                    header = list(row.keys())
                    self.ui.tableWidget.setColumnCount(len(header))
                    self.ui.tableWidget.setHorizontalHeaderLabels(header)
                    columns_set = True
                # adding values to the fresh row
                for j, col in enumerate(header):
                    self.ui.tableWidget.setItem(i, j, QTableWidgetItem(str(row[col])))
        except Exception as e:
            print(type(e), e)
        # checking if this query is plottable and calling the plot function
        query_info = self.queries[self.ui.comboBox.currentIndex()]
        if query_info[-1].strip() == 'plotit':
            self.plot_chart(self.db.query(query), query_info)

    def plot_chart(self, data, query_info):
        # plots the chart
        # clears any legend widgets
        for lcolor, ltext in self.legend_labels:
            lcolor.setParent(None)
            ltext.setParent(None)
        self.legend_labels = list()
        # gets new legends from matplotlib object & plots the chart
        legends = self.cv.plot(data, query_info)
        # making sure plot is visible on its tab
        self.ui.tabWidget.setCurrentIndex(1)
        # we wont have space to show all the legends, so limiting it
        max_legend = 20
        try:
            # going through all legends returned and creating labels for them
            for i, leg in enumerate(legends):
                # breaknig is max legend number is met
                if i > max_legend:
                    break
                # creating color label
                lcolor = QLabel(parent=self.ui.groupBox, text="---")
                self.ui.formLayout.setWidget(i, QFormLayout.LabelRole, lcolor)
                # coloring it
                qp = QPalette()
                qp.setColor(lcolor.backgroundRole(), QColor(*[255 * l for l in leg[1]]))
                qp.setColor(lcolor.foregroundRole(), QColor(*[255 * l for l in leg[1]]))
                lcolor.setAutoFillBackground(True)
                lcolor.setPalette(qp)
                # creating legend name label
                ltext = QLabel(parent=self.ui.groupBox, text=leg[0])
                self.ui.formLayout.setWidget(i, QFormLayout.FieldRole, ltext)
                # saving this widget pair
                self.legend_labels.append((lcolor, ltext))
        except Exception as e:
            print(e)
        return

    # this is triggered when admin hits "Add Query" button
    @pyqtSlot()
    def new_query(self):
        # creating new query object
        query = (
            self.ui.lineEdit.text(),
            self.ui.plainTextEdit.toPlainText()
        )
        # adding it to queries and combobox
        self.queries.append(query)
        self.ui.comboBox.addItem(query[0])
        # setting new query as currently selected
        self.ui.comboBox.setCurrentIndex(len(self.queries) - 1)

    # this happens when combo box chooses another query
    @pyqtSlot()
    def query_changed(self):
        query = self.queries[self.ui.comboBox.currentIndex()]
        # updating edit field and query title fields
        self.ui.lineEdit.setText(query[0])
        self.ui.plainTextEdit.setPlainText(query[1])

    # this is for login window to call prior to displaying = sets up rights
    def set_user(self, user):
        self.ui.label.setText(f'Logged in as {user}')
        # admin has access to editing all
        if user == 'Admin':
            self.ui.plainTextEdit.setDisabled(False)
            self.ui.lineEdit.show()
            self.ui.pushButton_2.show()
            self.ui.pushButton_3.show()
        # users are more of a read only type
        else:
            self.ui.plainTextEdit.setDisabled(True)
            self.ui.lineEdit.hide()
            self.ui.pushButton_2.hide()
            self.ui.pushButton_3.hide()
        self.query_changed()
        self.ui.tableWidget.setRowCount(0)
        self.ui.tableWidget.setColumnCount(0)
        self.ui.tabWidget.setCurrentIndex(0)

    # this happens when login button is pressed
    @pyqtSlot()
    def logout(self):
        # going back to logout window
        self.hide()
        self.lg.show()

    # this is triggered when application is about to exit
    def closeEvent(self, a0):
        # saving queries to the file
        with open(path.join('.profile', 'queries'), 'w') as f:
            f.write(':;:'.join([f':::'.join(q) for q in self.queries]))
        a0.accept()
Esempio n. 2
0
 def __init__(self):
     super().__init__()
     ui = Main_UI()
     ui.setupUi(self)
     self.ui = ui
     self._init_ui()
Esempio n. 3
0
class MyWindow(QMainWindow, QDialog):
    def __init__(self):
        super().__init__()
        self._ui = Ui_Dialog()
        self._ui.setupUi(self)

        self.label_w = self._ui.label_pix.width()
        self.label_h = self._ui.label_pix.height()
        self.content = Content(self)

        # ====================================================
        # set music
        pygame.init()
        print("有音樂哦! 注意聽 ~~~")
        pygame.mixer.init()
        pygame.mixer.music.load(self.music_path("music/main.mp3"))
        pygame.mixer.music.play(-1, 8)

        # ====================================================
        # set icon style
        stylesheet = "QPushButton {background: transparent}"

        self._ui.pushButton_home.setIconSize(QSize(32, 32))
        self._ui.pushButton_home.setIcon(QIcon(':/pix/home.png'))

        self._ui.pushButton_love.setIconSize(QSize(32, 32))
        self._ui.pushButton_love.setIcon(QIcon(':/pix/love.png'))

        self._ui.pushButton_next.setIconSize(QSize(32, 32))
        self._ui.pushButton_next.setIcon(QIcon(':/pix/next.png'))

        self._ui.pushButton_previous.setIconSize(QSize(32, 32))
        self._ui.pushButton_previous.setIcon(QIcon(':/pix/previous.png'))

        self._ui.pushButton_heart.setIconSize(QSize(32, 32))
        self._ui.pushButton_heart.setIcon(QIcon(':/pix/heart.png'))

        self._ui.pushButton_play.setIconSize(QSize(32, 32))
        self._ui.pushButton_play.setIcon(QIcon(':/pix/play.png'))

        self._ui.pushButton_close.setIconSize(QSize(32, 32))
        self._ui.pushButton_close.setIcon(QIcon(':/pix/close.png'))

        self._ui.pushButton_click.setIconSize(QSize(32, 32))
        self._ui.pushButton_click.setIcon(QIcon(':/pix/click.png'))

        self._ui.pushButton_start.setIconSize(QSize(32, 32))
        self._ui.pushButton_start.setIcon(QIcon(':/pix/start.png'))

        self._ui.pushButton_1.setIconSize(QSize(80, 80))
        self._ui.pushButton_1.setIcon(QIcon(':/pix/num_1.png'))
        self._ui.pushButton_1.setStyleSheet(stylesheet)

        self._ui.pushButton_2.setIconSize(QSize(80, 80))
        self._ui.pushButton_2.setIcon(QIcon(':/pix/num_2.png'))
        self._ui.pushButton_2.setStyleSheet(stylesheet)

        self._ui.pushButton_3.setIconSize(QSize(80, 80))
        self._ui.pushButton_3.setIcon(QIcon(':/pix/num_3.png'))
        self._ui.pushButton_3.setStyleSheet(stylesheet)

        self._ui.pushButton_4.setIconSize(QSize(80, 80))
        self._ui.pushButton_4.setIcon(QIcon(':/pix/num_4.png'))
        self._ui.pushButton_4.setStyleSheet(stylesheet)

        self._ui.pushButton_5.setIconSize(QSize(80, 80))
        self._ui.pushButton_5.setIcon(QIcon(':/pix/num_5.png'))
        self._ui.pushButton_5.setStyleSheet(stylesheet)

        self._ui.pushButton_6.setIconSize(QSize(80, 80))
        self._ui.pushButton_6.setIcon(QIcon(':/pix/num_6.png'))
        self._ui.pushButton_6.setStyleSheet(stylesheet)

        self._ui.pushButton_7.setIconSize(QSize(80, 80))
        self._ui.pushButton_7.setIcon(QIcon(':/pix/num_7.png'))
        self._ui.pushButton_7.setStyleSheet(stylesheet)

        self._ui.pushButton_8.setIconSize(QSize(80, 80))
        self._ui.pushButton_8.setIcon(QIcon(':/pix/num_8.png'))
        self._ui.pushButton_8.setStyleSheet(stylesheet)

        self._ui.pushButton_9.setIconSize(QSize(80, 80))
        self._ui.pushButton_9.setIcon(QIcon(':/pix/num_9.png'))
        self._ui.pushButton_9.setStyleSheet(stylesheet)

        # =====================================================
        self.count = 0
        self.on_pushButton_home_clicked()

    # =======================================================
    # set music
    def music_path(self, real_path):
        if hasattr(sys, "_MEIPASS"):
            base_path = sys._MEIPASS

        else:
            base_path = os.path.abspath(".")

        return os.path.join(base_path, real_path)

    # =====================================================
    # content
    @pyqtSlot()
    def on_pushButton_home_clicked(self):
        self._ui.stackedWidget.setCurrentIndex(0)
        self.content.home_control()

    @pyqtSlot()
    def on_pushButton_love_clicked(self):
        self._ui.stackedWidget.setCurrentIndex(0)
        self.content.love_control()
        self.count = 1

    @pyqtSlot()
    def on_pushButton_next_clicked(self):
        self._ui.stackedWidget.setCurrentIndex(0)
        self.count += 1
        count = self.content.next_control(self.count)
        self.count = count

    @pyqtSlot()
    def on_pushButton_previous_clicked(self):
        self._ui.stackedWidget.setCurrentIndex(0)
        self.count -= 1
        count = self.content.previous_control(self.count)
        self.count = count

    @pyqtSlot()
    def on_pushButton_heart_clicked(self):
        message = QMessageBox.question(self, "Reminder", '接下來會有驚喜哦!\n\n 你 準備好了嗎?')
        if message == QMessageBox.Yes:
            self._ui.stackedWidget.setCurrentIndex(1)
            self.content.heart_control(self.count)
            self.heart = 0

    @pyqtSlot()
    def on_pushButton_click_clicked(self):
        heart = self.content.heart_click_control(self.heart, self.count)
        self.heart = heart

    # =====================================================
    # play game
    @pyqtSlot()
    def on_pushButton_play_clicked(self):
        self.content.play_control()

    @pyqtSlot()
    def on_pushButton_start_clicked(self):
        self.content.start_control()

    @pyqtSlot()
    def on_pushButton_1_clicked(self):
        self.content.btn_1_control()

    @pyqtSlot()
    def on_pushButton_2_clicked(self):
        self.content.btn_2_control()

    @pyqtSlot()
    def on_pushButton_3_clicked(self):
        self.content.btn_3_control()

    @pyqtSlot()
    def on_pushButton_4_clicked(self):
        self.content.btn_4_control()

    @pyqtSlot()
    def on_pushButton_5_clicked(self):
        self.content.btn_5_control()

    @pyqtSlot()
    def on_pushButton_6_clicked(self):
        self.content.btn_6_control()

    @pyqtSlot()
    def on_pushButton_7_clicked(self):
        self.content.btn_7_control()

    @pyqtSlot()
    def on_pushButton_8_clicked(self):
        self.content.btn_8_control()

    @pyqtSlot()
    def on_pushButton_9_clicked(self):
        self.content.btn_9_control()

    # =====================================================
    @pyqtSlot()
    def on_pushButton_close_clicked(self):
        self.close()