I have a question about MVC pattern, concerning the controller part.
Most of the program I happen to write are basically spreadsheet like. They are written in C/GTK+ and Python3/Qt5/Tkinter but share a common design : I separate the gui from the logic (data struct and heavy lifting methods) that seems to be the MVC pattern.
Question
Do I really need to pass data from view to model each time I edit the GUI data and want to compute something ? Then pass the new data back from model to view ? Is that how it should be done ?
I understand this is fine for a small GUI that does heavy computation in the model. But what about a big GUI, like a big table or a spreadsheet.
Pseudo/Python Code
Let's consider a basic collection of items (shopping list, book library, ...).
My main class Collection contains a list (dozens or hundreds) of Item and methods to be applied on the list.
#The model package
class Collection():
def __init__(self, name=""):
self.name = name
self.items = []
def sort(self):
self.items.sort(key=lambda x:float(x.price))m(name,price))
class Item():
def __init__(self, name="", price=0.):
self.name = name
self.price = price
Now the Gui part:
class Gui():
#The View part.
def __init__(self):
self.collection = model.Collection()
#Build...then fill the guiView with widgets (Menus, EditTexts, Buttons, ...)
#The Controller part starts here
def setViewFromModel(self):
self.someEditText.setText(self.collection.items[0].name)
def setModelFromView(self):
self.container.items[0].name = self.someEditText.getText()
def onClickSort(self):
self.setModelFromView()#Read the GUI data
self.collection.sort()#Compute in Model
self.setViewFromModel()#Update the View with new data
Is this design fine ? For spreadsheet-like app ?
Thank you.