datasetSelectionWidget.py 7.97 KB
Newer Older
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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.
#
# ###########################################################################*/


__authors__ = ["J. Garriga"]
__license__ = "MIT"
29
__date__ = "02/12/2019"
30

31
import logging
32
33
34
import os

from silx.gui import qt
35
from darfix.core.dataset import Dataset
36

37
38
_logger = logging.getLogger(__file__)

39
40

class DatasetSelectionWidget(qt.QTabWidget):
41
    """
42
    Widget that creates a dataset from a list of files or from a single filename.
43
44
    It lets the user add the first filename of a directory of files, or to
    upload manually each of the files to be read.
45
46
    If both options are filled up, only the files in the list of filenames
    are read.
47
    """
48
    sigProgressChanged = qt.Signal(int)
49

50
51
52
53
54
    def __init__(self, parent=None):
        qt.QTabWidget.__init__(self, parent)

        # Raw data
        self._rawFilenameData = FilenameSelectionWidget(parent=self)
55
        self._rawFilesData = FilesSelectionWidget(parent=self)
56
        self._filterDataCB = qt.QCheckBox("Filter data", self)
57
58
59
60
        rawData = qt.QWidget(self)
        rawData.setLayout(qt.QVBoxLayout())
        rawData.layout().addWidget(self._rawFilenameData)
        rawData.layout().addWidget(self._rawFilesData)
61
        rawData.layout().addWidget(self._filterDataCB)
62
63
        self.addTab(rawData, 'raw data')

64
65
        self._filterData = False

66
67
68
69
        # Dark data
        self._darkFilenameData = FilenameSelectionWidget(parent=self)
        self.addTab(self._darkFilenameData, 'dark data')

70
        self._dataset = None
71

72
73
74
75
76
77
78
        self.getRawFilenames = self._rawFilesData.getFiles
        self.getRawFilename = self._rawFilenameData.getFilename
        self.getDarkFilename = self._darkFilenameData.getFilename
        self.setRawFilenames = self._rawFilesData.setFiles
        self.setRawFilename = self._rawFilenameData.setFilename
        self.setDarkFilename = self._darkFilenameData.setFilename

79
80
        self._filterDataCB.stateChanged.connect(self.__filterData)

81
    def loadDataset(self):
82
83
84
        """
        Loads the dataset from the filenames.
        """
85
        if not self._rawFilesData.getFiles():
86
87
            self._dataset = Dataset(raw_filename=self._rawFilenameData.getFilename(),
                                    dark_filename=self._darkFilenameData.getFilename(),
88
                                    load_data=False, filter_data=self._filterData)
89
        else:
90
91
            self._dataset = Dataset(filenames=self._rawFilesData.getFiles(),
                                    dark_filename=self._darkFilenameData.getFilename(),
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
92
                                    load_data=False, filter_data=self._filterData)
93
        self._dataset.sigProgressChanged.connect(self.updateProgress)
94
95
96
97
98
        _logger.info("Loading data urls")
        self._dataset.load_data(0.3)
        _logger.info("Loading data")
        print("Loading data")
        self._dataset.get_data(0.7, 30)
99
100
101
102

    @property
    def dataset(self):
        return self._dataset
103
104

    def updateProgress(self, progress):
105
        self.sigProgressChanged.emit(progress)
106

107
108
109
    def __filterData(self, filterData):
        self._filterData = filterData

110

111
112
113
114
class FilesSelectionWidget(qt.QWidget):
    """
    Widget used to get one or more files from the computer and add them to a list.
    """
115
116
117
118
119
120
121
122
    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)

        self._files = []

        self.setLayout(qt.QVBoxLayout())
        self._table = self._init_table()
        self._addButton = qt.QPushButton("Add")
123
        self._rmButton = qt.QPushButton("Remove")
124
125
        self.layout().addWidget(self._table)
        self.layout().addWidget(self._addButton)
126
127
128
        self.layout().addWidget(self._rmButton)
        self._addButton.clicked.connect(self._addFiles)
        self._rmButton.clicked.connect(self._removeFiles)
129
130
131
132
133

    def _init_table(self):

        table = qt.QTableWidget(0, 1, parent=self)
        table.horizontalHeader().hide()
134
        # Resize horizontal header to fill all the column size
135
136
137
138
139
140
141
142
        if hasattr(table.horizontalHeader(), 'setSectionResizeMode'):  # Qt5
            table.horizontalHeader().setSectionResizeMode(0, qt.QHeaderView.Stretch)
        else:  # Qt4
            table.horizontalHeader().setResizeMode(0, qt.QHeaderView.Stretch)

        return table

    def _addFiles(self):
143
144
145
        """
        Opens the file dialog and let's the user choose one or more files.
        """
146
        dialog = qt.QFileDialog(self)
147
        dialog.setFileMode(qt.QFileDialog.ExistingFiles)
148
149
150
151
152
153

        if not dialog.exec_():
            dialog.close()
            return

        for file in dialog.selectedFiles():
154
155
            self.addFile(file)

156
157
158
159
160
161
162
163
164
165
    def _removeFiles(self):
        """
        Removes the selected items from the table.
        """
        selectedItems = self._table.selectedItems()
        if selectedItems is not None:
            for item in selectedItems:
                self._files.remove(item.text())
                self._table.removeRow(item.row())

166
    def addFile(self, file):
167
168
169
170
171
        """
        Adds a file to the table.

        :param str file: filepath to add to the table.
        """
172
173
174
175
176
177
178
        assert (os.path.isfile(file))
        item = qt.QTableWidgetItem()
        item.setText(file)
        row = self._table.rowCount()
        self._table.setRowCount(row + 1)
        self._table.setItem(row, 0, item)
        self._files.append(file)
179
180
181
182

    def getFiles(self):
        return self._files

183
    def setFiles(self, files):
184
185
186
187
188
        """
        Adds a list of files to the table.

        :param array_like files: List to add
        """
189
        for file in files:
190
191
            self.addFile(file)

192
193

class FilenameSelectionWidget(qt.QWidget):
194
195
196
    """
    Widget used to obtain a filename (manually or from a file)
    """
197
198
199

    filenameChanged = qt.Signal()

200
201
202
203
    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)

        self._filename = qt.QLineEdit('', parent=self)
204
        self._filename.editingFinished.connect(self.filenameChanged)
205
206
207
208
209
210
211
212
213
214
215
        self._addButton = qt.QPushButton("Upload file", parent=self)
        # self._okButton =  qt.QPushButton("Ok", parent=self)
        self._addButton.pressed.connect(self._uploadFilename)
        # self._okButton.pressed.connect(self.close)
        self.setLayout(qt.QHBoxLayout())

        self.layout().addWidget(self._filename)
        self.layout().addWidget(self._addButton)
        # self.layout().addWidget(self._okButton)

    def _uploadFilename(self):
216
217
218
        """
        Loads the file from a FileDialog.
        """
219
220
        fileDialog = qt.QFileDialog()

221
        fileDialog.setFileMode(qt.QFileDialog.ExistingFile)
222
223
        if fileDialog.exec_():
            self._filename.setText(fileDialog.selectedFiles()[0])
224
            self.filenameChanged.emit()
225
        else:
226
            _logger.warning("Could not open file")
227
228
229

    def getFilename(self):
        return str(self._filename.text())
230
231
232

    def setFilename(self, filename):
        self._filename.setText(str(filename))