| Philipp Le | 84173f6 | 2022-04-19 22:27:12 +0200 | [diff] [blame^] | 1 | # SPDX-License-Identifier: MPL-2.0 |
| 2 | # Copyright (c) 2022 Philipp Le <philipp@philipple.de>. |
| 3 | # This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | |
| 7 | from dcs.frames import get_groups, get_win_of_group |
| 8 | from dcs.frames.base import Group, Window |
| 9 | import logging |
| 10 | from tkinter import ttk, Tk, BOTTOM, BOTH |
| 11 | from typing import Type, Dict, Any |
| 12 | import webbrowser |
| 13 | |
| 14 | |
| 15 | class MainWindow(ttk.Frame): |
| 16 | def __init__(self, *args, **kwargs): |
| 17 | super().__init__(*args, **kwargs) |
| 18 | |
| 19 | self.wdg_treeview = ttk.Treeview(self) |
| 20 | self.wdg_treeview.pack(expand=True, fill=BOTH) |
| 21 | self.wdg_treeview.bind('<Double-Button-1>', self._on_double_click) |
| 22 | |
| 23 | self.group_ids: Dict[Type[Group], Any] = {} |
| 24 | self.win_ids: Dict[Type[Window], Any] = {} |
| 25 | for grp in get_groups(): |
| 26 | self.group_ids[grp] = self.wdg_treeview.insert('', 'end', None, text=grp.NAME) |
| 27 | for win in get_win_of_group(grp): |
| 28 | self.win_ids[win] = self.wdg_treeview.insert(self.group_ids[grp], 'end', None, text=win.TITLE) |
| 29 | |
| 30 | self.frm_copyright = ttk.Frame(self, height=35) |
| 31 | self.frm_copyright.pack(side=BOTTOM, expand=False) |
| 32 | ttk.Label(self.frm_copyright, text='Copyright (c) 2022 Philipp Le').pack() |
| 33 | self.wdg_license = ttk.Button(self.frm_copyright, text='Mozilla Public License v2.0', |
| 34 | command=self._view_license) |
| 35 | self.wdg_license.pack() |
| 36 | |
| 37 | def find_win(self, iid) -> Type[Window]: |
| 38 | for win, win_iid in self.win_ids.items(): |
| 39 | if win_iid == iid: |
| 40 | return win |
| 41 | raise KeyError |
| 42 | |
| 43 | def _on_double_click(self, event): |
| 44 | item_id = event.widget.focus() |
| 45 | try: |
| 46 | win = self.find_win(item_id) |
| 47 | win.make_window(self) |
| 48 | except KeyError: |
| 49 | pass |
| 50 | |
| 51 | def _view_license(self): |
| 52 | webbrowser.open('https://mozilla.org/MPL/2.0/', new=1) |
| 53 | |
| 54 | |
| 55 | def main(): |
| 56 | logging.basicConfig(level=logging.INFO) |
| 57 | |
| 58 | root = Tk() |
| 59 | root.title('Digital Communication Systemes - Interactive') |
| 60 | root.geometry('800x600') |
| 61 | win = MainWindow(root) |
| 62 | win.pack(expand=True, fill=BOTH) |
| 63 | root.mainloop() |
| 64 | |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | main() |