aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/style/mstylesheet_p.h
blob: b17d9e8093a11ebc808d30fe1667f186f0c0191d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#ifndef MSTYLESHEET_P_H
#define MSTYLESHEET_P_H

// Macros for handling parenting priority,
// upper 16 bits contain priority in scene graph
// lower 16 bits contain priority in inheritance chain
#define MAKE_PRIORITY(sceneOrder, inheritanceOrder)     ((sceneOrder << 16) | (inheritanceOrder & 0xffff));
#define EXTRACT_SCENEORDER(priority)                    (priority >> 16)
#define EXTRACT_INHERITANCEORDER(priority)              (priority & 0xffff)

#include "muniquestringcache.h"

#include <QObject>
#include <QString>
#include <QList>

class MOriginContainer;
class MStyleSheetSelector;
class MStyle;
class MWidgetController;

class MStyleSheetPrivate
{
public:
    struct SelectorInfo {
        QByteArray filename;
        MStyleSheetSelector *selector;
        int classPriority;
        int parentPriority;
        const MStyleSheet *stylesheet;
    };

    // Aggregates the (non-parent) data that specifies which style we want to obtain
    struct StyleSpec {
        StyleSpec(const QByteArray &className, const QList<QByteArray> &classHierarchy, const QByteArray &objectName, const QByteArray &mode, const QByteArray &type, M::Orientation orientation) :
            className(className), classHierarchy(classHierarchy), objectName(objectName), mode(mode), type(type), orientation(orientation), parentInfo(0) {}

        void setParentInfo(const QList<SelectorInfo> *newParentInfo) {
            parentInfo = newParentInfo;
        }

        QByteArray entryCacheKey() const {
            if (!entryKey.isEmpty()) {
                return entryKey;
            }
            QByteArray orientationAsString((orientation == M::Landscape) ? "L" : "P");
            const_cast<StyleSpec*>(this)->entryKey = className + "[" + type + "]" + "#" + objectName + "." + orientationAsString + ":" + mode;
            return entryKey;
        }

        QByteArray styleCacheKey() const {
            QByteArray result = entryCacheKey();

            if (parentInfo && !parentInfo->isEmpty()) {
                result +=  + "{";
                for (QList<SelectorInfo>::const_iterator iterator = parentInfo->begin(); iterator != parentInfo->constEnd(); ++iterator) {
                    if (iterator != parentInfo->constBegin()) {
                        result += ',';
                    }
                    result += QByteArray::number((quintptr) iterator->selector, 16);
                }
                result += '}';
            }

            return result;
        }

        bool match(const MStyleSheetSelector *selector, unsigned int &classPriority) const;

        const QByteArray className;
        const QList<QByteArray> classHierarchy;

        const QByteArray objectName;
        const QByteArray mode;
        const QByteArray type;
        M::Orientation orientation;

        QByteArray entryKey;

        const QList<SelectorInfo> *parentInfo;
    };

    // Basic structure used to aggregate parent data of the style we want to obtain
    struct ParentData {
        QList<QByteArray> hierarchy;
        QList<const MStyleSheet *> sheets;
    };

    static QVector<ParentData> extractParentsData(const MWidgetController *parent);

    static MStyle *style(const QList<const MStyleSheet *> &sheets,
                         const QVector<ParentData> &parentsData,
                         const QByteArray &parentStyleName,
                         const QList<QByteArray> &styleMetaObjectHierarchy,
                         const QByteArray &styleClassName,
                         const QByteArray &objectName,
                         const QByteArray &mode,
                         const QByteArray &type,
                         M::Orientation orientation);

    typedef QHash<MUniqueStringCache::Index, MOriginContainer *> CacheEntry;
    static QHash<QByteArray, CacheEntry *> EntryCache;
    static QHash<QByteArray, MStyle *> StyleCache;

    static CacheEntry *buildCacheEntry(const QList<const MStyleSheet *> &sheets,
                                       const StyleSpec &spec,
                                       const QVector<ParentData> &parentsData,
                                       const QByteArray &parentStyleName);

    static bool combine(MStyle *style, const CacheEntry &entry, const StyleSpec &spec);

    static bool isHigherPriority(MOriginContainer *prev, MStyleSheetSelector *n, unsigned int classPriority, unsigned int parentPriority);

    static int orderNumber(const QByteArray &n, const QByteArray &sn, const QByteArray &parentStyleName, const QList<QByteArray> &parentHierarchy);

    static MStyle *buildStyle(const StyleSpec &spec,
                              const QList<const MStyleSheet *> &sheets,
                              const QVector<ParentData> &parentsData,
                              const QByteArray &parentStyleName);

    static bool matchParent(const MStyleSheetSelector *selector,
                            const QList<QByteArray> &parentHierarchy,
                            const QByteArray &parentStyleName,
                            unsigned int sceneOrder,
                            unsigned int &parentPriority);

    static bool matchParents(const MStyleSheetSelector *selector,
                             const QVector<ParentData> &parentsData,
                             const QByteArray &parentStyleName,
                             unsigned int &parentPriority);

    static QList<SelectorInfo> getMatchingSelectorsWithParent(const QList<const MStyleSheet *> &sheets,
                                                              const QVector<ParentData> &parentsData,
                                                              const QByteArray &parentStyleName,
                                                              const StyleSpec &spec);
};

#endif