#! /usr/bin/env python
"""
stiletto.py

A program that takes an image, cuts it into smaller images based on
user-defined areas, and spits out an HTML page that reconstitutes the original
image.
"""

# This software is distributed according to the terms of the Gnu General
# Public License. See http://www.gnu.org for more information. This software
# comes with no warranty whatsoever, implied or otherwise.


import sys
from qt import *


#------------------------------------------------------------------------------#
class MainWindow(QMainWindow):

    def __init__(self, actions, *args):
        apply(QMainWindow.__init__, (self, ) + args)
        
        self.resize(750, 700)

        # set up the menubar
        self.menu = Menu(actions, self)
        
        # set up the statusbar
        self.status = self.statusBar()

        # set up the central widget
        self.cw = QVBox(self)
        
        # set up the image view
        self.sv = QScrollView(self.cw)
        self.box = QVBox(self.sv.viewport())
        self.sv.addChild(self.box)

        QObject.connect(actions, PYSIGNAL("sigOpen"), self.showImage)

        self.setCentralWidget(self.cw)

    def showImage(self, imgfile):
        self.imgWidget = ImgWidget(imgfile, self.box)
        self.imgWidget.show()
#------------------------------------------------------------------------------#
class Menu(QMenuBar):
    "A class for encapsulating everything associated with the menu bar"

    def __init__(self, actions, *args):
        apply(QMenuBar.__init__, (self, ) + args)

        self.setFrameStyle(QFrame.NoFrame)

        self.filemenu = QPopupMenu(self)
        actions.openAction.addTo(self.filemenu)

        self.insertItem("File", self.filemenu)
#------------------------------------------------------------------------------#
class AppActions(QObject):
    "Contains all actions we'll want available application wide"

    def __init__(self, *args):
        apply(QObject.__init__, (self, ) + args)

        self.openAction = QAction(self)
        self.openAction.setText("Open")
        self.openAction.setMenuText("Open Image File")
        QObject.connect(self.openAction, SIGNAL("activated()"), self.open)

    def open(self):
        self.imgfile = QFileDialog.getOpenFileName(QString.null, "*.*")
        self.emit(PYSIGNAL("sigOpen"), (self.imgfile, ))
#------------------------------------------------------------------------------#
class ImgWidget(QWidget):
    "A widget that displays the image the user is working on"

    def __init__(self, imgfile, *args):
        apply(QWidget.__init__, (self, ) + args)

        self.img = QPixmap()

        if (not self.img.load(imgfile)):
            self.img = None

    def paintEvent(self, ev):
        if (self.img is not None):
            self.p = QPainter(self)
            self.p.drawPixmap(0, 0, self.img)

    def sizeHint(self):
        return QSize(self.img.width(), self.img.height())

    def sizeHint(self):
        return QSize(self.img.width(), self.img.height())

    def sizePolicy(self):
        return QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
#------------------------------------------------------------------------------#
def main(args):
    app = QApplication(args)

    # create the actions; hand em a meaningless QWidget to avoid seg faulting
    actWidget = QWidget(None)
    actions = AppActions(actWidget)

    win = MainWindow(actions)
    app.setMainWidget(win)
    win.show()
    app.exec_loop()
#------------------------------------------------------------------------------#
if __name__ == '__main__':
    main(sys.argv)
