File: filename_widget.py

package info (click to toggle)
python-pymeasure 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,788 kB
  • sloc: python: 47,201; makefile: 155
file content (160 lines) | stat: -rw-r--r-- 6,434 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2024 PyMeasure Developers
#
# 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.
#

import logging
import re
import textwrap

from ..Qt import QtCore, QtWidgets, QtGui

log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())


class FilenameLineEdit(QtWidgets.QLineEdit):
    """
    Widget that allows to choose a filename.
    A completer is implemented for quick completion of placeholders
    """

    def __init__(self, procedure_class, parent=None):
        super().__init__("DATA", parent=parent)

        self.placeholders = procedure_class.placeholder_names()
        self.placeholders.extend(["date", "time"])

        completer = PlaceholderCompleter(self.placeholders)
        self.setCompleter(completer)

        validator = FilenameValidator(self.placeholders, self)
        self.setValidator(validator)

        self.set_tool_tip()

    def set_tool_tip(self):
        ext = self.parent().extensions[0]
        extensions = ("'." + "', '.".join(self.parent().extensions[:-1]) +
                      f"', or '.{self.parent().extensions[-1]}'")
        placeholders = "'" + "';\n- '".join(self.placeholders) + "'"
        self.setToolTip("\n".join(textwrap.wrap(
            "The filename of the file to which the measurement will be stored. Placeholders (in "
            "standard python format, i.e.: '{variable name:formatspec}') will be replaced by "
            f"the respective value. The extension '.{ext}' will be appended, unless an extension "
            f"(one of {extensions}) is recognized. Additionally, an index number ('_#') is "
            "added to ensure the uniqueness of the filename.", width=100)) +
            f"\nValid placeholders are:\n- {placeholders}."
        )


class PlaceholderCompleter(QtWidgets.QCompleter):
    def __init__(self, placeholders):
        super().__init__()
        self.placeholders = placeholders

        self.setCompletionMode(QtWidgets.QCompleter.CompletionMode.PopupCompletion)
        self.setModelSorting(QtWidgets.QCompleter.ModelSorting.CaseInsensitivelySortedModel)
        self.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.setFilterMode(QtCore.Qt.MatchContains)

    def splitPath(self, path):
        if path.endswith("{"):
            options = [path + placeholder + "}" for placeholder in self.placeholders]
            model = QtCore.QStringListModel(options)
            self.setModel(model)
        elif path.count("{") == path.count("}"):
            # Clear the autocomplete options
            self.setModel(QtCore.QStringListModel())

        return [path]


class FilenameValidator(QtGui.QValidator):
    def __init__(self, placeholders, parent):
        self.parent = parent
        self.placeholders = placeholders

        self.full_placeholder = re.compile(r"{([^{}:]*)(:[^{}]*)?}")
        self.half_placeholder = re.compile(r"{([^{}:]*)(:[^{}]*)?$")
        self.valid_filename = re.compile(r"^[^<>:\"/\\|?*{}]*$")
        super().__init__()

    def fixup(self, input):
        half_placeholder = self.half_placeholder.findall(input)

        if half_placeholder:
            input = input + "}"

        return input

    def validate(self, input, pos):
        test_input = input

        full_placeholders = self.full_placeholder.findall(input)
        half_placeholder = self.half_placeholder.findall(input)

        test_input = self.full_placeholder.sub("_plchldr_", test_input)
        test_input = self.half_placeholder.sub("_plchldr", test_input)

        valid_filename = self.valid_filename.fullmatch(test_input)

        # Determine state of input
        if not valid_filename:
            state = QtGui.QValidator.Invalid
        elif half_placeholder:
            state = QtGui.QValidator.Intermediate
        else:
            state = QtGui.QValidator.Acceptable

        # Control the warning for the invalid placeholders
        incorrect_placeholders = [p for p in full_placeholders if p[0] not in self.placeholders]
        if incorrect_placeholders:
            if not self.parent.actions():
                pixmapi = QtWidgets.QStyle.StandardPixmap.SP_MessageBoxCritical
                icon = self.parent.style().standardIcon(pixmapi)
                self.parent.addAction(icon, self.parent.ActionPosition.TrailingPosition)

            # Add tooltip to show which placeholders are not valid
            act = self.parent.actions()[0]

            marked_input = input
            for placeholder in [f"{{{p[0] + p[1]}}}" for p in incorrect_placeholders]:
                marked_input = marked_input.replace(
                    placeholder, f"<b><font color='red'>{placeholder}</font></b>"
                )

            act.setToolTip(
                "<p style='white-space:pre'>"
                "The input filename contains placeholders with<br/>invalid variable names:<br/>"
                " - '" + "',<br/> - '".join([p[0] for p in incorrect_placeholders]) + "'."
                "<br/><br/>Received input:<br/>" + marked_input + "</p>"
            )
        else:
            # Remove action, if it exists
            if self.parent.actions():
                assert len(self.parent.actions()) == 1, (
                    "More than 1 action defined, not sure " "which to remove."
                )
                self.parent.removeAction(self.parent.actions()[0])

        return state, input, pos