aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/obscuren/qml/examples/snapweb/snapweb.go
blob: 53d1d6210be413dc4b8a68b2916541018021676c (plain) (blame)
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
package main

import (
    "fmt"
    "gopkg.in/qml.v1"
    "image/png"
    "os"
)

const webview = `
import QtQuick 2.0
import QtWebKit 3.0

WebView {
    width: 1024
    height: 768
}
`

func main() {
    if len(os.Args) != 3 {
        fmt.Fprintf(os.Stderr, "usage: %s <url> <png path>\n", os.Args[0])
        os.Exit(1)
    }
    if err := qml.Run(run); err != nil {
        fmt.Fprintf(os.Stderr, "error: %v\n", err)
        os.Exit(1)
    }
}

func run() error {
    engine := qml.NewEngine()
    component, err := engine.LoadString("webview.qml", webview)
    if err != nil {
        return err
    }
    ctrl := &Control{
        done: make(chan error),
        win:  component.CreateWindow(nil),
    }
    engine.Context().SetVar("ctrl", ctrl)
    root := ctrl.win.Root()
    root.On("loadingChanged", ctrl.Snapshot)
    root.Set("url", os.Args[1])
    ctrl.win.Show()
    return <-ctrl.done
}

type Control struct {
    win  *qml.Window
    done chan error
}

func (ctrl *Control) Snapshot(request qml.Object) {
    if request.Int("status") != 2 {
        return
    }
    f, err := os.Create(os.Args[2])
    if err != nil {
        ctrl.done <- err
        return
    }
    defer f.Close()
    img := ctrl.win.Snapshot()
    err = png.Encode(f, img)
    if err != nil {
        os.Remove(os.Args[2])
    }
    ctrl.done <- err
}