Esempio n. 1
0
class GET(object):

    JUMP_VAL = 20

    _VERSION = '0.1'
    _helptext = """
        GET - %s
        Geodezyjny Edytor Tekstowy
    """

    def __init__(self, root):
        self.root = root
        self.root.title('GET')
        self.root.geometry('450x200')
        self.root.bind('<Control-s>', self.save_file_shrt)
        self.file_path = None
        self.initialdir = './'

        self.column_select_var = tk.BooleanVar()
        self.move_window_var = tk.BooleanVar()

        self.make_widgets()
        self.menu_bar()

    def make_widgets(self):
        self.status_bar = StatusBar(self.root)
        self.status_bar.pack(side=tk.BOTTOM, fill='both', expand=True)
        self.text_area = TextArea(self.root, self.status_bar)
        self.text_area['font'] = ('consolas', '12')
        self.text_area.pack(expand=True, fill='both')

    def menu_bar(self):
        self.menu = tk.Menu(self.root)
        self.file_menu = tk.Menu(self.menu, tearoff=0)
        self.file_menu.add_command(label='Otwórz', command=self.open_file)
        self.file_menu.add_command(label='Zapisz',
                                   command=self.save_file,
                                   accelerator='Ctrl+S')
        self.file_menu.add_command(label='Zapisz jako...',
                                   command=self.save_file_as)
        # menu edycji
        self.edit_menu = tk.Menu(self.menu, tearoff=0)
        self.edit_menu.add_command(label='Kopiuj',
                                   command=self.copy,
                                   accelerator='Ctrl+C')
        self.edit_menu.add_command(label='Wklej',
                                   command=self.paste,
                                   accelerator='Ctrl+V')
        self.edit_menu.add_command(label='Wytnij',
                                   command=self.cut,
                                   accelerator='Ctrl+X')
        self.edit_menu.add_checkbutton(label='Zaznaczanie blokowe',
                                       onvalue=1,
                                       offvalue=0,
                                       variable=self.column_select_var,
                                       command=self.selection_mode)
        self.edit_menu.add_checkbutton(label='Tryb skokowy',
                                       onvalue=1,
                                       offvalue=0,
                                       variable=self.move_window_var,
                                       command=self.move_mode)
        self.edit_menu.add_command(label='Ustawa skok',
                                   command=self.chane_jump_val)
        # menu operacji na liniach
        self.line_menu = tk.Menu(self.menu, tearoff=0)
        self.line_menu.add_command(label='Usuń duplikaty',
                                   command=self.text_area.remove_duplicates)
        # menu operacji na kolumnach
        self.column_menu = tk.Menu(self.menu, tearoff=0)
        self.column_menu.add_command(label='Zamiana początkowych kolumn',
                                     command=self.text_area.column_swap)
        self.column_menu.add_command(label='Spłaszczenie danych',
                                     command=self.text_area.flat)
        # pomoc
        self.help_menu = tk.Menu(self.menu, tearoff=0)
        self.help_menu.add_command(label='O programie', command=self.help)

        self.menu.add_cascade(label='Plik', menu=self.file_menu)
        self.menu.add_cascade(label='Edycja', menu=self.edit_menu)
        self.menu.add_cascade(label='Linie', menu=self.line_menu)
        self.menu.add_cascade(label='Kolumny', menu=self.column_menu)
        self.menu.add_cascade(label='Pomoc', menu=self.help_menu)

        self.root.config(menu=self.menu)

    def read_chunk(self):
        return self.file.read(1024)

    def open_file(self):
        self.text_area.clear()
        self.file_path = askopenfilename(title='Wskaż plik',
                                         initialdir=self.initialdir)
        self.initialdir = os.path.dirname(
            self.file_path)  # to open last opend directory
        self.file = open(self.file_path)
        for frag in iter(self.read_chunk, ''):
            self.text_area.insert(tk.END, frag)
        self.status_bar.set(self.file_path)

    def save_file(self):
        content = self.text_area.get(1.0, tk.END)
        try:
            with open(self.file_path, 'w') as save_file:
                save_file.writelines(content)
                self.status_bar.set('Zapisano!')
        except AttributeError:
            self.save_file_as()

    def save_file_shrt(self, event):
        self.save_file()

    def save_file_as(self):
        content = self.text_area.get(1.0, tk.END)
        self.file_path = asksaveasfilename(title='Zapisz plik',
                                           initialdir=self.initialdir)
        if content != '':
            try:
                with open(self.file_path, 'w') as save_file:
                    save_file.writelines(content)
            except FileNotFoundError:
                showwarning('Uwaga', 'Nie wskazano pliku!!!')

    def copy(self, event):
        if self.column_select_var.get():
            try:
                self.root.clipboard_clear()
                self.root.clipboard_append(self.text_area.block_txt.strip())
            except AttributeError:
                pass
        else:
            self.text_area.event_generate("<<Copy>>")
        return "break"

    def paste(self, event):
        if self.column_select_var.get():
            try:
                w, k = self.text_area.index(tk.INSERT).split('.')
                w_kon, k_kon = self.text_area.index(tk.END).split('.')
                ran = int(w_kon) - int(w)
                clipboard = self.root.clipboard_get().split('\n')
                if ran < len(clipboard):
                    for i in range(0, len(clipboard) - ran):
                        self.text_area.insert(tk.END, '\n' + ' ' * int(k))
                for l in range(0, len(clipboard)):
                    wiersz = str(int(w) + l)
                    self.text_area.insert("%s.%s" % (wiersz, k), clipboard[l])
            except tk.TclError:
                pass
        else:
            self.text_area.event_generate("<<Paste>>")
        return "break"

    def cut(self, event):
        if self.column_select_var.get():
            try:
                self.root.clipboard_clear()
                self.root.clipboard_append(self.text_area.block_txt.strip())
                for i in range(int(self.text_area.start_row),
                               int(self.text_area.row) + 1):
                    self.text_area.delete(
                        '%s.%s' % (i, self.text_area.start_column),
                        '%s.%s' % (i, self.text_area.column))
            except AttributeError:
                pass
        else:
            self.text_area.event_generate("<<Cut>>")
        return "break"

    def selection_mode(self, event=None):
        if self.column_select_var.get():
            self.text_area.bind('<ButtonPress-1>',
                                self.text_area.column_select_start)
            self.text_area.bind('<B1-Motion>', self.text_area.active_choice)
            self.text_area.bind('<ButtonRelease-1>',
                                self.text_area.column_select)
            self.text_area.bind('<Control-x>', self.cut)
            self.text_area.bind('<Control-X>', self.cut)
            self.text_area.bind('<Control-v>', self.paste)
            self.text_area.bind('<Control-V>', self.paste)
            self.text_area.bind('<Control-c>', self.copy)
            self.text_area.bind('<Control-C>', self.copy)
            self.text_area.tag_configure("block", background="gainsboro")
            self.text_area.tag_configure("sel",
                                         background='white',
                                         foreground='black')
        else:
            self.text_area.tag_delete("block")
            self.text_area.unbind('<ButtonPress-1>')
            self.text_area.unbind('<B1-Motion>')
            self.text_area.unbind('<ButtonRelease-1>')
            self.text_area.tag_configure("sel", background='gainsboro')

    def chane_jump_val(self):
        d = Dialog(self.root, jump_val=self.JUMP_VAL, title='Ustaw skok')
        self.JUMP_VAL = d.result

    def move(self, event):
        """
        Metoda umożliwiajaca przesuwanie całego okna w pionie,
        przydatana podczas przepisywania współrzędnych
        :param event:
        :return:
        """
        current_x = self.root.winfo_x()
        current_y = self.root.winfo_y()
        y = str(int(current_y) + self.JUMP_VAL)
        self.root.geometry(f'+{current_x}+{y}')

    def move_mode(self, event=None):
        if self.move_window_var.get():
            self.root.bind('<Return>', self.move)
        else:
            self.root.unbind('<Return>')

    def help(self):
        showinfo('O programie', self._helptext % (self._VERSION))
Esempio n. 2
0
class App:
    def __init__(self, root):
        self.root = root
        self.root.title(u'Obliczenia graficzne')

        # report creation - out directory and filepath
        self.out_path = os.path.join(os.getcwd(), 'out')
        if not os.path.exists(self.out_path):
            os.mkdir(self.out_path)
        self.filename = datetime.datetime.now().strftime(
            '%Y%m%d_%H%M%S') + '.txt'
        self.file_path = os.path.join(self.out_path, self.filename)

        self.status_bar = StatusBar(self.root)
        self.map_canvas = Map(self.root, self.status_bar, self.file_path)
        self.make_widgets()
        self.last_searched = []
        self.root.iconbitmap('./img/gcalc.ico')

    def txt_report(self):
        with open(self.file_path, 'w') as outfile:
            for row in self.map_canvas.obliczone:
                wline = ' '.join(row[0]) + '\n'
                outfile.writelines(wline)
            outfile.close()
        showinfo('INFO', 'Zapisano raport %s' % self.filename)

    def make_widgets(self):

        menu = tk.Menu(self.root)
        menu_obiekty = tk.Menu(menu, tearoff=0)
        menu_etykiety = tk.Menu(menu, tearoff=0)
        menu.add_command(label="Import txt",
                         command=self.map_canvas.import_map)
        menu.add_command(label="Raport txt", command=self.txt_report)
        submenu_liniowe = tk.Menu(menu)
        submenu_liniowe.add_command(label="Linia Bazowa [LB]",
                                    command=self.map_canvas.linia_bazowa)
        submenu_liniowe.add_command(label="Miary ortogonalne [MO]",
                                    command=self.map_canvas.ortog)
        submenu_liniowe.add_command(
            label="Przecięcie prostych [PP]",
            command=self.map_canvas.przeciecie_prostych)
        submenu_liniowe.add_command(label="Szukaj", command=self.search)
        submenu_liniowe.add_command(label="Anuluj",
                                    command=self.map_canvas.clear)

        menu.add_cascade(label="Obliczenia", menu=submenu_liniowe)

        self.toolbar = tk.Frame(self.root, bd=1, relief=tk.RAISED)
        StartButton = tk.Button(self.toolbar,
                                text='LB',
                                relief=tk.FLAT,
                                command=self.map_canvas.linia_bazowa)
        StartButton.pack(side=tk.LEFT, padx=2, pady=2)
        CalcButton = tk.Button(self.toolbar,
                               text='MO',
                               relief=tk.FLAT,
                               command=self.map_canvas.ortog)
        CalcButton.pack(side=tk.LEFT, padx=2, pady=2)
        CalcButton = tk.Button(self.toolbar,
                               text='PP',
                               relief=tk.FLAT,
                               command=self.map_canvas.przeciecie_prostych)
        CalcButton.pack(side=tk.LEFT, padx=2, pady=2)
        SearchButton = tk.Button(self.toolbar,
                                 text='Szukaj',
                                 relief=tk.FLAT,
                                 command=self.search)
        SearchButton.pack(side=tk.LEFT, padx=2, pady=2)
        ClearButton = tk.Button(self.toolbar,
                                text='Anuluj',
                                relief=tk.FLAT,
                                command=self.map_canvas.clear)
        ClearButton.pack(side=tk.LEFT, padx=2, pady=2)
        UpdateButton = tk.Button(self.toolbar,
                                 text='PktNum',
                                 relief=tk.FLAT,
                                 command=self.map_canvas.update_point_shortcut)
        UpdateButton.pack(side=tk.LEFT, padx=2, pady=2)
        self.toolbar.pack(side=tk.TOP, fill=tk.X)
        self.root.config(menu=menu)
        self.map_canvas.pack(fill='both', expand=1)
        self.status_bar.pack(side=tk.LEFT)

    def search(self):
        if len(self.last_searched) > 0:
            for elem in self.last_searched:
                i, c = elem
                self.map_canvas.itemconfigure(i, fill=c)
        d = SDialog(self.root, title='Szukaj')
        if d.result is not None:
            id = d.result
        else:
            id = ''
        try:
            for cid, pt in self.map_canvas.points.items():
                if pt.number == id:
                    self.last_searched.append(
                        (cid, self.map_canvas.itemcget(cid, 'fill')))
                    self.map_canvas.itemconfigure(cid, fill='red')
                    self.map_canvas.configure(
                        scrollregion=self.map_canvas.bbox(cid))
        except KeyError:
            showwarning('UWAGA', 'Brak punktu o numerze %s' % id)