1#############################################################################
2##
3## Copyright (C) 2013 Riverbank Computing Limited.
4## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5## All rights reserved.
6##
7## This file is part of the examples of PyQt.
8##
9## $QT_BEGIN_LICENSE:LGPL$
10## Commercial Usage
11## Licensees holding valid Qt Commercial licenses may use this file in
12## accordance with the Qt Commercial License Agreement provided with the
13## Software or, alternatively, in accordance with the terms contained in
14## a written agreement between you and Nokia.
15##
16## GNU Lesser General Public License Usage
17## Alternatively, this file may be used under the terms of the GNU Lesser
18## General Public License version 2.1 as published by the Free Software
19## Foundation and appearing in the file LICENSE.LGPL included in the
20## packaging of this file.  Please review the following information to
21## ensure the GNU Lesser General Public License version 2.1 requirements
22## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23##
24## In addition, as a special exception, Nokia gives you certain additional
25## rights.  These rights are described in the Nokia Qt LGPL Exception
26## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27##
28## GNU General Public License Usage
29## Alternatively, this file may be used under the terms of the GNU
30## General Public License version 3.0 as published by the Free Software
31## Foundation and appearing in the file LICENSE.GPL included in the
32## packaging of this file.  Please review the following information to
33## ensure the GNU General Public License version 3.0 requirements will be
34## met: http://www.gnu.org/copyleft/gpl.html.
35##
36## If you have questions regarding the use of this file, please contact
37## Nokia at qt-info@nokia.com.
38## $QT_END_LICENSE$
39##
40#############################################################################
41
42
43from PyQt5.QtCore import QFile, QFileInfo, QRectF, QTextStream
44
45from colors import Colors
46from demoitem import DemoItem
47from demoitemanimation import DemoItemAnimation
48from demotextitem import DemoTextItem
49from headingitem import HeadingItem
50
51
52class MenuContentItem(DemoItem):
53    def __init__(self, el, parent=None):
54        super(MenuContentItem, self).__init__(parent)
55
56        self.name = el.getAttribute('name')
57        self.heading = None
58        self.description1 = None
59        self.description2 = None
60
61        readme_dir = QFileInfo(__file__).dir()
62        readme_dir.cdUp()
63        readme_dir.cd(el.getAttribute('dirname'))
64
65        self.readmePath = readme_dir.absoluteFilePath('README')
66
67        self._prepared = False
68
69    def prepare(self):
70        if not self._prepared:
71            self.createContent()
72            self._prepared= True
73
74    def animationStopped(self, id):
75        if self.name == Colors.rootMenuName:
76            # Optimization hack.
77            return
78
79        if id == DemoItemAnimation.ANIM_OUT:
80            # Free up some memory
81            self.heading = None
82            self.description1 = None
83            self.description2 = None
84            self._prepared = False
85
86    def loadDescription(self, startPara, nrPara):
87        readme = QFile(self.readmePath)
88        if not readme.open(QFile.ReadOnly):
89            Colors.debug("- MenuContentItem.loadDescription: Could not load:", self.readmePath)
90            return ""
91
92        in_str = QTextStream(readme)
93        # Skip a certain number of paragraphs.
94        while startPara:
95            if not in_str.readLine():
96                startPara -= 1
97
98        # Read in the number of wanted paragraphs.
99        result = ''
100        line = in_str.readLine()
101        while True:
102            result += line + " "
103            line = in_str.readLine()
104            if not line:
105                nrPara -= 1
106                line = "<br><br>" + in_str.readLine()
107
108            if nrPara == 0 or in_str.atEnd():
109                break
110
111        return Colors.contentColor + result
112
113    def createContent(self):
114        # Create the items.
115        self.heading = HeadingItem(self.name, self)
116        para1 = self.loadDescription(0, 1)
117        if not para1:
118            para1 = Colors.contentColor + "Could not load description. Ensure that the documentation for Qt is built."
119        bgcolor = Colors.sceneBg1.darker(200)
120        bgcolor.setAlpha(100)
121        self.description1 = DemoTextItem(para1, Colors.contentFont(),
122                Colors.heading, 500, self, DemoTextItem.STATIC_TEXT)
123        self.description2 = DemoTextItem(self.loadDescription(1, 2),
124                Colors.contentFont(), Colors.heading, 250, self,
125                DemoTextItem.STATIC_TEXT)
126
127        # Place the items on screen.
128        self.heading.setPos(0, 3)
129        self.description1.setPos(0, self.heading.pos().y() + self.heading.boundingRect().height() + 10)
130        self.description2.setPos(0, self.description1.pos().y() + self.description1.boundingRect().height() + 15)
131
132    def boundingRect(self):
133        return QRectF(0, 0, 500, 350)
134