ResizableRectangle.qml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import QtQuick 2.0
  2. import QtQuick.Window 2.15
  3. Item {
  4. id:rect
  5. property int minimumWidth: 20
  6. property int minimumHeight: 20
  7. property int mouseRegion: 5
  8. property var resizeTarget: rect
  9. anchors.fill: resizeTarget
  10. MouseArea{
  11. id: move
  12. anchors.fill: parent
  13. anchors.margins: mouseRegion
  14. cursorShape: Qt.ClosedHandCursor
  15. property int xPosition: 0
  16. property int yPosition: 0
  17. onPressed: function(mouse){
  18. xPosition = mouse.x
  19. yPosition = mouse.y
  20. }
  21. onPositionChanged: function(mouse){
  22. var xOffset = mouse.x-xPosition
  23. var yOffset = mouse.y-yPosition
  24. if(rect.resizeTarget.x+xOffset>0 && rect.resizeTarget.width+xOffset+rect.resizeTarget.x < rect.resizeTarget.parent.width){
  25. rect.resizeTarget.x = rect.resizeTarget.x+xOffset
  26. }
  27. if(rect.resizeTarget.y+yOffset>0 && rect.resizeTarget.height+yOffset+rect.resizeTarget.y < rect.resizeTarget.parent.height){
  28. rect.resizeTarget.y = rect.resizeTarget.y+yOffset
  29. }
  30. }
  31. }
  32. MouseArea {
  33. id:leftX
  34. width: mouseRegion
  35. anchors.left: parent.left
  36. anchors.top: parent.top
  37. anchors.bottom: parent.bottom
  38. anchors.leftMargin: 0
  39. anchors.bottomMargin: 0
  40. anchors.topMargin: 0
  41. cursorShape: Qt.SizeHorCursor
  42. property int xPosition: 0
  43. onPressed: function(mouse){
  44. xPosition = mouse.x
  45. }
  46. onPositionChanged: function(mouse){
  47. var xOffset = mouse.x-xPosition
  48. if(rect.resizeTarget.x+xOffset>0 && rect.resizeTarget.width-xOffset>minimumWidth){
  49. rect.resizeTarget.x = rect.resizeTarget.x+xOffset
  50. rect.resizeTarget.width = rect.resizeTarget.width-xOffset
  51. }
  52. }
  53. }
  54. MouseArea{
  55. id:rightX
  56. width: mouseRegion
  57. anchors.right: parent.right
  58. anchors.top: parent.top
  59. anchors.bottom: parent.bottom
  60. anchors.bottomMargin: 0
  61. anchors.topMargin: 0
  62. anchors.rightMargin: 0
  63. cursorShape: Qt.SizeHorCursor
  64. property int xPosition: 0
  65. onPressed: function(mouse){
  66. xPosition = mouse.x
  67. }
  68. onPositionChanged: function(mouse){
  69. var xOffset = mouse.x-xPosition
  70. var xWidth = rect.resizeTarget.width+xOffset
  71. var availableWidth = rect.resizeTarget.parent ? rect.resizeTarget.parent.width:Screen.desktopAvailableWidth
  72. if(xWidth+rect.resizeTarget.x<availableWidth && xWidth>minimumWidth){
  73. rect.resizeTarget.width = xWidth
  74. }
  75. }
  76. }
  77. MouseArea{
  78. id:topY
  79. height: mouseRegion
  80. anchors.left: parent.left
  81. anchors.right: parent.right
  82. anchors.top: parent.top
  83. anchors.rightMargin: 0
  84. anchors.leftMargin: 0
  85. anchors.topMargin: 0
  86. cursorShape: Qt.SizeVerCursor
  87. property int yPosition: 0
  88. onPressed: function(mouse){
  89. yPosition = mouse.y
  90. }
  91. onPositionChanged: function(mouse){
  92. var yOffset = mouse.y-yPosition
  93. if(rect.resizeTarget.y+yOffset>0 && rect.resizeTarget.height-yOffset>minimumHeight){
  94. rect.resizeTarget.y = rect.resizeTarget.y+yOffset
  95. rect.resizeTarget.height = rect.resizeTarget.height-yOffset
  96. }
  97. }
  98. }
  99. MouseArea{
  100. id:bottomY
  101. height: mouseRegion
  102. anchors.left: parent.left
  103. anchors.right: parent.right
  104. anchors.bottom: parent.bottom
  105. anchors.rightMargin: 0
  106. anchors.leftMargin: 0
  107. anchors.bottomMargin: 0
  108. cursorShape: Qt.SizeVerCursor
  109. property int yPosition: 0
  110. onPressed: function(mouse){
  111. yPosition = mouse.y
  112. }
  113. onPositionChanged: function(mouse){
  114. var yOffset = mouse.y-yPosition
  115. var yHeight = rect.resizeTarget.height+yOffset
  116. var availableHeight = rect.resizeTarget.parent ? rect.resizeTarget.parent.height : Screen.desktopAvailableHeight
  117. if(yHeight+rect.resizeTarget.y<availableHeight && yHeight>minimumHeight){
  118. rect.resizeTarget.height = yHeight
  119. }
  120. }
  121. }
  122. MouseArea{
  123. width: mouseRegion
  124. height: mouseRegion
  125. anchors.left: parent.left
  126. anchors.top: parent.top
  127. anchors.topMargin: 0
  128. anchors.leftMargin: 0
  129. cursorShape: Qt.SizeFDiagCursor
  130. property int xPosition: 0
  131. property int yPosition: 0
  132. onPressed: function(mouse){
  133. xPosition = mouse.x
  134. yPosition = mouse.y
  135. }
  136. onPositionChanged: function(mouse){
  137. var xOffset = mouse.x-xPosition
  138. if(rect.resizeTarget.x+xOffset>0 && rect.resizeTarget.width-xOffset>minimumWidth){
  139. rect.resizeTarget.x = rect.resizeTarget.x+xOffset
  140. rect.resizeTarget.width = rect.resizeTarget.width-xOffset
  141. }
  142. var yOffset = mouse.y-yPosition
  143. if(rect.resizeTarget.y+yOffset>0 && rect.resizeTarget.height-yOffset>minimumHeight){
  144. rect.resizeTarget.y = rect.resizeTarget.y+yOffset
  145. rect.resizeTarget.height = rect.resizeTarget.height-yOffset
  146. }
  147. }
  148. }
  149. MouseArea{
  150. width: mouseRegion
  151. height: mouseRegion
  152. anchors.right: parent.right
  153. anchors.top: parent.top
  154. anchors.topMargin: 0
  155. anchors.rightMargin: 0
  156. cursorShape: Qt.SizeBDiagCursor
  157. property int xPosition: 0
  158. property int yPosition: 0
  159. onPressed: function(mouse){
  160. xPosition = mouse.x
  161. yPosition = mouse.y
  162. }
  163. onPositionChanged: function(mouse){
  164. var xOffset = mouse.x-xPosition
  165. var xWidth = rect.resizeTarget.width+xOffset
  166. var availableWidth = rect.resizeTarget.parent ? rect.resizeTarget.parent.width:Screen.desktopAvailableWidth
  167. var availableHeight = rect.resizeTarget.parent ? rect.resizeTarget.parent.height : Screen.desktopAvailableHeight
  168. if(xWidth+rect.resizeTarget.x<availableWidth && xWidth>minimumWidth){
  169. rect.resizeTarget.width = xWidth
  170. }
  171. var yOffset = mouse.y-yPosition
  172. if(rect.resizeTarget.y+yOffset>0 && rect.resizeTarget.height-yOffset>minimumHeight){
  173. rect.resizeTarget.y = rect.resizeTarget.y+yOffset
  174. rect.resizeTarget.height = rect.resizeTarget.height-yOffset
  175. }
  176. }
  177. }
  178. MouseArea{
  179. width: mouseRegion
  180. height: mouseRegion
  181. anchors.left: parent.left
  182. anchors.bottom: parent.bottom
  183. anchors.leftMargin: 0
  184. anchors.bottomMargin: 0
  185. cursorShape: Qt.SizeBDiagCursor
  186. property int xPosition: 0
  187. property int yPosition: 0
  188. onPressed: function(mouse){
  189. xPosition = mouse.x
  190. yPosition = mouse.y
  191. }
  192. onPositionChanged: function(mouse){
  193. var xOffset = mouse.x-xPosition
  194. var availableWidth = rect.resizeTarget.parent ? rect.resizeTarget.parent.width:Screen.desktopAvailableWidth
  195. var availableHeight = rect.resizeTarget.parent ? rect.resizeTarget.parent.height : Screen.desktopAvailableHeight
  196. if(rect.resizeTarget.x+xOffset>0 && availableWidth-xOffset>minimumWidth){
  197. rect.resizeTarget.x = rect.resizeTarget.x+xOffset
  198. rect.resizeTarget.width = rect.resizeTarget.width-xOffset
  199. }
  200. var yOffset = mouse.y-yPosition
  201. var yHeight = rect.resizeTarget.height+yOffset
  202. if(yHeight+rect.resizeTarget.y<availableHeight && yHeight>minimumHeight){
  203. rect.resizeTarget.height = yHeight
  204. }
  205. }
  206. }
  207. MouseArea{
  208. width: mouseRegion
  209. height: mouseRegion
  210. anchors.right: parent.right
  211. anchors.bottom: parent.bottom
  212. anchors.rightMargin: 0
  213. anchors.bottomMargin: 0
  214. cursorShape: Qt.SizeFDiagCursor
  215. property int xPosition: 0
  216. property int yPosition: 0
  217. onPressed: function(mouse){
  218. xPosition = mouse.x
  219. yPosition = mouse.y
  220. }
  221. onPositionChanged: function(mouse){
  222. var xOffset = mouse.x-xPosition
  223. var xWidth = rect.resizeTarget.width+xOffset
  224. var availableWidth = rect.resizeTarget.parent ? rect.resizeTarget.parent.width:Screen.desktopAvailableWidth
  225. var availableHeight = rect.resizeTarget.parent ? rect.resizeTarget.parent.height : Screen.desktopAvailableHeight
  226. if(xWidth+rect.resizeTarget.x<availableWidth && xWidth>minimumWidth){
  227. rect.resizeTarget.width = xWidth
  228. }
  229. var yOffset = mouse.y-yPosition
  230. var yHeight = rect.resizeTarget.height+yOffset
  231. if(yHeight+rect.resizeTarget.y<availableHeight && yHeight>minimumHeight){
  232. rect.resizeTarget.height = yHeight
  233. }
  234. }
  235. }
  236. }