Advertisement
Guest User

Untitled

a guest
Jan 9th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import QtQuick 2.4
  2. import QtQuick.Controls 1.3
  3. import QtQuick.Window 2.2
  4. import QtQuick.Dialogs 1.2
  5.  
  6. ApplicationWindow {
  7.     width: pow2Width * 2 + 10
  8.     height: 700
  9.     visible: true
  10.  
  11.     property int baseWidth: 32
  12.     property int bitWidth: 256 / baseWidth
  13.     property int pow2Width: 256
  14.  
  15.     property int currentIndex: -1
  16.     property bool prefixGetter: false
  17.     property string memberName: "flags"
  18.     property string format: "BASETYPE GETNAME() const { return (DATA & GETMASK) >> OFFSET; }\nvoid SETNAME(BASETYPE v) { DATA = (DATA & SETMASK) | (v << OFFSET); }\n\n"
  19.  
  20.     ListModel { id: myModel }
  21.  
  22.     function compile() {
  23.         if (myModel.count > 0) {
  24.             var code = "public:\n"
  25.             var basetype
  26.             switch (baseWidth) {
  27.             case 8:
  28.                 basetype = "quint8"
  29.                 break
  30.             case 16:
  31.                 basetype = "quint16"
  32.                 break
  33.             case 32:
  34.                 basetype = "quint32"
  35.                 break
  36.             case 64:
  37.                 basetype = "quint64"
  38.                 break
  39.             }
  40.  
  41.             var len = flow.children[myModel.count - 1].y
  42.             if (len) len /= 50
  43.             len += 1
  44.             for (var i = 0; i < myModel.count; ++i) {
  45.                 var offset = flow.children[i].x
  46.                 if (offset) offset /= bitWidth
  47.                 var c = format
  48.                 var name = myModel.get(i).name
  49.                 var capName = cap(name)
  50.                 c = c.replace(/BASETYPE/g, basetype)
  51.                 if (prefixGetter) c = c.replace(/GETNAME/g, "get" + capName)
  52.                 else c = c.replace(/GETNAME/g, name)
  53.                 var data = ""
  54.                 if (len > 1) {
  55.                     var index = flow.children[i].y
  56.                     if (index) index /= 50
  57.                     data += memberName + "[" + index + "]"
  58.                 }
  59.                 else data += memberName
  60.                 c = c.replace(/DATA/g, data)
  61.                 c = c.replace(/GETMASK/g, getMask(myModel.get(i).size, offset))
  62.                 c = c.replace(/SETMASK/g, setMask(myModel.get(i).size, offset))
  63.                 c = c.replace(/OFFSET/g, offset)
  64.                 c = c.replace(/SETNAME/g, "set" + capName)
  65.                 if (!offset) {
  66.                     var trim = ""
  67.                     var begin = 0
  68.                     var end = c.indexOf("(" + data)
  69.                     trim += c.substring(begin, end)
  70.                     begin = end + 1
  71.                     end = c.indexOf(") >")
  72.                     trim += c.substring(begin, end)
  73.                     begin = c.indexOf(";")
  74.                     end = c.lastIndexOf("(")
  75.                     trim += c.substring(begin, end)
  76.                     trim += "v"
  77.                     begin = c.lastIndexOf(";")
  78.                     trim += c.substring(begin)
  79.                     c = trim
  80.                 }
  81.                 code += c
  82.             }
  83.             code += "private:\n"
  84.             if (len > 1) code += basetype + " " + memberName + "[" + len + "];"
  85.             else code += basetype + " " + memberName + ";"
  86.  
  87.             codeField.text = code
  88.             codeD.visible = true
  89.         }
  90.     }
  91.  
  92.  
  93.     function cap(string) {
  94.         return string.charAt(0).toUpperCase() + string.slice(1)
  95.     }
  96.  
  97.     function getMask(size, offset) {
  98.         var bin = ""
  99.         while (offset--) bin += '0'
  100.         while (size--) bin += '1'
  101.         while (bin.length != baseWidth) bin += '0'
  102.         return parseInt(bin.split("").reverse().join(""), 2)
  103.     }
  104.  
  105.     function setMask(size, offset) {
  106.         var bin = ""
  107.         while (offset--) bin += '1'
  108.         while (size--) bin += '0'
  109.         while (bin.length != baseWidth) bin += '1'
  110.         return parseInt(bin.split("").reverse().join(""), 2)
  111.     }
  112.  
  113.     Column {
  114.         Row {
  115.             anchors.horizontalCenter: parent.horizontalCenter
  116.             spacing: 6
  117.             Rectangle {
  118.                 width: 80
  119.                 height: 50
  120.                 color: "lightgray"
  121.                 Text {
  122.                     anchors.centerIn: parent
  123.                     text: "new field"
  124.                 }
  125.                 MouseArea {
  126.                     anchors.fill: parent
  127.                     onClicked: myModel.append({ "name": "newField" + myModel.count, "size": 1})
  128.                 }
  129.             }
  130.  
  131.             Rectangle {
  132.                 width: 80
  133.                 height: 50
  134.                 color: "lightgray"
  135.                 opacity: currentIndex > 0 ? 1 : 0.3
  136.                 Text {
  137.                     anchors.centerIn: parent
  138.                     text: "move up"
  139.                 }
  140.                 MouseArea {
  141.                     enabled: parent.opacity == 1
  142.                     anchors.fill: parent
  143.                     onClicked: {
  144.                         var newIndex = currentIndex -1
  145.                         myModel.move(currentIndex, newIndex, 1)
  146.                         currentIndex = newIndex
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             Rectangle {
  152.                 width: 80
  153.                 height: 50
  154.                 color: "lightgray"
  155.                 opacity: (currentIndex > -1) && (currentIndex < (myModel.count - 1)) ? 1 : 0.3
  156.                 Text {
  157.                     anchors.centerIn: parent
  158.                     text: "move down"
  159.                 }
  160.                 MouseArea {
  161.                     enabled: parent.opacity == 1
  162.                     anchors.fill: parent
  163.                     onClicked: {
  164.                         var newIndex = currentIndex + 1
  165.                         myModel.move(currentIndex, newIndex, 1)
  166.                         currentIndex = newIndex
  167.                     }
  168.                 }
  169.             }
  170.  
  171.             Rectangle {
  172.                 width: 80
  173.                 height: 50
  174.                 color: "lightgray"
  175.                 opacity: currentIndex > -1 ? 1 : 0.3
  176.                 Text {
  177.                     anchors.centerIn: parent
  178.                     text: "delete field"
  179.                 }
  180.                 MouseArea {
  181.                     enabled: parent.opacity == 1
  182.                     anchors.fill: parent
  183.                     onClicked: {
  184.                         myModel.remove(currentIndex)
  185.                         currentIndex = -1
  186.                     }
  187.                 }
  188.             }
  189.  
  190.             Rectangle {
  191.                 width: 80
  192.                 height: 50
  193.                 color: "lightgray"
  194.                 opacity: myModel.count > 0 ? 1 : 0.3
  195.                 Text {
  196.                     anchors.centerIn: parent
  197.                     text: "compile"
  198.                 }
  199.                 MouseArea {
  200.                     anchors.fill: parent
  201.                     onClicked: compile()
  202.                 }
  203.             }
  204.  
  205.             Rectangle {
  206.                 width: 80
  207.                 height: 50
  208.                 color: "lightgray"
  209.                 Text {
  210.                     anchors.centerIn: parent
  211.                     text: "settings"
  212.                 }
  213.                 MouseArea {
  214.                     anchors.fill: parent
  215.                     onClicked: settings.visible = !settings.visible
  216.                 }
  217.             }
  218.         }
  219.  
  220.         Row {
  221.             spacing: 10
  222.             Rectangle {
  223.                 width: pow2Width
  224.                 height: 650
  225.  
  226.                 Flickable {
  227.                     clip: true
  228.                     anchors.fill: parent
  229.                     contentHeight: flow.childrenRect.height
  230.                     contentWidth: parent.width
  231.  
  232.                     Flow {
  233.                         id: flow
  234.                         width: parent.width
  235.  
  236.                         Repeater {
  237.                             model: myModel
  238.  
  239.                             delegate: Item {
  240.                                 width: size * bitWidth
  241.                                 height: 50
  242.  
  243.                                 Rectangle {
  244.                                     anchors.fill: parent
  245.                                     color: size > baseWidth ? "red" : "darkgray"
  246.                                     border.color: "black"
  247.                                     border.width: 1
  248.                                 }
  249.                                 Rectangle {
  250.                                     anchors.fill: parent
  251.                                     color: "white"
  252.                                     opacity: currentIndex === index ? 0.5 : 0
  253.                                 }
  254.                                 MouseArea {
  255.                                     anchors.fill: parent
  256.                                     onClicked: currentIndex = currentIndex == index ? -1 : index
  257.                                 }
  258.                             }
  259.                         }
  260.                     }
  261.                 }
  262.             }
  263.  
  264.             Rectangle {
  265.                 width: pow2Width
  266.                 height: 650
  267.  
  268.                 ListView {
  269.                     id: list
  270.                     clip: true
  271.                     anchors.fill: parent
  272.                     model: myModel
  273.  
  274.                     delegate: Item {
  275.                         width: pow2Width
  276.                         height: 50
  277.  
  278.                         Rectangle {
  279.                             anchors.fill: parent
  280.                             color: size > baseWidth ? "red" : "darkgray"
  281.                             border.color: "black"
  282.                             border.width: 1
  283.  
  284.  
  285.                         }
  286.                         Rectangle {
  287.                             anchors.fill: parent
  288.                             color: "white"
  289.                             opacity: currentIndex === index ? 0.5 : 0
  290.                         }
  291.                         MouseArea {
  292.                             anchors.fill: parent
  293.                             onClicked: currentIndex = currentIndex == index ? -1 : index
  294.                         }
  295.                         Row {
  296.                             anchors.verticalCenter: parent.verticalCenter
  297.                             x: 10
  298.                             spacing: 10
  299.                             TextField {
  300.                                 text: size
  301.                                 width: 50
  302.                                 validator: IntValidator { bottom: 1; top: baseWidth }
  303.                                 onEditingFinished: {
  304.                                     myModel.setProperty(index, "size", parseInt(text))
  305.                                 }
  306.                             }
  307.  
  308.                             TextField {
  309.                                 text: name
  310.                                 width: 176
  311.                                 validator: RegExpValidator { regExp: /([A-Za-z][a-z0-9]+)+/ }
  312.                                 onEditingFinished: {
  313.                                     myModel.setProperty(index, "name", text)
  314.                                 }
  315.                             }
  316.                         }
  317.                     }
  318.                 }
  319.             }
  320.         }
  321.     }
  322.  
  323.     Rectangle {
  324.         id: settings
  325.         anchors.fill: parent
  326.         color: "black"
  327.         opacity: 0.9
  328.         visible: false
  329.  
  330.         MouseArea {
  331.             anchors.fill: parent
  332.             onClicked: parent.visible = false
  333.         }
  334.  
  335.         Column {
  336.             anchors.horizontalCenter: parent.horizontalCenter
  337.             spacing: 10
  338.             y: 100
  339.             Row {
  340.                 anchors.horizontalCenter: parent.horizontalCenter
  341.                 spacing: 10
  342.                 Rectangle {
  343.                     width: 100
  344.                     height: 50
  345.                     color: "lightgray"
  346.                     Text {
  347.                         anchors.centerIn: parent
  348.                         text: "base size:"
  349.                     }
  350.                 }
  351.  
  352.                 Rectangle {
  353.                     width: 50
  354.                     height: 50
  355.                     color: baseWidth == 8 ? "red" : "lightgray"
  356.                     Text {
  357.                         anchors.centerIn: parent
  358.                         text: "8"
  359.                     }
  360.                     MouseArea {
  361.                         anchors.fill: parent
  362.                         onClicked: baseWidth = 8
  363.                     }
  364.                 }
  365.  
  366.                 Rectangle {
  367.                     width: 50
  368.                     height: 50
  369.                     color: baseWidth == 16 ? "red" : "lightgray"
  370.                     Text {
  371.                         anchors.centerIn: parent
  372.                         text: "16"
  373.                     }
  374.                     MouseArea {
  375.                         anchors.fill: parent
  376.                         onClicked: baseWidth = 16
  377.                     }
  378.                 }
  379.  
  380.                 Rectangle {
  381.                     width: 50
  382.                     height: 50
  383.                     color: baseWidth == 32 ? "red" : "lightgray"
  384.                     Text {
  385.                         anchors.centerIn: parent
  386.                         text: "32"
  387.                     }
  388.                     MouseArea {
  389.                         anchors.fill: parent
  390.                         onClicked: baseWidth = 32
  391.                     }
  392.                 }
  393.  
  394.                 Rectangle {
  395.                     width: 50
  396.                     height: 50
  397.                     color: baseWidth == 64 ? "red" : "lightgray"
  398.                     Text {
  399.                         anchors.centerIn: parent
  400.                         text: "64"
  401.                     }
  402.                     MouseArea {
  403.                         anchors.fill: parent
  404.                         onClicked: baseWidth = 64
  405.                     }
  406.                 }
  407.             }
  408.  
  409.             Row {
  410.                 spacing: 10
  411.                 anchors.horizontalCenter: parent.horizontalCenter
  412.                 Rectangle {
  413.                     width: 100
  414.                     height: 50
  415.                     color: "lightgray"
  416.                     Text {
  417.                         anchors.centerIn: parent
  418.                         text: "member name:"
  419.                     }
  420.                 }
  421.  
  422.                 Rectangle {
  423.                     width: 200
  424.                     height: 50
  425.                     color: "lightgray"
  426.                     TextField {
  427.                         anchors.centerIn: parent
  428.                         text: memberName
  429.                         width: 180
  430.                         validator: RegExpValidator { regExp: /([A-Za-z][a-z0-9]+)+/ }
  431.                         onEditingFinished: {
  432.                             memberName = text
  433.                         }
  434.                     }
  435.                 }
  436.             }
  437.  
  438.             Row {
  439.                 spacing: 10
  440.                 anchors.horizontalCenter: parent.horizontalCenter
  441.                 Rectangle {
  442.                     width: 100
  443.                     height: 50
  444.                     color: "lightgray"
  445.                     Text {
  446.                         anchors.centerIn: parent
  447.                         text: "format:"
  448.                     }
  449.                 }
  450.  
  451.                 Rectangle {
  452.                     width: 100
  453.                     height: 50
  454.                     color: "lightgray"
  455.                     Text {
  456.                         anchors.centerIn: parent
  457.                         text: "set"
  458.                     }
  459.                     MouseArea {
  460.                         anchors.fill: parent
  461.                         onClicked: {
  462.                             format = form.text
  463.                         }
  464.                     }
  465.                 }
  466.  
  467.                 Rectangle {
  468.                     width: 100
  469.                     height: 50
  470.                     color: "lightgray"
  471.                     Text {
  472.                         anchors.centerIn: parent
  473.                         text: "reset"
  474.                     }
  475.                     MouseArea {
  476.                         anchors.fill: parent
  477.                         onClicked: {
  478.                             format = "BASETYPE GETNAME() const { return (DATA & GETMASK) >> OFFSET; }\nvoid SETNAME(BASETYPE v) { DATA = (DATA & SETMASK) | (v << OFFSET); }\n\n"
  479.                             form.text = format
  480.                         }
  481.                     }
  482.                 }
  483.             }
  484.  
  485.             TextArea {
  486.                 id: form
  487.                 width: 400
  488.                 height: 200
  489.                 text: format
  490.                 font.pointSize: 8
  491.             }
  492.  
  493.             Rectangle {
  494.                 width: 100
  495.                 height: 50
  496.                 anchors.horizontalCenter: parent.horizontalCenter
  497.                 color: prefixGetter ? "red" : "lightgray"
  498.                 Text {
  499.                     anchors.centerIn: parent
  500.                     text: "prefix getter"
  501.                 }
  502.                 MouseArea {
  503.                     anchors.fill: parent
  504.                     onClicked: prefixGetter = !prefixGetter
  505.                 }
  506.             }
  507.         }
  508.     }
  509.  
  510.     Rectangle {
  511.         id: codeD
  512.         anchors.fill: parent
  513.         color: "black"
  514.         opacity: 0.9
  515.         visible: false
  516.  
  517.         Column {
  518.             spacing: 20
  519.             y: 50
  520.             anchors.horizontalCenter: parent.horizontalCenter
  521.             TextArea {
  522.                 id: codeField
  523.                 width: 400
  524.                 height: 550
  525.                 readOnly: true
  526.             }
  527.  
  528.             Row {
  529.                 spacing: 20
  530.                 anchors.horizontalCenter: parent.horizontalCenter
  531.                 Rectangle {
  532.                     width: 100
  533.                     height: 50
  534.                     color: "lightgray"
  535.                     Text {
  536.                         anchors.centerIn: parent
  537.                         text: "copy"
  538.                     }
  539.  
  540.                     MouseArea {
  541.                         anchors.fill: parent
  542.                         onClicked: {
  543.                             codeField.selectAll()
  544.                             codeField.copy()
  545.                         }
  546.                     }
  547.                 }
  548.  
  549.                 Rectangle {
  550.                     width: 100
  551.                     height: 50
  552.                     color: "lightgray"
  553.                     Text {
  554.                         anchors.centerIn: parent
  555.                         text: "close"
  556.                     }
  557.  
  558.                     MouseArea {
  559.                         anchors.fill: parent
  560.                         onClicked: {
  561.                             codeD.visible = false
  562.                         }
  563.                     }
  564.                 }
  565.             }
  566.         }
  567.     }
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement