-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui.py
278 lines (220 loc) · 8.43 KB
/
ui.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
# PiTiVi , Non-linear video editor
#
# pitivi/utils/ui.py
#
# Copyright (c) 2005, Edward Hervey <[email protected]>
# Copyright (c) 2012, Thibault Saunier <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""
UI utilities. This file contain the UI constants, and various functions and
classes that help with UI drawing around the application
"""
import gtk
import os
import cairo
from itertools import izip
from urllib import unquote
from gettext import ngettext, gettext as _
from xml.sax.saxutils import escape
from decimal import Decimal
from loggable import doLog, ERROR
# ---------------------- Constants -------------------------------------------#
##
# UI pixels information constants
##
LAYER_HEIGHT_EXPANDED = 50
LAYER_HEIGHT_COLLAPSED = 15
LAYER_SPACING = 15
TRACK_SPACING = 8
SPACING = 6
PADDING = 6
##
# Drag'n drop constants
##
TYPE_TEXT_PLAIN = 24
TYPE_URI_LIST = 25
# FileSourceFactory (or subclasses)
TYPE_PITIVI_FILESOURCE = 26
# What objects to these correspond to ???
TYPE_PITIVI_EFFECT = 27
TYPE_PITIVI_AUDIO_EFFECT = 28
TYPE_PITIVI_VIDEO_EFFECT = 29
TYPE_PITIVI_AUDIO_TRANSITION = 30
TYPE_PITIVI_VIDEO_TRANSITION = 31
FILE_TUPLE = ("text/plain", 0, TYPE_TEXT_PLAIN)
URI_TUPLE = ("text/uri-list", 0, TYPE_URI_LIST)
FILESOURCE_TUPLE = ("pitivi/file-source", 0, TYPE_PITIVI_FILESOURCE)
EFFECT_TUPLE = ("pitivi/effect", 0, TYPE_PITIVI_EFFECT)
AUDIO_EFFECT_TUPLE = ("pitivi/audio-effect", 0, TYPE_PITIVI_AUDIO_EFFECT)
VIDEO_EFFECT_TUPLE = ("pitivi/video-effect", 0, TYPE_PITIVI_VIDEO_EFFECT)
AUDIO_TRANSITION_TUPLE = ("pitivi/audio-transition", 0, TYPE_PITIVI_AUDIO_TRANSITION)
VIDEO_TRANSITION_TUPLE = ("pitivi/video-transition", 0, TYPE_PITIVI_VIDEO_TRANSITION)
# ---------------------- ARGB color helper-------------------------------------#
def pack_color_32(red, green, blue, alpha=0xFFFF):
"""Packs the specified 16bit color values in a 32bit RGBA value."""
red = red >> 8
green = green >> 8
blue = blue >> 8
alpha = alpha >> 8
return (red << 24 | green << 16 | blue << 8 | alpha)
def pack_color_64(red, green, blue, alpha=0xFFFF):
"""Packs the specified 16bit color values in a 64bit RGBA value."""
return (red << 48 | green << 32 | blue << 16 | alpha)
def unpack_color(value):
"""Unpacks the specified RGBA value into four 16bit color values.
Args:
value: A 32bit or 64bit RGBA value.
"""
if not (value >> 32):
return unpack_color_32(value)
else:
return unpack_color_64(value)
def unpack_color_32(value):
"""Unpacks the specified 32bit RGBA value into four 16bit color values."""
red = (value >> 24) << 8
green = ((value >> 16) & 0xFF) << 8
blue = ((value >> 8) & 0xFF) << 8
alpha = (value & 0xFF) << 8
return red, green, blue, alpha
def unpack_color_64(value):
"""Unpacks the specified 64bit RGBA value into four 16bit color values."""
red = (value >> 48) & 0xFFFF
green = (value >> 32) & 0xFFFF
blue = (value >> 16) & 0xFFFF
alpha = value & 0xFFFF
return red, green, blue, alpha
def unpack_cairo_pattern(value):
"""Transforms the specified RGBA value into a SolidPattern object."""
red, green, blue, alpha = unpack_color(value)
return cairo.SolidPattern(
red / 65535.0,
green / 65535.0,
blue / 65535.0,
alpha / 65535.0)
def unpack_cairo_gradient(value):
"""Creates a LinearGradient object out of the specified RGBA value."""
red, green, blue, alpha = unpack_color(value)
gradient = cairo.LinearGradient(0, 0, 0, 50)
gradient.add_color_stop_rgba(
1.0,
red / 65535.0,
green / 65535.0,
blue / 65535.0,
alpha / 65535.0)
gradient.add_color_stop_rgba(
0,
(red / 65535.0) * 1.5,
(green / 65535.0) * 1.5,
(blue / 65535.0) * 1.5,
alpha / 65535.0)
return gradient
def hex_to_rgb(value):
return tuple(float(int(value[i:i + 2], 16)) / 255.0 for i in range(0, 6, 2))
def info_name(info):
"""Return a human-readable filename (without the path and quoting)."""
return escape(unquote(os.path.basename(info.get_uri())))
def beautify_time_delta(seconds):
"""
Converts the given time in seconds to a human-readable estimate.
This is intended for "Unsaved changes" and "Backup file found" dialogs.
"""
mins = seconds / 60
sec = int(seconds % 60)
hours = mins / 60
mins = int(mins % 60)
days = int(hours / 24)
hours = int(hours % 24)
parts = []
if days > 0:
parts.append(ngettext("%d day", "%d days", days) % days)
if hours > 0:
parts.append(ngettext("%d hour", "%d hours", hours) % hours)
if days == 0 and mins > 0:
parts.append(ngettext("%d minute", "%d minutes", mins) % mins)
if hours == 0 and mins < 2 and sec:
parts.append(ngettext("%d second", "%d seconds", sec) % sec)
return ", ".join(parts)
#--------------------- UI drawing helper -------------------------------------#
# from http://cairographics.org/cookbook/roundedrectangles/
def roundedrec(context, x, y, w, h, r=10):
"Draw a rounded rectangle"
# A****BQ
# H C
# * *
# G D
# F****E
context.move_to(x + r, y) # Move to A
context.line_to(x + w - r, y) # Straight line to B
# Curve to C, Control points are both at Q
context.curve_to(x + w, y, x + w, y, x + w, y + r)
context.line_to(x + w, y + h - r) # Move to D
context.curve_to(x + w, y + h, x + w, y + h, x + w - r, y + h) # Curve to E
context.line_to(x + r, y + h) # Line to F
context.curve_to(x, y + h, x, y + h, x, y + h - r) # Curve to G
context.line_to(x, y + r) # Line to H
context.curve_to(x, y, x, y, x + r, y) # Curve to A
return
#--------------------- Gtk widget helpers ------------------------------------#
def model(columns, data):
ret = gtk.ListStore(*columns)
for datum in data:
ret.append(datum)
return ret
def set_combo_value(combo, value, default_index=-1):
model = combo.props.model
for i, row in enumerate(model):
if row[1] == value:
combo.set_active(i)
return
combo.set_active(default_index)
def get_combo_value(combo):
active = combo.get_active()
return combo.props.model[active][1]
# ---------------------- Classes ---------------------------------------------#
class Point(tuple):
def __new__(cls, x, y):
return tuple.__new__(cls, (x, y))
def __pow__(self, scalar):
"""Returns the scalar multiple self, scalar"""
return Point(self[0] * scalar, self[1] * scalar)
def __rpow__(self, scalar):
"""Returns the scalar multiple of self, scalar"""
return self ** scalar
def __mul__(self, p2):
return Point(*(a * b for a, b in izip(self, p2)))
def __div__(self, other):
return Point(*(a / b for a, b in izip(self, p2)))
def __floordiv__(self, scalar):
"""Returns the scalar division of self and scalar"""
return Point(self[0] / scalar, self[1] / scalar)
def __add__(self, p2):
"""Returns the 2d vector sum self + p2"""
return Point(*(a + b for a, b in izip(self, p2)))
def __sub__(self, p2):
"""Returns the 2-dvector difference self - p2"""
return Point(*(a - b for a, b in izip(self, p2)))
def __abs__(self):
return Point(*(abs(a) for a in self))
@classmethod
def from_item_bounds(self, item):
bounds = item.get_bounds()
return Point(bounds.x1, bounds.y1), Point(bounds.x2, bounds.y2)
@classmethod
def from_widget_bounds(self, widget):
x1, y1, x2, y2 = widget.get_bounds()
return Point(x1, y1), Point(x2, y2)