camelot.camelot.view.controls.modeltree

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