summaryrefslogtreecommitdiffstats
path: root/dimension/preview.py
blob: e4e9c80e7a8d1aec1e35daffd8c40b3e4b9709a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3

#########################################################################
# Copyright (C) 2011-2014 Tavian Barnes <tavianator@tavianator.com>     #
#                                                                       #
# This file is part of Dimension.                                       #
#                                                                       #
# Dimension is free software; you can redistribute it and/or modify it  #
# under the terms of the GNU General Public License as published by the #
# Free Software Foundation; either version 3 of the License, or (at     #
# your option) any later version.                                       #
#                                                                       #
# Dimension is distributed in the hope that it will be useful, but      #
# WITHOUT ANY WARRANTY; without even the implied warranty of            #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     #
# General Public License for more details.                              #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################

from PyQt4 import QtCore, QtGui, QtOpenGL

class Preview(QtOpenGL.QGLWidget):
  """Surface that the scene is rendered to."""
  def __init__(self, parent, canvas, future):
    QtOpenGL.QGLWidget.__init__(self, parent)
    self.canvas = canvas
    self.future = future

  def paintGL(self):
    try:
      self.future.pause()
      self.canvas.draw_GL()
    except:
      self.future.cancel()
      self.parent().close()
      raise
    finally:
      self.future.resume()

class PreviewWindow(QtGui.QMainWindow):
  """Main window for a rendering preview."""
  def __init__(self, canvas, future):
    QtGui.QMainWindow.__init__(self)
    self.canvas = canvas
    self.future = future

    self.setMinimumSize(canvas.width, canvas.height)
    self.setMaximumSize(canvas.width, canvas.height)
    self.widget = Preview(self, canvas, future)
    self.setCentralWidget(self.widget)

    self.render_timer = QtCore.QTimer(self)
    self.render_timer.timeout.connect(self.update_preview)
    self.render_timer.start(0)

  @QtCore.pyqtSlot()
  def update_preview(self):
    self.widget.updateGL()
    if self.future.is_done():
      self.render_timer.stop()
      self.close_timer = QtCore.QTimer(self)
      self.close_timer.singleShot(1000, self.close)

  @QtCore.pyqtSlot()
  def close(self):
    QtCore.QCoreApplication.instance().quit()

def show_preview(canvas, future):
  app = QtGui.QApplication(["Dimension Preview"])
  window = PreviewWindow(canvas, future)
  window.show()
  app.exec()
  del window