ProjectNodes.py 5.51 KB
Newer Older
Damien Naudet's avatar
WIP  
Damien Naudet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

from __future__ import absolute_import

__authors__ = ["D. Naudet"]
__license__ = "MIT"
__date__ = "01/11/2016"

Damien Naudet's avatar
WIP  
Damien Naudet committed
32
import os
Damien Naudet's avatar
WIP  
Damien Naudet committed
33

Damien Naudet's avatar
WIP  
Damien Naudet committed
34
from silx.gui import qt as Qt, icons
Damien Naudet's avatar
WIP  
Damien Naudet committed
35 36 37

from ..model.ModelDef import ModelColumns
from ..model.NodeEditor import EditorMixin
DN's avatar
DN committed
38 39

from .IntensityGroup import IntensityItem
Damien Naudet's avatar
WIP  
Damien Naudet committed
40
from .XsocsH5Factory import h5NodeToProjectItem
DN's avatar
DN committed
41
from .Hdf5Nodes import H5GroupNode, H5NodeClassDef, H5DatasetNode
Damien Naudet's avatar
WIP  
Damien Naudet committed
42 43 44 45 46 47 48 49 50 51 52


class ScatterPlotButton(EditorMixin, Qt.QWidget):
    persistent = True

    sigValueChanged = Qt.Signal()

    def __init__(self, parent, option, index):
        super(ScatterPlotButton, self).__init__(parent, option, index)
        layout = Qt.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
DN's avatar
DN committed
53
        icon = icons.getQIcon('plot-widget')
Damien Naudet's avatar
WIP  
Damien Naudet committed
54 55 56 57 58 59 60 61
        button = Qt.QToolButton()
        button.setIcon(icon)
        layout.addWidget(button)
        layout.addStretch(1)

        button.clicked.connect(self.__clicked)

    def __clicked(self):
Damien Naudet's avatar
WIP  
Damien Naudet committed
62
        # node = self.node
Damien Naudet's avatar
WIP  
Damien Naudet committed
63 64 65 66
        event = {'event': 'scatter'}
        self.notifyView(event)


Damien Naudet's avatar
WIP  
Damien Naudet committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
class QSpaceButton(EditorMixin, Qt.QWidget):
    persistent = True

    sigValueChanged = Qt.Signal()

    def __init__(self, parent, option, index):
        super(QSpaceButton, self).__init__(parent, option, index)
        layout = Qt.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        icon = icons.getQIcon('item-ndim')
        button = Qt.QToolButton()
        button.setIcon(icon)
        layout.addWidget(button)
        layout.addStretch(1)

        button.clicked.connect(self.__clicked)

    def __clicked(self):
        # node = self.node
        event = {'event': 'qspace'}
        self.notifyView(event)


Damien Naudet's avatar
WIP  
Damien Naudet committed
90
@H5NodeClassDef('IntensityGroupNode',
DN's avatar
DN committed
91 92
                attribute=('XsocsClass', 'IntensityGroup'),
                icons='math-sigma')
Damien Naudet's avatar
WIP  
Damien Naudet committed
93 94 95
class IntensityGroupNode(H5GroupNode):
    editors = ScatterPlotButton

DN's avatar
DN committed
96 97 98
    # def _loadChildren(self):
    #     return []

Damien Naudet's avatar
WIP  
Damien Naudet committed
99 100 101 102 103 104 105 106 107

@H5NodeClassDef('IntensityNode',
                attribute=('XsocsClass', 'IntensityItem'))
class IntensityNode(H5DatasetNode):
    # editors = ScatterPlotButton

    def _setupNode(self):
        with IntensityItem(self.h5File, self.h5Path, mode='r') as item:
            self.setData(ModelColumns.NameColumn,
Damien Naudet's avatar
WIP  
Damien Naudet committed
108
                         str(item.projectRoot().shortName(item.entry)),
Damien Naudet's avatar
WIP  
Damien Naudet committed
109
                         Qt.Qt.DisplayRole)
Damien Naudet's avatar
WIP  
Damien Naudet committed
110 111 112 113 114 115


@H5NodeClassDef('QSpaceItem',
                attribute=('XsocsClass', 'QSpaceItem'))
class QSpaceItemNode(H5GroupNode):
    editors = QSpaceButton
Damien Naudet's avatar
WIP  
Damien Naudet committed
116 117 118 119 120 121 122 123 124 125 126


class FitButton(EditorMixin, Qt.QWidget):
    persistent = True

    sigValueChanged = Qt.Signal()

    def __init__(self, parent, option, index):
        super(FitButton, self).__init__(parent, option, index)
        layout = Qt.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
DN's avatar
DN committed
127
        icon = icons.getQIcon('plot-widget')
Damien Naudet's avatar
WIP  
Damien Naudet committed
128 129
        button = Qt.QToolButton()
        button.setIcon(icon)
Damien Naudet's avatar
WIP  
Damien Naudet committed
130
        button.clicked.connect(self.__clicked)
Damien Naudet's avatar
WIP  
Damien Naudet committed
131 132
        layout.addWidget(button)

Damien Naudet's avatar
WIP  
Damien Naudet committed
133 134 135 136 137 138 139
        button = Qt.QToolButton()
        style = Qt.QApplication.style()
        icon = style.standardIcon(Qt.QStyle.SP_DialogSaveButton)
        button.setIcon(icon)
        button.clicked.connect(self.__export)
        layout.addWidget(button)
        layout.addStretch(1)
Damien Naudet's avatar
WIP  
Damien Naudet committed
140 141 142 143 144 145

    def __clicked(self):
        # node = self.node
        event = {'event': 'fit'}
        self.notifyView(event)

Damien Naudet's avatar
WIP  
Damien Naudet committed
146 147 148 149 150 151 152 153 154 155
    def __export(self):
        fitItem = h5NodeToProjectItem(self.node)
        workdir = fitItem.projectRoot().workdir
        itemBasename = os.path.basename(fitItem.fitFile).rsplit('.')[0]
        itemBasename += '.txt'
        dialog = Qt.QFileDialog(self, 'Export fit results.')
        dialog.setFileMode(Qt.QFileDialog.AnyFile)
        dialog.selectFile(os.path.join(workdir, itemBasename))
        if dialog.exec_():
            csvPath = dialog.selectedFiles()[0]
156
            fitItem.fitH5.export_csv(fitItem.fitH5.entries()[0], csvPath)
Damien Naudet's avatar
WIP  
Damien Naudet committed
157

Damien Naudet's avatar
WIP  
Damien Naudet committed
158

DN's avatar
DN committed
159 160 161 162 163 164 165
@H5NodeClassDef('FitGroup',
                attribute=('XsocsClass', 'FitGroup'),
                icons='math-fit')
class FitGroupNode(H5GroupNode):
    pass


Damien Naudet's avatar
WIP  
Damien Naudet committed
166
@H5NodeClassDef('FitItem',
DN's avatar
DN committed
167 168
                attribute=('XsocsClass', 'FitItem'),
                icons='math-fit')
Damien Naudet's avatar
WIP  
Damien Naudet committed
169
class FitItemNode(H5GroupNode):
Damien Naudet's avatar
WIP  
Damien Naudet committed
170
    editors = FitButton
DN's avatar
DN committed
171 172 173

    def _loadChildren(self):
        return []