Exemplo n.º 1
0
class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.createWidgets()
        self.selectedFamily = None
        self.selectedPerson = None
        self.lastInspected = None
        self.viewMode = "inspect"
        self.comparePerson = None

        self.community = Community()
        self.updateWidgets()

    def createWidgets(self):
        '''
		Creates widgets. Hell awaits you.
		'''
        self.btnFrame = Frame()
        self.btnFrame.pack(fill=X)

        self.passTimeBtn = Button(self.btnFrame,
                                  text="Pass time",
                                  command=self.passTime)
        self.passTimeBtn.grid(row=0, column=0, sticky=E + W)
        self.saveBtn = Button(self.btnFrame, text="Save", command=self.save)
        self.saveBtn.grid(row=0, column=1, sticky=E + W)
        self.loadBtn = Button(self.btnFrame, text="Load", command=self.load)
        self.loadBtn.grid(row=0, column=2, sticky=E + W)
        self.exitBtn = Button(self.btnFrame, text="Exit", command=root.destroy)
        self.exitBtn.grid(row=0, column=3, sticky=E + W)
        self.mayorListBtn = Button(self.btnFrame,
                                   text="Mayor list",
                                   command=self.mayorListWindow)
        self.mayorListBtn.grid(row=0, column=4, sticky=E + W)
        self.mayorListBtn = Button(self.btnFrame,
                                   text="Library",
                                   command=self.libraryWindow)
        self.mayorListBtn.grid(row=0, column=5, sticky=E + W)
        self.dateLabel = Label(self.btnFrame, text="")
        self.dateLabel.grid(row=0, column=6, sticky=E + W, padx=10)
        self.inspectBtn = Button(self.btnFrame,
                                 text="Inspect",
                                 command=self.onInspectBtnClick)
        self.inspectBtn.grid(row=0, column=7, sticky=E + W)
        self.compareBtn = Button(self.btnFrame,
                                 text="Compare",
                                 command=self.onCompareBtnClick)
        self.compareBtn.grid(row=0, column=8, sticky=E + W)
        self.restFrame = Frame()
        self.restFrame.pack(fill=X)
        self.restFrame.grid_columnconfigure(0, minsize=5, weight=0)
        self.restFrame.grid_columnconfigure(1, minsize=5, weight=0)
        self.restFrame.grid_columnconfigure(2, minsize=5, weight=1)

        self.familiesLabel = Label(self.restFrame, text="Families")
        self.familiesLabel.grid(row=0, column=0, sticky=W)
        self.familiesList = Listbox(self.restFrame, exportselection=False)
        self.familiesList.grid(row=1, column=0, sticky=E + W, padx=5)
        self.familiesList.bind('<<ListboxSelect>>', self.onFamiliesLbChange)

        self.peopleLabel = Label(self.restFrame, text="People")
        self.peopleLabel.grid(row=0, column=1, sticky=W)
        self.peopleList = Listbox(self.restFrame, exportselection=False)
        self.peopleList.grid(row=1, column=1, sticky=E + W, padx=5)
        self.peopleList.bind('<<ListboxSelect>>', self.onPeopleLbChange)

        self.personInfoLabel = Label(self.restFrame, text="Inspector")
        self.personInfoLabel.grid(row=0, column=2, sticky=W)
        self.personInfo = Frame(self.restFrame, borderwidth=2, relief=GROOVE)
        self.personInfo.grid(row=1,
                             column=2,
                             rowspan=3,
                             sticky=N + E + W + S,
                             padx=5)
        self.personInfoText = Label(self.personInfo,
                                    text="",
                                    anchor=W,
                                    justify=LEFT)
        self.personInfoText.config(wraplength=350)
        self.personInfoText.pack(fill=X)
        self.personEvents = Text(self.personInfo, height=10, state=DISABLED)
        self.personEvents.pack(fill=X, padx=5, pady=5, side=BOTTOM)
        self.personEventsLabel = Label(self.personInfo, text="Events")
        self.personEventsLabel.pack(anchor=W, side=BOTTOM)

        self.inspectorBtnFrame = Frame(self.personInfo)
        self.inspectorBtnFrame.pack(anchor=W, side=BOTTOM, padx=5)
        self.fatherBtn = Button(self.inspectorBtnFrame,
                                text="Father",
                                command=self.blank)
        self.fatherBtn.grid(row=0, column=0, sticky=W + E)
        self.fatherBtn.bind('<Button-1>', self.onRelativeButtonClick)
        self.motherBtn = Button(self.inspectorBtnFrame,
                                text="Mother",
                                command=self.blank)
        self.motherBtn.grid(row=0, column=1, sticky=W + E)
        self.motherBtn.bind('<Button-1>', self.onRelativeButtonClick)
        self.partnerBtn = Button(self.inspectorBtnFrame,
                                 text="Partner",
                                 command=self.blank)
        self.partnerBtn.grid(row=0, column=2, sticky=W + E)
        self.partnerBtn.bind('<Button-1>', self.onRelativeButtonClick)
        self.familyTreeBtn = Button(self.inspectorBtnFrame,
                                    text="Family Tree",
                                    command=self.familyTreeWindow)
        self.familyTreeBtn.grid(row=0, column=3, sticky=W + E)

        self.eventsLabel = Label(self.restFrame, text="Events")
        self.eventsLabel.grid(row=2, column=0, sticky=W)
        self.eventLog = Text(self.restFrame,
                             width=50,
                             height=19,
                             state=DISABLED)
        self.eventLog.grid(row=3,
                           column=0,
                           columnspan=2,
                           sticky=N + E + W + S,
                           padx=5)

    def updateWidgets(self):
        self.dateLabel.config(text=self.community.dateToString())
        self.familiesList.delete(0, END)
        for f in self.community.families:
            self.familiesList.insert(END, f.familyName)
        self.familiesList.insert(END, "[Graveyard]")
        # Set previous selection
        if self.selectedFamily is not None:
            self.familiesList.selection_set(self.selectedFamily)
        self.updatePeopleList()
        self.updateMainEventLog()
        self.updateInspectorButtons()

        if self.viewMode == "inspect":
            if self.lastInspected == "family":
                self.inspectFamily()
            elif self.lastInspected == "person":
                self.inspectPerson()
            else:
                self.personInfoText.config(text="Select a family to begin")
        elif self.viewMode == "compare":
            if self.comparePerson is None:
                self.writeToInspector("Select a person to compare")

        if self.viewMode == "inspect":
            self.compareBtn.config(relief=RAISED, state=NORMAL)
            self.inspectBtn.config(relief=SUNKEN, state=DISABLED)
        elif self.viewMode == "compare":
            self.compareBtn.config(relief=SUNKEN, state=DISABLED)
            self.inspectBtn.config(relief=RAISED, state=NORMAL)

    def updatePeopleList(self):
        self.peopleList.delete(0, END)
        if self.selectedFamily is not None:
            if self.selectedFamily < len(self.community.families):
                f = self.community.getFamilyByIndex(self.selectedFamily)
                for p in f.people:
                    self.peopleList.insert(END, p.firstName())
            else:
                graveyard = self.community.graveyard()
                for p in graveyard:
                    self.peopleList.insert(END, p.printableFullName())

        if self.selectedFamily is not None and self.selectedPerson is not None:
            self.peopleList.selection_set(self.selectedPerson)

    def updateInspectorButtons(self):
        partner = self.partnerBtn
        father = self.fatherBtn
        mother = self.motherBtn
        tree = self.familyTreeBtn

        partner.config(state=DISABLED)
        father.config(state=DISABLED)
        mother.config(state=DISABLED)
        tree.config(state=DISABLED)
        if self.selectedPerson is not None:
            p = self.getSelectedPerson()
            if p:
                if p.partner is not None:
                    partner.config(state=NORMAL)
                if p.father():
                    father.config(state=NORMAL)
                if p.mother():
                    mother.config(state=NORMAL)
                tree.config(state=NORMAL)

    def inspectFamily(self):
        self.lastInspected = "family"
        if self.selectedFamily < len(self.community.families):
            f = self.community.getFamilyByIndex(self.selectedFamily)
            self.writeToInspector(f.inspect())
            self.writeInspectorEvents(f.eventLog)
        else:
            self.writeToInspector(
                "== The GRAVEYARD ==\n" +
                "The graveyard holds the memories of all souls that have passed through the community."
            )
            self.writeInspectorEvents("")

    def inspectPerson(self):
        self.lastInspected = "person"
        if self.selectedPerson is not None:
            self.updateInspectorButtons()
            p = self.getSelectedPerson()
            if p:
                self.writeToInspector(p.inspect())
                self.writeInspectorEvents(p.eventLog)
            else:
                self.writeToInspector("Select a family to begin")
                self.writeInspectorEvents([])

    def comparePeople(self):
        # TODO compare
        p1 = self.comparePerson
        p2 = self.getSelectedPerson()
        self.writeToInspector(p1.compareTo(p2))

    def updateMainEventLog(self):
        self.eventLog.config(state=NORMAL)
        self.eventLog.delete(1.0, END)
        for e in self.community.eventLog:
            self.eventLog.insert(END, e + "\n")
        self.eventLog.see(END)
        self.eventLog.config(state=DISABLED)

    def onFamiliesLbChange(self, evt):
        if len(self.familiesList.curselection()) > 0:
            ind = int(self.familiesList.curselection()[0])
            self.selectedFamily = ind
            self.selectedPerson = None
            self.updatePeopleList()
            self.updateInspectorButtons()

            if self.viewMode == "inspect":
                self.inspectFamily()
            elif self.viewMode == "compare":
                None

    def onPeopleLbChange(self, evt):
        if len(self.peopleList.curselection()) > 0:
            ind = int(self.peopleList.curselection()[0])
            self.selectedPerson = ind

            if self.viewMode == "inspect":
                self.inspectPerson()
            elif self.viewMode == "compare":
                self.comparePeople()

    def onInspectBtnClick(self):
        self.viewMode = "inspect"
        self.comparePerson = None
        self.updateWidgets()

    def onCompareBtnClick(self):
        if self.selectedPerson is not None and self.lastInspected == "person":
            self.viewMode = "compare"
            self.comparePerson = self.getSelectedPerson()
            self.writeToInspector("Select a person to compare to {}".format(
                self.comparePerson.firstName()))
            self.writeInspectorEvents([])  # clear events panel
        self.updateWidgets()

    def onRelativeButtonClick(self, evt):
        p = self.getSelectedPerson()
        wgt = evt.widget
        if p:
            gyard = self.community.graveyard()
            if wgt == self.fatherBtn and p.father():
                father = p.father()
                if father in gyard:
                    self.selectedFamily = len(self.community.families)
                    self.selectedPerson = gyard.index(father)
                else:
                    self.selectedFamily = self.community.families.index(
                        father.family)
                    self.selectedPerson = father.family.people.index(father)
            elif wgt == self.motherBtn and p.mother():
                mother = p.mother()
                if mother in gyard:
                    self.selectedFamily = len(self.community.families)
                    self.selectedPerson = gyard.index(mother)
                else:
                    self.selectedFamily = self.community.families.index(
                        mother.family)
                    self.selectedPerson = mother.family.people.index(mother)
            elif wgt == self.partnerBtn and p.partner is not None:
                partner = p.partner
                if partner in gyard:
                    self.selectedFamily = len(self.community.families)
                    self.selectedPerson = gyard.index(partner)
                else:
                    self.selectedFamily = self.community.families.index(
                        partner.family)
                    self.selectedPerson = partner.family.people.index(partner)

            self.updateWidgets()

    def writeToInspector(self, s):
        self.personInfoText.config(text=s)

    def writeInspectorEvents(self, events):
        self.personEvents.config(state=NORMAL)
        self.personEvents.delete(1.0, END)
        for e in events:
            self.personEvents.insert(END, e + "\n")
        self.personEvents.see(END)
        self.personEvents.config(state=DISABLED)

    def getSelectedPerson(self):
        if self.selectedFamily is not None and self.selectedPerson is not None:
            if self.selectedFamily < len(self.community.families):
                f = self.community.getFamilyByIndex(self.selectedFamily)
                p = f.getPerson(self.selectedPerson)
                return p
            else:
                # graveyard is selected
                graveyard = self.community.graveyard()
                if self.selectedPerson < len(graveyard):
                    p = graveyard[self.selectedPerson]
                    return p
                return False

    def passTime(self):
        self.community.passTime()
        self.updateWidgets()

    def save(self):
        cwd = os.path.dirname(os.path.realpath(__file__))
        filename = filedialog.asksaveasfilename(
            initialdir=cwd,
            title="Select file",
            filetypes=(("Community save file", "*.cmu"), ("all files", "*.*")))
        if filename:
            with open(filename, "wb") as file:
                try:
                    pickle.dump(self.community, file)
                except Exception as e:
                    print("An error occurred: {}".format(e))

    def load(self):
        cwd = os.path.dirname(os.path.realpath(__file__))
        filename = filedialog.askopenfilename(
            initialdir=cwd,
            title="Select file",
            filetypes=(("Community save file", "*.cmu"), ("all files", "*.*")))
        if filename:
            with open(filename, "rb") as file:
                try:
                    tempCom = pickle.load(file)
                    if isinstance(tempCom, Community):
                        self.community = tempCom
                        self.updateWidgets()
                    else:
                        raise Exception(
                            "Could not decode community from save file")
                except Exception as e:
                    print("An error occurred: {}".format(e))

    def familyTreeWindow(self):
        w = Toplevel(self)
        w.geometry("600x400")
        w.resizable(0, 0)
        w.wm_attributes("-topmost", 1)
        w.grab_set()
        w.main = FamilyTree(w, self.getSelectedPerson())
        w.main.pack(fill=BOTH)

    def mayorListWindow(self):
        w = Toplevel(self)
        w.geometry("600x600")
        w.resizable(0, 0)
        w.wm_attributes("-topmost", 1)
        w.grab_set()
        w.main = MayorList(w, community=self.community)
        w.main.pack(fill=BOTH)

    def libraryWindow(self):
        w = Toplevel(self)
        w.geometry("600x600")
        w.resizable(0, 0)
        w.wm_attributes("-topmost", 1)
        w.grab_set()
        w.main = Library(w, community=self.community)
        w.main.pack(fill=BOTH)

    def blank(self):
        'Placeholder function'
        None
Exemplo n.º 2
0
	def run(self):
		c = Community()
		f1 = Family(c)
		f2 = Family(c)
		c.addFamily(f1)
		c.addFamily(f2)
		c.removeFamily(f2)
		f2 = c.newFamily()
		f1ByInd = c.getFamilyByIndex(0)

		p1 = Person([None, None], gender="male", family=f1)
		p2 = Person([None, None], gender="female", family=f1)
		p1.partner = p2
		p2.partner = p1
		p1.marryPartner()

		f1.addPerson(p1)
		f1.addPerson(p2)
		p3 = f1.generatePerson([p1, p2])
		p3ByInd = f1.getPerson(2)
		p1.addChild(p3)
		p2.addChild(p3)

		p4 = f1.generatePerson([p1, p2])
		f1.removePerson(p4)
		f1.addPerson(p4)
		f1.removePersonByIndex(3)

		p5 = Person([None, None], gender="male", family=f1)
		p6 = Person([None, None], gender="female", family=f1)
		p5.partner = p6
		p6.partner = p5
		p5.updateRapport(p6, -0.5)
		p5.breakUp()

		p5.calculateCap(p6)
		p5.likes(p6)
		p5.calculateRomanticInterest(p6)
		p5.romanticallyLikes(p6)

		p1.isRelative(p3)

		p1.firstName()
		p1.surname()
		p1.printableFullName()
		p1.setFirstName("test")
		p1.setSurname("test")
		p1.regenerateFirstName()

		p1.rapportStatus(p2)
		p1.friends()
		p1.countFriends()
		p1.getMood()
		p1.oneWordMood()
		p1.moodReasons()
		p1.father()
		p1.mother()
		p1.baseMood()
		p1.addModifier(2)
		p1.emotionality()
		p1.politicalOrientation()
		p1.sociopathy()
		p1.isChild()
		p1.getAttr("o")
		p1.explainCap(p2)
		p1.ageToString()
		p1.attributesToString()
		p1.attributesAsDescription()
		p1.otherAttributesAsDescription()
		p1.getLastInteractions(5)
		p1.compareTo(p2)
		p1.logKeyEvent("test")
		p1.inspect()
		p1.die()

		c.log("test message")
		f1.log("test message")

		allPeople = c.allPeople()

		c.passTime()
		c.graveyard()