Example #1
0
    def dropEvent(self, e):
        "Modify incoming drag and dropped urls to use a relative path."
        try:
            #The startswith thing witll only filter out some of the annoying exception logs but it's better than nothing
            #todo: only modify things that actuall
            if e.mimeData().hasUrls() and e.mimeData().urls()[0].toString(
            ).startswith("file://"):
                droppedpath = e.mimeData().urls()[0].toString()[len("file://"
                                                                    ):]
                here = self.url().toString()[len("file://"):]
                print(here, droppedpath)
                #Don't modify absolute paths to anything outside the notes directory.
                if not os.path.relpath(droppedpath,
                                       config.notespath).startswith(".."):
                    #e.mimeData().setUrls([QtCore.QUrl(os.path.relpath(self.url().toString(), e.mimeData().urls()[0].toString()))])

                    #Create a link as Html that looks the way we want it to.
                    e.mimeData().setHtml('<a href="' + urllib.parse.quote_plus(
                        os.path.relpath(droppedpath, os.path.dirname(here))
                    ) + '">' + capitalize(
                        fn_to_title('.'.join(
                            os.path.basename(droppedpath).split(".")[:-1]))) +
                                         "</a>")
        except:
            logging.exception("Failed to modify to relative path")
        QWebView.dropEvent(self, e)