ProjectNodes.py 5.2 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 38 39

from ..model.ModelDef import ModelColumns
from .IntensityGroup import IntensityGroup, IntensityItem
from ..model.NodeEditor import EditorMixin
from .Hdf5Nodes import H5GroupNode, H5NodeClassDef, H5DatasetNode
Damien Naudet's avatar
WIP  
Damien Naudet committed
40
from .XsocsH5Factory import h5NodeToProjectItem
Damien Naudet's avatar
WIP  
Damien Naudet committed
41
from ..model.Node import Node
Damien Naudet's avatar
WIP  
Damien Naudet committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61


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)
        icon = icons.getQIcon('item-1dim')
        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 91 92 93 94 95 96 97 98 99 100 101 102 103
@H5NodeClassDef('IntensityGroupNode',
                attribute=('XsocsClass', 'IntensityGroup'))
class IntensityGroupNode(H5GroupNode):
    editors = ScatterPlotButton


@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
104
                         str(item.projectRoot().shortName(item.entry)),
Damien Naudet's avatar
WIP  
Damien Naudet committed
105
                         Qt.Qt.DisplayRole)
Damien Naudet's avatar
WIP  
Damien Naudet committed
106 107 108 109 110 111


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


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)
        icon = icons.getQIcon('item-1dim')
        button = Qt.QToolButton()
        button.setIcon(icon)
Damien Naudet's avatar
WIP  
Damien Naudet committed
126
        button.clicked.connect(self.__clicked)
Damien Naudet's avatar
WIP  
Damien Naudet committed
127 128
        layout.addWidget(button)

Damien Naudet's avatar
WIP  
Damien Naudet committed
129 130 131 132 133 134 135
        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
136 137 138 139 140 141

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

Damien Naudet's avatar
WIP  
Damien Naudet committed
142 143 144 145 146 147 148 149 150 151 152 153
    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]
            fitItem.fitH5.export_txt(csvPath)

Damien Naudet's avatar
WIP  
Damien Naudet committed
154 155 156

@H5NodeClassDef('FitItem',
                attribute=('XsocsClass', 'FitItem'))
Damien Naudet's avatar
WIP  
Damien Naudet committed
157
class FitItemNode(H5GroupNode):
Damien Naudet's avatar
WIP  
Damien Naudet committed
158
    editors = FitButton