classes.py 9.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
############################################################################
#
# Copyright (C) 2016 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# This file is part of Qt Creator.
#
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and The Qt Company. For licensing terms
# and conditions see https://www.qt.io/terms-conditions. For further
# information use the contact form at https://www.qt.io/contact-us.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3 as published by the Free Software
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
#
############################################################################
25

26
import __builtin__
27 28
import operator

29
# for easier re-usage (because Python hasn't an enum type)
Christian Stenger's avatar
Christian Stenger committed
30
class Targets:
31
    ALL_TARGETS = tuple(map(lambda x: 2 ** x , range(5)))
32

33
    (DESKTOP_487_DEFAULT,
34 35
     EMBEDDED_LINUX,
     DESKTOP_531_DEFAULT,
36 37
     DESKTOP_541_GCC,
     DESKTOP_561_DEFAULT) = ALL_TARGETS
38

39 40 41 42 43 44 45 46 47
    @staticmethod
    def availableTargetClasses():
        availableTargets = list(Targets.ALL_TARGETS)
        if platform.system() in ('Windows', 'Microsoft'):
            availableTargets.remove(Targets.EMBEDDED_LINUX)
        elif platform.system() == 'Darwin':
            availableTargets.remove(Targets.DESKTOP_541_GCC)
        return availableTargets

48 49
    @staticmethod
    def desktopTargetClasses():
50 51 52
        desktopTargets = Targets.availableTargetClasses()
        if Targets.EMBEDDED_LINUX in desktopTargets:
            desktopTargets.remove(Targets.EMBEDDED_LINUX)
53 54
        return desktopTargets

55 56
    @staticmethod
    def qt4Classes():
57
        return (Targets.DESKTOP_487_DEFAULT | Targets.EMBEDDED_LINUX)
58

59 60
    @staticmethod
    def getStringForTarget(target):
61 62
        if target == Targets.DESKTOP_487_DEFAULT:
            return "Desktop 487 default"
Christian Stenger's avatar
Christian Stenger committed
63
        elif target == Targets.EMBEDDED_LINUX:
64
            return "Embedded Linux"
65 66
        elif target == Targets.DESKTOP_531_DEFAULT:
            return "Desktop 531 default"
Robert Loehning's avatar
Robert Loehning committed
67 68
        elif target == Targets.DESKTOP_541_GCC:
            return "Desktop 541 GCC"
69 70
        elif target == Targets.DESKTOP_561_DEFAULT:
            return "Desktop 561 default"
71 72 73
        else:
            return None

74 75 76 77 78
    @staticmethod
    def getTargetsAsStrings(targets):
        if not isinstance(targets, (tuple,list)):
            test.fatal("Wrong usage... This function handles only tuples or lists.")
            return None
Christian Stenger's avatar
Christian Stenger committed
79
        result = map(Targets.getStringForTarget, targets)
80 81 82 83
        if None in result:
            test.fatal("You've passed at least one unknown target!")
        return result

84 85
    @staticmethod
    def getDefaultKit():
86
        return Targets.DESKTOP_531_DEFAULT
87

88 89 90 91 92 93 94
# this class holds some constants for easier usage inside the Projects view
class ProjectSettings:
    BUILD = 1
    RUN = 2

# this class defines some constants for the views of the creator's MainWindow
class ViewConstants:
95
    WELCOME, EDIT, DESIGN, DEBUG, PROJECTS, HELP = range(6)
96
    FIRST_AVAILABLE = 0
97 98 99 100 101 102 103 104
    # always adjust the following to the highest value of the available ViewConstants when adding new
    LAST_AVAILABLE = HELP

    # this function returns a regex of the tooltip of the FancyTabBar elements
    # this is needed because the keyboard shortcut is OS specific
    # if the provided argument does not match any of the ViewConstants it returns None
    @staticmethod
    def getToolTipForViewTab(viewTab):
105
        if viewTab == ViewConstants.WELCOME:
106
            toolTip = ur'Switch to <b>Welcome</b> mode <span style="color: gray; font-size: small">(Ctrl\+|\u2303)%d</span>'
107
        elif viewTab == ViewConstants.EDIT:
108
            toolTip = ur'Switch to <b>Edit</b> mode <span style="color: gray; font-size: small">(Ctrl\+|\u2303)%d</span>'
109
        elif viewTab == ViewConstants.DESIGN:
110
            toolTip = ur'Switch to <b>Design</b> mode <span style="color: gray; font-size: small">(Ctrl\+|\u2303)%d</span>'
111
        elif viewTab == ViewConstants.DEBUG:
112
            toolTip = ur'Switch to <b>Debug</b> mode <span style="color: gray; font-size: small">(Ctrl\+|\u2303)%d</span>'
113
        elif viewTab == ViewConstants.PROJECTS:
114
            toolTip = ur'Switch to <b>Projects</b> mode <span style="color: gray; font-size: small">(Ctrl\+|\u2303)%d</span>'
115
        elif viewTab == ViewConstants.HELP:
116
            toolTip = ur'Switch to <b>Help</b> mode <span style="color: gray; font-size: small">(Ctrl\+|\u2303)%d</span>'
117 118
        else:
            return None
119
        return toolTip % (viewTab + 1)
120 121 122 123 124 125 126 127

class SubprocessType:
    QT_WIDGET=0
    QT_QUICK_APPLICATION=1
    QT_QUICK_UI=2
    USER_DEFINED=3

    @staticmethod
128
    def getWindowType(subprocessType, qtQuickVersion="1.1"):
129 130 131
        if subprocessType == SubprocessType.QT_WIDGET:
            return "QMainWindow"
        if subprocessType == SubprocessType.QT_QUICK_APPLICATION:
132 133 134 135
            qqv = "2"
            if qtQuickVersion[0] == "1":
                qqv = "1"
            return "QtQuick%sApplicationViewer" % qqv
136
        if subprocessType == SubprocessType.QT_QUICK_UI:
137
            if qtQuickVersion == "1.1":
138 139 140
                return "QDeclarativeViewer"
            else:
                return "QQuickView"
141 142 143 144 145
        if subprocessType == SubprocessType.USER_DEFINED:
            return "user-defined"
        test.fatal("Could not determine the WindowType for SubprocessType %s" % subprocessType)
        return None

146 147 148 149
class QtInformation:
    QT_VERSION = 0
    QT_BINPATH = 1
    QT_LIBPATH = 2
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

class LibType:
    SHARED = 0
    STATIC = 1
    QT_PLUGIN = 2

    @staticmethod
    def getStringForLib(libType):
        if libType == LibType.SHARED:
            return "Shared Library"
        if libType == LibType.STATIC:
            return "Statically Linked Library"
        if libType == LibType.QT_PLUGIN:
            return "Qt Plugin"
        return None
Christian Stenger's avatar
Christian Stenger committed
165 166 167 168 169 170 171

class Qt5Path:
    DOCS = 0
    EXAMPLES = 1

    @staticmethod
    def getPaths(pathSpec):
172
        qt5targets = [Targets.DESKTOP_531_DEFAULT, Targets.DESKTOP_561_DEFAULT]
173 174
        if platform.system() != 'Darwin':
            qt5targets.append(Targets.DESKTOP_541_GCC)
Christian Stenger's avatar
Christian Stenger committed
175
        if pathSpec == Qt5Path.DOCS:
176
            return map(lambda target: Qt5Path.docsPath(target), qt5targets)
Christian Stenger's avatar
Christian Stenger committed
177
        elif pathSpec == Qt5Path.EXAMPLES:
178
            return map(lambda target: Qt5Path.examplesPath(target), qt5targets)
Christian Stenger's avatar
Christian Stenger committed
179 180 181
        else:
            test.fatal("Unknown pathSpec given: %s" % str(pathSpec))
            return []
182 183 184 185

    @staticmethod
    def __preCheckAndExtractQtVersionStr__(target):
        if target not in Targets.ALL_TARGETS:
186
            raise Exception("Unexpected target '%s'" % str(target))
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

        matcher = re.match("^Desktop (5\\d{2}).*$", Targets.getStringForTarget(target))
        if matcher is None:
            raise Exception("Currently this is supported for Desktop Qt5 only, got target '%s'"
                            % str(Targets.getStringForTarget(target)))
        return matcher.group(1)

    @staticmethod
    def __createPlatformQtPath__(qt5Minor):
        # special handling for Qt5.2
        if qt5Minor == 2:
            if platform.system() in ('Microsoft', 'Windows'):
                return "C:/Qt/Qt5.2.1/5.2.1/msvc2010"
            elif platform.system() == 'Linux':
                if __is64BitOS__():
                    return os.path.expanduser("~/Qt5.2.1/5.2.1/gcc_64")
                else:
                    return os.path.expanduser("~/Qt5.2.1/5.2.1/gcc")
            else:
                return os.path.expanduser("~/Qt5.2.1/5.2.1/clang_64")
        # Qt5.3+
        if platform.system() in ('Microsoft', 'Windows'):
            return "C:/Qt/Qt5.%d.1" % qt5Minor
        else:
            return os.path.expanduser("~/Qt5.%d.1" % qt5Minor)

    @staticmethod
    def examplesPath(target):
        qtVersionStr = Qt5Path.__preCheckAndExtractQtVersionStr__(target)
        qtMinorVersion = __builtin__.int(qtVersionStr[1])
        if qtMinorVersion == 2:
            path = "examples"
        else:
            path = "Examples/Qt-5.%d" % qtMinorVersion

        return os.path.join(Qt5Path.__createPlatformQtPath__(qtMinorVersion), path)

    @staticmethod
    def docsPath(target):
        qtVersionStr = Qt5Path.__preCheckAndExtractQtVersionStr__(target)
        qtMinorVersion = __builtin__.int(qtVersionStr[1])
        if qtMinorVersion == 2:
            path = "doc"
        else:
            path = "Docs/Qt-5.%d" % qtMinorVersion

        return os.path.join(Qt5Path.__createPlatformQtPath__(qtMinorVersion), path)
234 235 236 237 238 239 240 241 242 243

class TestSection:
    def __init__(self, description):
        self.description = description

    def __enter__(self):
        test.startSection(self.description)

    def __exit__(self, exc_type, exc_value, traceback):
        test.endSection()