示例#1
0
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()
        self.ui.setupUi(self)
        self.ui.txtEditor.setAcceptRichText(True)

        # txtedit our context menu
        self.ui.txtEditor.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.connect(self.ui.txtEditor, QtCore.SIGNAL('customContextMenuRequested(const QPoint &)'), self.context_menu)
        self.ui.treeView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(["Objects"])
        self.ui.treeView.setModel(model)
        self.ui.treeView.setUniformRowHeights(True)

        # add items to the file menu
        self.ui.menuFile.addAction("Open", lambda: self.file_open(model))
        self.ui.menuFile.addAction("Save", lambda: self.file_save_json(model))
        self.ui.menuFile.addAction("Close File", lambda: self.file_close(model))
        self.ui.menuFile.addSeparator()
        self.ui.menuFile.addAction("Quit", self.close_app)
        self.ui.treeView.clicked.connect(lambda: self.on_treeview_clicked(model))
        # here we connect signals with our slots

        self.ui.treeView.setModel(model)
        QtCore.QObject.connect(self.ui.txtEditor, QtCore.SIGNAL("textChanged()"), lambda: self.json_update(model))
示例#2
0
文件: start.py 项目: qai41/OpenPy
 def __init__(self, parent=None):
     QtGui.QWidget.__init__(self, parent)
     self.ui = Ui_notepad()
     self.ui.setupUi(self)
     self.watcher = QtCore.QFileSystemWatcher(self)
     QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"),
                            self.file_dialog)
     QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"),
                            self.file_save)
     QtCore.QObject.connect(self.ui.editor_window,
                            QtCore.SIGNAL("textChanged()"),
                            self.enable_save)
     QtCore.QObject.connect(self.watcher,
                            QtCore.SIGNAL("fileChanged(const QString&)"),
                            self.file_changed)
     self.filename = False
示例#3
0
 def __init__(self, parent=None):
     QtGui.QWidget.__init__(self, parent)
     self.ui = Ui_notepad()
     self.ui.setupUi(self)
     self.watcher = QtCore.QFileSystemWatcher(self)
     QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"), self.file_dialog)
     QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"), self.file_save)
     QtCore.QObject.connect(self.ui.editor_window, QtCore.SIGNAL("textChanged()"), self.enable_save)
     QtCore.QObject.connect(self.watcher, QtCore.SIGNAL("fileChanged(const QString&)"), self.file_changed)
     self.filename = False
示例#4
0
class StartQt4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()
        self.ui.setupUi(self)
        self.watcher = QtCore.QFileSystemWatcher(self)
        QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"), self.file_dialog)
        QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"), self.file_save)
        QtCore.QObject.connect(self.ui.editor_window, QtCore.SIGNAL("textChanged()"), self.enable_save)
        QtCore.QObject.connect(self.watcher, QtCore.SIGNAL("fileChanged(const QString&)"), self.file_changed)
        self.filename = False

    def enable_save(self):
        self.ui.button_save.setEnabled(True)

    def file_changed(self, path):
        response = False
        SAVE = u"名前をつけて保存"
        RELOAD = u"ファイルを再読込"
        CANCEL = u"キャンセル"
        message = QtGui.QMessageBox(self)
        message.setText(u"ファイルは変更されています")
        message.setWindowTitle("Notepad")
        message.setIcon(QtGui.QMessageBox.Warning)
        message.addButton(SAVE, QtGui.QMessageBox.AcceptRole)
        message.addButton(RELOAD, QtGui.QMessageBox.DestructiveRole)
        message.addButton(CANCEL, QtGui.QMessageBox.RejectRole)
        message.setDetailedText(str(path) + u" は、他のアプリケーションで内容を変更されたか削除されました。どうしますか?")
        message.exec_()
        response = message.clickedButton().text()

        if response == SAVE:
            fd = QtGui.QFileDialog(self)
            newfile = fd.getSaveFileName()
            if newfile:
                s = codecs.open(newfile, "w", "utf-8")
                s.write(unicode(self.ui.editor_window.toPlainText()))
                s.close()
                self.ui.button_save.setEnabled(False)
                if self.filename and str(newfile) != str(self.filename):
                    self.watcher.removePath(self.filename)
                    self.watcher.addPath(self.filename)
                    self.filename = newfile
        elif response == RELOAD:
            s = codecs.open(self.filename, "r", "utf-8").read()
            self.ui.editor_window.setPlainText(s)
            self.ui.button_save.setEnabled(False)

    def file_dialog(self):
        response = False
        SAVE = u"保存"
        DISCARD = u"保存せずに終了"
        CANCEL = u"キャンセル"

        if self.ui.button_save.isEnabled():
            message = QtGui.QMessageBox(self)
            message.setText(u"内容が変更されています。保存しますか?")
            message.setWindowTitle("Notepad")
            message.setIcon(QtGui.QMessageBox.Question)
            message.addButton(SAVE, QtGui.QMessageBox.AcceptRole)
            message.addButton(DISCARD, QtGui.QMessageBox.DestructiveRole)
            message.addButton(CANCEL, QtGui.QMessageBox.RejectRole)
            message.setDetailedText(str(self.filename) + u"の変更が保存されていません。")
            message.exec_()
            response = message.clickedButton().text()

            if response == SAVE:
                self.file_save()
                self.ui.button_save.setEnabled(False)
            elif response == DISCARD:
                self.ui.button_save.setEnabled(False)

        if response != CANCEL:
            fd = QtGui.QFileDialog(self)
            if self.filename:
                self.watcher.removePath(self.filename)

            self.filename = fd.getOpenFileName()

            if isfile(self.filename):
                s = codecs.open(self.filename, "r", "utf-8").read()
                self.ui.editor_window.setPlainText(s)
                self.ui.button_save.setEnabled(False)
                self.watcher.addPath(self.filename)

    def file_save(self):
        from os.path import isfile
        if isfile(self.filename):
            import codecs
            s = codecs.open(self.filename, "w", "utf-8")
            s.write(unicode(self.ui.editor_window.toPlainText()))
            s.close()
            self.ui.button_save.setEnabled(False)
示例#5
0
class StartQT4(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()
        self.ui.setupUi(self)
        self.ui.txtEditor.setAcceptRichText(True)

        # txtedit our context menu
        self.ui.txtEditor.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.connect(self.ui.txtEditor, QtCore.SIGNAL('customContextMenuRequested(const QPoint &)'), self.context_menu)
        self.ui.treeView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(["Objects"])
        self.ui.treeView.setModel(model)
        self.ui.treeView.setUniformRowHeights(True)

        # add items to the file menu
        self.ui.menuFile.addAction("Open", lambda: self.file_open(model))
        self.ui.menuFile.addAction("Save", lambda: self.file_save_json(model))
        self.ui.menuFile.addAction("Close File", lambda: self.file_close(model))
        self.ui.menuFile.addSeparator()
        self.ui.menuFile.addAction("Quit", self.close_app)
        self.ui.treeView.clicked.connect(lambda: self.on_treeview_clicked(model))
        # here we connect signals with our slots

        self.ui.treeView.setModel(model)
        QtCore.QObject.connect(self.ui.txtEditor, QtCore.SIGNAL("textChanged()"), lambda: self.json_update(model))

    def json_update(self, model):
        # when text is changed in the txtEditor update the corresponding dictionary data
        # this throws and exception the very first time you open a file because the selected index
        # hasn't actually been selected (even though the first item is highlighted in the treeview
        # at load time
        try:
            indexItem = self.ui.treeView.selectionModel().selectedIndexes()
            key = str(model.itemFromIndex(indexItem[0]).text())
            json_controller.setDataAt(key, self.ui.txtEditor.toHtml()) # will need a condition for html or plaintext
        except Exception as e:
            print ""

    def on_treeview_clicked(self, model):
        indexItem = self.ui.treeView.selectionModel().selectedIndexes()
        self.ui.txtEditor.setText(json_controller.getDataAt(str(model.itemFromIndex(indexItem[0]).text())))

    def context_menu(self): # txtEditor right click context menu
        menu = QtGui.QMenu(self)
        menu.addAction("bold", self.bold_txt)
        menu.addAction("header", self.header_txt)
        menu.addAction("link", self.insert_link)
        menu.exec_(QtGui.QCursor.pos())

    def insert_link(self):
        link, ok = QtGui.QInputDialog.getText(self, "Input Dialog", "enter URL")

        if ok:
            cursor = self.ui.txtEditor.textCursor() # get selected text
            txt_selected = cursor.selectedText()
            s = "<a href=" + link + ">" + txt_selected + "</a>"
            self.ui.txtEditor.textCursor().insertHtml(s)

    def bold_txt(self): # place holder for context menu action
        cursor = self.ui.txtEditor.textCursor()
        txt_selected = cursor.selectedText()
        s = "<b>" + txt_selected + "</b>"
        self.ui.txtEditor.textCursor().insertHtml(s)

    def header_txt(self):
        cursor = self.ui.txtEditor.textCursor()
        txt_selected = cursor.selectedText()
        s = "<h1>" + txt_selected + "</h1>"
        self.ui.txtEditor.textCursor().insertHtml(s)

    def file_open(self, model):
        # open a file
        fd = QtGui.QFileDialog(self)
        try:
            file_to_open = json_controller.open_json_file(fd.getOpenFileName())
            for i in file_to_open: # populate the treeview
                child = QtGui.QStandardItem(str(i))
                model.appendRow(child)
            self.ui.txtEditor.setText(str(file_to_open.values()[0])) # load the first object's value into txtEditor
            index = model.index(0, 0) # select the first key in the treeview
            self.ui.treeView.setCurrentIndex(index)
        except Exception as e:
            print "error: " + str(e)

    def file_save_json(self, model):
        # get json object name from user
        # and write it to a file
        string_to_write = "{"
        for i in range(model.rowCount()): # iterate through all keys in the model
            key = str(model.itemFromIndex(model.index(i, 0)).text()) # get key
            s = self.parse_html(json_controller.getDataAt(key))
            string_to_write += "\"" + key + "\":\"" + s + "\""
            if i is not model.rowCount()-1:
                string_to_write += ","
        string_to_write += "}"
        fd = QtGui.QFileDialog(self).getSaveFileName(self, "Save File", "", "Html (*.html);;Text (*.text);;All (*.*)")

        try:
            myfile = open(fd, "w")
            myfile.write(string_to_write)
            myfile.close()
        except IOError:
            print ""

    def file_close(self, model):
        model.clear()
        self.ui.txtEditor.clear()

    def parse_html(self, s):
        # parse html into usable json object
        # s = str(self.ui.txtEditor.toHtml())
        s = str(s)
        x = s.find("<p")
        y = s.find("</body>")
        str_list = (s[x:y]).split("\"")
        for index, wrd in enumerate(str_list):
            if index != len(str_list)-1:
                str_list[index] = wrd + '\\\"'
        s = "".join(str_list)
        str_list = s.split("/")
        for index, wrd in enumerate(str_list):
            if index != len(str_list)-1:
                str_list[index] = wrd + '\\/'
        s = "".join(str_list)
        str_list = s.split("\n")

        s = "".join(str_list)
        return s

    def close_app(self):
        # close the application
        self.close()
示例#6
0
文件: start.py 项目: qai41/OpenPy
class StartQt4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()
        self.ui.setupUi(self)
        self.watcher = QtCore.QFileSystemWatcher(self)
        QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"),
                               self.file_dialog)
        QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"),
                               self.file_save)
        QtCore.QObject.connect(self.ui.editor_window,
                               QtCore.SIGNAL("textChanged()"),
                               self.enable_save)
        QtCore.QObject.connect(self.watcher,
                               QtCore.SIGNAL("fileChanged(const QString&)"),
                               self.file_changed)
        self.filename = False

    def enable_save(self):
        self.ui.button_save.setEnabled(True)

    def file_changed(self, path):
        response = False
        SAVE = u"名前をつけて保存"
        RELOAD = u"ファイルを再読込"
        CANCEL = u"キャンセル"
        message = QtGui.QMessageBox(self)
        message.setText(u"ファイルは変更されています")
        message.setWindowTitle("Notepad")
        message.setIcon(QtGui.QMessageBox.Warning)
        message.addButton(SAVE, QtGui.QMessageBox.AcceptRole)
        message.addButton(RELOAD, QtGui.QMessageBox.DestructiveRole)
        message.addButton(CANCEL, QtGui.QMessageBox.RejectRole)
        message.setDetailedText(
            str(path) + u" は、他のアプリケーションで内容を変更されたか削除されました。どうしますか?")
        message.exec_()
        response = message.clickedButton().text()

        if response == SAVE:
            fd = QtGui.QFileDialog(self)
            newfile = fd.getSaveFileName()
            if newfile:
                s = codecs.open(newfile, "w", "utf-8")
                s.write(unicode(self.ui.editor_window.toPlainText()))
                s.close()
                self.ui.button_save.setEnabled(False)
                if self.filename and str(newfile) != str(self.filename):
                    self.watcher.removePath(self.filename)
                    self.watcher.addPath(self.filename)
                    self.filename = newfile
        elif response == RELOAD:
            s = codecs.open(self.filename, "r", "utf-8").read()
            self.ui.editor_window.setPlainText(s)
            self.ui.button_save.setEnabled(False)

    def file_dialog(self):
        response = False
        SAVE = u"保存"
        DISCARD = u"保存せずに終了"
        CANCEL = u"キャンセル"

        if self.ui.button_save.isEnabled():
            message = QtGui.QMessageBox(self)
            message.setText(u"内容が変更されています。保存しますか?")
            message.setWindowTitle("Notepad")
            message.setIcon(QtGui.QMessageBox.Question)
            message.addButton(SAVE, QtGui.QMessageBox.AcceptRole)
            message.addButton(DISCARD, QtGui.QMessageBox.DestructiveRole)
            message.addButton(CANCEL, QtGui.QMessageBox.RejectRole)
            message.setDetailedText(str(self.filename) + u"の変更が保存されていません。")
            message.exec_()
            response = message.clickedButton().text()

            if response == SAVE:
                self.file_save()
                self.ui.button_save.setEnabled(False)
            elif response == DISCARD:
                self.ui.button_save.setEnabled(False)

        if response != CANCEL:
            fd = QtGui.QFileDialog(self)
            if self.filename:
                self.watcher.removePath(self.filename)

            self.filename = fd.getOpenFileName()

            if isfile(self.filename):
                s = codecs.open(self.filename, "r", "utf-8").read()
                self.ui.editor_window.setPlainText(s)
                self.ui.button_save.setEnabled(False)
                self.watcher.addPath(self.filename)

    def file_save(self):
        from os.path import isfile
        if isfile(self.filename):
            import codecs
            s = codecs.open(self.filename, "w", "utf-8")
            s.write(unicode(self.ui.editor_window.toPlainText()))
            s.close()
            self.ui.button_save.setEnabled(False)