File: qdelegates.py

package info (click to toggle)
linux-show-player 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,896 kB
  • sloc: python: 12,408; sh: 154; makefile: 17; xml: 8
file content (218 lines) | stat: -rw-r--r-- 7,308 bytes parent folder | download | duplicates (4)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtWidgets import QStyledItemDelegate, QComboBox, QSpinBox, \
    QLineEdit, QStyle, QDialog

from lisp.application import Application
from lisp.cues.cue import CueAction
from lisp.ui.qmodels import CueClassRole
from lisp.ui.ui_utils import translate
from lisp.ui.widgets import CueActionComboBox


class LabelDelegate(QStyledItemDelegate):
    def _text(self, painter, option, index):
        return ''

    def paint(self, painter, option, index):
        # Add 4px of left an right padding
        option.rect.adjust(4, 0, 4, 0)

        text = self._text(painter, option, index)
        text = option.fontMetrics.elidedText(text, Qt.ElideRight,
                                             option.rect.width())

        painter.save()

        if option.state & QStyle.State_Selected:
            painter.setBrush(option.palette.highlight())

            pen = painter.pen()
            pen.setBrush(option.palette.highlightedText())
            painter.setPen(pen)

        painter.drawText(option.rect, option.displayAlignment, text)

        painter.restore()


class ComboBoxDelegate(LabelDelegate):
    def __init__(self, options=(), tr_context=None, **kwargs):
        super().__init__(**kwargs)
        self.options = options
        self.tr_context = tr_context

    def _text(self, painter, option, index):
        return translate(self.tr_context, index.data())

    def paint(self, painter, option, index):
        option.displayAlignment = Qt.AlignHCenter | Qt.AlignVCenter
        super().paint(painter, option, index)

    def createEditor(self, parent, option, index):
        editor = QComboBox(parent)
        editor.setFrame(False)
        for option in self.options:
            editor.addItem(translate(self.tr_context, option), option)

        return editor

    def setEditorData(self, comboBox, index):
        value = index.model().data(index, Qt.EditRole)
        comboBox.setCurrentText(translate(self.tr_context, value))

    def setModelData(self, comboBox, model, index):
        model.setData(index, comboBox.currentData(), Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)


class SpinBoxDelegate(QStyledItemDelegate):
    def __init__(self, minimum=-100, maximum=100, step=1, **kwargs):
        super().__init__(**kwargs)

        self.minimum = minimum
        self.maximum = maximum
        self.step = step

    def createEditor(self, parent, option, index):
        editor = QSpinBox(parent)
        editor.setRange(self.minimum, self.maximum)
        editor.setSingleStep(self.step)

        return editor

    def setEditorData(self, spinBox, index):
        value = index.model().data(index, Qt.EditRole)
        if isinstance(value, int):
            spinBox.setValue(value)

    def setModelData(self, spinBox, model, index):
        spinBox.interpretText()
        model.setData(index, spinBox.value(), Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)


class LineEditDelegate(QStyledItemDelegate):
    def __init__(self, max_length=-1, validator=None, **kwargs):
        super().__init__(**kwargs)

        self.max_length = max_length
        self.validator = validator

    def createEditor(self, parent, option, index):
        editor = QLineEdit(parent)
        if self.max_length > 0:
            editor.setMaxLength(self.max_length)
        if self.validator is not None:
            editor.setValidator(self.validator)
        editor.setFrame(False)

        return editor

    def setEditorData(self, lineEdit, index):
        value = index.model().data(index, Qt.EditRole)
        lineEdit.setText(str(value))

    def setModelData(self, lineEdit, model, index):
        model.setData(index, lineEdit.text(), Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)


class CueActionDelegate(LabelDelegate):
    Mode = CueActionComboBox.Mode

    def __init__(self, cue_class=None, mode=Mode.Action, **kwargs):
        super().__init__(**kwargs)
        self.cue_class = cue_class
        self.mode = mode

    def _text(self, painter, option, index):
        value = index.data(Qt.EditRole)
        if self.mode == CueActionDelegate.Mode.Action:
            name = value.name
        elif self.mode == CueActionDelegate.Mode.Name:
            name = value
        else:
            name = CueAction(value).name

        return translate('CueAction', name)

    def paint(self, painter, option, index):
        option.displayAlignment = Qt.AlignHCenter | Qt.AlignVCenter
        super().paint(painter, option, index)

    def createEditor(self, parent, option, index):
        if self.cue_class is None:
            self.cue_class = index.data(CueClassRole)

        editor = CueActionComboBox(self.cue_class,
                                   mode=self.mode,
                                   parent=parent)
        editor.setFrame(False)

        return editor

    def setEditorData(self, comboBox, index):
        value = index.data(Qt.EditRole)
        if self.mode == CueActionDelegate.Mode.Action:
            action = value
        elif self.mode == CueActionDelegate.Mode.Name:
            action = CueAction[value]
        else:
            action = CueAction(value)

        comboBox.setCurrentText(translate('CueAction', action.name))

    def setModelData(self, comboBox, model, index):
        model.setData(index, comboBox.currentData(), Qt.EditRole)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)


class CueSelectionDelegate(LabelDelegate):
    def __init__(self, cue_select_dialog, **kwargs):
        super().__init__(**kwargs)
        self.cue_select = cue_select_dialog

    def _text(self, painter, option, index):
        cue = Application().cue_model.get(index.data())
        if cue is not None:
            return '{} | {}'.format(cue.index, cue.name)

        return 'UNDEF'

    def editorEvent(self, event, model, option, index):
        if event.type() == QEvent.MouseButtonDblClick:
            if self.cue_select.exec_() == QDialog.Accepted:
                cue = self.cue_select.selected_cue()
                if cue is not None:
                    model.setData(index, cue.id, Qt.EditRole)
                    model.setData(index, cue.__class__, CueClassRole)
            return True

        return super().editorEvent(event, model, option, index)