~akshatj/view/trunk

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
import sys
from os import path
from xml.sax.saxutils import quoteattr, escape
import argparse
import tempfile
from subprocess import check_call

parser = argparse.ArgumentParser()
parser.add_argument('--browser',
    help='debug in browser',
    action='store_true',
    default=False,
)
parser.add_argument('image')
args = parser.parse_args()

src = path.abspath(args.image)
if not path.isfile(src):
    sys.exit('not a file: {!r}'.format(src))
src = 'file://' + src

css = """
body {
    font-family: Ubuntu;
    font-size: 20px;
    background: #000;
    margin: 0px;
}
#pic {
    display: block;
    margin: auto;
}
#toolbar {
	margin: auto;
    height: 100%;
    width: 400px;
    position: fixed;
    height: 50px;
    left: 0;
    right: 0;
    bottom: 11px;
    z-index: 10;
    color: #fff;
    background: #555;
    opacity: 0.6;
    border-radius: 5.0;
}
.hide {
    display: none;
}
"""

js = """
var View = {
    load: function() {
        View.toolbar = document.getElementById('toolbar');
        View.restart();
        window.onmousemove = View.on_mousemove;
    },

    restart: function() {
        if (View.timeoutID) {
            clearTimeout(View.timeoutID);
        }
        else {
            View.toolbar.classList.remove('hide');
        }
        View.timeoutID = setInterval(View.on_timeout, 3000);
    },

    on_timeout: function() {
        clearTimeout(View.timeoutID);
        View.timeoutID = null;
        View.toolbar.classList.add('hide');
    },

    on_mousemove: function() {
        View.restart();
    },
}
"""

html = u"""
<html>
<head>
<style type="text/css">
{css}
</style>
<script type="text/javascript">
{js}
</script>
</head>
<body onload="View.load()">
<img id="pic" src={src}>
<div id="toolbar">Stuff</div>
</body>
</html>
""".format(src=quoteattr(src), css=escape(css), js=escape(js)).encode('utf-8')

if args.browser:
    (fileno, tmp) = tempfile.mkstemp(suffix='.html')
    open(tmp, 'wb').write(html)
    check_call(['firefox', tmp])
    sys.exit()


import gtk
import webkit

window = gtk.Window()
window.set_title('Image Viewer')
window.set_default_size(800, 600)
window.connect('destroy', gtk.main_quit)

scroll = gtk.ScrolledWindow()
scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
window.add(scroll)

view = webkit.WebView()
scroll.add(view)

view.load_string(html, 'text/html', 'utf-8', 'file:///')

window.show_all()
gtk.main()