Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
===========================
Complete GUI from Exercises
===========================
Main module
###########
.. code-block:: python
from __future__ import (absolute_import,division,print_function)
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
import sys
import model
import masterView
import masterPresenter
"""
A wrapper class for setting the main window
"""
class demo(QtGui.QMainWindow):
def __init__(self, parent=None):
super(demo,self).__init__(parent)
data_model = model.DataGenerator()
colour_model = model.ColourConvertor()
self.window = QtGui.QMainWindow()
my_view = masterView.MasterView(parent=self)
self.master_presenter = masterPresenter.MasterPresenter(my_view, data_model, colour_model)
# set the view for the main window
self.setCentralWidget(my_view)
self.setWindowTitle("view tutorial")
def qapp():
if QtGui.QApplication.instance():
_app = QtGui.QApplication.instance()
else:
_app = QtGui.QApplication(sys.argv)
return _app
app = qapp()
window = demo()
window.show()
app.exec_()
which has the addition of the data and colour models being passed to
the Presenter. This makes it easier for us to replace the Model at a
later date.
Master View
###########
.. code-block:: python
from __future__ import (absolute_import, division, print_function)
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
import view
import plotView
import numpy as np
class MasterView(QtGui.QWidget):
def __init__(self, parent=None):
super(MasterView, self).__init__(parent)
grid = QtGui.QVBoxLayout(self)
self.plot_view = plotView.PlotView()
self.options_view=view.view()
grid.addWidget(self.plot_view)
grid.addWidget(self.options_view)
self.setLayout(grid)
def getOptionView(self):
return self.options_view
def getPlotView(self):
return self.plot_view
Master Presenter
################
.. code-block:: python
from __future__ import (absolute_import, division, print_function)
import model
import presenter
import plotPresenter
class MasterPresenter(object):
def __init__(self, view, data_model, colour_model):
self.view = view
self.data_model = data_model
self.colour_model = colour_model
colours = self.colour_model.getColourSelection()
self.presenter = presenter.Presenter(self.view.getOptionView(), colours)
self.plot_presenter = plotPresenter.PlotPresenter(self.view.getPlotView())
# connect statements
self.view.getOptionView().plotSignal.connect(self.updatePlot)
# handle signals
def updatePlot(self):
# only care about the colour if the button is pressed
colour, freq,phi = self.presenter.getPlotInfo()
grid_lines = self.presenter.getGridLines()
self.data_model.genData(freq,phi )
x_data = self.data_model.getXData()
y_data = self.data_model.getYData()
self.plot_presenter.plot(x_data, y_data, grid_lines, colour)
The signal from the View is caught here and the models are used to create the correct plot.
Plot Presenter
##############
.. code-block:: python
from __future__ import (absolute_import, division, print_function)
class PlotPresenter(object):
def __init__(self, view):
self.view = view
def plot(self, x_data, y_data, grid_lines, colour_code):
self.view.addData(x_data, y_data, grid_lines, colour_code, "x")
PlotView
########
Unchanged from :ref:`Matplotlib and MVP <Matplotlib>`.
Presenter
#########
.. code-block:: python
from __future__ import (absolute_import, division, print_function)
class Presenter(object):
def __init__(self, view, colours):
self.view = view
self.view.setColours(colours)
def getPlotInfo(self):
return str(self.view.getColour()), self.view.getFreq(), self.view.getPhase()
def getGridLines(self):
return self.view.getGridLines()
View
####
Unchanged from :ref:`Model Exercise Solution <ModelExerciseSolution>`.
Unchanged from :ref:`Model Exercise Solution <ModelExerciseSolution>`.