~stefan-schwarzburg/qreator/qreator-qml

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
###############################################################################
#
# Copyright 2013 Stefan Schwarzburg <stefan.schwarzburg@gmail.com>
#
# 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; version 3.
#
# 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, see <http://www.gnu.org/licenses/>.
#
###############################################################################


################################################################################
# Database
# 
# local storage database. Can be found at (this changes quite
# often...)
# ~/.local/share/QtProject/QtQmlViewer/QML/OfflineStorage/Databases/
################################################################################
getDatabase = (name="qreator", version="0.1") ->
     LocalStorage.openDatabaseSync name, version, "StorageDatabase", 100000

# make sure that all tables exist
initialize = () ->
    db = getDatabase()
    db.transaction (tx) -> tx.executeSql 'CREATE TABLE IF NOT EXISTS colors(colorid INTEGER PRIMARY KEY, themename TEXT, color TEXT, role TEXT, importance INT);'
    db.transaction (tx) -> tx.executeSql 'CREATE TABLE IF NOT EXISTS settings(key TEXT UNIQUE, value TEXT);'

# run during import of this file to make sure that the database exists
initialize()


################################################################################
# Settings
# 
# A set of basic functions to allow saving settings
#
# Settings are:
# "dottheme"
# "colortheme"
################################################################################
getSetting = (key) ->
    db = getDatabase()
    res = ""
    db.transaction (tx) ->
        rs = tx.executeSql 'SELECT value FROM settings WHERE key=?;', [key]
        if rs.rows.length > 0
            res = rs.rows[0].value
    return res

setSetting = (key, value) ->
    db = getDatabase()
    db.transaction (tx) ->
        rs = tx.executeSql 'INSERT OR REPLACE  INTO settings VALUES (?,?);', [key, value]

################################################################################
#                  Color Themes    
# all colormodels are in the database, since they require no code
# colormodels consist of a themename (e.g. basic, ubuntu,...), a role
# (foreground or background) and within each role an importance.  It
# is up to the dot- and eye-themes to interpret the color theme and
# use the colors in a special way.  The basic themes "rect" and
# "circle" only use one forground and one background color.
################################################################################
initializeColorModel = (modelID) ->
    db = getDatabase()
    noColors = true
    db.transaction (tx) ->
        rs = tx.executeSql 'SELECT count(*) as C FROM colors;'
        noColors = not (rs.rows[0].C > 0)
    if noColors
        # in case there are no colors in the db, we provide a set of basic colors to start with
        themes = [
            ["basic", "bg", "white", 0]
            ["basic", "fg", "black", 0]
            ["ubuntu", "bg", "white", 0]
            ["ubuntu", "bg", '#AEA79F', 1]
            ["ubuntu", "bg", '#DD4814', 2]
            ["ubuntu", "fg", "#DD4814", 0]
            ["ubuntu", "fg", '#333333', 1]
            ]
        for line in themes
            db.transaction (tx) ->
                rs = tx.executeSql 'INSERT INTO colors VALUES (NULL,?,?,?,?);', [line[0], line[2], line[1], line[3]]
        setSetting("dottheme", "1")
        setSetting("colortheme", "ubuntu")
        
    selectedtheme = getSetting("colortheme")
    console.log(selectedtheme)
    db.transaction (tx) ->
        rs = tx.executeSql "SELECT DISTINCT themename FROM colors"
        themes = (row.themename for row in rs.rows)
        for themename in themes
            modelID.append
                "name": themename
                "ident": themename
                "ischecked": if selectedtheme is themename then true else false
                "fgColor0": getColor(themename, "fg", 0)
                "fgColor1": getColor(themename, "fg", 1)
                "bgColor0": getColor(themename, "bg", 0)
                "bgColor1": getColor(themename, "bg", 1)


updateColorModel = (modelID, index, ident, value) ->
    modelID.setProperty(index, "ischecked", value)
    for i in [0...modelID.count]
        if i isnt index
            modelID.setProperty(i, "ischecked", false) 
    
getColor = (themename, role, importance) ->
    db = getDatabase()
    color = ""
    db.transaction (tx) ->
        rs = tx.executeSql 'SELECT * FROM colors WHERE themename=? AND role=? ORDER BY importance;', [themename, role]
        if rs.rows.length > 0
            try
                color = rs.rows[importance].color
            catch error
                color = rs.rows[rs.rows.length - 1].color
    return color

################################################################################
#                  Dot Themes
#
# Dot themes are supposed to draw the modules (or dots) in the QR
# code. They should result in scannable codes, but can play with the
# colors from the color theme and different shapes or images.
################################################################################


class DotTheme
    constructor: (@qrCode, @colorThemeName="basic", @canvasSize=8*33, @boarderSize=2) ->
        @pixelSize = @canvasSize / (@qrCode.getModuleCount() + 2 * @boarderSize)

    draw: (@ctx) ->
        @drawBackground()
        @drawEyes()
        for row in [0...@qrCode.getModuleCount()]
            for col in [0...@qrCode.getModuleCount()]
                @drawDot(row, col, (@boarderSize + col) * @pixelSize, (@boarderSize + row) * @pixelSize, @pixelSize, @pixelSize)

    drawBackground: () ->
        @ctx.fillStyle = getColor(@colorThemeName, "bg", 0)
        @ctx.fillRect(0, 0, width, height)
    
    drawEyes: () -> 
        
    drawDot: (row, col, x, y, width, height) ->
        if @qrCode.isDark(row, col)
            @ctx.fillStyle = getColor(@colorThemeName, "fg", 0)
        else
            @ctx.fillStyle = getColor(@colorThemeName, "bg", 0)
        @ctx.fillRect(x, y, width, height)

        
class DotThemeCircle extends DotTheme
    drawDot: (row, col, x, y, width, height) ->
        if @qrCode.isDark(row, col)
            @ctx.fillStyle = getColor(@colorThemeName, "fg", 0)
        else
            @ctx.fillStyle = getColor(@colorThemeName, "bg", 0)
        @ctx.beginPath()
        @ctx.arc(x + width/2, y + height/2, width/2, 0, 2 * Math.PI, false)
        @ctx.fill()


# FIXME: there should be an automatic way to register classes
DotThemes = 
    1:
        name: i18n.tr("Rectangle")
        ident: "rect"
        cl: DotTheme
    2:
        name: i18n.tr("Circle")
        ident: "circle"
        cl: DotThemeCircle

initializeDotThemeModel = (modelID) ->
    console.log("initializeDotThemeModel")
    for id, theme of DotThemes
        modelID.append("name": theme.name, "ident": theme.ident, "id": id)

###############################################################################
#  Drawing a QR code
#
# Select the current dot-, eye-, and color-theme in order to draw the
# given text.  The QR code version (its size) can range from 1 to 40
# (in the modified qrcode.js file) and is automatically adapted to
# include all given data.
###############################################################################
drawQrCode = (textdata, QRCodeVersion = 1, QRErrorCorrectLevel = 'M') ->
    try
        qr = QrCode.qrcode(QRCodeVersion, QRErrorCorrectLevel)
        qr.addData(textdata)
        qr.make()
    catch error
        # FIXME this is too slow, we should calculate the required code version
        if error.name is "QRcodeVersionError"
            drawQrCode(textdata, QRCodeVersion + 1, QRErrorCorrectLevel)
        else
            console.debug(error.name + ": " + error.message)
        return
    ctx = qrcanvas.getContext('2d')
    # the currently selected DotTheme and ColorTheme are taken from
    # the settings values in the database
    theme = new DotThemes[getSetting("dottheme")].cl(qr, getSetting("colortheme"))
    theme.draw(ctx)