OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / preview / stylemanager.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "stylemanager.h"
35 #include <QStyleFactory>
36 #include <QApplication>
37 #include <QStyle>
38
39 namespace QmlDesigner {
40
41 namespace Internal {
42
43 // TODO KAI: REMOVE THIS CLASS
44
45 //### if we use this pattern often: make a template out of this!
46 class StyleManagerGuard { //This guard destroys the singleton in its destructor
47 public:                   //This should avoid that a memory leak is reported
48    ~StyleManagerGuard() {
49    if (StyleManager::m_instance != 0)
50      delete StyleManager::m_instance;
51    }
52 };
53 } //namespace Internal
54
55 StyleManager* StyleManager::m_instance = 0;
56
57 void StyleManager::addView(NodeInstanceView* view)
58 {
59     instance()->m_views.append(view);
60 }
61
62 void StyleManager::removeView(NodeInstanceView* view)
63 {
64     instance()->m_views.removeAll(view);
65 }
66
67 QStringList StyleManager::styles()
68 {
69     return QStyleFactory::keys();
70 }
71
72 void StyleManager::setStyle(const QString &styleName)
73 {
74     QStyle *style = QStyleFactory::create(styleName);
75     if (style) {
76         foreach (NodeInstanceView* view, instance()->m_views)
77             view->setStyle(style);
78     }
79 }
80
81 StyleManager* StyleManager::instance()
82 {
83     static Internal::StyleManagerGuard guard; //The destructor destroys the singleton. See above
84     if (m_instance == 0)
85         m_instance = new StyleManager();
86     return m_instance;
87 }
88
89 QString StyleManager::applicationStyle()
90 {
91     QStyle *applicationStyle = qApp->style();
92     QStyle *style;
93     if (applicationStyle)
94     foreach(const QString &name, styles())
95         if ((style = QStyleFactory::create(name)) &&
96             (applicationStyle->metaObject()->className() ==
97                         style->metaObject()->className()))
98           return name;
99     return QString();
100 }
101
102
103
104
105
106 } //namespace QmlDesigner