Package Camelot :: Package camelot :: Package view :: Package controls :: Module liteboxview |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.liteboxview

      1  #  ============================================================================

      2  #

      3  #  Copyright (C) 2007-2010 Conceptive Engineering bvba. All rights reserved.

      4  #  www.conceptive.be / project-camelot@conceptive.be

      5  #

      6  #  This file is part of the Camelot Library.

      7  #

      8  #  This file may be used under the terms of the GNU General Public

      9  #  License version 2.0 as published by the Free Software Foundation

     10  #  and appearing in the file LICENSE.GPL included in the packaging of

     11  #  this file.  Please review the following information to ensure GNU

     12  #  General Public Licensing requirements will be met:

     13  #  http://www.trolltech.com/products/qt/opensource.html

     14  #

     15  #  If you are unsure which license is appropriate for your use, please

     16  #  review the following information:

     17  #  http://www.trolltech.com/products/qt/licensing.html or contact

     18  #  project-camelot@conceptive.be.

     19  #

     20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE

     21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

     22  #

     23  #  For use of this library in commercial applications, please contact

     24  #  project-camelot@conceptive.be

     25  #

     26  #  ============================================================================

     27

     28  from PyQt4.QtGui import (

     29      QPainter,

     30      QGraphicsView,

     31      QGraphicsScene,

     32      QColor, QPixmap,

     33      QGraphicsPixmapItem,

     34  )
     35  from PyQt4.QtCore import Qt
     36

     37  from camelot.view.art import Pixmap
     38

     39

     40 -def get_desktop():
     41      from PyQt4.QtCore import QCoreApplication
     42      return QCoreApplication.instance().desktop()
     43

     44 -def get_desktop_pixmap():
     45      return QPixmap.grabWindow(get_desktop().winId())
     46

     47 -def fit_to_screen(pixmap):
     48      d = get_desktop()
     49      dh = d.height()
     50      dw = d.width()
     51      if dh < pixmap.height() or dw < pixmap.width():
     52          fit = .95
     53          return pixmap.scaled(dw * fit, dh * fit, Qt.KeepAspectRatio)
     54      return pixmap
     55

     56

     57 -class CloseMark(QGraphicsPixmapItem):
     58

     59 -    def __init__(self, pixmap=None, hover_pixmap=None, parent=None):
     60          super(CloseMark, self).__init__(parent)
     61

     62          DEFAULT_PIXMAP = Pixmap('close_mark.png').getQPixmap()
     63          DEFAULT_HOVER_PIXMAP = Pixmap('close_mark_hover.png').getQPixmap()
     64

     65          self._pixmap = pixmap or DEFAULT_PIXMAP
     66          self._hover_pixmap = hover_pixmap or DEFAULT_HOVER_PIXMAP
     67

     68          self.setPixmap(self._pixmap)
     69

     70          # move to top right corner

     71          width = self.pixmap().width()
     72          height = self.pixmap().height()
     73          parent_width = self.parentItem().boundingRect().width()
     74          self.setPos(-width/2 + parent_width, -height/2)
     75

     76          self.setAcceptsHoverEvents(True)
     77          # stays on top of other items

     78          self.setZValue(10)
     79

     80 -    def hoverEnterEvent(self, event):
     81          self.setPixmap(self._hover_pixmap)
     82          self.update()
     83

     84 -    def hoverLeaveEvent(self, event):
     85          self.setPixmap(self._pixmap)
     86          self.update()
     87

     88 -    def mousePressEvent(self, event):
     89          view = self.scene().views()[0]
     90          view.close()
     91

     92

     93 -class LiteBoxView(QGraphicsView):
     94

     95      ALPHA = QColor(0, 0, 0, 192)
     96

     97 -    def __init__(self, parent=None):
     98          super(LiteBoxView, self).__init__(parent)
     99          self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    100          self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    101          self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
    102

    103          self.desktopshot = None
    104

    105          # will propagate to children

    106          self.setRenderHint(QPainter.Antialiasing)
    107          self.setRenderHint(QPainter.TextAntialiasing)
    108

    109          self.scene = QGraphicsScene()
    110          self.setScene(self.scene)
    111

    112 -    def drawBackground(self, painter, rect):
    113          if self.desktopshot is None:
    114              self.desktopshot = get_desktop_pixmap()
    115

    116          painter.drawPixmap(self.mapToScene(0, 0), self.desktopshot)
    117          painter.setBrush(LiteBoxView.ALPHA)
    118          painter.drawRect(rect)
    119

    120 -    def show_fullscreen_svg(self, path):
    121          """:param path: path to an svg file"""
    122          from PyQt4 import QtSvg
    123          item = QtSvg.QGraphicsSvgItem(path)
    124          self.show_fullscreen_item(item)
    125

    126 -    def show_fullscreen_image(self, image):
    127          """:param image: a QImage"""
    128          pixmap = QPixmap.fromImage(image)
    129          item = QGraphicsPixmapItem(pixmap)
    130          self.show_fullscreen_item(item)
    131

    132 -    def show_fullscreen_item(self, item):
    133          """:param item: a QGraphicsItem to be shown fullscreen"""
    134          self.scene.clear()
    135          self.scene.addItem(item)
    136          CloseMark(parent=item)
    137          self.showFullScreen()
    138