Commits

megamahi committed 3128160

Ready for use. Can create QRCodes of any width and level. The CSS
seems to render fine.

Comments (0)

Files changed (8)

 from setuptools import setup, find_packages
 
+_extra_mako = ["Mako >= 0.1.1"]
 setup(
-    name='',
-    version='',
-    description='',
-    author='',
-    author_email='',
-    url='',
+    name='tw2.qrcode',
+    version='2.0.1b',
+    description='Client-side QR Code Widget',
+    author='Greg Jurman',
+    author_email='gdj2214@rit.edu',
+    license='MIT',
+    url='http://github.com/gregjurman/tw2.qrcode',
     install_requires=[
-        "tw2.core",
-        ## Add other requirements here
-        # "Genshi",
+        "tw2.core>=2.0b2",
+        "tw2.jquery",
+        "tw2.jqplugins.ui",
         ],
+    extras_require={
+        'mako': _extra_mako,
+        },
     packages=find_packages(exclude=['ez_setup', 'tests']),
     namespace_packages = ['tw2'],
     zip_safe=False,
     """,
     keywords = [
         'toscawidgets.widgets',
+        'qrcode',
     ],
     classifiers = [
         'Development Status :: 3 - Alpha',

tw2/qrcode/base.py

+import tw2.core as twc
+
+qrcode_js = twc.JSLink(modname=__name__, filename='static/qrcode.js')
+qrcode_css = twc.CSSLink(modname=__name__, filename='static/qrcode.css')
+

tw2/qrcode/samples.py

 
 import widgets
 
-class DemoQrcode(widgets.Qrcode):
+class DemoQrcode(widgets.QRCodeWidget):
     # Provide default parameters, value, etc... here
     # default = <some-default-value>
-    pass
+    id = "demoqr"
+    data = "Test text qrcode. This has a lot of data in it."
+    level = 9
+    width = 300

tw2/qrcode/static/qrcode.css

+.qr-code {
+    background-color:#fff;
+}
+
+.qr-pixel {
+    float:left;
+}
+
+.qr-row {
+    width: 100%;
+    margin: 0;
+    padding: 0;
+    clear: left;
+}
+
+.light {
+    background-color: #fff;
+    color: #fff;
+}
+
+.dark {
+    background-color: #000;
+    color: #000;
+}

tw2/qrcode/static/qrcode.js

 		this.length++;
 	}
 };
+
+function draw_qrcode(id, text, level, correction_level, width) {
+    var qr = new QRCode(level, QRErrorCorrectLevel.H);
+    qr.addData(text);
+    qr.make();
+    item = $("#"+id);
+    item.empty();
+    var pix_width = Math.floor(width / qr.getModuleCount());
+
+    for (var r = 0; r < qr.getModuleCount(); r++) {
+        item.append("<div id='"+id+"-"+r+"' class='qr-row'>");
+        for (var c = 0; c < qr.getModuleCount(); c++) {
+            if (qr.isDark(r, c) ) {
+                item.append("<div class='qr-pixel dark' style='width:"+pix_width+"px; height:"+pix_width+"px;'></div>");
+            } else {
+                item.append("<div class='qr-pixel light' style='width:"+pix_width+"px; height:"+pix_width+"px;'></div>");
+            }
+        item.append("</div>");
+        }
+    }
+}

tw2/qrcode/templates/qrcode.html

-<div xmlns:py="http://genshi.edgewall.org/" py:attrs="attrs"></div>

tw2/qrcode/templates/qrcode.mak

+<%namespace name="tw" module="tw2.core.mako_util"/>
+<div ${tw.attrs(attrs=w.attrs)} class="qr-code" style="width:${w.width}; height:${w.width};"></div>
+<script type="text/javascript">
+draw_qrcode('${w.selector}', '${w.data}', ${w.level}, QRErrorCorrectLevel.${w.correction_level}, ${w.width});
+</script>

tw2/qrcode/widgets.py

 import tw2.core as twc
+import tw2.jquery
 
+import tw2.jquery
+import tw2.jquery.base as tw2_jq_c_b
+import tw2.jqplugins.ui.base as tw2_jq_ui
 
-class Qrcode(twc.Widget):
-    template = "genshi:tw.qrcode.templates.qrcode"
+import base
 
+class QRCodeWidget(tw2_jq_ui.JQueryUIWidget):
+    '''
+    Creates a QRCode using client side javascript.
+    '''
     # declare static resources here
     # you can remove either or both of these, if not needed
     resources = [
-        twc.JSLink(modname=__name__, filename='static/qrcode.js'),
-        twc.CSSLink(modname=__name__, filename='static/qrcode.css'),
+        tw2.jquery.jquery_js,
+        base.qrcode_js,
+        base.qrcode_css,
     ]
+    template = "mako:tw2.qrcode.templates.qrcode"
 
-    @classmethod
-    def post_define(cls):
-        pass
-        # put custom initialisation code here
+    data = twc.Param("The QRCode data", default="http://github.com/gregjurman/tw2.qrcode")
+
+    correction_level = twc.Param("The QRCode Correction Level", default="H")
+
+    level = twc.Param("The size of the QRCode", default=4)
+
+    width = twc.Param("The total width of the entire QRCode", default=160)
 
     def prepare(self):
-        super(Qrcode, self).prepare()
+        super(QRCodeWidget, self).prepare()
         # put code here to run just before the widget is displayed
+        if self.correction_level not in ['H', 'Q', 'M', 'L']:
+            self.correction_level = 'H'
+        if self.level <= 0 and self.level > 4:
+            self.level = 4
+
+        self.width = str(self.width)
+        self.level = str(self.level)