Guest User

Untitled

a guest
Jul 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import sys
  2. from PyQt5.QtGui import QGuiApplication
  3. from PyQt5.QtQml import QQmlApplicationEngine
  4. from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
  5.  
  6.  
  7. class Plot(QObject):
  8. def __init__(self):
  9. QObject.__init__(self)
  10.  
  11. updCanvas = pyqtSignal(list, arguments=['upd'])
  12.  
  13. @pyqtSlot(list)
  14. def upd(self, data):
  15. print(data)
  16. # do something with the data
  17. # and load it back in the canvas
  18. self.updCanvas.emit(data)
  19.  
  20.  
  21. if __name__ == "__main__":
  22. sys.argv += ['--style', 'material']
  23. app = QGuiApplication(sys.argv)
  24. engine = QQmlApplicationEngine()
  25. plot = Plot()
  26. engine.rootContext().setContextProperty("plot", plot)
  27. engine.load("base.qml")
  28. engine.quit.connect(app.quit)
  29. sys.exit(app.exec_())
  30.  
  31. import QtQuick 2.0
  32. import QtQuick.Controls 2.1
  33. import QtQuick.Controls.Material 2.1
  34. import QtQuick.Layouts 1.2
  35. import QtQuick.Dialogs 1.0
  36.  
  37. ApplicationWindow {
  38. id: window
  39. visible: true
  40. width: 1000
  41. height: 750
  42. Material.theme: Material.Dark
  43. Material.background: "#2C303A"
  44. Material.accent: "#65B486"
  45. Material.foreground: "#efefef"
  46.  
  47. Canvas {
  48. id: canvas
  49. x: 200
  50. y: 100
  51. height: 500
  52. width: 500
  53. property bool loaded: false
  54. property var filepath: ''
  55. property var toGrayscale: false
  56.  
  57. onToGrayscaleChanged: requestPaint()
  58. onFilepathChanged: {
  59. loadImage(filepath)
  60. }
  61. onImageLoaded: {
  62. loaded = true
  63. requestPaint()
  64. }
  65. onPaint: {
  66. if (loaded) {
  67. var ctx = getContext("2d");
  68. ctx.drawImage(filepath, 0, 0, width, height)
  69. }
  70. if (toGrayscale) {
  71. var ctx = getContext("2d");
  72. var ar = ctx.getImageData(0, 0, width, height)
  73. plot.upd(ar.data)
  74. }
  75. }
  76. }
  77.  
  78. FileDialog {
  79. id: fileDialog
  80. title: "Please choose a file"
  81. nameFilters: ["Image files (*.jpg *.png *.jpeg)"]
  82. onAccepted: {
  83. console.log("You chose: " + fileDialog.fileUrls)
  84. canvas.filepath = fileDialog.fileUrls
  85. canvas.requestPaint()
  86. }
  87. onRejected: {
  88. console.log("Canceled")
  89. }
  90. }
  91.  
  92. Button {
  93. text: "choose image"
  94. onClicked: fileDialog.visible = true
  95. }
  96.  
  97. Button {
  98. x: 150
  99. text: "toGrayscale"
  100. onClicked: canvas.toGrayscale = true
  101. }
  102. Connections {
  103. target: plot
  104. //onUpdCanvas: print('')
  105. }
  106. }
Add Comment
Please, Sign In to add comment