|
| 1 | +import os |
| 2 | +from kivy.uix.gridlayout import GridLayout |
| 3 | +from UIElements.fileBrowser import FileBrowser |
| 4 | +from kivy.uix.popup import Popup |
| 5 | +from DataStructures.makesmithInitFuncs import MakesmithInitFuncs |
| 6 | +from UIElements.backgroundPickDlg import BackgroundPickDlg |
| 7 | +from kivy.core.image import Image as CoreImage |
| 8 | +from PIL import Image as PILImage |
| 9 | +from io import BytesIO |
| 10 | +import json |
| 11 | + |
| 12 | +graphicsExtensions = (".jpg", ".png", ".jp2",".webp",".pbm",".ppm",".pgm") |
| 13 | + |
| 14 | + |
| 15 | +class BackgroundMenu(GridLayout, MakesmithInitFuncs): |
| 16 | + |
| 17 | + def __init__(self, data, **kwargs): |
| 18 | + super(BackgroundMenu, self).__init__(**kwargs) |
| 19 | + self.data = data |
| 20 | + |
| 21 | + def updateAlignmentInConfig(self): |
| 22 | + self.data.config.set('Background Settings', 'manualReg', |
| 23 | + self.data.backgroundManualReg) |
| 24 | + self.data.config.set('Background Settings', 'backgroundfile', |
| 25 | + str(self.data.backgroundFile)) |
| 26 | + self.data.config.write() |
| 27 | + |
| 28 | + def openBackground(self): |
| 29 | + ''' |
| 30 | + Open The Pop-up To Load A File |
| 31 | + Creates a new pop-up which can be used to open a file. |
| 32 | + ''' |
| 33 | + # Starting path is either where the last opened file was |
| 34 | + # or the users home directory |
| 35 | + if not os.path.isdir(self.data.backgroundFile): |
| 36 | + startingPath = os.path.dirname(self.data.backgroundFile) |
| 37 | + else: |
| 38 | + # Don't go up a dir if the "backgroundFile" is a directory! |
| 39 | + startingPath = self.data.backgroundFile |
| 40 | + if startingPath is "": |
| 41 | + startingPath = os.path.expanduser('~') |
| 42 | + # We want to filter to show only files that ground control can open |
| 43 | + validFileTypes = graphicsExtensions |
| 44 | + validFileTypes = ['*{0}'.format(fileType) for fileType in validFileTypes] |
| 45 | + |
| 46 | + content = FileBrowser(select_string='Select', |
| 47 | + favorites=[(startingPath, 'Last Location')], |
| 48 | + path=startingPath, |
| 49 | + filters=validFileTypes, dirselect=False) |
| 50 | + content.bind(on_success=self.load, on_canceled=self.dismiss_popup, |
| 51 | + on_submit=self.load) |
| 52 | + self._popup = Popup(title="Select a file...", content=content, |
| 53 | + size_hint=(0.9, 0.9)) |
| 54 | + self._popup.open() |
| 55 | + |
| 56 | + def reloadBackground(self): |
| 57 | + self.processBackground() |
| 58 | + self.close() |
| 59 | + |
| 60 | + def processBackground(self): |
| 61 | + if self.data.backgroundFile == "" or os.path.isdir( |
| 62 | + self.data.backgroundFile): |
| 63 | + self.data.backgroundTexture = None |
| 64 | + self.data.backgroundManualReg = [] |
| 65 | + self.updateAlignmentInConfig() |
| 66 | + self.data.backgroundRedraw = True |
| 67 | + return |
| 68 | + else: |
| 69 | + img = PILImage.open(self.data.backgroundFile) |
| 70 | + img.thumbnail((1920, 1080), PILImage.ANTIALIAS) |
| 71 | + img = img.transpose(PILImage.FLIP_TOP_BOTTOM) |
| 72 | + imgBytes = BytesIO() |
| 73 | + img.save(imgBytes, format="png") |
| 74 | + imgBytes.seek(0) |
| 75 | + texture = CoreImage(imgBytes, ext="png").texture |
| 76 | + self.data.backgroundTexture = texture |
| 77 | + if self.data.backgroundManualReg: |
| 78 | + # Already have centers to use; just go on to warp_image |
| 79 | + self.warp_image() |
| 80 | + else: |
| 81 | + # Start the manual alignment process |
| 82 | + self.realignBackground() |
| 83 | + |
| 84 | + def realignBackground(self): |
| 85 | + content = BackgroundPickDlg(self.data) |
| 86 | + content.setUpData(self.data) |
| 87 | + content.close = self.close_PickDlg |
| 88 | + self._popup = Popup(title="Background PointPicker", content=content, |
| 89 | + size_hint=(0.9, 0.9)) |
| 90 | + self._popup.open() |
| 91 | + |
| 92 | + def close_PickDlg(self, instance): |
| 93 | + if instance.accepted: |
| 94 | + # Update manual image registration marks |
| 95 | + self.data.backgroundManualReg = instance.tex_coords |
| 96 | + # Save the data from the popup |
| 97 | + self.updateAlignmentInConfig() |
| 98 | + self.warp_image() |
| 99 | + self.dismiss_popup() |
| 100 | + self.close() |
| 101 | + |
| 102 | + def warp_image(self): |
| 103 | + self.data.backgroundRedraw = True |
| 104 | + |
| 105 | + def clear_background(self): |
| 106 | + ''' |
| 107 | + Clear background |
| 108 | + ''' |
| 109 | + self.data.backgroundFile = "" |
| 110 | + self.processBackground() |
| 111 | + self.close() |
| 112 | + |
| 113 | + def load(self, instance): |
| 114 | + ''' |
| 115 | + Load A Background Image File from the file dialog |
| 116 | + Takes in a file path (from the pop-up filepicker |
| 117 | + or directory, if no file was picked) and processes it. |
| 118 | + ''' |
| 119 | + if len(instance.selection) == 1: |
| 120 | + filename = instance.selection[0] |
| 121 | + else: |
| 122 | + # User pressed Submit without picking a file |
| 123 | + filename = instance.path |
| 124 | + # Save the file in the config... |
| 125 | + self.data.backgroundFile = filename |
| 126 | + # close the open file popup |
| 127 | + self.dismiss_popup() |
| 128 | + # new image loaded so clear manual alignment centers |
| 129 | + self.data.backgroundManualReg = [] |
| 130 | + # process it |
| 131 | + self.processBackground() |
| 132 | + # Close the menu, going back to the main page. |
| 133 | + self.close() |
| 134 | + |
| 135 | + def dismiss_popup(self, *args): |
| 136 | + ''' |
| 137 | + Close The File Picker (cancel was pressed instead of OK). |
| 138 | + ''' |
| 139 | + self._popup.dismiss() |
| 140 | + pass |
0 commit comments