Esempio n. 1
0
class CustomWidget(QWidget):
    def __init__(self, title, parent):
        super(CustomWidget, self).__init__(parent=None)
        self.initUI(title)

    def initUI(self, title):
        layout = QVBoxLayout()
        self.label = QLabel(self)
        self.label.setStyleSheet("QLabel{border-image:url(./images/nullImage.jpg);}")
        self.label.setFixedSize(100, 100)
        self.checkBox = QCheckBox(title, self)
        self.label.setMinimumSize(100, 100)

        layout.addWidget(self.label)
        layout.addWidget(self.checkBox)
        self.setLayout(layout)

    def close(self):
        self.label.close()
        self.checkBox.close()
Esempio n. 2
0
class CustomWidget(QWidget):
    def __init__(self, title, parent):
        super(CustomWidget, self).__init__(parent=None)
        self.initUI(title)

    def initUI(self, title):
        layout = QVBoxLayout()
        self.button = QPushButton(self)
        self.button.setStyleSheet("QPushButton{border-image:url(../images/nullImage.jpg);}")
        self.button.setFixedSize(100, 100)
        self.checkBox = QCheckBox(title, self)
        self.checkBox.setCheckable(False)
        self.button.setMinimumSize(100, 100)

        layout.addWidget(self.button)
        layout.addWidget(self.checkBox)
        self.setLayout(layout)


    def close(self):
        self.button.close()
        self.checkBox.close()
class DownloadImage(DownloadImage):
    def __init__(self, c):
        super(DownloadImage, self).__init__(c)
        # initial puzzle settings
        self.width, self.height, self.diff, self.option = 15, 15, 1, 0
        # initialize download numbers
        self.picture_on = False
        # initialize language mode
        self.korean = False
        # initialize chosung or scramble mode
        self.chosung_scramable = False
        # initialize uppercase mode
        self.uppercase = False

    def init_UI(self):
        # Title for widget
        title = QLabel("3. 이미지를 선택하세요.")
        self.grid.addWidget(title, 0, 0)

        super(DownloadImage, self).init_UI()
        # get data of puzzle settings
        self.c.puzzle_setting.connect(self.puzzle_setting)
        # see if its korean or english/ default is english
        self.c.korean.connect(self.korean_on)

        self.uppercase_checkbox = QCheckBox("대문자", self)
        self.uppercase_checkbox.stateChanged.connect(
            self.uppercase_checkbox_on)

        self.chosung_scramable_checkBox = QCheckBox("scramble word", self)
        self.chosung_scramable_checkBox.setToolTip("단어의 철자가 뒤섞여서 제시됩니다.")
        self.chosung_scramable_checkBox.stateChanged.connect(
            self.chosung_scramable_on)

        self.make_puzzle_bt = QPushButton("Word Search\n퍼즐 만들기")
        self.make_puzzle_bt.clicked.connect(self.make_puzzle)
        self.make_puzzle_bt.setToolTip("단축키 : Ctrl + D")
        self.make_puzzle_bt.setShortcut('Ctrl+D')
        self.vbox.addWidget(self.uppercase_checkbox)

        self.vbox.addWidget(self.chosung_scramable_checkBox)
        self.vbox.addWidget(self.make_puzzle_bt)

    # define puzzle settings
    @pyqtSlot(list)
    def puzzle_setting(self, puzzle_setting):
        self.width, self.height, self.diff, self.option = puzzle_setting[
            0], puzzle_setting[1], puzzle_setting[2], puzzle_setting[3]

    @pyqtSlot(bool)
    def korean_on(self, bool):
        self.korean = bool
        if bool is True:
            self.chosung_scramable_checkBox.setText('초성')
            self.chosung_scramable_checkBox.setToolTip("단어의 초성으로 제시됩니다.")
            self.make_puzzle_bt.setText('낱말 퍼즐\n 만들기')
            self.uppercase_checkbox.close()
        else:
            self.chosung_scramable_checkBox.setText('scramble word')
            self.chosung_scramable_checkBox.setToolTip("단어의 철자가 뒤섞여서 제시됩니다.")
            self.make_puzzle_bt.setText('Word Search\n퍼즐 만들기')
            self.uppercase_checkbox.show()
        self.make_puzzle_bt.setToolTip("단축키 : Ctrl + D")
        self.make_puzzle_bt.setShortcut('Ctrl+D')

    @pyqtSlot()
    def uppercase_checkbox_on(self):
        if self.uppercase_checkbox.isChecked() is True:
            self.uppercase = True
        else:
            self.uppercase = False

    @pyqtSlot()
    def chosung_scramable_on(self):
        if self.chosung_scramable_checkBox.isChecked() is True:
            self.chosung_scramable = True
        else:
            self.chosung_scramable = False

    def enable_buttons(self):
        self.download_bt.setEnabled(True)
        self.make_puzzle_bt.setEnabled(True)
        self.c.enable_set_keyword_bt.emit()

    def disable_buttons(self):
        self.download_bt.setEnabled(False)
        self.make_puzzle_bt.setEnabled(False)
        self.c.disable_set_keyword_bt.emit()

    @pyqtSlot()
    def make_puzzle(self):
        word_image = []
        if self.tree.topLevelItemCount() == 0:
            self.start_download()
            return

        self.disable_buttons()

        if self.picture_on:
            iterator = QTreeWidgetItemIterator(
                self.tree, QTreeWidgetItemIterator.HasChildren)
        else:
            iterator = QTreeWidgetItemIterator(self.tree,
                                               QTreeWidgetItemIterator.All)
            if iterator.value() is None:
                self.c.press_set_keyword_bt.emit()
                q = QMessageBox(self)
                q.information(self, 'information',
                              '검색어 키워드가 존재하지 않아요. 그래서 검색어 키워드 버튼을 대신 눌렀습니다~.',
                              QMessageBox.Ok)
                self.enable_buttons()
                return
        while iterator.value():
            item = iterator.value()
            word = item.data(0, 0)
            pic = ''
            if self.picture_on:
                pic = item.path
                if not os.path.exists(pic):
                    self.c.press_set_keyword_bt.emit()
                    self.enable_buttons()
                    q = QMessageBox(self)
                    q.information(self, 'information',
                                  '선택하신 이미지가 존재하지 않습니다. 다시 다운로드 눌러주세요.',
                                  QMessageBox.Ok)
                    return
            if word.strip().find(' ') != -1:
                if self.korean:
                    word = '가'.join(word.split())
                else:
                    word = 'a'.join(word.split())
            word_image.append([word, pic])
            iterator += 1

        self.path = self.get_save_hwp_dir()
        if self.path:
            puzzle_worker = PuzzleWorker(wordsearch_generater.MakeWordSearch,
                                         word_image, self.width, self.height,
                                         self.diff, self.option,
                                         self.picture_on, self.korean,
                                         self.chosung_scramable,
                                         self.uppercase, self.path)
            puzzle_worker.signal.puzzle_complete.connect(self.puzzle_finish)
            puzzle_worker.signal.recursionerrormsg.connect(self.recurerrormsg)
            puzzle_worker.signal.valueerrormsg.connect(self.valerrormsg)
            self.threadpool.start(puzzle_worker)
        else:
            self.enable_buttons()

    def get_save_hwp_dir(self):
        file_path = os.path.join(os.getcwd(), 'dir_path.json')
        is_dir_path = os.path.exists(file_path)
        if is_dir_path:
            with open(file_path) as f:
                data = json.load(f)
            if 'hwp_dir' in data.keys():
                dir_path = data['hwp_dir']
                return dir_path
            else:
                q = QMessageBox(self)
                q.information(self, 'information', '퍼즐을 저장할 폴더를 선택하세요.',
                              QMessageBox.Ok)
                fname = str(
                    QFileDialog.getExistingDirectory(self, "퍼즐을 저장할 폴더"))
                if fname:
                    data['hwp_dir'] = fname
                    with open(file_path, 'w') as f:
                        json.dump(data, f)
                    dir_path = fname
                    return dir_path
                else:
                    return
        else:
            q = QMessageBox(self)
            q.information(self, 'information', '퍼즐을 저장할 폴더를 선택하세요.',
                          QMessageBox.Ok)
            fname = str(QFileDialog.getExistingDirectory(self, "퍼즐을 저장할 폴더"))
            if fname:
                with open(file_path, 'w') as f:
                    json.dump({'hwp_dir': fname}, f)
                dir_path = fname
                return dir_path
            else:
                return

    @pyqtSlot()
    def puzzle_finish(self):
        q = QMessageBox(self)
        q.information(self, 'information',
                      '{}에 퍼즐 파일이 저장되었습니다.'.format(self.path), QMessageBox.Ok)
        self.enable_buttons()

    @pyqtSlot()
    def recurerrormsg(self):
        q = QMessageBox(self)
        q.warning(self, 'Warning', '단어의 개수에 비해서 퍼즐의 크기가 너무 작습니다.',
                  QMessageBox.Ok)
        self.enable_buttons()

    @pyqtSlot()
    def valerrormsg(self):
        q = QMessageBox(self)
        q.warning(self, 'Warning', '퍼즐의 크기에 비해 단어가 너무 길어요.', QMessageBox.Ok)
        self.enable_buttons()
class DownloadImage(DownloadImage):
    def __init__(self, c):
        super(DownloadImage, self).__init__(c)
        # initial puzzle settings
        self.width, self.height, self.diff, self.option = 15, 15, 1, 0
        # initialize download numbers
        self.picture_on = False
        # initialize language mode
        self.korean = False
        # initialize language mode
        self.chosung = False

    def init_UI(self):
        super(DownloadImage, self).init_UI()
        # get data of puzzle settings
        self.c.puzzle_setting.connect(self.puzzle_setting)
        # see if its korean or english/ default is english
        self.c.korean.connect(self.korean_on)

        self.chosung_checkBox = QCheckBox("초성", self)
        self.chosung_checkBox.setToolTip("단어가 초성으로 제시됩니다.")
        self.chosung_checkBox.close()
        self.chosung_checkBox.stateChanged.connect(self.chosung_on)

        self.make_puzzle_bt = QPushButton("Word Search 퍼즐 만들기")
        self.make_puzzle_bt.clicked.connect(self.make_puzzle)
        self.make_puzzle_bt.setToolTip("단축키 : Ctrl + D")
        self.make_puzzle_bt.setShortcut('Ctrl+D')

        hbox_puzzle_bt = QHBoxLayout()
        hbox_puzzle_bt.addStretch(1)
        hbox_puzzle_bt.addWidget(self.chosung_checkBox)
        hbox_puzzle_bt.addWidget(self.make_puzzle_bt)
        self.grid.addLayout(hbox_puzzle_bt, 3, 0, 1, 2)

    # define puzzle settings
    def puzzle_setting(self, puzzle_setting):
        self.width, self.height, self.diff, self.option = puzzle_setting[
            0], puzzle_setting[1], puzzle_setting[2], puzzle_setting[3]

    def korean_on(self, bool):
        self.korean = bool
        if bool == True:
            self.chosung_checkBox.show()
            self.make_puzzle_bt.setText('낱말 찾기 퍼즐 만들기')
        else:
            self.chosung_checkBox.close()
            self.make_puzzle_bt.setText('Word Search 퍼즐 만들기')
        self.make_puzzle_bt.setToolTip("단축키 : Ctrl + D")
        self.make_puzzle_bt.setShortcut('Ctrl+D')

    def chosung_on(self):
        if self.chosung_checkBox.isChecked() == True:
            self.chosung = True
        else:
            self.chosung = False

    def enable_buttons(self):
        self.download_bt.setEnabled(True)
        self.make_puzzle_bt.setEnabled(True)
        self.c.enable_set_keyword_bt.emit()

    def disable_buttons(self):
        self.download_bt.setEnabled(False)
        self.make_puzzle_bt.setEnabled(False)
        self.c.disable_set_keyword_bt.emit()

    def make_puzzle(self):
        word_image = []
        if self.tree.topLevelItemCount() == 0:
            self.start_download()
            return

        self.disable_buttons()

        if self.picture_on:
            iterator = QTreeWidgetItemIterator(
                self.tree, QTreeWidgetItemIterator.HasChildren)
        else:
            iterator = QTreeWidgetItemIterator(self.tree,
                                               QTreeWidgetItemIterator.All)
        while iterator.value():
            item = iterator.value()
            word = item.data(0, 0)
            pic = ''
            if self.picture_on:
                pic = item.path
                if not os.path.exists(pic):
                    self.c.press_set_keyword_bt.emit()
                    self.enable_buttons()
                    q = QMessageBox(self)
                    q.information(self, 'information',
                                  '선택하신 이미지가 존재하지 않습니다. 다시 다운로드 눌러주세요.',
                                  QMessageBox.Ok)
                    return
            word_image.append([word, pic])
            iterator += 1
        puzzle_worker = PuzzleWorker(wordsearch_generater.MakeWordSearch,
                                     word_image, self.width, self.height,
                                     self.diff, self.option, self.picture_on,
                                     self.korean, self.chosung)
        puzzle_worker.signal.puzzle_complete.connect(self.puzzle_finish)
        puzzle_worker.signal.recursionerrormsg.connect(self.errormsg)
        self.threadpool.start(puzzle_worker)

    def puzzle_finish(self):
        q = QMessageBox(self)
        q.information(self, 'information', '바탕화면에 퍼즐 파일이 저장되었습니다.',
                      QMessageBox.Ok)
        # TODO button, whether to open the file or not --> i should get the file's path and create a thread to open it
        self.enable_buttons()

    def errormsg(self):
        q = QMessageBox(self)
        q.information(self, 'information', '단어의 개수에 비해서 퍼즐의 크기가 너무 작습니다.',
                      QMessageBox.Ok)
        self.enable_buttons()