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.
Answer Here are the elements that I took from the discussion with @dcorking: You have to update the data yourself. One shortcut is to update on a per-case basis, only what needs to be updated and don't touch the rest. Obviously, if you are sorting the collection after a single change, you may need to re-update the whole GUI eitherway. "Observers" have been mentionned that sounds like an automated way to do as above.
Let's consider a basic
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 =  self.itmesToUpdate =  def sort(self): self.items.sort(key=lambda x:float(x.price)) self.itemsToUpdate =  self.itemsToUpdate = listItemsThatWereMovedAround() 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() ...then fill the View with widgets (Menus, EditTexts, Buttons, ...) #The Controller part starts here def setViewFromModel(self): for i in self.collection.itemsToUpdate: self.editTextName[i].setText(self.container.items[i].name) self.editTextPrice[i].setText(self.container.items[i].price) self.collection.itemsToUpdate = # reset the list def setModelFromView(self): for i in self.collection.itemsToUpdate: self.container.items[i].name = self.editTextName[i].getText() self.container.items[i].price = self.editTextPrice[i].getText() #CALLBACKS def onItemModified(self, index): self.collection.itemsToUpdate.append(index) 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.