Advertisement
Guest User

Untitled

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