# Path: reference_highlighter.py
# class ReferenceHighlighter(QtGui.QSyntaxHighlighter):
#   ### SIGNALS ###
#   refs_edited = pyqtSignal()
#   
#   def __init__(self, parent = None):
#     super(ReferenceHighlighter, self).__init__(parent)
#     
#     self.format = QTextCharFormat()
#     self.format.setForeground(QColor(0, 0, 255))
#     
#     self.references = []
#   
#   def highlightBlock(self, text):
#     
#     # If there is no previous state, then it's -1, which makes the first line 0.
#     # And every line after that increases as expected.
#     line = self.previousBlockState() + 1
#     self.setCurrentBlockState(line)
#     
#     old_refs = copy.deepcopy(self.references)
#     
#     # Make sure we aren't too long.
#     if len(self.references) > self.parent().blockCount():
#       self.references = self.references[:self.parent().blockCount()]
#     
#     # Make sure our matches list is long enough to hold this line.
#     for i in range(len(self.references), line + 1):
#       self.references.append([])
#     
#     if len(self.references) == 0:
#       return
#     
#     self.references[line] = []
#     
#     text = common.qt_to_unicode(text).lower()
#     
#     matches = RE_REFERENCE.finditer(text)
#     
#     for match in matches:
#       self.setFormat(match.start(), match.end() - match.start(), self.format)
#       self.references[line].append((match.group(1), match.start() + 1))
#     
#     if not old_refs == self.references:
#       self.refs_edited.emit()


from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QRect, pyqtSignal
from PyQt4.QtGui import QTextCursor
from reference_highlighter import ReferenceHighlighter
from make_unique import make_unique
import copy
import terminology
import time
### 
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################




class ReferenceEdit(QtGui.QTextEdit):
  ### SIGNALS ###
  refs_edited = pyqtSignal()
  
  def __init__(self, parent):
    super(ReferenceEdit, self).__init__(parent)
    self.setAcceptRichText(False)
    self.highlighter = ReferenceHighlighter(self.document())
    
    # Pull the highlighter's signal up a level so we can filter meaningful changes.
    self.highlighter.refs_edited.connect(self.__references_changed__)
    
    self.references = []
  
  def __references_changed__(self):
    old_refs = copy.deepcopy(self.references)
    
    self.references = []
    
    for ref_list in self.highlighter.references:
      for ref in ref_list:

# Path: reference_highlighter.py
# class ReferenceHighlighter(QtGui.QSyntaxHighlighter):
#   ### SIGNALS ###
#   refs_edited = pyqtSignal()
#   
#   def __init__(self, parent = None):
#     super(ReferenceHighlighter, self).__init__(parent)
#     
#     self.format = QTextCharFormat()
#     self.format.setForeground(QColor(0, 0, 255))
#     
#     self.references = []
#   
#   def highlightBlock(self, text):
#     
#     # If there is no previous state, then it's -1, which makes the first line 0.
#     # And every line after that increases as expected.
#     line = self.previousBlockState() + 1
#     self.setCurrentBlockState(line)
#     
#     old_refs = copy.deepcopy(self.references)
#     
#     # Make sure we aren't too long.
#     if len(self.references) > self.parent().blockCount():
#       self.references = self.references[:self.parent().blockCount()]
#     
#     # Make sure our matches list is long enough to hold this line.
#     for i in range(len(self.references), line + 1):
#       self.references.append([])
#     
#     if len(self.references) == 0:
#       return
#     
#     self.references[line] = []
#     
#     text = common.qt_to_unicode(text).lower()
#     
#     matches = RE_REFERENCE.finditer(text)
#     
#     for match in matches:
#       self.setFormat(match.start(), match.end() - match.start(), self.format)
#       self.references[line].append((match.group(1), match.start() + 1))
#     
#     if not old_refs == self.references:
#       self.refs_edited.emit()


# Path: referenceedit.py
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QRect, pyqtSignal
from PyQt4.QtGui import QTextCursor
from reference_highlighter import ReferenceHighlighter
from make_unique import make_unique
import copy
import terminology
import time

### 
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################




class ReferenceEdit(QtGui.QTextEdit):
  ### SIGNALS ###
  refs_edited = pyqtSignal()
  
  def __init__(self, parent):
    super(ReferenceEdit, self).__init__(parent)
    self.setAcceptRichText(False)
    self.highlighter = ReferenceHighlighter(self.document())
    
    # Pull the highlighter's signal up a level so we can filter meaningful changes.
    self.highlighter.refs_edited.connect(self.__references_changed__)
    
    self.references = []
  
  def __references_changed__(self):
    old_refs = copy.deepcopy(self.references)
    
    self.references = []
    
    for ref_list in self.highlighter.references:
      for ref in ref_list:

        self.references.append(ref[0])