[1198] | 1 | import tkinter as tk
|
---|
| 2 | import tkinter.ttk as ttk
|
---|
| 3 | from tkinter import simpledialog
|
---|
| 4 | from typing import Tuple
|
---|
| 5 | from framsfiles import reader
|
---|
| 6 | from collections import Counter
|
---|
| 7 |
|
---|
| 8 | class ImportWindow(simpledialog._QueryDialog):
|
---|
| 9 | def __init__(self, *args, **kw):
|
---|
| 10 | if "filename" in kw:
|
---|
| 11 | self.__filename = kw["filename"]
|
---|
| 12 | del kw["filename"]
|
---|
| 13 | else:
|
---|
| 14 | self.__filename = None
|
---|
| 15 |
|
---|
| 16 | self.vec = [tk.IntVar(), tk.IntVar(), tk.IntVar(), tk.IntVar(), tk.IntVar(), tk.IntVar()]
|
---|
| 17 |
|
---|
| 18 | simpledialog._QueryDialog.__init__(self, "Select import options for '{}'".format(self.__filename.split("/")[-1]), "options", *args, **kw)
|
---|
| 19 |
|
---|
| 20 | def body(self, master):
|
---|
| 21 | objectCounters = self.__countOccurrences(self.__filename)
|
---|
| 22 |
|
---|
| 23 | text = "Import genotypes - {} genotypes found".format(objectCounters[0])
|
---|
| 24 | cb = ttk.Checkbutton(master, text=text, variable=self.vec[0], state="enabled" if objectCounters[0] > 0 else "disabled")
|
---|
| 25 | cb.pack(anchor=tk.W)
|
---|
| 26 |
|
---|
| 27 | text = "Import simulator parameters without changing expdef - {} parameter sets found".format(objectCounters[1])
|
---|
| 28 | cb = ttk.Checkbutton(master, text=text, variable=self.vec[1], state="enabled" if objectCounters[1] > 0 else "disabled")
|
---|
| 29 | cb.pack(anchor=tk.W)
|
---|
| 30 |
|
---|
| 31 | text = "Import genepool settings - {} gene pools found".format(objectCounters[2])
|
---|
| 32 | cb = ttk.Checkbutton(master, text=text, variable=self.vec[2], state="enabled" if objectCounters[2] > 0 else "disabled")
|
---|
| 33 | cb.pack(anchor=tk.W)
|
---|
| 34 |
|
---|
| 35 | text = "Import population settings - {} populations found".format(objectCounters[3])
|
---|
| 36 | cb = ttk.Checkbutton(master, text=text, variable=self.vec[3], state="enabled" if objectCounters[3] > 0 else "disabled")
|
---|
| 37 | cb.pack(anchor=tk.W)
|
---|
| 38 |
|
---|
| 39 | cb = ttk.Checkbutton(master, text="Create new groups for imported genepools and populations", variable=self.vec[4])
|
---|
| 40 | tk.Label(master, text="").pack(anchor=tk.W)
|
---|
| 41 |
|
---|
| 42 | cb.pack(anchor=tk.W)
|
---|
| 43 |
|
---|
| 44 | cb = ttk.Checkbutton(master, text="Allow switching to a different expdef while importing simulator parameters", variable=self.vec[5])
|
---|
| 45 | cb.pack(anchor=tk.W)
|
---|
| 46 |
|
---|
| 47 | return cb
|
---|
| 48 |
|
---|
| 49 | def getresult(self):
|
---|
| 50 | options = 0
|
---|
| 51 | for i, v in enumerate(self.vec):
|
---|
| 52 | options += v.get() * (2 ** (i + 1))
|
---|
| 53 | return options
|
---|
| 54 |
|
---|
| 55 | def __countOccurrences(self, filename) -> Tuple[int, int, int, int]:
|
---|
| 56 | objects = ["org", "sim_params", "GenePool", "Population"]
|
---|
| 57 |
|
---|
| 58 | file = reader.load(filename)
|
---|
| 59 | counts = Counter(p["_classname"] for p in file)
|
---|
| 60 | result = [counts[o] for o in objects]
|
---|
| 61 |
|
---|
| 62 | return tuple(result) |
---|