<commit_before>#!/usr/bin/python3

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import colorchooser
from tkinter import filedialog
from tkinter import messagebox
import math
import json
import time
import os
import sys

import terralib.palette as palette
import terralib.tool as tool
from terralib.pixelgrid import *
import terralib.importer as importer

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master = master
        self.selectedx = 0
        self.selectedy = 0
        self.selecting = False
        self.midstep = False
        self.clipboard = None

        self.currenttool = tool.Pencil(False)
        self.pack()
        self.configure()
        self.createWidgets()
        self.new()
        
        self.config = {
            "lastdir" : "./"}
        
        try:
            os.makedirs(
                    os.path.expanduser("~/.config/terraformer/"),
                    exist_ok=True)
            with open(
                    os.path.expanduser(
                        "~/.config/terraformer/config.json"), "r") as fileo:
                self.config = json.load(fileo)
        except IOError:
            pass # Just use default

    def configure(self):
        self.basezoom = 2
        self.multiple = 1

    def createWidgets(self):
        self.master.title("Terraformer")
        
        self.master.bind("<Delete>", self.delete)
        self.master.bind("<Control-X>", self.cut)
        self.master.bind("<Control-x>", self.cut)
        self.master.bind("<Control-C>", self.copy)
        self.master.bind("<Control-c>", self.copy)
        self.master.bind("<Control-V>", self.paste)
        self.master.bind("<Control-v>", self.paste)
        self.master.bind("<Control-Z>", tool.undo)
        self.master.bind("<Control-z>", tool.undo)
        self.master.bind("<Control-S>", self.save)
        self.master.bind("<Control-s>", self.save)
        
        self.master.bind("<Left>", lambda x: self.reselecttile(
                    self.selectedx-self.multiple, self.selectedy))
        self.master.bind("<Right>", lambda x: self.reselecttile(
                    self.selectedx+self.multiple, self.selectedy))
        self.master.bind("<Up>", lambda x: self.reselecttile(
                    self.selectedx, self.selectedy-self.multiple))
        self.master.bind("<Down>", lambda x: self.reselecttile(
                    self.selectedx, self.selectedy+self.multiple))

        # Create a menu
        menubar = tk.Menu(self)
        self.master.config(menu=menubar)

        filemenu = tk.Menu(menubar)
        filemenu.add_command(label="New File", command=self.new)
        filemenu.add_separator()
        filemenu.add_command(label="Open Terraformer File", command=self.open)
        filemenu.add_separator()
        filemenu.add_command(label="Save Terraformer File", command=self.save)
        filemenu.add_command(label="Save As..", command=self.saveas)
        filemenu.add_separator()
        filemenu.add_command(label="Import image (16 color)", 
                             command=lambda: self.imports(16))
        filemenu.add_command(label="Import image (4 color)", 
                             command=lambda: self.imports(4))
        filemenu.add_separator()
        filemenu.add_command(label="Export image to PNG", command=self.export)
        filemenu.add_command(label="Export selection to PNG", 
                command=self.exportselection)
        exportmenu = tk.Menu(filemenu)
        exportmenu.add_command(label="8x8", 
                               command=lambda: self.exportstrip(1))
        exportmenu.add_command(label="16x16", 
                               command=lambda: self.exportstrip(2))
        exportmenu.add_command(label="24x24", 
                               command=lambda: self.exportstrip(3))
        exportmenu.add_command(label="32x32", 
                               command=lambda: self.exportstrip(4))
        filemenu.add_cascade(label="Export image as strip...", 
                             menu=exportmenu)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=sys.exit)
        menubar.add_cascade(label="File", menu=filemenu)
        
        editmenu = tk.Menu(menubar)
        editmenu.add_command(label="Undo", command=tool.undo)
        editmenu.add_separator()
        editmenu.add_command(label="Cut", command=self.cut)
        editmenu.add_command(label="Copy", command=self.copy)
        editmenu.add_command(label="Paste", command=self.paste)
        editmenu.add_command(label="Clear clipboard", 
                             command=self.clearclipboard)
        menubar.add_cascade(label="Edit", menu=editmenu)

        palettemenu = tk.Menu(menubar)
        palettemenu.add_command(
            label="Expand palette",
            command=self.expandpalette)
        palettemenu.add_command(
            label="Reduce palette",
            command=self.reducepalette)            
        palettemenu.add_separator()
        palette1menu = tk.Menu(palettemenu)
        palette1menu.add_command(
                label="Gameboy",
                command=lambda: self.setpalette(palette.gameboy))
        palette1menu.add_command(
                label="EGA",
                command=lambda: self.setpalette(palette.ega))
        palette1menu.add_command(
                label="Windows 16-color",
                command=lambda: self.setpalette(palette.win16))
        palette1menu.add_command(
                label="DB 16-color",
                command=lambda: self.setpalette(palette.db16))
        palettemenu.add_cascade(label="Predefined palettes", 
                                menu=palette1menu)
        palette2menu = tk.Menu(palettemenu)
        palette2menu.add_command(
                label="Genesis",
                command=lambda: self.constrainpalette(
                    palette.Constraint.Genesis)) 
        palettemenu.add_cascade(label="Constrain palette",
                                menu=palette2menu)
        palettemenu.add_command(
                label="Clear palette",
                command=lambda: self.setpalette(palette.none))
        menubar.add_cascade(label="Palette", menu=palettemenu)

        optionsmenu = tk.Menu(menubar)
        self.transparentexport = tk.BooleanVar()
        self.transparentexport.set(True)
        optionsmenu.add_checkbutton(label="Export with opaque background",
                                    variable=self.transparentexport)
        menubar.add_cascade(label="Options", menu=optionsmenu)

        debugmenu = tk.Menu(menubar)
        debugmenu.add_command(
                label="About", command=lambda: (
                    messagebox.showinfo("About",
                        "Terraformer v0.0.0\n(c) <NAME> 2016"
                        )))
        menubar.add_cascade(label="Info", menu=debugmenu)
        
        # Shifters
        shiftframe = tk.Frame(self)
        shiftframe.grid(row=0, column=0)
        hflipbutton = tk.Button(
                shiftframe, text="H. Flip", 
                command=lambda:tool.LinearFunc(
                    -1,0,1,0,1,0).step1(
                    self.getCurrentSelection()))
        hflipbutton.grid(row=0, column=0)
        vflipbutton = tk.Button(
                shiftframe, text="V. Flip", 
                command=lambda:tool.LinearFunc(
                    1,0,0,0,-1,1).step1(
                    self.getCurrentSelection()))
        vflipbutton.grid(row=0, column=1)
        rotcwbutton = tk.Button(
                shiftframe, text="CW", 
                command=lambda:tool.LinearFunc(
                    0,-1,1,1,0,0).step1(self.getCurrentSelection()))<commit_msg>Switched flip modes<commit_after>#!/usr/bin/python3

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import colorchooser
from tkinter import filedialog
from tkinter import messagebox
import math
import json
import time
import os
import sys

import terralib.palette as palette
import terralib.tool as tool
from terralib.pixelgrid import *
import terralib.importer as importer

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master = master
        self.selectedx = 0
        self.selectedy = 0
        self.selecting = False
        self.midstep = False
        self.clipboard = None

        self.currenttool = tool.Pencil(False)
        self.pack()
        self.configure()
        self.createWidgets()
        self.new()
        
        self.config = {
            "lastdir" : "./"}
        
        try:
            os.makedirs(
                    os.path.expanduser("~/.config/terraformer/"),
                    exist_ok=True)
            with open(
                    os.path.expanduser(
                        "~/.config/terraformer/config.json"), "r") as fileo:
                self.config = json.load(fileo)
        except IOError:
            pass # Just use default

    def configure(self):
        self.basezoom = 2
        self.multiple = 1

    def createWidgets(self):
        self.master.title("Terraformer")
        
        self.master.bind("<Delete>", self.delete)
        self.master.bind("<Control-X>", self.cut)
        self.master.bind("<Control-x>", self.cut)
        self.master.bind("<Control-C>", self.copy)
        self.master.bind("<Control-c>", self.copy)
        self.master.bind("<Control-V>", self.paste)
        self.master.bind("<Control-v>", self.paste)
        self.master.bind("<Control-Z>", tool.undo)
        self.master.bind("<Control-z>", tool.undo)
        self.master.bind("<Control-S>", self.save)
        self.master.bind("<Control-s>", self.save)
        
        self.master.bind("<Left>", lambda x: self.reselecttile(
                    self.selectedx-self.multiple, self.selectedy))
        self.master.bind("<Right>", lambda x: self.reselecttile(
                    self.selectedx+self.multiple, self.selectedy))
        self.master.bind("<Up>", lambda x: self.reselecttile(
                    self.selectedx, self.selectedy-self.multiple))
        self.master.bind("<Down>", lambda x: self.reselecttile(
                    self.selectedx, self.selectedy+self.multiple))

        # Create a menu
        menubar = tk.Menu(self)
        self.master.config(menu=menubar)

        filemenu = tk.Menu(menubar)
        filemenu.add_command(label="New File", command=self.new)
        filemenu.add_separator()
        filemenu.add_command(label="Open Terraformer File", command=self.open)
        filemenu.add_separator()
        filemenu.add_command(label="Save Terraformer File", command=self.save)
        filemenu.add_command(label="Save As..", command=self.saveas)
        filemenu.add_separator()
        filemenu.add_command(label="Import image (16 color)", 
                             command=lambda: self.imports(16))
        filemenu.add_command(label="Import image (4 color)", 
                             command=lambda: self.imports(4))
        filemenu.add_separator()
        filemenu.add_command(label="Export image to PNG", command=self.export)
        filemenu.add_command(label="Export selection to PNG", 
                command=self.exportselection)
        exportmenu = tk.Menu(filemenu)
        exportmenu.add_command(label="8x8", 
                               command=lambda: self.exportstrip(1))
        exportmenu.add_command(label="16x16", 
                               command=lambda: self.exportstrip(2))
        exportmenu.add_command(label="24x24", 
                               command=lambda: self.exportstrip(3))
        exportmenu.add_command(label="32x32", 
                               command=lambda: self.exportstrip(4))
        filemenu.add_cascade(label="Export image as strip...", 
                             menu=exportmenu)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=sys.exit)
        menubar.add_cascade(label="File", menu=filemenu)
        
        editmenu = tk.Menu(menubar)
        editmenu.add_command(label="Undo", command=tool.undo)
        editmenu.add_separator()
        editmenu.add_command(label="Cut", command=self.cut)
        editmenu.add_command(label="Copy", command=self.copy)
        editmenu.add_command(label="Paste", command=self.paste)
        editmenu.add_command(label="Clear clipboard", 
                             command=self.clearclipboard)
        menubar.add_cascade(label="Edit", menu=editmenu)

        palettemenu = tk.Menu(menubar)
        palettemenu.add_command(
            label="Expand palette",
            command=self.expandpalette)
        palettemenu.add_command(
            label="Reduce palette",
            command=self.reducepalette)            
        palettemenu.add_separator()
        palette1menu = tk.Menu(palettemenu)
        palette1menu.add_command(
                label="Gameboy",
                command=lambda: self.setpalette(palette.gameboy))
        palette1menu.add_command(
                label="EGA",
                command=lambda: self.setpalette(palette.ega))
        palette1menu.add_command(
                label="Windows 16-color",
                command=lambda: self.setpalette(palette.win16))
        palette1menu.add_command(
                label="DB 16-color",
                command=lambda: self.setpalette(palette.db16))
        palettemenu.add_cascade(label="Predefined palettes", 
                                menu=palette1menu)
        palette2menu = tk.Menu(palettemenu)
        palette2menu.add_command(
                label="Genesis",
                command=lambda: self.constrainpalette(
                    palette.Constraint.Genesis)) 
        palettemenu.add_cascade(label="Constrain palette",
                                menu=palette2menu)
        palettemenu.add_command(
                label="Clear palette",
                command=lambda: self.setpalette(palette.none))
        menubar.add_cascade(label="Palette", menu=palettemenu)

        optionsmenu = tk.Menu(menubar)
        self.transparentexport = tk.BooleanVar()
        self.transparentexport.set(True)
        optionsmenu.add_checkbutton(label="Export with opaque background",
                                    variable=self.transparentexport)
        menubar.add_cascade(label="Options", menu=optionsmenu)

        debugmenu = tk.Menu(menubar)
        debugmenu.add_command(
                label="About", command=lambda: (
                    messagebox.showinfo("About",
                        "Terraformer v0.0.0\n(c) <NAME> 2016"
                        )))
        menubar.add_cascade(label="Info", menu=debugmenu)
        
        # Shifters
        shiftframe = tk.Frame(self)
        shiftframe.grid(row=0, column=0)
        hflipbutton = tk.Button(
                shiftframe, text="H. Flip", 
                command=lambda:tool.LinearFunc(
                    1,0,0,0,-1,1).step1(
                    self.getCurrentSelection()))
        hflipbutton.grid(row=0, column=0)
        vflipbutton = tk.Button(
                shiftframe, text="V. Flip", 
                command=lambda:tool.LinearFunc(
                    -1,0,1,0,1,0).step1(
                    self.getCurrentSelection()))
        vflipbutton.grid(row=0, column=1)
        rotcwbutton = tk.Button(
                shiftframe, text="CW", 
                command=lambda:tool.LinearFunc(
                    0,-1,1,1,0,0).step1(self.getCurrentSelection()))
{'fraction_non_alphanumeric': 0.11363636363636363, 'fraction_numerical': 0.010330578512396695, 'mean_word_length': 1.9587392550143266, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1403613', 'n_tokens_mistral': 4606, 'n_tokens_neox': 4395, 'n_words': 683}