source: sasview/src/sas/qtgui/Perspectives/Fitting/ViewDelegate.py @ ad6b4e2

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since ad6b4e2 was ad6b4e2, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Custom delegate for fancy display of units

  • Property mode set to 100755
File size: 2.1 KB
Line 
1from PyQt4 import QtGui
2from PyQt4 import QtCore
3
4import sas.qtgui.Utilities.GuiUtils as GuiUtils
5
6# Table view columns
7PROPERTY=0
8VALUE=1
9MIN=2
10MAX=3
11UNIT=4
12
13class ModelViewDelegate(QtGui.QStyledItemDelegate):
14    """
15    Custom delegate for appearance and behavior control of the model view
16    """
17    def paint(self, painter, option, index):
18        """
19        Overwrite generic painter for certain columns
20        """
21        if index.column() == UNIT or index.column() == MIN or index.column() == MAX:
22            # Units - present in nice HTML
23            options = QtGui.QStyleOptionViewItemV4(option)
24            self.initStyleOption(options,index)
25            style = QtGui.QApplication.style() if options.widget is None else options.widget.style()
26
27            # Prepare document for inserting into cell
28            doc = QtGui.QTextDocument()
29
30            # Convert the unit description into HTML
31            text_html = GuiUtils.convertUnitToHTML(str(options.text))
32            doc.setHtml(text_html)
33
34            # delete the original content
35            options.text = ""
36            style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter);
37
38            context = QtGui.QAbstractTextDocumentLayout.PaintContext()
39            textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options)
40
41            painter.save()
42            painter.translate(textRect.topLeft())
43            painter.setClipRect(textRect.translated(-textRect.topLeft()))
44            # Draw the QTextDocument in the cell
45            doc.documentLayout().draw(painter, context)
46
47            painter.restore()
48        else:
49            # Just the default paint
50            QtGui.QStyledItemDelegate.paint(self, painter, option, index)
51
52    #def sizeHint(self, option, index):
53    #    options = QtGui.QStyleOptionViewItemV4(option)
54    #    self.initStyleOption(options,index)
55
56    #    doc = QtGui.QTextDocument()
57    #    doc.setHtml(options.text)
58    #    doc.setTextWidth(options.rect.width())
59    #    return QtCore.QSize(doc.idealWidth(), doc.size().height())
Note: See TracBrowser for help on using the repository browser.