00001 ############################################################################## 00002 # 00003 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved. 00004 # Copyright (c) 2007-2008 Albert Cervera i Areny <albert@nan-tic.com> 00005 # 00006 # WARNING: This program as such is intended to be used by professional 00007 # programmers who take the whole responsability of assessing all potential 00008 # consequences resulting from its eventual inadequacies and bugs 00009 # End users who are looking for a ready-to-use solution with commercial 00010 # garantees and support are strongly adviced to contract a Free Software 00011 # Service Company 00012 # 00013 # This program is Free Software; you can redistribute it and/or 00014 # modify it under the terms of the GNU General Public License 00015 # as published by the Free Software Foundation; either version 2 00016 # of the License, or (at your option) any later version. 00017 # 00018 # This program is distributed in the hope that it will be useful, 00019 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 # GNU General Public License for more details. 00022 # 00023 # You should have received a copy of the GNU General Public License 00024 # along with this program; if not, write to the Free Software 00025 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00026 # 00027 ############################################################################## 00028 00029 import os 00030 from PyQt4.QtGui import * 00031 from Koo.Common import Plugins 00032 from DummyView import * 00033 00034 # The 'ViewFactory' class specializes in creating the appropiate views. Searches 00035 # for available views and calls the parser of the appropiate one. 00036 # 00037 # Each directory could handle more than one view types. 00038 # Standard types are 'form', 'tree', 'calendar' and 'graph'. 00039 00040 class ViewFactory: 00041 views = {} 00042 00043 @staticmethod 00044 def viewAction(name, parent): 00045 ViewFactory.scan() 00046 if not name in ViewFactory.views: 00047 return None 00048 action = QAction( parent ) 00049 view = ViewFactory.views[name] 00050 action.setObjectName( name ) 00051 action.setText( view['label'] ) 00052 action.setIcon( QIcon( view['icon'] ) ) 00053 action.setCheckable( True ) 00054 return action 00055 00056 @staticmethod 00057 def scan(): 00058 # Scan only once 00059 if ViewFactory.views: 00060 return 00061 # Search for all available views 00062 Plugins.scan( 'Koo.View', os.path.abspath(os.path.dirname(__file__)) ) 00063 00064 @staticmethod 00065 def create(viewId, parent, model, root_node, fields): 00066 ViewFactory.scan() 00067 # Search for the view and parse the XML 00068 widget = None 00069 for node in root_node.childNodes: 00070 if not node.nodeType == node.ELEMENT_NODE: 00071 continue 00072 if node.localName in ViewFactory.views: 00073 parser = ViewFactory.views[ node.localName ]['parser']() 00074 return parser.create(viewId, parent, model, node, fields) 00075 dummy = DummyView() 00076 dummy.setViewType( node.localName ) 00077 return dummy 00078 00079 @staticmethod 00080 def register(parser, viewName, label, icon): 00081 ViewFactory.views[viewName] = { 00082 'parser': parser, 00083 'label': label, 00084 'icon': icon 00085 } 00086