Example #1
0
 def edit_array(self):
     """Open an array editor dialog"""
     parent = self.parent_layout.parent
     label = self.item.get_prop_value("display", "label")
     from spyderlib.widgets.arrayeditor import ArrayEditor
     editor = ArrayEditor(parent)
     if editor.setup_and_check(self.arr, title=label):
         if editor.exec_():
             self.update(self.arr)
Example #2
0
 def edit_array(self):
     """Open an array editor dialog"""
     parent = self.parent_layout.parent
     label = self.item.get_prop_value("display", "label")
     from spyderlib.widgets.arrayeditor import ArrayEditor
     editor = ArrayEditor(parent)
     if editor.setup_and_check(self.arr, title=label):
         if editor.exec_():
             self.update(self.arr)
Example #3
0
def oedit(obj):
    """
    Edit the object 'obj' in a GUI-based editor and return the edited copy
    (if Cancel is pressed, return None)

    The object 'obj' is a container
    
    Supported container types:
    dict, list, tuple, str/unicode or numpy.array
    
    (instantiate a new QApplication if necessary,
    so it can be called directly from the interpreter)
    """
    # Local import
    from spyderlib.widgets.texteditor import TextEditor
    from spyderlib.widgets.dicteditor import DictEditor, ndarray, FakeObject
    from spyderlib.widgets.arrayeditor import ArrayEditor
    from spyderlib.utils.qthelpers import qapplication
    _app = qapplication()

    if isinstance(obj, ndarray) and ndarray is not FakeObject:
        dialog = ArrayEditor()
        if dialog.setup_and_check(obj):
            if dialog.exec_():
                return obj
    elif isinstance(obj, (str, unicode)):
        dialog = TextEditor(obj)
        if dialog.exec_():
            return dialog.get_copy()
    elif isinstance(obj, (dict, tuple, list)):
        dialog = DictEditor(obj)
        if dialog.exec_():
            return dialog.get_copy()
    else:
        raise RuntimeError("Unsupported datatype")
Example #4
0
def create_dialog(obj, obj_name):
    """Creates the editor dialog and returns a tuple (dialog, func) where func
    is the function to be called with the dialog instance as argument, after 
    quitting the dialog box
    
    The role of this intermediate function is to allow easy monkey-patching.
    (uschmitt suggested this indirection here so that he can monkey patch 
    oedit to show eMZed related data)
    """
    # Local import
    from spyderlib.widgets.texteditor import TextEditor
    from spyderlib.widgets.dicteditorutils import (ndarray, FakeObject,
                                                   Image, is_known_type,
                                                   DataFrame, TimeSeries)
    from spyderlib.widgets.dicteditor import DictEditor
    from spyderlib.widgets.arrayeditor import ArrayEditor
    if DataFrame is not FakeObject:
        from spyderlib.widgets.dataframeeditor import DataFrameEditor
    
    conv_func = lambda data: data
    readonly = not is_known_type(obj)
    if isinstance(obj, ndarray) and ndarray is not FakeObject:
        dialog = ArrayEditor()
        if not dialog.setup_and_check(obj, title=obj_name,
                                      readonly=readonly):
            return
    elif isinstance(obj, Image) and Image is not FakeObject \
         and ndarray is not FakeObject:
        dialog = ArrayEditor()
        import numpy as np
        data = np.array(obj)
        if not dialog.setup_and_check(data, title=obj_name,
                                      readonly=readonly):
            return
        from spyderlib.pil_patch import Image
        conv_func = lambda data: Image.fromarray(data, mode=obj.mode)
    elif isinstance(obj, (DataFrame, TimeSeries)) \
         and DataFrame is not FakeObject:
        dialog = DataFrameEditor()
        if not dialog.setup_and_check(obj):
            return		
    elif is_text_string(obj):
        dialog = TextEditor(obj, title=obj_name, readonly=readonly)
    else:
        dialog = DictEditor()
        dialog.setup(obj, title=obj_name, readonly=readonly)

    def end_func(dialog):
        return conv_func(dialog.get_value())

    return dialog, end_func
Example #5
0
 def createEditor(self, parent, option, index):
     """Overriding method createEditor"""
     if index.column() < 3:
         return None
     value = self.get_value(index)
     key = index.model().get_key(index)
     readonly = isinstance(value, tuple) or self.parent().readonly
     #---editor = DictEditor
     if isinstance(value, (list, tuple, dict)) and not self.inplace:
         editor = DictEditor(value,
                             key,
                             icon=self.parent().windowIcon(),
                             readonly=readonly)
         if editor.exec_() and not readonly:
             self.set_value(index, editor.get_copy())
         return None
     #---editor = ArrayEditor
     elif isinstance(value, ndarray) and ndarray is not FakeObject \
                                     and not self.inplace:
         if value.size == 0:
             return None
         editor = ArrayEditor(parent)
         if editor.setup_and_check(value, title=key, readonly=readonly):
             if editor.exec_():
                 # Only necessary for child class RemoteDictDelegate:
                 # (ArrayEditor does not make a copy of value)
                 self.set_value(index, value)
         return None
     #---editor = QDateTimeEdit
     elif isinstance(value, datetime.datetime) and not self.inplace:
         editor = QDateTimeEdit(value, parent)
         editor.setCalendarPopup(True)
         editor.setFont(get_font('dicteditor'))
         self.connect(editor, SIGNAL("returnPressed()"),
                      self.commitAndCloseEditor)
         return editor
     #---editor = QDateEdit
     elif isinstance(value, datetime.date) and not self.inplace:
         editor = QDateEdit(value, parent)
         editor.setCalendarPopup(True)
         editor.setFont(get_font('dicteditor'))
         self.connect(editor, SIGNAL("returnPressed()"),
                      self.commitAndCloseEditor)
         return editor
     #---editor = QTextEdit
     elif isinstance(value, (str, unicode)) and len(value) > 40:
         editor = TextEditor(value, key)
         if editor.exec_() and not readonly:
             conv = str if isinstance(value, str) else unicode
             self.set_value(index, conv(editor.get_copy()))
         return None
     #---editor = QLineEdit
     else:
         editor = QLineEdit(parent)
         editor.setFont(get_font('dicteditor'))
         editor.setAlignment(Qt.AlignLeft)
         self.connect(editor, SIGNAL("returnPressed()"),
                      self.commitAndCloseEditor)
         return editor
Example #6
0
def oedit(obj):
    """
    Edit the object 'obj' in a GUI-based editor and return the edited copy
    (if Cancel is pressed, return None)

    The object 'obj' is a container
    
    Supported container types:
    dict, list, tuple, str/unicode or numpy.array
    
    (instantiate a new QApplication if necessary,
    so it can be called directly from the interpreter)
    """
    # Local import
    from spyderlib.widgets.texteditor import TextEditor
    from spyderlib.widgets.dicteditor import DictEditor, ndarray, FakeObject
    from spyderlib.widgets.arrayeditor import ArrayEditor

    # Creating QApplication if necessary
    from PyQt4.QtGui import QApplication
    if QApplication.startingUp():
        QApplication([])

    if isinstance(obj, ndarray) and ndarray is not FakeObject:
        dialog = ArrayEditor()
        if dialog.setup_and_check(obj):
            if dialog.exec_():
                return obj
    elif isinstance(obj, (str, unicode)):
        dialog = TextEditor(obj)
        if dialog.exec_():
            return dialog.get_copy()
    elif isinstance(obj, (dict, tuple, list)):
        dialog = DictEditor(obj)
        if dialog.exec_():
            return dialog.get_copy()
    else:
        raise RuntimeError("Unsupported datatype")
Example #7
0
    def oedit(obj, modal=True, namespace=None, keeper=objecteditor.DialogKeeper()):
        """
        Edit the object 'obj' in a GUI-based editor and return the edited copy
        (if Cancel is pressed, return None)

        The object 'obj' is a container

        Supported container types:
        dict, list, tuple, str/unicode or numpy.array

        (instantiate a new QApplication if necessary,
        so it can be called directly from the interpreter)
        """
        # Local import
        from spyderlib.widgets.texteditor import TextEditor
        from spyderlib.widgets.dicteditorutils import (ndarray, FakeObject,
                                                       Image, is_known_type)
        from spyderlib.widgets.dicteditor import DictEditor
        from spyderlib.widgets.arrayeditor import ArrayEditor

        from spyderlib.utils.qthelpers import qapplication
        app = qapplication()

        # STARTMODIFICATION EMZED
        from emzed.core.data_types import PeakMap, Table
        from emzed.core.explorers  import PeakMapExplorer, TableExplorer
        # ENDMODIFICATION EMZED


        if modal:
            obj_name = ''
        else:
            assert isinstance(obj, basestring)
            obj_name = obj
            if namespace is None:
                namespace = globals()
            keeper.set_namespace(namespace)
            obj = namespace[obj_name]
            # keep QApplication reference alive in the Python interpreter:
            namespace['__qapp__'] = app

        conv_func = lambda data: data
        readonly = not is_known_type(obj)
        if isinstance(obj, ndarray) and ndarray is not FakeObject:
            dialog = ArrayEditor()
            if not dialog.setup_and_check(obj, title=obj_name,
                                          readonly=readonly):
                return
        elif isinstance(obj, Image) and Image is not FakeObject \
             and ndarray is not FakeObject:
            dialog = ArrayEditor()
            import numpy as np
            data = np.array(obj)
            if not dialog.setup_and_check(data, title=obj_name,
                                          readonly=readonly):
                return
            from spyderlib.pil_patch import Image
            conv_func = lambda data: Image.fromarray(data, mode=obj.mode)
        elif isinstance(obj, (str, unicode)):
            dialog = TextEditor(obj, title=obj_name, readonly=readonly)

        # START MODIFICATION EMZED
        elif isinstance(obj, PeakMap):
            dialog = PeakMapExplorer()
            dialog.setup(obj)
        elif isinstance(obj, Table):
            dialog = TableExplorer([obj], False)
            conv_func = lambda (x,) : x
        elif isinstance(obj, list) and all(isinstance(t, Table) for t in obj):
            dialog = TableExplorer(obj, False)
        # END MODIFICATION EMZED

        else:
            dialog = DictEditor()
            dialog.setup(obj, title=obj_name, readonly=readonly)

        def end_func(dialog):
            return conv_func(dialog.get_value())

        if modal:
            if dialog.exec_():
                return end_func(dialog)
        else:
            keeper.create_dialog(dialog, obj_name, end_func)
            import os
            qt_inputhook = os.environ.get("INSTALL_QT_INPUTHOOK",
                                          "").lower() == "true"
            if os.name == 'nt' and not qt_inputhook \
               and not os.environ.get('IPYTHON', False):
                app.exec_()
Example #8
0
    def oedit(obj, modal=True, namespace=None, keeper=objecteditor.DialogKeeper()):
        """
        Edit the object 'obj' in a GUI-based editor and return the edited copy
        (if Cancel is pressed, return None)

        The object 'obj' is a container
        
        Supported container types:
        dict, list, tuple, str/unicode or numpy.array
        
        (instantiate a new QApplication if necessary,
        so it can be called directly from the interpreter)
        """
        # Local import
        from spyderlib.widgets.texteditor import TextEditor
        from spyderlib.widgets.dicteditorutils import (ndarray, FakeObject,
                                                       Image, is_known_type)
        from spyderlib.widgets.dicteditor import DictEditor
        from spyderlib.widgets.arrayeditor import ArrayEditor
        
        from spyderlib.utils.qthelpers import qapplication
        app = qapplication()

        # STARTMODIFICATION EMZED
        import libms.Explorers
        from libms.DataStructures import PeakMap, Table
        # ENDMODIFICATION EMZED

        
        if modal:
            obj_name = ''
        else:
            assert isinstance(obj, basestring)
            obj_name = obj
            if namespace is None:
                namespace = globals()
            keeper.set_namespace(namespace)
            obj = namespace[obj_name]
            # keep QApplication reference alive in the Python interpreter:
            namespace['__qapp__'] = app

        conv_func = lambda data: data
        readonly = not is_known_type(obj)
        if isinstance(obj, ndarray) and ndarray is not FakeObject:
            dialog = ArrayEditor()
            if not dialog.setup_and_check(obj, title=obj_name,
                                          readonly=readonly):
                return
        elif isinstance(obj, Image) and Image is not FakeObject \
             and ndarray is not FakeObject:
            dialog = ArrayEditor()
            import numpy as np
            data = np.array(obj)
            if not dialog.setup_and_check(data, title=obj_name,
                                          readonly=readonly):
                return
            from spyderlib.pil_patch import Image
            conv_func = lambda data: Image.fromarray(data, mode=obj.mode)
        elif isinstance(obj, (str, unicode)):
            dialog = TextEditor(obj, title=obj_name, readonly=readonly)

        # START MODIFICATION EMZED
        elif isinstance(obj, PeakMap):
            dialog = libms.Explorers.MzExplorer()
            dialog.setup(obj)
        elif isinstance(obj, Table):
            dialog = libms.Explorers.TableExplorer([obj], False)
            conv_func = lambda (x,) : x
        elif isinstance(obj, list) and all(isinstance(t, Table) for t in obj):
            dialog = libms.Explorers.TableExplorer(obj, False)
        # END MODIFICATION EMZED

        else:
            dialog = DictEditor()
            dialog.setup(obj, title=obj_name, readonly=readonly)
        
        def end_func(dialog):
            return conv_func(dialog.get_value())
        
        if modal:
            if dialog.exec_():
                return end_func(dialog)
        else:
            keeper.create_dialog(dialog, obj_name, end_func)
            import os
            qt_inputhook = os.environ.get("INSTALL_QT_INPUTHOOK",
                                          "").lower() == "true"
            if os.name == 'nt' and not qt_inputhook \
               and not os.environ.get('IPYTHON', False):
                app.exec_()
def create_dialog(obj, obj_name):
    """Creates the editor dialog and returns a tuple (dialog, func) where func
    is the function to be called with the dialog instance as argument, after 
    quitting the dialog box
    
    The role of this intermediate function is to allow easy monkey-patching.
    (uschmitt suggested this indirection here so that he can monkey patch 
    oedit to show eMZed related data)
    """
    # Local import
    from spyderlib.widgets.texteditor import TextEditor
    from spyderlib.widgets.dicteditorutils import (ndarray, FakeObject, Image,
                                                   is_known_type, DataFrame,
                                                   TimeSeries)
    from spyderlib.widgets.dicteditor import DictEditor
    from spyderlib.widgets.arrayeditor import ArrayEditor
    if DataFrame is not FakeObject:
        from spyderlib.widgets.dataframeeditor import DataFrameEditor

    conv_func = lambda data: data
    readonly = not is_known_type(obj)
    if isinstance(obj, ndarray) and ndarray is not FakeObject:
        dialog = ArrayEditor()
        if not dialog.setup_and_check(obj, title=obj_name, readonly=readonly):
            return
    elif isinstance(obj, Image) and Image is not FakeObject \
         and ndarray is not FakeObject:
        dialog = ArrayEditor()
        import numpy as np
        data = np.array(obj)
        if not dialog.setup_and_check(data, title=obj_name, readonly=readonly):
            return
        from spyderlib.pil_patch import Image
        conv_func = lambda data: Image.fromarray(data, mode=obj.mode)
    elif isinstance(obj, (DataFrame, TimeSeries)) \
         and DataFrame is not FakeObject:
        dialog = DataFrameEditor()
        if not dialog.setup_and_check(obj):
            return
    elif is_text_string(obj):
        dialog = TextEditor(obj, title=obj_name, readonly=readonly)
    else:
        dialog = DictEditor()
        dialog.setup(obj, title=obj_name, readonly=readonly)

    def end_func(dialog):
        return conv_func(dialog.get_value())

    return dialog, end_func