

from pytank.Core import Settings,SystemFunctions
from pytank.Core import Cleaner

import sys

from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import Qt
from PIL import ImageGrab, ImageFilter, Image
import pytesseract
import cv2
from PyQt4.QtGui import QPalette
import numpy as np
import os, os.path

class MyWindow(QtGui.QWidget):

    def __init__(self,MainObject, parent = None):
        super(MyWindow, self).__init__(parent)
        self.MainObject = MainObject
        self.state = 0
        self.setWindowOpacity(0.30)
        self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint)
        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
        self.setStyleSheet("background-color:red;")
        screen = QtGui.QDesktopWidget().screenGeometry()
        X,Y,W,H = SystemFunctions.GetRectForAllMonitorsCombined()
        self.setGeometry(X,Y,W,H)
        self.setCursor(Qt.CursorShape(Qt.CrossCursor))


        Img1BackgroundColorLarge = ImageGrab.grab()
        Img1BackgroundColorLarge = np.array(Img1BackgroundColorLarge)
        Img1BackgroundGrayLarge = cv2.cvtColor(Img1BackgroundColorLarge, cv2.COLOR_BGR2GRAY)


        temp3 = Settings.ProccesingLocation + 'Img1BackgroundColorLarge.png'
        cv2.imwrite(temp3,Img1BackgroundColorLarge)

        temp3 = Settings.ProccesingLocation + 'Img1BackgroundGrayLarge.png'
        cv2.imwrite(temp3,Img1BackgroundGrayLarge)

        self.rb = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle)
        #self.rb.setStyleSheet("selection-background-color: red")

        self.show()

    @QtCore.pyqtSlot()
    def on_pushButton_clicked(self):
        self.dialogTextBrowser.exec_()

        
    def mousePressEvent(self,ev):
        self.setStyleSheet('')
        self.setWindowOpacity(0.01)
        if ev.button() != Qt.LeftButton:
            self.close()
        if self.state == 0:        
            self.state = 1                        
            self.origin = ev.globalPos()
            self.rb.setGeometry(QtCore.QRect(self.origin,ev.globalPos()).normalized())
            self.rb.show()
    
    def mouseMoveEvent(self,ev):
        if self.state == 1:
            self.rb.setGeometry(QtCore.QRect(self.origin,ev.globalPos()).normalized())
    
    def mouseReleaseEvent(self,ev):
        if self.state == 1:
            self.state = 2
            self.end = ev.globalPos()        
            self.rb.hide()
            self.doSnip()

    def CopyToClipBoard(self):
        """Copy image to clipboard"""
        clip = QtCore.QCoreApplication.instance().clipboard()
        clip.setPixmap(self.basePixmap,0)
        self.ProcessImage()

    def keyPressEvent(self, ev):
        if ev.key() == Qt.Key_Escape: 
            self.abort()

    def doSnip(self):
        DIR = Settings.ImageScripterPath + "\\SystemFiles\\Python\\Lib\\site-packages\\pytank\\User\\UserData\\POI"
        if not os.path.exists(DIR):
            os.makedirs(DIR)

        LEN = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]) + 1
        PictureName = DIR + '\\' + 'POI_' + str(LEN) + '.png'
        x = min(self.origin.x(),self.end.x())
        y = min(self.origin.y(),self.end.y())
        w = abs(self.origin.x() - self.end.x())
        h = abs(self.origin.y() - self.end.y())
        #print x,y,w,h
        pm = QtGui.QPixmap.grabWindow(QtGui.QApplication.desktop().winId(),x,y,w,h)
        pm.save(PictureName,'PNG')
        PictureName = '"' + 'POI_' + str(LEN) + '.png' + '"'
        #textCursor = self.MainObject.MainTextWidget.textCursor()
        #textCursor.insertText(PictureName)
        self.MainObject.editor.insert(PictureName)








