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

Source Code for Module Camelot.camelot.view.controls.modeltree

  1  #  ============================================================================ 
  2  # 
  3  #  Copyright (C) 2007-2008 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  """custom tree and tree-items widgets""" 
 29   
 30  import logging 
 31   
 32  logger = logging.getLogger('camelot.view.controls.modeltree') 
 33   
 34  from PyQt4 import QtGui 
 35  from PyQt4 import QtCore 
 36  from PyQt4.QtCore import Qt 
 37   
 38  from camelot.view.art import Icon 
 39   
 40  QT_MAJOR_VERSION = float('.'.join(str(QtCore.QT_VERSION_STR).split('.')[0:2])) 
 41   
 42   
43 -class ModelItem(QtGui.QTreeWidgetItem):
44 """Custom tree item widget""" 45
46 - def __init__(self, parent, columns_names):
47 logger.debug('creating new modelitem') 48 QtGui.QTreeWidgetItem.__init__(self, parent, columns_names) 49 self.column = 0 50 self.set_icon()
51
52 - def _underline(self, enable=False):
53 font = self.font(self.column) 54 font.setUnderline(enable) 55 self.setFont(self.column, font)
56
57 - def set_icon(self, qicon=None):
58 if qicon is None: 59 qicon = Icon('tango/16x16/actions/window-new.png').getQIcon() 60 self.setIcon(self.column, qicon)
61 62
63 -class ModelTree(QtGui.QTreeWidget):
64 """Custom tree widget""" 65
66 - def __init__(self, header_labels=[''], parent=None):
67 logger.debug('creating new modeltree') 68 QtGui.QTreeWidget.__init__(self, parent) 69 # we don't select entire rows 70 self.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems) 71 # we track mouse movement when no button is pressed 72 self.setMouseTracking(True) 73 self.parent = parent 74 self.header_labels = header_labels 75 self.clear_model_items() 76 self.fix_header_labels()
77
78 - def fix_header_labels(self):
79 if QT_MAJOR_VERSION > 4.3: 80 self.setHeaderHidden(True) 81 else: 82 self.setHeaderLabels(self.header_labels)
83
84 - def clear_model_items(self):
85 self.modelitems = []
86
87 - def mousePressEvent(self, event):
88 """Custom context menu""" 89 if event.button() == Qt.RightButton: 90 self.emit(QtCore.SIGNAL('customContextMenuRequested(const QPoint &)'), 91 event.pos()) 92 event.accept() 93 else: 94 QtGui.QTreeWidget.mousePressEvent(self, event)
95
96 - def leaveEvent(self, event):
97 if not self.modelitems: 98 return 99 100 for item in self.modelitems: 101 item._underline(False)
102
103 - def mouseMoveEvent(self, event):
104 if not self.modelitems: 105 return 106 107 for item in self.modelitems: 108 item._underline(False) 109 110 item = self.itemAt(self.mapFromGlobal(self.cursor().pos())) 111 if item: 112 item._underline(True)
113
114 - def focusInEvent(self, event):
115 item = self.itemAt(self.mapFromGlobal(self.cursor().pos())) 116 if item: 117 self.setCurrentItem(item)
118