示例#1
0
    def openObject(self, index):
        def object_saved(key, updated_object):
            parent_object = copy(self.original_object)
            setattr(parent_object, key, updated_object.to_json())
            self.valueChanged(parent_object)

        def object_dismissed():
            del self.object_editor

        key = index.model().data(index.model().index(index.row(), 0))
        if isinstance(self.original_object[key], dict) and \
            not index.model().flags(index) & QtCore.Qt.ItemIsEditable:
            self.object_editor = ObjectEditor(
                self, key, MetaObject(dict(self.model().basemodel[key])))
            self.object_editor.saved.connect(object_saved)
            self.object_editor.rejected.connect(object_dismissed)
            self.object_editor.show()
示例#2
0
    def editObject(self, index):
        def object_saved(updated_object):
            row = self.proxyModel.mapToSource(index).row()
            obj = index.model().sourceModel().objects[row]
            telepat_object = TelepatBaseObject(updated_object.to_json())
            patches = obj.patch_against(telepat_object)
            self.channel.patch(updated_object)

        def object_dismissed():
            if hasattr(self, "object_editor"):
                del self.object_editor

        row = self.proxyModel.mapToSource(index).row()
        obj = index.model().sourceModel().objects[row]
        self.object_editor = ObjectEditor(self, self.telepat_context,
                                          self.app_users,
                                          TelepatObject(obj.to_json()))
        self.object_editor.saved.connect(object_saved)
        self.object_editor.rejected.connect(object_dismissed)
        self.object_editor.show()
示例#3
0
    def openObject(self, index):
        def object_saved(key, updated_object):
            parent_object = copy(self.original_object)
            setattr(parent_object, key, updated_object.to_json())
            self.valueChanged(parent_object)

        def object_dismissed():
            del self.object_editor

        key = index.model().data(index.model().index(index.row(), 0))
        if isinstance(self.original_object[key], dict) and \
            not index.model().flags(index) & QtCore.Qt.ItemIsEditable:
            self.object_editor = ObjectEditor(self, key, MetaObject(dict(self.model().basemodel[key])))
            self.object_editor.saved.connect(object_saved)
            self.object_editor.rejected.connect(object_dismissed)
            self.object_editor.show()
示例#4
0
    def editObject(self, index):
        def object_saved(updated_object):
            row = self.proxyModel.mapToSource(index).row()
            obj = index.model().sourceModel().objects[row]
            telepat_object = TelepatBaseObject(updated_object.to_json())
            patches = obj.patch_against(telepat_object)
            self.channel.patch(updated_object)

        def object_dismissed():
            if hasattr(self, "object_editor"):
                del self.object_editor

        row = self.proxyModel.mapToSource(index).row()
        obj = index.model().sourceModel().objects[row]
        self.object_editor = ObjectEditor(self, self.telepat_context, self.app_users, TelepatObject(obj.to_json()))
        self.object_editor.saved.connect(object_saved)
        self.object_editor.rejected.connect(object_dismissed)
        self.object_editor.show()
示例#5
0
class ModelBrowser(QtWidgets.QWidget):
    channel = None

    def browseModel(self, telepat_context, telepat_model, app_users):
        self.telepat_context = telepat_context
        self.telepat_model = telepat_model
        self.app_users = app_users

        if self.channel:
            self.unsubscribe()

        def on_subscribe_success(channel, objects):
            self.model = BrowserModel(self.treeView, objects)
            self.proxyModel = ModelSortFilterProxyModel(self)
            self.proxyModel.setSourceModel(self.model)
            self.proxyModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
            self.treeView.setModel(self.proxyModel)
            self.treeView.resizeColumnToContents(0)

            self.channel = channel
            self.channel.on_update_object = self.on_update_object

        def on_subscribe_failure(err_code, err_message):
            print("Error msg: {0}".format(err_message))

        if not hasattr(self, "treeView"):
            self.treeView = self.findChild(QtWidgets.QTreeView, "treeView")
            self.treeView.setSortingEnabled(True)
            self.treeView.setRootIsDecorated(False)
            self.treeView.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
            self.treeView.doubleClicked.connect(self.editObject)
        
        if not hasattr(self, "bFilterLineEdit"):
            self.bFilterLineEdit = self.findChild(QtWidgets.QLineEdit, "bFilterLineEdit")
            self.bFilterLineEdit.textChanged.connect(self.filterChanged)

        self.treeView.setModel(None)

        worker = SubscribeWorker(self, telepat_context, telepat_model, TelepatBaseObject)
        worker.success.connect(on_subscribe_success)
        worker.failed.connect(on_subscribe_failure)
        worker.log.connect(console.log)
        worker.start()

    def filterChanged(self):
        if hasattr(self, "proxyModel"):
            self.proxyModel.invalidateFilter()
            self.proxyModel.setFilterFixedString(self.bFilterLineEdit.text())

    def editObject(self, index):
        def object_saved(updated_object):
            row = self.proxyModel.mapToSource(index).row()
            obj = index.model().sourceModel().objects[row]
            telepat_object = TelepatBaseObject(updated_object.to_json())
            patches = obj.patch_against(telepat_object)
            self.channel.patch(updated_object)

        def object_dismissed():
            if hasattr(self, "object_editor"):
                del self.object_editor

        row = self.proxyModel.mapToSource(index).row()
        obj = index.model().sourceModel().objects[row]
        self.object_editor = ObjectEditor(self, self.telepat_context, self.app_users, TelepatObject(obj.to_json()))
        self.object_editor.saved.connect(object_saved)
        self.object_editor.rejected.connect(object_dismissed)
        self.object_editor.show()

    def on_update_object(self, updated_object, notification):
        event = TelepatObjectUpdateEvent(updated_object, notification)
        QtWidgets.QApplication.postEvent(self.model, event)
        
    def unsubscribe(self):
        def on_unsubscribe_success():
            pass

        def on_unsubscribe_failure(err_code, err_message):
            print("Error msg: {0}".format(err_message))

        worker = UnsubscribeWorker(self, self.channel)
        worker.success.connect(on_unsubscribe_success)
        worker.failed.connect(on_unsubscribe_failure)
        worker.log.connect(console.log)
        worker.start()
示例#6
0
class EditorTableView(QtWidgets.QTableView):
    original_object = None
    updated_object = None

    def __init__(self, parent=None):
        super(EditorTableView, self).__init__(parent)

        self.setSelectionMode(QtWidgets.QTableView.SingleSelection)
        self.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
        self.setEditTriggers(QtWidgets.QAbstractItemView.DoubleClicked)
        self.verticalHeader().setVisible(False)
        self.doubleClicked.connect(self.openObject)

    def resizeEvent(self, event):
        width = event.size().width()
        self.setColumnWidth(0, width * 0.25)
        self.setColumnWidth(1, width * 0.75)

    def editObject(self, basemodel, objects_map=None):
        if self.original_object and self.original_object.id == basemodel.id:  # If it's the same object just look for updates
            self.model().basemodel = basemodel
            return
        self.original_object = copy(basemodel)
        self.objects_map = objects_map if objects_map else {}
        model = EditorTableModel(self, basemodel)
        model.valueChanged.connect(self.valueChanged)
        self.setModel(model)
        self.setItemDelegateForColumn(
            1, ComboBoxDelegate(self, basemodel, self.objects_map))

        for row in range(0, model.rowCount(None)):
            key = model.rows[row][0][:-3]
            if key in self.objects_map:
                self.openPersistentEditor(model.index(row, 1))

    def openObject(self, index):
        def object_saved(key, updated_object):
            parent_object = copy(self.original_object)
            setattr(parent_object, key, updated_object.to_json())
            self.valueChanged(parent_object)

        def object_dismissed():
            del self.object_editor

        key = index.model().data(index.model().index(index.row(), 0))
        if isinstance(self.original_object[key], dict) and \
            not index.model().flags(index) & QtCore.Qt.ItemIsEditable:
            self.object_editor = ObjectEditor(
                self, key, MetaObject(dict(self.model().basemodel[key])))
            self.object_editor.saved.connect(object_saved)
            self.object_editor.rejected.connect(object_dismissed)
            self.object_editor.show()

    def valueChanged(self, updated_object):
        def patch_success(response):
            self.editObject(self.updated_object)
            self.updated_object = None

        def patch_failed(status, message):
            QtWidgets.QMessageBox.critical(
                self, "Patch error", "Error {0}: {1}".format(status, message))
            self.updated_object = None

        self.updated_object = updated_object
        if self.updated_object == self.original_object:
            return

        if isinstance(self.updated_object, TelepatContext):
            worker = ContextPatchWorker(self, self.updated_object)
            worker.success.connect(patch_success)
            worker.failed.connect(patch_failed)
            worker.log.connect(console.log)
            worker.start()
示例#7
0
class ModelBrowser(QtWidgets.QWidget):
    channel = None

    def browseModel(self, telepat_context, telepat_model, app_users):
        self.telepat_context = telepat_context
        self.telepat_model = telepat_model
        self.app_users = app_users

        if self.channel:
            self.unsubscribe()

        def on_subscribe_success(channel, objects):
            self.model = BrowserModel(self.treeView, objects)
            self.proxyModel = ModelSortFilterProxyModel(self)
            self.proxyModel.setSourceModel(self.model)
            self.proxyModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
            self.treeView.setModel(self.proxyModel)
            self.treeView.resizeColumnToContents(0)

            self.channel = channel
            self.channel.on_update_object = self.on_update_object

        def on_subscribe_failure(err_code, err_message):
            print("Error msg: {0}".format(err_message))

        if not hasattr(self, "treeView"):
            self.treeView = self.findChild(QtWidgets.QTreeView, "treeView")
            self.treeView.setSortingEnabled(True)
            self.treeView.setRootIsDecorated(False)
            self.treeView.setEditTriggers(
                QtWidgets.QAbstractItemView.NoEditTriggers)
            self.treeView.doubleClicked.connect(self.editObject)

        if not hasattr(self, "bFilterLineEdit"):
            self.bFilterLineEdit = self.findChild(QtWidgets.QLineEdit,
                                                  "bFilterLineEdit")
            self.bFilterLineEdit.textChanged.connect(self.filterChanged)

        self.treeView.setModel(None)

        worker = SubscribeWorker(self, telepat_context, telepat_model,
                                 TelepatBaseObject)
        worker.success.connect(on_subscribe_success)
        worker.failed.connect(on_subscribe_failure)
        worker.log.connect(console.log)
        worker.start()

    def filterChanged(self):
        if hasattr(self, "proxyModel"):
            self.proxyModel.invalidateFilter()
            self.proxyModel.setFilterFixedString(self.bFilterLineEdit.text())

    def editObject(self, index):
        def object_saved(updated_object):
            row = self.proxyModel.mapToSource(index).row()
            obj = index.model().sourceModel().objects[row]
            telepat_object = TelepatBaseObject(updated_object.to_json())
            patches = obj.patch_against(telepat_object)
            self.channel.patch(updated_object)

        def object_dismissed():
            if hasattr(self, "object_editor"):
                del self.object_editor

        row = self.proxyModel.mapToSource(index).row()
        obj = index.model().sourceModel().objects[row]
        self.object_editor = ObjectEditor(self, self.telepat_context,
                                          self.app_users,
                                          TelepatObject(obj.to_json()))
        self.object_editor.saved.connect(object_saved)
        self.object_editor.rejected.connect(object_dismissed)
        self.object_editor.show()

    def on_update_object(self, updated_object, notification):
        event = TelepatObjectUpdateEvent(updated_object, notification)
        QtWidgets.QApplication.postEvent(self.model, event)

    def unsubscribe(self):
        def on_unsubscribe_success():
            pass

        def on_unsubscribe_failure(err_code, err_message):
            print("Error msg: {0}".format(err_message))

        worker = UnsubscribeWorker(self, self.channel)
        worker.success.connect(on_unsubscribe_success)
        worker.failed.connect(on_unsubscribe_failure)
        worker.log.connect(console.log)
        worker.start()
示例#8
0
class EditorTableView(QtWidgets.QTableView):
    original_object = None
    updated_object = None
    
    def __init__(self, parent=None):
        super(EditorTableView,  self).__init__(parent)
        
        self.setSelectionMode(QtWidgets.QTableView.SingleSelection)
        self.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
        self.setEditTriggers(QtWidgets.QAbstractItemView.DoubleClicked)
        self.verticalHeader().setVisible(False)
        self.doubleClicked.connect(self.openObject)
        
    def resizeEvent(self, event):
        width = event.size().width()
        self.setColumnWidth(0, width * 0.25)
        self.setColumnWidth(1, width * 0.75)
        
    def editObject(self, basemodel, objects_map=None):
        if self.original_object and self.original_object.id == basemodel.id:  # If it's the same object just look for updates
            self.model().basemodel = basemodel
            return
        self.original_object = copy(basemodel)
        self.objects_map = objects_map if objects_map else {}
        model = EditorTableModel(self, basemodel)
        model.valueChanged.connect(self.valueChanged)
        self.setModel(model)
        self.setItemDelegateForColumn(1, ComboBoxDelegate(self, basemodel, self.objects_map))

        for row in range(0, model.rowCount(None)):
            key = model.rows[row][0][:-3]
            if key in self.objects_map:
                self.openPersistentEditor(model.index(row, 1))

    def openObject(self, index):
        def object_saved(key, updated_object):
            parent_object = copy(self.original_object)
            setattr(parent_object, key, updated_object.to_json())
            self.valueChanged(parent_object)

        def object_dismissed():
            del self.object_editor

        key = index.model().data(index.model().index(index.row(), 0))
        if isinstance(self.original_object[key], dict) and \
            not index.model().flags(index) & QtCore.Qt.ItemIsEditable:
            self.object_editor = ObjectEditor(self, key, MetaObject(dict(self.model().basemodel[key])))
            self.object_editor.saved.connect(object_saved)
            self.object_editor.rejected.connect(object_dismissed)
            self.object_editor.show()
        
    def valueChanged(self, updated_object):
        def patch_success(response):
            self.editObject(self.updated_object)
            self.updated_object = None
            
        def patch_failed(status,  message):
            QtWidgets.QMessageBox.critical(self, "Patch error", "Error {0}: {1}".format(status, message))
            self.updated_object = None

        self.updated_object = updated_object
        if self.updated_object == self.original_object:
            return

        if isinstance(self.updated_object, TelepatContext):
            worker = ContextPatchWorker(self, self.updated_object)
            worker.success.connect(patch_success)
            worker.failed.connect(patch_failed)
            worker.log.connect(console.log)
            worker.start()