<s> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import absolute_import <EOL> import cgi <EOL> import datetime <EOL> import logging <EOL> import mimetypes <EOL> import os <EOL> import re <EOL> import shutil <EOL> from PySide import QtCore , QtGui <EOL> from Motome . config import HIGHLIGHT_COLOR , MEDIA_FOLDER , PLATFORM <EOL> from Motome . Models . Utils import safe_filename , grab_urls <EOL> logger = logging . getLogger ( __name__ ) <EOL> class MotomeTextBrowser ( QtGui . QTextBrowser ) : <EOL> """<STR_LIT>""" <EOL> noteSaved = QtCore . Signal ( ) <EOL> def __init__ ( self , parent ) : <EOL> super ( MotomeTextBrowser , self ) . __init__ ( parent ) <EOL> self . parent = parent <EOL> self . setTextInteractionFlags ( QtCore . Qt . TextEditorInteraction ) <EOL> self . setAcceptDrops ( True ) <EOL> self . setReadOnly ( False ) <EOL> self . setAcceptRichText ( False ) <EOL> self . setMouseTracking ( True ) <EOL> self . setOpenLinks ( False ) <EOL> self . setOpenExternalLinks ( False ) <EOL> self . setUndoRedoEnabled ( True ) <EOL> self . setTabChangesFocus ( True ) <EOL> self . setFrameShape ( QtGui . QFrame . NoFrame ) <EOL> self . save_interval = <NUM_LIT:1000> <EOL> self . save_timer = QtCore . QTimer ( ) <EOL> self . save_timer . timeout . connect ( self . save_note ) <EOL> self . textChanged . connect ( self . save_note ) <EOL> self . keyboard_shortcuts = { } <EOL> self . setup_keyboard_shortcuts ( ) <EOL> @ property <EOL> def notes_dir ( self ) : <EOL> return self . parent . current_note . notedirectory <EOL> @ property <EOL> def notemodel ( self ) : <EOL> return self . parent . current_note <EOL> def setup_keyboard_shortcuts ( self ) : <EOL> self . keyboard_shortcuts = { '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_keyseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_insertseq ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : QtGui . QKeySequence ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda item = None : self . process_insertseq ( '<STR_LIT>' ) } <EOL> } <EOL> for s in self . keyboard_shortcuts . values ( ) : <EOL> QtGui . QShortcut ( s [ '<STR_LIT>' ] , self , s [ '<STR_LIT>' ] ) <EOL> def process_keyseq ( self , seq ) : <EOL> cursor = self . textCursor ( ) <EOL> example = False <EOL> start_pos = <NUM_LIT:0> <EOL> end_pos = <NUM_LIT:0> <EOL> if not cursor . hasSelection ( ) : <EOL> cursor . select ( QtGui . QTextCursor . WordUnderCursor ) <EOL> text = cursor . selectedText ( ) <EOL> if text == '<STR_LIT>' : <EOL> text = '<STR_LIT>' <EOL> example = True <EOL> else : <EOL> text = cursor . selectedText ( ) <EOL> if seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> if example : <EOL> start_pos = cursor . selectionEnd ( ) - len ( text ) - <NUM_LIT:2> <EOL> end_pos = start_pos + len ( text ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> if example : <EOL> start_pos = cursor . selectionEnd ( ) - len ( text ) - <NUM_LIT:1> <EOL> end_pos = start_pos + len ( text ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> elif seq == '<STR_LIT>' : <EOL> cursor . insertText ( '<STR_LIT>' . format ( text ) ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' . format ( seq ) ) <EOL> if example : <EOL> if end_pos == <NUM_LIT:0> : <EOL> start_pos = cursor . selectionEnd ( ) - len ( text ) <EOL> end_pos = start_pos + len ( text ) <EOL> cursor . setPosition ( start_pos ) <EOL> cursor . setPosition ( end_pos , QtGui . QTextCursor . KeepAnchor ) <EOL> self . setTextCursor ( cursor ) <EOL> def process_insertseq ( self , seq ) : <EOL> cursor = self . textCursor ( ) <EOL> current_pos = cursor . position ( ) <EOL> link_title = '<STR_LIT>' <EOL> link_address = '<STR_LIT>' <EOL> if not cursor . hasSelection ( ) : <EOL> cursor . select ( QtGui . QTextCursor . WordUnderCursor ) <EOL> text = cursor . selectedText ( ) <EOL> if text == '<STR_LIT>' : <EOL> text = '<STR_LIT>' <EOL> else : <EOL> link_title = cursor . selectedText ( ) <EOL> if seq == '<STR_LIT>' : <EOL> self . _insert_hyperlink ( title = link_title ) <EOL> elif seq == '<STR_LIT>' : <EOL> filepath , _ = QtGui . QFileDialog . getOpenFileName ( self , "<STR_LIT>" , os . path . expanduser ( '<STR_LIT>' ) ) <EOL> if filepath != '<STR_LIT>' : <EOL> self . _insert_filelink ( filepath ) <EOL> def event ( self , event ) : <EOL> if ( event . type ( ) == QtCore . QEvent . KeyPress ) and ( event . key ( ) == QtCore . Qt . Key_Tab ) : <EOL> self . insertHtml ( '<STR_LIT>' ) <EOL> return True <EOL> return QtGui . QTextBrowser . event ( self , event ) <EOL> def set_note_text ( self , content = None ) : <EOL> try : <EOL> if content is not None : <EOL> text = cgi . escape ( content ) <EOL> else : <EOL> text = cgi . escape ( self . notemodel . content ) <EOL> text = text . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> link_pattern = r'<STR_LIT>' <EOL> link_transform = r'<STR_LIT>' <EOL> linked_content = re . sub ( link_pattern , link_transform , text ) <EOL> intralink_pattern = r'<STR_LIT>' <EOL> intralink_transform = r'<STR_LIT>' <EOL> intralink_content = re . sub ( intralink_pattern , intralink_transform , linked_content ) <EOL> self . setHtml ( intralink_content . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) ) <EOL> self . setDocumentTitle ( self . notemodel . title ) <EOL> except AttributeError : <EOL> self . setHtml ( '<STR_LIT>' ) <EOL> def canInsertFromMimeData ( self , source ) : <EOL> """<STR_LIT>""" <EOL> if source . hasImage ( ) : <EOL> return True <EOL> elif source . hasUrls ( ) : <EOL> return True <EOL> else : <EOL> return super ( MotomeTextBrowser , self ) . canInsertFromMimeData ( source ) <EOL> def insertFromMimeData ( self , source ) : <EOL> """<STR_LIT>""" <EOL> if source . hasImage ( ) : <EOL> image = source . imageData ( ) <EOL> now = datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> imagepath = os . path . join ( self . notes_dir , MEDIA_FOLDER , now + '<STR_LIT>' ) <EOL> image . save ( imagepath ) <EOL> self . _insert_filelink ( imagepath ) <EOL> elif source . hasUrls ( ) : <EOL> urls = source . urls ( ) <EOL> self . _insert_list_of_files ( urls ) <EOL> super ( MotomeTextBrowser , self ) . insertFromMimeData ( source ) <EOL> def dragEnterEvent ( self , e ) : <EOL> """<STR_LIT>""" <EOL> e . accept ( ) <EOL> def dropEvent ( self , e ) : <EOL> """<STR_LIT>""" <EOL> if e . mimeData ( ) . hasUrls ( ) : <EOL> self . _insert_list_of_files ( e . mimeData ( ) . urls ( ) ) <EOL> def dragMoveEvent ( self , e ) : <EOL> """<STR_LIT>""" <EOL> e . accept ( ) <EOL> def start_save ( self ) : <EOL> if self . save_timer . isActive ( ) : <EOL> self . save_timer . stop ( ) <EOL> self . save_timer . start ( self . save_interval ) <EOL> def save_note ( self ) : <EOL> if self . notemodel is None : <EOL> return <EOL> if self . save_timer . isActive ( ) : <EOL> self . save_timer . stop ( ) <EOL> content = self . toPlainText ( ) <EOL> self . notemodel . content = content <EOL> self . noteSaved . emit ( ) <EOL> def highlight_search ( self , query ) : <EOL> """<STR_LIT>""" <EOL> current_cursor = self . textCursor ( ) <EOL> extra_selections = [ ] <EOL> for term in query : <EOL> self . moveCursor ( QtGui . QTextCursor . Start ) <EOL> while self . find ( term ) : <EOL> extra = QtGui . QTextEdit . ExtraSelection ( ) <EOL> extra . format . setBackground ( HIGHLIGHT_COLOR ) <EOL> extra . cursor = self . textCursor ( ) <EOL> extra_selections . append ( extra ) <EOL> self . setExtraSelections ( extra_selections ) <EOL> self . setTextCursor ( current_cursor ) <EOL> def get_note_links ( self ) : <EOL> return self . notemodel . urls <EOL> def _insert_list_of_files ( self , file_list ) : <EOL> for filepath in file_list : <EOL> if filepath . isLocalFile ( ) : <EOL> if '<STR_LIT:win32>' in PLATFORM : <EOL> fpath = filepath . path ( ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> fpath = filepath . path ( ) <EOL> self . _insert_filelink ( fpath ) <EOL> def _insert_hyperlink ( self , title = None ) : <EOL> cursor = self . textCursor ( ) <EOL> current_pos = cursor . position ( ) <EOL> if title is not None : <EOL> link_title = title <EOL> else : <EOL> link_title = '<STR_LIT>' <EOL> link_address = '<STR_LIT>' <EOL> start_pos = current_pos + <NUM_LIT:1> <EOL> end_pos = start_pos + len ( link_title ) <EOL> clipboard_text = QtGui . QClipboard ( ) . text ( ) <EOL> if len ( grab_urls ( clipboard_text ) ) > <NUM_LIT:0> : <EOL> link_address = clipboard_text <EOL> text , ret = QtGui . QInputDialog . getText ( self , '<STR_LIT>' , '<STR_LIT>' , QtGui . QLineEdit . Normal , <EOL> link_address ) <EOL> if cursor . hasSelection ( ) : <EOL> start_pos = cursor . selectionEnd ( ) + <NUM_LIT:3> <EOL> end_pos = start_pos + len ( link_address ) <EOL> if ret : <EOL> if text != '<STR_LIT>' : <EOL> link_address = text <EOL> cursor . insertHtml ( '<STR_LIT>' . format ( link_title , link_address ) ) <EOL> cursor . setPosition ( start_pos ) <EOL> cursor . setPosition ( end_pos , QtGui . QTextCursor . KeepAnchor ) <EOL> self . setTextCursor ( cursor ) <EOL> def _insert_filelink ( self , filepath ) : <EOL> try : <EOL> html_dir = os . path . join ( self . notes_dir , MEDIA_FOLDER ) <EOL> os . makedirs ( html_dir ) <EOL> except OSError : <EOL> pass <EOL> except AttributeError : <EOL> return <EOL> cursor = self . textCursor ( ) <EOL> current_pos = cursor . position ( ) <EOL> filename = safe_filename ( os . path . basename ( filepath ) ) <EOL> dst_path = os . path . join ( self . notes_dir , MEDIA_FOLDER , filename ) <EOL> link_address = '<STR_LIT>' . format ( MEDIA_FOLDER , filename ) <EOL> if cursor . hasSelection ( ) : <EOL> link_title = cursor . selectedText ( ) <EOL> else : <EOL> link_title = filename <EOL> try : <EOL> is_image = '<STR_LIT:image>' in mimetypes . guess_type ( filepath ) [ <NUM_LIT:0> ] <EOL> except TypeError : <EOL> is_image = False <EOL> if is_image : <EOL> try : <EOL> shutil . copyfile ( filepath , dst_path ) <EOL> except ( IOError , shutil . Error ) : <EOL> pass <EOL> self . insertHtml ( '<STR_LIT>' . format ( link_title , link_address ) ) <EOL> else : <EOL> try : <EOL> shutil . copyfile ( filepath , dst_path ) <EOL> except ( IOError , shutil . Error ) : <EOL> pass <EOL> self . insertHtml ( '<STR_LIT>' . format ( link_title , link_address ) ) </s>
