Changeset 267 for nodebox/branches/try-qt
- Timestamp:
- 12/06/07 11:49:02 (13 months ago)
- Location:
- nodebox/branches/try-qt
- Files:
-
- 4 modified
-
nodebox/console.py (modified) (2 diffs)
-
nodebox/graphics/qt.py (modified) (20 diffs)
-
nodebox/gui/qt/__init__.py (modified) (6 diffs)
-
tests/graphics/runtests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
nodebox/branches/try-qt/nodebox/console.py
r188 r267 1 from AppKit import NSApplication 1 import sys 2 from PyQt4.QtGui import QApplication 2 3 3 4 from nodebox import graphics … … 7 8 8 9 def __init__(self): 9 # Force NSApp initialisation. 10 NSApplication.sharedApplication().activateIgnoringOtherApps_(0) 10 # Force QApplication initialisation. 11 self.app = QApplication(sys.argv) 12 11 13 self.namespace = {} 12 14 self.canvas = graphics.Canvas() -
nodebox/branches/try-qt/nodebox/graphics/qt.py
r261 r267 3 3 from random import choice, shuffle 4 4 5 from PyQt4.QtGui import QPainterPath, QColor, QTransform, QBrush, QPen, QImage, QPrinter, QPainter 5 from PyQt4.QtGui import QPainterPath, QColor, QTransform, QBrush, QPen, QImage, QPrinter, QPainter, QFontMetrics, QFont 6 6 from PyQt4.QtCore import Qt, QSize 7 7 from PyQt4.QtSvg import QSvgGenerator … … 256 256 def _get_bounds(self): 257 257 try: 258 return self._qpath.boundingRect() 258 r = self._qpath.boundingRect() 259 return (r.x(), r.y()), (r.width(), r.height()) 260 return 259 261 except: 260 262 # Path is empty -- no bounds … … 344 346 def _draw(self, painter): 345 347 painter.save() 346 #self.transform.concat()348 self.transform.concat(painter) 347 349 if self._fillcolor: 348 350 painter.fillPath(self._qpath, QBrush(self._fillcolor._rgb)) … … 509 511 args = self._normalizeList(args) 510 512 g, a = args 511 clr = QColor.f ormRgbF(g, g, g, a)513 clr = QColor.fromRgbF(g, g, g, a) 512 514 elif params == 3 and self._ctx._colormode == RGB: # RGB, no alpha 513 515 args = self._normalizeList(args) … … 570 572 #self._rgb = self._cmyk.colorUsingColorSpaceName_(NSDeviceRGBColorSpace) 571 573 574 def _get_hue(self): 575 return self._rgb.hueF() 576 def _set_hue(self, val): 577 val = self._normalize(val) 578 c = self._rgb 579 h, s, b, a = c.hueF(), c.saturationF(), c.valueF(), c.alpha() 580 self._rgb.setHsvF(val, s, b, a) 581 self._updateCmyk() 582 h = hue = property(_get_hue, _set_hue, doc="the hue of the color") 572 583 573 584 def _normalize(self, v): … … 587 598 transform = QTransform() 588 599 elif isinstance(transform, Transform): 589 transform = QTransform(transform._q Transform)600 transform = QTransform(transform._qtransform) 590 601 elif isinstance(transform, (list, tuple)): 591 602 matrix = tuple(transform) 592 transform = QTransform 603 transform = QTransform() 593 604 transform.setMatrix(*matrix) 594 605 elif isinstance(transform, QTransform): … … 596 607 else: 597 608 raise NodeBoxError, "Don't know how to handle transform %s." % transform 598 self._q Transform = transform609 self._qtransform = transform 599 610 600 611 def _get_transform(self): 601 612 warnings.warn("The 'transform' attribute is deprecated. Please use _nsAffineTransform instead.", DeprecationWarning, stacklevel=2) 602 return self._q Transform613 return self._qtransform 603 614 transform = property(_get_transform) 604 615 605 def set(self): 606 print "transform set" 607 #self._nsAffineTransform.set() 608 609 def concat(self): 610 print "transform concat" 611 # self._nsAffineTransform.concat() 616 def set(self, painter): 617 painter.setTransform(self._qtransform) 618 619 def concat(self, painter): 620 trans = painter.transform() 621 painter.setTransform(trans * self._qtransform) 622 #m1 = trans.m11(), trans.m12(), trans.m13(), trans.m21(), trans.m22(), trans.dx(), trans.dy() 623 #trans = self._qtransform 624 #m2 = trans.m11(), trans.m12(), trans.m13(), trans.m21(), trans.m22(), trans.dx(), trans.dy() 625 #m3 = transform.mmult(m1, m2) 626 #trans = QTransform(*m3) 627 # TODO: returns a NotImplemented 628 #trans += QTransform(self._qtransform) 629 #painter.setTransform(trans) 612 630 613 631 def copy(self): … … 623 641 624 642 def _get_matrix(self): 625 q = self._q Transform643 q = self._qtransform 626 644 return (q.m11(), q.m12(), q.m13(), q.m21(), q.m22(), q.m23(), q.m31(), q.m32(), q.m33()) 627 645 def _set_matrix(self, value): 628 self._q Transform.setMatrix(value)646 self._qtransform.setMatrix(value) 629 647 matrix = property(_get_matrix, _set_matrix) 630 648 631 649 def rotate(self, degrees=0, radians=0): 632 650 if degrees: 633 self._q Transform.rotate(degrees)634 else: 635 self._q Transform.rotateRadians(radians)651 self._qtransform.rotate(degrees) 652 else: 653 self._qtransform.rotateRadians(radians) 636 654 637 655 def translate(self, x=0, y=0): 638 self._q Transform.translate(x, y)656 self._qtransform.translate(x, y) 639 657 640 658 def scale(self, x=1, y=None): 641 659 if y is None: 642 660 y = x 643 self._q Transform.scale(x, y)661 self._qtransform.scale(x, y) 644 662 645 663 def skew(self, x=0, y=0): 646 self._q Transform.shear(x, y)664 self._qtransform.shear(x, y) 647 665 648 666 def invert(self): 649 self._qTransform = self._qTransform.inverted() 650 651 #def append(self, other): 652 # if isinstance(other, Transform): 653 # other = other._nsAffineTransform 654 # self._nsAffineTransform.appendTransform_(other) 655 656 #def prepend(self, other): 657 # if isinstance(other, Transform): 658 # other = other._nsAffineTransform 659 # self._nsAffineTransform.prependTransform_(other) 667 self._qtransform = self._qtransform.inverted() 668 669 def append(self, other): 670 if isinstance(other, Transform): 671 other = other._qtransform 672 self._qtransform *= other 673 674 def prepend(self, other): 675 if isinstance(other, Transform): 676 other = other._qtransform 677 other = QTransform(other) 678 other *= self._qtransform 679 self._qtransform = other 660 680 661 681 def transformPoint(self, point): … … 664 684 elif isinstance(point, Point): 665 685 point = point._qPoint 666 return self._q Transform.map(point)686 return self._qtransform.map(point) 667 687 668 688 def transformBezierPath(self, path): … … 671 691 else: 672 692 raise NodeBoxError, "Can only transform BezierPaths" 673 path._q Path = self._qTransform.map(path._qPath)693 path._qpath = self._qtransform.map(path._qpath) 674 694 return path 675 695 … … 767 787 t = Transform() 768 788 t.translate(self.x, self.y) 769 #t.concat() 770 painter.setTransform(t._qTransform) 789 t.concat(painter) 771 790 772 791 # Set new width and height factors. Note that no scaling is done yet: they're just here … … 780 799 t = Transform() 781 800 t.translate(dX, dY) 782 #t.concat()801 t.concat(painter) 783 802 784 803 # Do current transformation. 785 #self._transform.concat()804 self._transform.concat(painter) 786 805 787 806 # Move back to the previous position. 788 807 t = Transform() 789 808 t.translate(-dX, -dY) 790 #t.concat()809 t.concat(painter) 791 810 792 811 # Finally, scale the image according to the factors. 793 812 t = Transform() 794 813 t.scale(factor) 795 #t.concat()814 t.concat(painter) 796 815 else: 797 816 # Do current transformation … … 801 820 t.translate(self.x, self.y) # Here we add the positioning of the image. 802 821 t.scale(factor) 803 #t.concat()822 t.concat(painter) 804 823 805 824 # A debugImage draws a black rectangle instead of an image. … … 825 844 t = Transform() 826 845 t.translate(x+deltaX, y+deltaY) 827 # t.concat()846 t.concat(painter) 828 847 x = -deltaX 829 848 y = -deltaY 830 849 # Do current transformation 831 # self._transform.concat()850 self._transform.concat(painter) 832 851 # A debugImage draws a black rectangle instead of an image. 833 852 if self.debugImage: … … 845 864 846 865 __dummy_color = QColor() 866 __alignMap = { LEFT: Qt.AlignLeft, RIGHT:Qt.AlignRight, CENTER:Qt.AlignCenter, JUSTIFY:Qt.AlignJustify } 847 867 848 868 def __init__(self, ctx, text, x=0, y=0, width=None, height=None, **kwargs): … … 858 878 self._fontsize = kwargs.get('fontsize', 24) 859 879 self._lineheight = max(kwargs.get('lineheight', 1.2), 0.01) 860 self._align = kwargs.get('align', NSLeftTextAlignment)880 self._align = kwargs.get('align', LEFT) 861 881 862 882 def copy(self): … … 869 889 def font_exists(cls, fontname): 870 890 # Check if the font exists. 871 f = QFont .font(fontName)891 f = QFont(fontname) 872 892 return f.exactMatch() 873 893 font_exists = classmethod(font_exists) … … 875 895 def _get_font(self): 876 896 return QFont(self._fontname, self._fontsize) 877 font = property(_get_font)897 _qfont = property(_get_font) 878 898 879 899 def _draw(self, painter): 900 if self.width is None: 901 w = 100000 902 else: 903 w = self.width 904 if self.height is None: 905 h = 100000 906 else: 907 h = self.height 908 fm = painter.fontMetrics() 909 flags = self.__alignMap[self._align] 910 r = fm.boundingRect(self.x, self.y, w, h, flags, self.text) 911 880 912 if self._fillcolor is None: return 881 x,y = self.x, self.y 913 x,y = r.x(), r.y() 914 preferredWidth, preferredHeight = r.width(), r.height() 882 915 if self.width is not None: 883 916 if self._align == RIGHT: … … 892 925 deltaY = h / 2 893 926 t = Transform() 894 t.translate(x+deltaX, y- self.font.defaultLineHeightForFont()+deltaY)895 #t.concat()896 self._transform.concat( )897 painter.drawText(x, y, w, h, self.text)927 t.translate(x+deltaX, y-fm.ascent()+deltaY) 928 t.concat(painter) 929 self._transform.concat(painter) 930 painter.drawText(x, y, w, h, flags, self.text) 898 931 #layoutManager.drawGlyphsForGlyphRange_atPoint_(glyphRange, (-deltaX-dx,-deltaY-dy)) 899 932 else: … … 906 939 def _get_metrics(self): 907 940 # TODO: Measure using boundingRect 908 return 100, 100 941 flags = self.__alignMap[self._align] 942 fm = QFontMetrics(self._qfont) 943 944 if self.width is None: 945 w = 100000 946 else: 947 w = self.width 948 if self.height is None: 949 h = 100000 950 else: 951 h = self.height 952 953 r = fm.boundingRect(self.x, self.y, w, h, flags, self.text) 954 return r.width(), r.height() 909 955 metrics = property(_get_metrics) 910 956 911 957 def _get_path(self): 912 return None 958 p = QPainterPath() 959 p.addText(0, 0, self._qfont, self.text) 960 path = BezierPath(self._ctx, p) 961 path.inheritFromContext() 962 return path 913 963 path = property(_get_path) 914 964 -
nodebox/branches/try-qt/nodebox/gui/qt/__init__.py
r261 r267 74 74 outputView.setTextColor(QColor(255, 0, 0)) # TODO: Refactor color 75 75 outputView.insertPlainText(data) 76 p.end() 76 finally: 77 p.end() # TODO: This doesn't fix the QWidget warning if error happens during drawing. 77 78 self._image = img 78 79 … … 168 169 169 170 def createGUI(self): 171 # The font should come and be set as a preference. 172 # We prefer Monaco as a startup font. 170 173 171 174 codeFont = QFont() 172 175 codeFont.setStyleHint(QFont.TypeWriter, QFont.PreferAntialias) 173 print codeFont.defaultFamily() 174 codeFont.setFamily(codeFont.defaultFamily()) 176 if sys.platform == "darwin": 177 codeFont.setFamily("Monaco") 178 else: 179 codeFont.setFamily(codeFont.defaultFamily()) 180 codeFont.setPointSize(12) 175 181 codeFormat = QTextCharFormat() 176 182 codeFormat.setFont(codeFont) … … 203 209 self.view_edit.addWidget(self.graphicsScroll) 204 210 self.view_edit.addWidget(self.code_errors) 205 206 #mainLayout = QGridLayout() 207 #mainLayout.addWidget(self.view_edit) 211 l = QGridLayout() 212 l.setHorizontalSpacing(10) 213 l.setVerticalSpacing(10) 214 self.view_edit.setLayout(l) 215 216 #self.centralWidget = QWidget() 217 #self.centralWidget.addWidget(self.view_edit) 218 #self.centralWidget.setLayout(QGridLayout()) 219 208 220 self.setCentralWidget(self.view_edit) 221 self.setUnifiedTitleAndToolBarOnMac(True) 222 self.codeView.setFocus() 209 223 210 224 self.setWindowTitle(self.tr("Untitled")) … … 280 294 @pyqtSignature("") 281 295 def doRun(self): 282 print "run"283 296 if self.fullScreen is not None: return 284 297 self.currentView = self.graphicsView … … 526 539 lastErr = isErr 527 540 outputView.insertPlainText(data) 541 outputView.ensureCursorVisible() 528 542 529 543 def _compileScript(self, source=None): … … 571 585 self.home = 'C:/' # os.getenv('HOME') 572 586 self.lib = os.path.join(self.home, "NodeBox") # os.path.join(self.home, "Library", "Application Support", "NodeBox") 573 elif sys.platform == " mac":587 elif sys.platform == "darwin": 574 588 self.home = os.getenv('HOME') 575 589 self.lib = os.path.join(self.home, "Library", "Application Support", "NodeBox") -
nodebox/branches/try-qt/tests/graphics/runtests.py
r205 r267 50 50 if stats.number_of_differences > 0: 51 51 differences = True 52 print " E",52 print "F", 53 53 else: 54 54 print ".",
