Friday, March 19, 2010

KC Training Package

'''
Created on March 21, 2010

@author: selva
'''
#!/usr/bin/env python
import random
import sys, os, ImageQt
from PyQt4.QtCore import *
from PyQt4 import QtCore, QtGui
from opencv.highgui import *
from PIL import Image, ImageOps
from opencv import adaptors
from opencv.cv import *
import cStringIO
from PyQt4.QtGui import (QApplication, QPainter, QWidget, QCursor, QLabel)


class PrefDlg(QtGui.QDialog):
    def __init__(self, parent=None):
        super(PrefDlg, self).__init__(parent)

        vecWidget = QtGui.QWidget()
        formatLabel = QtGui.QLabel(self.tr("Aspect Ratio: "), vecWidget)

        self.formatComboBox = QtGui.QComboBox(vecWidget)
        self.formatComboBox.addItem("1")
        self.formatComboBox.addItem("2")
        self.formatComboBox.addItem("3")
        self.formatComboBox.addItem("4")
        self.formatComboBox.addItem("6")
        self.formatComboBox.addItem("8")
        self.formatComboBox.addItem("9")
        self.formatComboBox.addItem("12")
        self.formatComboBox.addItem("16")
        self.formatComboBox.setCurrentIndex(0)
      
        self.formatComboBox2 = QtGui.QComboBox(vecWidget)
        self.formatComboBox2.addItem("1")
        self.formatComboBox2.addItem("2")
        self.formatComboBox2.addItem("3")
        self.formatComboBox2.addItem("4")
        self.formatComboBox2.addItem("6")
        self.formatComboBox2.addItem("8")
        self.formatComboBox2.addItem("9")
        self.formatComboBox2.addItem("12")
        self.formatComboBox2.addItem("16")
        self.formatComboBox2.setCurrentIndex(0)

        formatLabel4 = QtGui.QLabel(self.tr(":"), vecWidget)
        formatLabel4.setAlignment(QtCore.Qt.AlignCenter)

        formatLabel2 = QtGui.QLabel(self.tr("Width, Height: "), vecWidget)

        self.label = QtGui.QLineEdit(vecWidget)
        self.label.setText ("48")

        formatLabel3 = QtGui.QLabel(self.tr("X"), vecWidget)
        formatLabel3.setAlignment(QtCore.Qt.AlignCenter)

        self.label2 = QtGui.QLineEdit(vecWidget)
        self.label2.setText ("16")

      
        okButton = QtGui.QPushButton("&OK")
        cancelButton = QtGui.QPushButton("Cancel")
      
        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addStretch()
        buttonLayout.addWidget(okButton)
        buttonLayout.addWidget(cancelButton)
        layout = QtGui.QGridLayout()
        layout.addWidget(formatLabel, 0, 0)
        layout.addWidget(self.formatComboBox, 0, 1)
        layout.addWidget(formatLabel4, 0, 2)
        layout.addWidget(self.formatComboBox2, 0, 3)
        layout.addWidget(formatLabel2, 2, 0)
        layout.addWidget(formatLabel3, 3, 1)
      
        layout.addWidget(self.label, 3, 0)
        layout.addWidget(self.label2, 3, 2, 1, 4)
        layout.addLayout(buttonLayout, 5, 0, 1, 7)
        self.setLayout(layout)
      
        self.connect(okButton, SIGNAL("clicked()"),self, SLOT("accept()"))
        self.connect(cancelButton, SIGNAL("clicked()"),self, SLOT("reject()"))
        self.setWindowTitle("Preferences")



class ctrlList(QtGui.QListWidget):
    def __init__(self, parent=None):
        QtGui.QListWidget.__init__(self, parent)
      
        self.width = 0
        self.height = 0
        self.symmetry = 'sym'
        self.mode = 'BASIC'
        self.error = 'None'
        self.boost = 'GAB'
      
        tabWidget = QtGui.QTabWidget()
        vecWidget = QtGui.QWidget()
        detectWidget = QtGui.QWidget()
        trainWidget = QtGui.QWidget()
        generalWidget = QtGui.QWidget()
      
      
        tabWidget.addTab(generalWidget, "General")
        tabWidget.addTab(trainWidget, "Train")
        tabWidget.addTab(detectWidget, "Detect")
        #tabWidget.addTab(vecWidget, "Add")
      
        cropButton = QtGui.QPushButton("Crop", generalWidget)
        cropButton.setGeometry(45, 50, 140, 35)
        self.connect(cropButton, QtCore.SIGNAL("clicked()"), self.cropimg)
      
        uncropButton = QtGui.QPushButton("uncrop", generalWidget)
        uncropButton.setGeometry(45, 100, 140, 35)
        self.connect(uncropButton, QtCore.SIGNAL("clicked()"), self.uncropimg)
      
        createButton = QtGui.QPushButton("Process", generalWidget)
        createButton.setGeometry(45, 150, 140, 35)
        self.connect(createButton, QtCore.SIGNAL("clicked()"), self.createimg)
      
        self.imageLabel2 = QtGui.QLabel(generalWidget)
        self.imageLabel2.setBackgroundRole(QtGui.QPalette.Base)
        self.imageLabel2.setSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Ignored)
        self.imageLabel2.setAlignment(QtCore.Qt.AlignCenter)
        self.imageLabel2.setGeometry(40, 230, 48, 48)
        self.imageLabel2.setScaledContents(True)
      
        detectButton = QtGui.QPushButton("Detect", detectWidget)
        detectButton.setGeometry(45, 50, 140, 35)
        self.connect(detectButton, QtCore.SIGNAL("clicked()"), self.detectimg)
      
        formatLabel = QtGui.QLabel(self.tr("Load XML file:"), detectWidget)
        formatLabel.setGeometry(25, 120, 160, 35)
      
        browseButton = QtGui.QPushButton("Browse", detectWidget)
        browseButton.setGeometry(45, 160, 140, 35)
        self.connect(browseButton, QtCore.SIGNAL("clicked()"), self.browse)
      
        self.label = QtGui.QLineEdit(detectWidget)
        self.label.setGeometry(20,210, 190, 25)
     
        browseButton = QtGui.QPushButton("Browse", trainWidget)
        browseButton.setGeometry(45, 5, 140, 35)
        self.connect(browseButton, QtCore.SIGNAL("clicked()"), self.negbrowse)
      
        self.label2 = QtGui.QLineEdit(trainWidget)
        self.label2.setGeometry(20, 45, 190, 25)
      
        formatLabel2 = QtGui.QLabel(self.tr("# Stages"), trainWidget)
        formatLabel2.setGeometry(35, 75, 60, 25)
      
        self.label3 = QtGui.QLineEdit(trainWidget)
        self.label3.setGeometry(55, 100, 40, 25)
        self.label3.setText ("20")
      
        formatLabel3 = QtGui.QLabel(self.tr("# Splits"), trainWidget)
        formatLabel3.setGeometry(135, 75, 60, 25)
      
        self.label4 = QtGui.QLineEdit(trainWidget)
        self.label4.setGeometry(155, 100, 40, 25)
        self.label4.setText ("2")
      
        formatLabel4 = QtGui.QLabel(self.tr("Min hit rate"), trainWidget)
        formatLabel4.setGeometry(25, 130, 75, 25)
      
        self.label5 = QtGui.QLineEdit(trainWidget)
        self.label5.setGeometry(50, 155, 45, 25)
        self.label5.setText ("0.995")
      
        formatLabel5 = QtGui.QLabel(self.tr("Max false alarm"), trainWidget)
        formatLabel5.setGeometry(115, 130, 105, 25)
  
        self.label6 = QtGui.QLineEdit(trainWidget)
        self.label6.setGeometry(155, 155, 40, 25)
        self.label6.setText ("0.5")
      
        formatLabel6 = QtGui.QLabel(self.tr("Weight trimming"), trainWidget)
        formatLabel6.setGeometry(5, 185, 110, 25)
      
        self.label7 = QtGui.QLineEdit(trainWidget)
        self.label7.setGeometry(55, 210, 40, 25)
        self.label7.setText ("0.95")
      
        formatLabel7 = QtGui.QLabel(self.tr("Memory (MB)"), trainWidget)
        formatLabel7.setGeometry(130, 185, 90, 25)
       
        self.label8 = QtGui.QLineEdit(trainWidget)
        self.label8.setGeometry(155, 210, 40, 25)
        self.label8.setText ("512")
      
        self.formatComboBox = QtGui.QComboBox(trainWidget)
        self.formatComboBox.addItem("symmetry")
        self.formatComboBox.addItem("non-symmetry")
        self.formatComboBox.setCurrentIndex(0)
        self.formatComboBox.setGeometry(30, 265, 100, 30)
        self.connect(self.formatComboBox, QtCore.SIGNAL('currentIndexChanged(int)'), self.comboxBoxUpdated)
      
        formatLabel8 = QtGui.QLabel(self.tr("Mode:"), trainWidget)
        formatLabel8.setGeometry(140, 240, 90, 25)
      
        self.formatComboBox2 = QtGui.QComboBox(trainWidget)
        self.formatComboBox2.addItem("BASIC")
        self.formatComboBox2.addItem("CORE")
        self.formatComboBox2.addItem("ALL")
        self.formatComboBox2.setCurrentIndex(0)
        self.formatComboBox2.setGeometry(140, 265, 80, 30)
        self.connect(self.formatComboBox2, QtCore.SIGNAL('currentIndexChanged(int)'), self.comboxBoxUpdated2)
      
        formatLabel9 = QtGui.QLabel(self.tr("Error type:"), trainWidget)
        formatLabel9.setGeometry(30, 295, 90, 25)
      
        self.formatComboBox3 = QtGui.QComboBox(trainWidget)
        self.formatComboBox3.addItem("None")
        self.formatComboBox3.addItem("misclass")
        self.formatComboBox3.addItem("entropy")
        self.formatComboBox3.addItem("gini")
        self.formatComboBox3.setCurrentIndex(0)
        self.formatComboBox3.setGeometry(30, 320, 100, 30)
        self.connect(self.formatComboBox3, QtCore.SIGNAL('currentIndexChanged(int)'), self.comboxBoxUpdated3)
      
        formatLabel10 = QtGui.QLabel(self.tr("Boosting:"), trainWidget)
        formatLabel10.setGeometry(140, 295, 90, 25)
      
        self.formatComboBox4 = QtGui.QComboBox(trainWidget)
        self.formatComboBox4.addItem("GAB")
        self.formatComboBox4.addItem("LB")
        self.formatComboBox4.addItem("RAB")
        self.formatComboBox4.addItem("DAB")
        self.formatComboBox4.setCurrentIndex(0)
        self.formatComboBox4.setGeometry(140, 320, 80, 30)
        self.connect(self.formatComboBox4, QtCore.SIGNAL('currentIndexChanged(int)'), self.comboxBoxUpdated4)
      
        vecButton = QtGui.QPushButton("Train", trainWidget)
        vecButton.setGeometry(45, 360, 140, 35)
        self.connect(vecButton, QtCore.SIGNAL("clicked()"), self.vector)
      
        layout = QtGui.QVBoxLayout()
        layout.addWidget(tabWidget)
        self.setLayout(layout)
      
  
    def openproc(self, ipl_img):
        ipl_width = ipl_img.width
        ipl_height = ipl_img.height
        if(ipl_width > ipl_height):
            width = 150
            diff = (ipl_width*100)/150
            height = int((ipl_height*100)/diff)

        else:
            height = 150
            diff = (ipl_height*100)/150
            width = int((ipl_width*100)/diff)
      
        newImage = cvCreateImage(cvSize(width, height), ipl_img.depth, ipl_img.nChannels);
        cvResize(ipl_img, newImage, CV_INTER_LINEAR);
        pil_img2 = adaptors.Ipl2PIL(newImage)
        pix = imageViewer.pil2qimage(pil_img2)
        self.imageLabel2.setPixmap(pix)
        self.imageLabel2.adjustSize()
      
    def cropimg(self):
        imageViewer.cropimg()
      
    def uncropimg(self):
        imageViewer.uncropimg()
      
    def createimg(self):
        imageViewer.createimg()
      
    def detectimg(self):
        imageViewer.detectimg(str(self.xml))
      
    def browse(self):
        self.xml = QtGui.QFileDialog.getOpenFileName(self,
                            self.tr("Choose XML file"), "", "XML Files (*.xml)")
        if self.xml.isEmpty():
            return False
      
        self.label.setText (self.xml)
  
    def negbrowse(self):
        self.neg_foldername = QtGui.QFileDialog.getOpenFileName(self,
                            self.tr("Choose negative file"), "", "data Files (*.dat)")
        if self.neg_foldername.isEmpty():
            return False
      
        self.label2.setText (self.neg_foldername)
      
    def comboxBoxUpdated(self, index):
    
        if index == 0:
            self.symmetry = 'sym'
        elif index == 1:
            self.symmetry = 'nonsym'
      
          
    def comboxBoxUpdated2(self, index):
    
        if index == 0:
            self.mode = 'BASIC'
        elif index == 1:
            self.mode = 'CORE'
        elif index == 2:
            self.mode = 'ALL'

  
    def comboxBoxUpdated3(self, index):
    
        if index == 0:
            self.error = 'None'
        elif index == 1:
            self.error = 'misclass'
        elif index == 2:
            self.error = 'gini'
        elif index == 3:
            self.error = 'entropy'
     
  
    def comboxBoxUpdated4(self, index):
    
        if index == 0:
            self.boost = 'GAB'
        elif index == 1:
            self.boost = 'LB'
        elif index == 2:
            self.boost = 'RAB'
        elif index == 3:
            self.boost = 'DAB'
    
   
  
    def vector(self):
      
        self.nstages =  str(self.label3.text())
        self.nsplits =  str(self.label4.text())
        self.minhitrate =  str(self.label5.text())
        self.maxfalsealarm =  str(self.label6.text())
        self.weighttrimming =  str(self.label7.text())
        self.memory =  str(self.label8.text())
        imageViewer.vector(self.neg_foldername, self.nstages, self.nsplits, self.minhitrate,
                           self.maxfalsealarm, self.weighttrimming, self.memory,
                           self.symmetry, self.mode, self.error, self.boost)
     

class PiecesList(QtGui.QListWidget):
    def __init__(self, parent=None):
        QtGui.QListWidget.__init__(self, parent)
        self.setViewMode(QtGui.QListView.IconMode)
        self.setIconSize(QtCore.QSize(64, 64))
        self.setSpacing(30)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)
        self.resize(100, 10)
  
    def checklist(self, item):
        item.setCheckState(QtCore.Qt.Checked)
      
    def unchecklist(self, item):
        item.setCheckState(QtCore.Qt.Unchecked)
      
    def addPiece(self, pixmap, location, name):
        pieceItem = QtGui.QListWidgetItem(self)
        pieceItem.setIcon(QtGui.QIcon(pixmap))
        pieceItem.setData(QtCore.Qt.UserRole, QtCore.QVariant(pixmap))
        pieceItem.setData(QtCore.Qt.UserRole+1, QtCore.QVariant(location))
        pieceItem.setData(QtCore.Qt.UserRole+2, QtCore.QVariant(name))
        pieceItem.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable)
        pieceItem.setCheckState(QtCore.Qt.Unchecked)
      
    def mousePressEvent(self, event):
        if (event.buttons() == QtCore.Qt.RightButton):
            reply = QtGui.QMessageBox.question(self, 'Message',
                                               "Do you want to remove this image from the list?",
                                               QtGui.QMessageBox.No, QtGui.QMessageBox.Yes)
            if reply == QtGui.QMessageBox.Yes:
                item = self.itemAt(event.x(), event.y())
                if item:
                    self.setCurrentItem(item)
                    item = self.currentItem()
                    self.takeItem(self.row(item))
            else:
                return
        else:
            item = self.itemAt(event.x(), event.y())
            if item:
                self.setCurrentItem(item)
                item = self.currentItem()
                itemData = QtCore.QByteArray()
                dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
                self.pixmap_choose = QtGui.QPixmap(item.data(QtCore.Qt.UserRole))
                location = item.data(QtCore.Qt.UserRole+1).toPoint()
                self.name = item.data(QtCore.Qt.UserRole+2).toString()
                dataStream << self.pixmap_choose << location << self.name
                imageViewer.OpenChoose(self.pixmap_choose, item)
                imageViewer.calltext(self.name)
              
class ImageViewer(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
      
        self.databk = 0
        self.count = 0
        self.ratio_x = 0
        self.ratio_y = 0
        self.datacheck = 0
        self.rubberBand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
        self.rubberBand2 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand3 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand4 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand5 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand6 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand7 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand8 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand9 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand10 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand11 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand12 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand13 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand14 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand15 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand16 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand17 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand18 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand19 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand20 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand21 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand22 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand23 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand24 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand25 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand26 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand27 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand28 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand29 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand30 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
        self.rubberBand31 = QtGui.QRubberBand(QtGui.QRubberBand.Line, self)
      
        self.printer = QtGui.QPrinter()
        self.scaleFactor = 1.0
        self.Image = []
        self.rsz_width = 100
        self.rsz_height = 100
        screen = QtGui.QDesktopWidget().screenGeometry()
        self.imageWidget = QtGui.QWidget()
      
        self.imageLabel = QtGui.QLabel(self.imageWidget)
        self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
        self.imageLabel.setSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Ignored)
        self.imageLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.imageLabel.setScaledContents(True)
       # self.imageLabel.setGeometry(300, 50, 145, 35)
        self.setCentralWidget(self.imageWidget)
        self.setWindowTitle(self.tr("KC Training Package"))
        self.resize(screen.width(), screen.height())
        self.createActions()
        self.createMenus()
        self.setupWidgets()
      

    def calltext(self, name):
        self.name = name
        self.setWindowTitle("KC Training Package - %s" % self.name)

     
    def qimage2pil(self, qimage):
        buffer = QtCore.QBuffer()
        buffer.open(QtCore.QIODevice.ReadWrite)
        qimage.save(buffer, "JPG")
        strio = cStringIO.StringIO()
        strio.write(buffer.data())
        buffer.close()
        strio.seek(0)
        pil_img = Image.open(strio)
        return pil_img
  
    def pil2qimage(self, pil_img):
        x, y = pil_img.size
        data = pil_img.tostring("raw", "BGRX")
        qimage = QtGui.QImage(data, x, y, QtGui.QImage.Format_RGB32)
        qpixmap = QtGui.QPixmap(x,y)
        self.pix = QtGui.QPixmap.fromImage(qimage)
        return self.pix

    def index(self, directory):
    # like os.listdir, but traverses directory trees
        stack = [directory]
        files = []
        while stack:
            directory = stack.pop()
            for file in os.listdir(directory):
                fullname = os.path.join(directory, file)
                files.append(fullname)
                if os.path.isdir(fullname) and not os.path.islink(fullname):
                    stack.append(fullname)
        return files

    def openfolder(self):
      
        self.foldername = QtGui.QFileDialog.getExistingDirectory(self,
                            self.tr("Choose Folder"), "", QtGui.QFileDialog.ShowDirsOnly)
      
        if not self.foldername.isEmpty():
            for file in self.index(str(self.foldername)):
                print file
                self.openImage(QtCore.QString(file))
      
      
    def openImage(self, path=QtCore.QString()):
        self.x = 0
        self.y = 0
        self.fileName = path

        if self.fileName.isNull():
            self.fileName = QtGui.QFileDialog.getOpenFileName(self,
                            self.tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp *JPG *jpeg)")
        if not self.fileName.isEmpty():
            newImage = QtGui.QPixmap()
            if not newImage.load(self.fileName):
                return
            self.Image = newImage
            self.piecesList.addPiece(self.Image, QtCore.QPoint(self.x,self.y), self.fileName)
            random.seed(QtGui.QCursor.pos().x() ^ QtGui.QCursor.pos().y())

            for i in range(self.piecesList.count()):
                if random.random() < 0.5:
                    item = self.piecesList.takeItem(i)
                    self.piecesList.insertItem(0, item)

                                      
    def OpenChoose(self, pixmap, item):
        self.imageLabel.setPixmap(pixmap)
        self.printAct.setEnabled(True)
        self.fitToWindowAct.setEnabled(True)
        self.updateActions()
        self.item = item  
        if not self.fitToWindowAct.isChecked():
            self.imageLabel.adjustSize()
            #getting image and convert to ipl image
            qimage =  self.imageLabel.pixmap()
            pil_img = self.qimage2pil(qimage)
            ipl_img = adaptors.PIL2Ipl(pil_img)
            self.width = ipl_img.width
            self.height = ipl_img.height
            if(self.width > self.height):
                width = 500
                diff = (self.width*100)/500
                height = int((self.height*100)/diff)

            else:
                height = 400
                diff = (self.height*100)/400
                width = int((self.width*100)/diff)
              
            newImage = cvCreateImage(cvSize(width, height), ipl_img.depth, ipl_img.nChannels);
            cvResize(ipl_img, newImage, CV_INTER_LINEAR);
            # convert to qimage and display
            pil_img2 = adaptors.Ipl2PIL(newImage)
            self.pix = self.pil2qimage(pil_img2)
            self.imageLabel.setPixmap(self.pix)
            self.normalSize()
          
           
    def saveAs(self):
        qimage =  self.imageLabel.pixmap()
        pil_img = self.qimage2pil(qimage)
        fileName = QtGui.QFileDialog.getSaveFileName(self)
        if fileName.isEmpty():
            return False
        return pil_img.save(str(fileName), 'jpeg')
  
    def print_(self):
        dialog = QtGui.QPrintDialog(self.printer, self)
        if dialog.exec_():
            painter = QtGui.QPainter()
            painter.begin(self.printer)
            rect = painter.viewport()
            size = self.imageLabel.pixmap().size()
            size.scale(rect.size(), QtCore.Qt.KeepAspectRatio)
            painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
            painter.setWindow(self.imageLabel.pixmap().rect())
            painter.drawPixmap(0, 0, self.imageLabel.pixmap())

    def zoomIn(self):
        self.scaleImage(1.25)
        # delete rubber rect
        self.deleterubber()   

    def zoomOut(self):
        self.scaleImage(0.8)
        # delete rubber rect
        self.deleterubber()   
  
    def createcall(self):
        self.ctrlList.createimg()
  
    def cropcall(self):
        self.ctrlList.cropimg()
      
    def uncropcall(self):
        self.ctrlList.uncropimg()
      
    def cropimg(self):
        filename = str(self.foldername) + '/.INI'
        data3 = str(self.name) + "," + str(self.x1) + "," + str(self.y1 - 27) + ","
        data2 = data3 + str(self.x2) + "," + str(self.y2 - 27) + "," + str(self.rsz_width)
        data = data2 + "," + str(self.rsz_height)
       
        if (self.datacheck == 0):
            if os.path.exists(filename):
                os.remove(filename)
            file = open(filename, "w")
            if (data != self.databk):
                file.write(data + '\n')
        else:
            file = open(filename, "r+")
            while 1:
                line = file.readline()
                if not line:
                    break
                #print line
            if (data != self.databk):
                file.write(data + '\n')
        self.datacheck = self.datacheck + 1
      
        #getting image and convert to ipl image
        qimage =  self.imageLabel.pixmap()
        pil_img = self.qimage2pil(qimage)
        ipl_img = adaptors.PIL2Ipl(pil_img)
        x1 = self.x1
        y1 = self.y1 - 27
        x2 = self.x2
        y2 = self.y2 - 27
        # crop image
        new_width = x2 - x1
        new_height = y2 - y1
        cropped = cvCreateImage( cvSize(new_width, new_height), 8, 3)
        src_region = cvGetSubRect(ipl_img, cvRect(x1, y1, new_width, new_height) )
        cvCopy(src_region, cropped)
        self.ctrlList.openproc(cropped)
        self.piecesList.checklist(self.item)
        self.databk = data
      
        self.deleterubber()
      
    def uncropimg(self):
        filename = str(self.foldername) + '/.INI'
        new = []
        file = open(filename, "r+")
        while 1:
            line = file.readline()
            if not line:
                break
            word = line.split(",")
            if (word[0] == str(self.name)):
                continue
            else: 
                new.append(line)
        file.close()
        file = open(filename, "w")
        file.writelines(new)
        file.close()
        self.piecesList.unchecklist(self.item) 
     
    def createimg(self):
        file = open(str(self.foldername) + '/.INI')
        if not os.path.exists(str(self.foldername) + "/processed_images"):
            command = 'mkdir ' + str(self.foldername) + '/processed_images'
            os.system(command)
      
        while 1:
            filename = str(self.foldername) + "/processed_images/" + str(self.count) + ".jpeg"
            line = file.readline()
            if not line:
                break
            word = line.split(",");
            name = str(word[0])
            x1 = int(word[1])
            y1 = int(word[2])
            x2 = int(word[3])
            y2 = int(word[4])
            rsz_x = int(word[5])
            rsz_y = int(word[6])
      
      
            ipl_img = cvLoadImage(name)
            img_width = ipl_img.width
            img_height = ipl_img.height
            if(img_width > img_height):
                width = 500
                diff = (img_width*100)/500
                height = int((img_height*100)/diff)

            else:
                height = 400
                diff = (img_height*100)/400
                width = int((img_width*100)/diff)
          
            rszImage = cvCreateImage(cvSize(width, height), ipl_img.depth, ipl_img.nChannels);
            cvResize(ipl_img, rszImage, CV_INTER_LINEAR);   
          
            # crop image
            new_width = x2 - x1
            new_height = y2 - y1
            cropped = cvCreateImage( cvSize(new_width, new_height), 8, 3)
            src_region = cvGetSubRect(rszImage, cvRect(x1, y1, new_width, new_height) )
            cvCopy(src_region, cropped)

            newImage = cvCreateImage(cvSize(rsz_x, rsz_y), cropped.depth, cropped.nChannels);
            cvResize(cropped, newImage, CV_INTER_LINEAR);
      
            gimage = cvCreateImage( cvGetSize(newImage), 8, 1 );
            cvCvtColor(newImage,gimage,CV_BGR2GRAY)
            cvSaveImage(filename, gimage)
            self.count = self.count + 1
        self.descFile()
      
    def descFile(self):
        x = 0
        filename = str(self.foldername) + '/description.dat'
        if os.path.exists(filename):
            os.remove(filename)
        file = open(filename, "w")
      
        for x in range(0, 10000):
            filename2 = str(self.foldername) + "/processed_images/" + str(x) + ".jpeg"
            if os.path.exists(filename2):
                file.write("processed_images/" + str(x) + ".jpeg 1 0 0 " + str(self.rsz_width) + " " + str(self.rsz_height) + "\n")
            x = x + 1

        QtGui.QMessageBox.information(self,
                                "Ready!",
                                "Images succesfully processed !")

      
    def vector(self, neg_foldername, nstages, nsplits, minhitrate, maxfalsealarm,
               weighttrimming, memory, symmetry, mode, error, boost):
      
        command = 'cd /usr/local/bin/'
        os.system(command)
        command2 = 'opencv-createsamples -info ' + str(self.foldername) + '/description.dat -vec '
        command3 = command2 + str(self.foldername) + '/samples.vec -w '  + str(self.rsz_width) + ' -h ' + str(self.rsz_height)
        print command3
        os.system(command3)

      
        QtGui.QMessageBox.information(self,
                                "Ready!",
                                "Vector file succesfully created !")
      
        file = open(neg_foldername, "r+")
        nneg = 0
        npos = 0
        data = str(self.foldername) + "/haarcascade"
        vec = str(self.foldername) + "/samples.vec"
        w = self.rsz_width
        h = self.rsz_height
        while 1:
            line = file.readline()
            if not line:
                break
            nneg = nneg + 1
        x = 0
        for x in range(0, 10000):
            filename2 = str(self.foldername) + "/processed_images/" + str(x) + ".jpeg"
            if os.path.exists(filename2):
                npos = npos + 1
            x = x + 1  
          
        if os.path.exists(str(self.foldername) + '/description.dat'):  
            os.remove(str(self.foldername) + '/description.dat')
      
        direct = str(self.foldername) + "/processed_images"
        if os.path.exists(direct):
            if not QtCore.QString(direct).isEmpty():
                for file in self.index(direct):
                    os.remove (file)
          
            os.system (' rmdir ' + str(self.foldername) + '/processed_images')
     
        command = 'cd /usr/local/bin/'
        os.system(command)
        command2 = 'opencv-haartraining -data ' + data + ' -vec ' + vec + ' -bg ' + str(neg_foldername)
        command3 = command2 + ' -nstages '  + nstages + ' -nsplits ' + nsplits
        command4 = command3 + ' -minhitrate ' + minhitrate + ' -maxfalsealarm ' + maxfalsealarm
        command5 = command4 + ' -weighttrimming ' + weighttrimming + ' -npos ' + str(npos)
        command6 = command5 + ' -nneg ' + str(nneg) + ' -w ' + str(w) + ' -h ' + str(h) + ' -' + symmetry
        command7 = command6 + ' -mem ' + memory + ' -mode ' + mode + ' -bt ' + boost
        if (error != 'None'):
            command7 = command7 + ' -err ' + error
        print command7
        os.system(command7)     
      
  
    def detectimg(self, xml):
        qimage =  self.imageLabel.pixmap()
        pil_img = self.qimage2pil(qimage)
        ipl_img = adaptors.PIL2Ipl(pil_img)
        storage = cvCreateMemStorage(0)
        cvClearMemStorage(storage)
        cascade = cvLoadHaarClassifierCascade(xml, cvSize(1,1))
        positive_image = cvHaarDetectObjects(ipl_img, cascade, storage, 1.2, 2, 0, cvSize(48, 16))
        if( cascade ):
            if positive_image:
                for pos_rect in positive_image:
                    # the input to cvHaarDetectObjects was resized, so scale the
                    # bounding box of each face and convert it to two CvPoints
                    pt1 = cvPoint( int(pos_rect.x), int(pos_rect.y))
                    pt2 = cvPoint( int((pos_rect.x+pos_rect.width)),
                                   int((pos_rect.y+pos_rect.height)) )
                    cvRectangle(ipl_img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0)
      
                    
        pil_img2 = adaptors.Ipl2PIL(ipl_img)
        self.pix = self.pil2qimage(pil_img2)
        self.imageLabel.setPixmap(self.pix)
          
    def normalSize(self):
        self.imageLabel.adjustSize()
        self.scaleFactor = 1.0
        # delete rubber rect
        self.deleterubber()   

    def fitToWindow(self):
        fitToWindow = self.fitToWindowAct.isChecked()
        self.scrollArea.setWidgetResizable(fitToWindow)
        if not fitToWindow:
            self.imageLabel.adjustSize()
        self.updateActions()
        self.deleterubber()   

    def deleterubber(self):
        self.rbOrigin = QtCore.QPoint(0, 0)
        self.rubberBand.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand2.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand3.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand4.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand5.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand6.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand7.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand8.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand9.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand10.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand11.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand12.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand13.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand14.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand15.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand16.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand17.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand18.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand19.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand20.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand21.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand22.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand23.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand24.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand25.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand26.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand27.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand28.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand29.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand30.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand31.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
  
    def setPref(self):
      
        dialog = PrefDlg(self)
        if dialog.exec_():
            self.rsz_width =  dialog.label.text()
            self.rsz_height =  dialog.label2.text()
            self.ratio_x = int(dialog.formatComboBox.currentIndex())
            self.ratio_y = int(dialog.formatComboBox2.currentIndex())
          
  
    def about(self):
        QtGui.QMessageBox.about(self, self.tr("About Image Viewer"), self.tr(
              "<p>The <b>Image Viewer</b> is a program to list and display "
              "imported images. It also provides some basic and fixed image  "
              "processing functions such as zoom in, zoom out, cropping and etc. "
              "</p><p>created by: -KALAI-"))
      
    def createActions(self):
      
        self.openAct = QtGui.QAction(self.tr("&Open..."), self)
        self.openAct.setShortcut(self.tr("Ctrl+O"))
        self.connect(self.openAct, QtCore.SIGNAL("triggered()"), self.openImage)
      
        self.openfolderAct = QtGui.QAction(self.tr("&Import Folder"), self)
        self.openfolderAct.setShortcut(self.tr("Ctrl+I"))
        self.connect(self.openfolderAct, QtCore.SIGNAL("triggered()"), self.openfolder)
      
        self.saveimgAct = QtGui.QAction(self.tr("&Save"), self)
        self.saveimgAct.setShortcut(self.tr("Ctrl+S"))
        self.connect(self.saveimgAct, QtCore.SIGNAL("triggered()"), self.saveAs)

        self.printAct = QtGui.QAction(self.tr("&Print..."), self)
        self.printAct.setShortcut(self.tr("Ctrl+P"))
        self.printAct.setEnabled(False)
        self.connect(self.printAct, QtCore.SIGNAL("triggered()"), self.print_)

        self.exitAct = QtGui.QAction(self.tr("E&xit"), self)
        self.exitAct.setShortcut(self.tr("Ctrl+Q"))
        self.connect(self.exitAct, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("close()"))

        self.zoomInAct = QtGui.QAction(self.tr("Zoom &In (25%)"), self)
        self.zoomInAct.setShortcut(self.tr("Ctrl++"))
        self.zoomInAct.setEnabled(False)
        self.connect(self.zoomInAct, QtCore.SIGNAL("triggered()"), self.zoomIn)

        self.zoomOutAct = QtGui.QAction(self.tr("Zoom &Out (25%)"), self)
        self.zoomOutAct.setShortcut(self.tr("Ctrl+-"))
        self.zoomOutAct.setEnabled(False)
        self.connect(self.zoomOutAct, QtCore.SIGNAL("triggered()"), self.zoomOut)
      
        self.cropImgAct = QtGui.QAction(self.tr("&Crop"), self)
        self.cropImgAct.setShortcut(self.tr("Ctrl+C"))
        self.cropImgAct.setEnabled(False)
        self.connect(self.cropImgAct, QtCore.SIGNAL("triggered()"), self.cropcall)
      
        self.uncropImgAct = QtGui.QAction(self.tr("Uncrop"), self)
        self.uncropImgAct.setEnabled(False)
        self.connect(self.uncropImgAct, QtCore.SIGNAL("triggered()"), self.uncropcall)
      
        self.createImgAct = QtGui.QAction(self.tr("Process"), self)
        self.createImgAct.setEnabled(False)
        self.connect(self.createImgAct, QtCore.SIGNAL("triggered()"), self.createcall)
      
        self.normalSizeAct = QtGui.QAction(self.tr("&Normal Size"), self)
        self.normalSizeAct.setShortcut(self.tr("Ctrl+N"))
        self.normalSizeAct.setEnabled(False)
        self.connect(self.normalSizeAct, QtCore.SIGNAL("triggered()"), self.normalSize)

        self.fitToWindowAct = QtGui.QAction(self.tr("&Fit to Window"), self)
        self.fitToWindowAct.setEnabled(False)
        self.fitToWindowAct.setCheckable(True)
        self.fitToWindowAct.setShortcut(self.tr("Ctrl+F"))
        self.connect(self.fitToWindowAct, QtCore.SIGNAL("triggered()"), self.fitToWindow)
      
        self.Preferences = QtGui.QAction(self.tr("Preferences"), self)
        self.connect(self.Preferences, QtCore.SIGNAL("triggered()"), self.setPref)

        self.aboutAct = QtGui.QAction(self.tr("&About"), self)
        self.connect(self.aboutAct, QtCore.SIGNAL("triggered()"), self.about)

        self.aboutQtAct = QtGui.QAction(self.tr("About &Qt"), self)
        self.connect(self.aboutQtAct, QtCore.SIGNAL("triggered()"), QtGui.qApp, QtCore.SLOT("aboutQt()"))

    def createMenus(self):
        self.fileMenu = QtGui.QMenu(self.tr("&File"), self)
        self.fileMenu.addAction(self.openAct)
        self.fileMenu.addAction(self.openfolderAct)
        self.fileMenu.addAction(self.saveimgAct)
        self.fileMenu.addAction(self.printAct)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.exitAct)

        self.viewMenu = QtGui.QMenu(self.tr("&View"), self)
        self.viewMenu.addAction(self.zoomInAct)
        self.viewMenu.addAction(self.zoomOutAct)
        self.viewMenu.addAction(self.cropImgAct)
        self.viewMenu.addAction(self.uncropImgAct)
        self.viewMenu.addAction(self.createImgAct)
        self.viewMenu.addAction(self.normalSizeAct)
        self.viewMenu.addSeparator()
        self.viewMenu.addAction(self.fitToWindowAct)
      
        self.OptionMenu = QtGui.QMenu(self.tr("&Option"), self)
        self.OptionMenu.addAction(self.Preferences)

        self.helpMenu = QtGui.QMenu(self.tr("&Help"), self)
        self.helpMenu.addAction(self.aboutAct)
        self.helpMenu.addAction(self.aboutQtAct)

        self.menuBar().addMenu(self.fileMenu)
        self.menuBar().addMenu(self.viewMenu)
        self.menuBar().addMenu(self.OptionMenu)
        self.menuBar().addMenu(self.helpMenu)
      

    def updateActions(self):
        self.zoomInAct.setEnabled(not self.fitToWindowAct.isChecked())
        self.zoomOutAct.setEnabled(not self.fitToWindowAct.isChecked())
        self.cropImgAct.setEnabled(not self.fitToWindowAct.isChecked())
        self.uncropImgAct.setEnabled(not self.fitToWindowAct.isChecked())
        self.createImgAct.setEnabled(not self.fitToWindowAct.isChecked())
        self.normalSizeAct.setEnabled(not self.fitToWindowAct.isChecked())

    def scaleImage(self, factor):
        self.scaleFactor *= factor
        self.imageLabel.resize(self.scaleFactor * self.imageLabel.pixmap().size())
      
        self.zoomInAct.setEnabled(self.scaleFactor < 3.0)
        self.zoomOutAct.setEnabled(self.scaleFactor > 0.333)



    def mousePressEvent(self, event):
        self.x1 = int(QtCore.QPoint.x(event.pos()))
        self.y1 = int(QtCore.QPoint.y(event.pos()))
        print self.x1
        print self.y1
        self.rbOrigin = QtCore.QPoint(QtCore.QPoint.x(event.pos()), QtCore.QPoint.y(event.pos()))
        self.rubberBand.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand.show()
        self.rubberBand2.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand2.show()
        self.rubberBand3.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand3.show()
        self.rubberBand4.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand4.show()
        self.rubberBand5.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand5.show()
        self.rubberBand6.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand6.show()
        self.rubberBand7.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand7.show()
        self.rubberBand8.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand8.show()
        self.rubberBand9.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand9.show()
        self.rubberBand10.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand10.show()
        self.rubberBand11.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand11.show()
        self.rubberBand12.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand12.show()
        self.rubberBand13.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand13.show()
        self.rubberBand14.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand14.show()
        self.rubberBand15.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand15.show()
        self.rubberBand16.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand16.show()
        self.rubberBand17.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand17.show()
        self.rubberBand18.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand18.show()
        self.rubberBand19.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand19.show()
        self.rubberBand20.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand20.show()
        self.rubberBand21.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand21.show()
        self.rubberBand22.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand22.show()
        self.rubberBand23.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand23.show()
        self.rubberBand24.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand24.show()
        self.rubberBand25.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand25.show()
        self.rubberBand26.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand26.show()
        self.rubberBand27.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand27.show()
        self.rubberBand28.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand28.show()
        self.rubberBand29.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand29.show()
        self.rubberBand30.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand30.show()
        self.rubberBand31.setGeometry(QtCore.QRect(self.rbOrigin, QtCore.QSize()))
        self.rubberBand31.show()
      

    def mouseMoveEvent ( self, event ) :
      
        if self.ratio_x == 0:
            self.aratio_x = 1
        elif self.ratio_x == 1:
            self.aratio_x = 2
        elif self.ratio_x == 2:
            self.aratio_x = 3
        elif self.ratio_x == 3:
            self.aratio_x = 4
        elif self.ratio_x == 4:
            self.aratio_x = 6
        elif self.ratio_x == 5:
            self.aratio_x = 8
        elif self.ratio_x == 6:
            self.aratio_x = 9
        elif self.ratio_x == 7:
            self.aratio_x = 12
        elif self.ratio_x == 8:
            self.aratio_x = 16
          
        if self.ratio_y == 0:
            self.aratio_y = 1
        elif self.ratio_y== 1:
            self.aratio_y = 2
        elif self.ratio_y == 2:
            self.aratio_y = 3
        elif self.ratio_y == 3:
            self.aratio_y = 4
        elif self.ratio_y == 4:
            self.aratio_y = 6
        elif self.ratio_y == 5:
            self.aratio_y = 8
        elif self.ratio_y == 6:
            self.aratio_y = 9
        elif self.ratio_y == 7:
            self.aratio_y = 12
        elif self.ratio_y == 8:
            self.aratio_y = 16
      
        self.aratio = float(self.aratio_x)/float(self.aratio_y)

          
        self.x2 = int(QtCore.QPoint.x(event.pos()))
        self.y2 = int(QtCore.QPoint.y(event.pos()))
        self.x = self.x2 - self.x1
        self.y = self.y2 - self.y1
      
        if (self.x == 0):
            self.x = self.aratio*self.y
        else:
            self.y = float(self.x) /float(self.aratio)
        self.x2 = int(self.x + self.x1)
        self.y2 = int(self.y + self.y1)
      
        self.rubberBand.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, self.y1), QtCore.QPoint(self.x2, self.y2)).normalized())
      
        if(self.aratio_x == 2):
            point_x = int((self.x/2) + self.x1)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
          
        if(self.aratio_x == 3):
            point_x = int((self.x/3) + self.x1)
            point2_x = int((self.x/3) + point_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
       
        if(self.aratio_x == 4):
            point_x = int((self.x/4) + self.x1)
            point2_x = int((self.x/4) + point_x)
            point3_x = int((self.x/4) + point2_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
            self.rubberBand4.setGeometry(QtCore.QRect(QtCore.QPoint(point3_x, self.y1), QtCore.QPoint(point3_x, self.y2)).normalized())
        
        if(self.aratio_x == 6):
            point_x = int((self.x/6) + self.x1)
            point2_x = int((self.x/6) + point_x)
            point3_x = int((self.x/6) + point2_x)
            point4_x = int((self.x/6) + point3_x)
            point5_x = int((self.x/6) + point4_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
            self.rubberBand4.setGeometry(QtCore.QRect(QtCore.QPoint(point3_x, self.y1), QtCore.QPoint(point3_x, self.y2)).normalized())
            self.rubberBand5.setGeometry(QtCore.QRect(QtCore.QPoint(point4_x, self.y1), QtCore.QPoint(point4_x, self.y2)).normalized())
            self.rubberBand6.setGeometry(QtCore.QRect(QtCore.QPoint(point5_x, self.y1), QtCore.QPoint(point5_x, self.y2)).normalized())
       
        if(self.aratio_x == 8):
            point_x = int((self.x/8) + self.x1)
            point2_x = int((self.x/8) + point_x)
            point3_x = int((self.x/8) + point2_x)
            point4_x = int((self.x/8) + point3_x)
            point5_x = int((self.x/8) + point4_x)
            point6_x = int((self.x/8) + point5_x)
            point7_x = int((self.x/8) + point6_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
            self.rubberBand4.setGeometry(QtCore.QRect(QtCore.QPoint(point3_x, self.y1), QtCore.QPoint(point3_x, self.y2)).normalized())
            self.rubberBand5.setGeometry(QtCore.QRect(QtCore.QPoint(point4_x, self.y1), QtCore.QPoint(point4_x, self.y2)).normalized())
            self.rubberBand6.setGeometry(QtCore.QRect(QtCore.QPoint(point5_x, self.y1), QtCore.QPoint(point5_x, self.y2)).normalized())
            self.rubberBand7.setGeometry(QtCore.QRect(QtCore.QPoint(point6_x, self.y1), QtCore.QPoint(point6_x, self.y2)).normalized())
            self.rubberBand8.setGeometry(QtCore.QRect(QtCore.QPoint(point7_x, self.y1), QtCore.QPoint(point7_x, self.y2)).normalized())
              
        if(self.aratio_x == 9):
            point_x = int((self.x/9) + self.x1)
            point2_x = int((self.x/9) + point_x)
            point3_x = int((self.x/9) + point2_x)
            point4_x = int((self.x/9) + point3_x)
            point5_x = int((self.x/9) + point4_x)
            point6_x = int((self.x/9) + point5_x)
            point7_x = int((self.x/9) + point6_x)
            point8_x = int((self.x/9) + point7_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
            self.rubberBand4.setGeometry(QtCore.QRect(QtCore.QPoint(point3_x, self.y1), QtCore.QPoint(point3_x, self.y2)).normalized())
            self.rubberBand5.setGeometry(QtCore.QRect(QtCore.QPoint(point4_x, self.y1), QtCore.QPoint(point4_x, self.y2)).normalized())
            self.rubberBand6.setGeometry(QtCore.QRect(QtCore.QPoint(point5_x, self.y1), QtCore.QPoint(point5_x, self.y2)).normalized())
            self.rubberBand7.setGeometry(QtCore.QRect(QtCore.QPoint(point6_x, self.y1), QtCore.QPoint(point6_x, self.y2)).normalized())
            self.rubberBand8.setGeometry(QtCore.QRect(QtCore.QPoint(point7_x, self.y1), QtCore.QPoint(point7_x, self.y2)).normalized())
            self.rubberBand9.setGeometry(QtCore.QRect(QtCore.QPoint(point8_x, self.y1), QtCore.QPoint(point8_x, self.y2)).normalized())
              
        if(self.aratio_x == 12):
            point_x = int((self.x/12) + self.x1)
            point2_x = int((self.x/12) + point_x)
            point3_x = int((self.x/12) + point2_x)
            point4_x = int((self.x/12) + point3_x)
            point5_x = int((self.x/12) + point4_x)
            point6_x = int((self.x/12) + point5_x)
            point7_x = int((self.x/12) + point6_x)
            point8_x = int((self.x/12) + point7_x)
            point9_x = int((self.x/12) + point8_x)
            point10_x = int((self.x/12) + point9_x)
            point11_x = int((self.x/12) + point10_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
            self.rubberBand4.setGeometry(QtCore.QRect(QtCore.QPoint(point3_x, self.y1), QtCore.QPoint(point3_x, self.y2)).normalized())
            self.rubberBand5.setGeometry(QtCore.QRect(QtCore.QPoint(point4_x, self.y1), QtCore.QPoint(point4_x, self.y2)).normalized())
            self.rubberBand6.setGeometry(QtCore.QRect(QtCore.QPoint(point5_x, self.y1), QtCore.QPoint(point5_x, self.y2)).normalized())
            self.rubberBand7.setGeometry(QtCore.QRect(QtCore.QPoint(point6_x, self.y1), QtCore.QPoint(point6_x, self.y2)).normalized())
            self.rubberBand8.setGeometry(QtCore.QRect(QtCore.QPoint(point7_x, self.y1), QtCore.QPoint(point7_x, self.y2)).normalized())
            self.rubberBand9.setGeometry(QtCore.QRect(QtCore.QPoint(point8_x, self.y1), QtCore.QPoint(point8_x, self.y2)).normalized())
            self.rubberBand10.setGeometry(QtCore.QRect(QtCore.QPoint(point9_x, self.y1), QtCore.QPoint(point9_x, self.y2)).normalized())
            self.rubberBand11.setGeometry(QtCore.QRect(QtCore.QPoint(point10_x, self.y1), QtCore.QPoint(point10_x, self.y2)).normalized())
            self.rubberBand12.setGeometry(QtCore.QRect(QtCore.QPoint(point11_x, self.y1), QtCore.QPoint(point11_x, self.y2)).normalized())
        
        if(self.aratio_x == 16):
            point_x = int((self.x/16) + self.x1)
            point2_x = int((self.x/16) + point_x)
            point3_x = int((self.x/16) + point2_x)
            point4_x = int((self.x/16) + point3_x)
            point5_x = int((self.x/16) + point4_x)
            point6_x = int((self.x/16) + point5_x)
            point7_x = int((self.x/16) + point6_x)
            point8_x = int((self.x/16) + point7_x)
            point9_x = int((self.x/16) + point8_x)
            point10_x = int((self.x/16) + point9_x)
            point11_x = int((self.x/16) + point10_x)
            point12_x = int((self.x/16) + point11_x)
            point13_x = int((self.x/16) + point12_x)
            point14_x = int((self.x/16) + point13_x)
            point15_x = int((self.x/16) + point14_x)
            self.rubberBand2.setGeometry(QtCore.QRect(QtCore.QPoint(point_x, self.y1), QtCore.QPoint(point_x, self.y2)).normalized())
            self.rubberBand3.setGeometry(QtCore.QRect(QtCore.QPoint(point2_x, self.y1), QtCore.QPoint(point2_x, self.y2)).normalized())
            self.rubberBand4.setGeometry(QtCore.QRect(QtCore.QPoint(point3_x, self.y1), QtCore.QPoint(point3_x, self.y2)).normalized())
            self.rubberBand5.setGeometry(QtCore.QRect(QtCore.QPoint(point4_x, self.y1), QtCore.QPoint(point4_x, self.y2)).normalized())
            self.rubberBand6.setGeometry(QtCore.QRect(QtCore.QPoint(point5_x, self.y1), QtCore.QPoint(point5_x, self.y2)).normalized())
            self.rubberBand7.setGeometry(QtCore.QRect(QtCore.QPoint(point6_x, self.y1), QtCore.QPoint(point6_x, self.y2)).normalized())
            self.rubberBand8.setGeometry(QtCore.QRect(QtCore.QPoint(point7_x, self.y1), QtCore.QPoint(point7_x, self.y2)).normalized())
            self.rubberBand9.setGeometry(QtCore.QRect(QtCore.QPoint(point8_x, self.y1), QtCore.QPoint(point8_x, self.y2)).normalized())
            self.rubberBand10.setGeometry(QtCore.QRect(QtCore.QPoint(point9_x, self.y1), QtCore.QPoint(point9_x, self.y2)).normalized())
            self.rubberBand11.setGeometry(QtCore.QRect(QtCore.QPoint(point10_x, self.y1), QtCore.QPoint(point10_x, self.y2)).normalized())
            self.rubberBand12.setGeometry(QtCore.QRect(QtCore.QPoint(point11_x, self.y1), QtCore.QPoint(point11_x, self.y2)).normalized())
            self.rubberBand13.setGeometry(QtCore.QRect(QtCore.QPoint(point12_x, self.y1), QtCore.QPoint(point12_x, self.y2)).normalized())
            self.rubberBand14.setGeometry(QtCore.QRect(QtCore.QPoint(point13_x, self.y1), QtCore.QPoint(point13_x, self.y2)).normalized())
            self.rubberBand15.setGeometry(QtCore.QRect(QtCore.QPoint(point14_x, self.y1), QtCore.QPoint(point14_x, self.y2)).normalized())
            self.rubberBand16.setGeometry(QtCore.QRect(QtCore.QPoint(point15_x, self.y1), QtCore.QPoint(point15_x, self.y2)).normalized())


        if(self.aratio_y == 2):
            point_y = int((self.y/2) + self.y1)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
          
        if(self.aratio_y == 3):
            point_y = int((self.y/3) + self.y1)
            point2_y = int((self.y/3) + point_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
       
        if(self.aratio_y == 4):
            point_y = int((self.y/4) + self.y1)
            point2_y = int((self.y/4) + point_y)
            point3_y = int((self.y/4) + point2_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
            self.rubberBand19.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point3_y), QtCore.QPoint(self.x2, point3_y)).normalized())
      

        if(self.aratio_y == 6):
            point_y = int((self.y/6) + self.y1)
            point2_y = int((self.y/6) + point_y)
            point3_y = int((self.y/6) + point2_y)
            point4_y = int((self.y/6) + point3_y)
            point5_y = int((self.y/6) + point4_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
            self.rubberBand19.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point3_y), QtCore.QPoint(self.x2, point3_y)).normalized())
            self.rubberBand20.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point4_y), QtCore.QPoint(self.x2, point4_y)).normalized())
            self.rubberBand21.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point5_y), QtCore.QPoint(self.x2, point5_y)).normalized())
 

        if(self.aratio_y == 8):
            point_y = int((self.y/8) + self.y1)
            point2_y = int((self.y/8) + point_y)
            point3_y = int((self.y/8) + point2_y)
            point4_y = int((self.y/8) + point3_y)
            point5_y = int((self.y/8) + point4_y)
            point6_y = int((self.y/8) + point5_y)
            point7_y = int((self.y/8) + point6_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
            self.rubberBand19.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point3_y), QtCore.QPoint(self.x2, point3_y)).normalized())
            self.rubberBand20.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point4_y), QtCore.QPoint(self.x2, point4_y)).normalized())
            self.rubberBand21.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point5_y), QtCore.QPoint(self.x2, point5_y)).normalized())
            self.rubberBand22.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point6_y), QtCore.QPoint(self.x2, point6_y)).normalized())
            self.rubberBand23.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point7_y), QtCore.QPoint(self.x2, point7_y)).normalized())
          
 
        if(self.aratio_y == 9):
            point_y = int((self.y/9) + self.y1)
            point2_y = int((self.y/9) + point_y)
            point3_y = int((self.y/9) + point2_y)
            point4_y = int((self.y/9) + point3_y)
            point5_y = int((self.y/9) + point4_y)
            point6_y = int((self.y/9) + point5_y)
            point7_y = int((self.y/9) + point6_y)
            point8_y = int((self.y/9) + point7_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
            self.rubberBand19.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point3_y), QtCore.QPoint(self.x2, point3_y)).normalized())
            self.rubberBand20.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point4_y), QtCore.QPoint(self.x2, point4_y)).normalized())
            self.rubberBand21.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point5_y), QtCore.QPoint(self.x2, point5_y)).normalized())
            self.rubberBand22.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point6_y), QtCore.QPoint(self.x2, point6_y)).normalized())
            self.rubberBand23.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point7_y), QtCore.QPoint(self.x2, point7_y)).normalized())
            self.rubberBand24.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point8_y), QtCore.QPoint(self.x2, point8_y)).normalized())
        
  
        if(self.aratio_y == 12):
            point_y = int((self.y/12) + self.y1)
            point2_y = int((self.y/12) + point_y)
            point3_y = int((self.y/12) + point2_y)
            point4_y = int((self.y/12) + point3_y)
            point5_y = int((self.y/12) + point4_y)
            point6_y = int((self.y/12) + point5_y)
            point7_y = int((self.y/12) + point6_y)
            point8_y = int((self.y/12) + point7_y)
            point9_y = int((self.y/12) + point8_y)
            point10_y = int((self.y/12) + point9_y)
            point11_y = int((self.y/12) + point10_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
            self.rubberBand19.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point3_y), QtCore.QPoint(self.x2, point3_y)).normalized())
            self.rubberBand20.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point4_y), QtCore.QPoint(self.x2, point4_y)).normalized())
            self.rubberBand21.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point5_y), QtCore.QPoint(self.x2, point5_y)).normalized())
            self.rubberBand22.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point6_y), QtCore.QPoint(self.x2, point6_y)).normalized())
            self.rubberBand23.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point7_y), QtCore.QPoint(self.x2, point7_y)).normalized())
            self.rubberBand24.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point8_y), QtCore.QPoint(self.x2, point8_y)).normalized())
            self.rubberBand25.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point9_y), QtCore.QPoint(self.x2, point9_y)).normalized())
            self.rubberBand26.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point10_y), QtCore.QPoint(self.x2, point10_y)).normalized())
            self.rubberBand27.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point11_y), QtCore.QPoint(self.x2, point11_y)).normalized())


        if(self.aratio_y == 16):
            point_y = int((self.y/16) + self.y1)
            point2_y = int((self.y/16) + point_y)
            point3_y = int((self.y/16) + point2_y)
            point4_y = int((self.y/16) + point3_y)
            point5_y = int((self.y/16) + point4_y)
            point6_y = int((self.y/16) + point5_y)
            point7_y = int((self.y/16) + point6_y)
            point8_y = int((self.y/16) + point7_y)
            point9_y = int((self.y/16) + point8_y)
            point10_y = int((self.y/16) + point9_y)
            point11_y = int((self.y/16) + point10_y)
            point12_y = int((self.y/16) + point11_y)
            point13_y = int((self.y/16) + point12_y)
            point14_y = int((self.y/16) + point13_y)
            point15_y = int((self.y/16) + point14_y)
            self.rubberBand17.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point_y), QtCore.QPoint(self.x2, point_y)).normalized())
            self.rubberBand18.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point2_y), QtCore.QPoint(self.x2, point2_y)).normalized())
            self.rubberBand19.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point3_y), QtCore.QPoint(self.x2, point3_y)).normalized())
            self.rubberBand20.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point4_y), QtCore.QPoint(self.x2, point4_y)).normalized())
            self.rubberBand21.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point5_y), QtCore.QPoint(self.x2, point5_y)).normalized())
            self.rubberBand22.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point6_y), QtCore.QPoint(self.x2, point6_y)).normalized())
            self.rubberBand23.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point7_y), QtCore.QPoint(self.x2, point7_y)).normalized())
            self.rubberBand24.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point8_y), QtCore.QPoint(self.x2, point8_y)).normalized())
            self.rubberBand25.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point9_y), QtCore.QPoint(self.x2, point9_y)).normalized())
            self.rubberBand26.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point10_y), QtCore.QPoint(self.x2, point10_y)).normalized())
            self.rubberBand27.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point11_y), QtCore.QPoint(self.x2, point11_y)).normalized())
            self.rubberBand28.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point12_y), QtCore.QPoint(self.x2, point12_y)).normalized())
            self.rubberBand29.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point13_y), QtCore.QPoint(self.x2, point13_y)).normalized())
            self.rubberBand30.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point14_y), QtCore.QPoint(self.x2, point14_y)).normalized())
            self.rubberBand31.setGeometry(QtCore.QRect(QtCore.QPoint(self.x1, point15_y), QtCore.QPoint(self.x2, point15_y)).normalized())


    def setupWidgets(self):
      
        self.piecesList = PiecesList()
      
        listDockWidget = QtGui.QDockWidget("Image List (*right click to remove "
                                                       "image from the list)", self)
        listDockWidget.setObjectName("LogDockWidget")
        listDockWidget.setAllowedAreas(QtCore.Qt.TopDockWidgetArea|
                              QtCore.Qt.BottomDockWidgetArea)
        listDockWidget.setFeatures (QtGui.QDockWidget.NoDockWidgetFeatures)
        self.listWidget = QtGui.QListWidget()
        listDockWidget.setWidget(self.piecesList)
        self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, listDockWidget)
      
        ctrlDockWidget = QtGui.QDockWidget("", self)
        ctrlDockWidget.setObjectName("ctrlDockWidget")
        ctrlDockWidget.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea|
                              QtCore.Qt.RightDockWidgetArea)
        ctrlDockWidget.setFeatures (QtGui.QDockWidget.NoDockWidgetFeatures)
        self.ctrlList = ctrlList()
        ctrlDockWidget.setWidget(self.ctrlList)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, ctrlDockWidget)
      
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    imageViewer = ImageViewer()
    ctrlList = ctrlList()
  
    imageViewer.show()

    sys.exit(app.exec_())