summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/Palettes/Inspector/MaterialRefView.cpp
blob: b2d1a595ba0b727e6cec572c67b2ab7f7ce5f1ac (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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "MaterialRefView.h"
#include "StudioApp.h"
#include "StudioPreferences.h"
#include "Doc.h"
#include "Core.h"
#include "Qt3DSDMStudioSystem.h"
#include "ClientDataModelBridge.h"
#include "IObjectReferenceHelper.h"
#include "IDocumentEditor.h"

#include <QtCore/qtimer.h>

MaterialRefView::MaterialRefView(QWidget *parent)
: QListWidget(parent)
{
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
    setResizeMode(QListWidget::Fixed);
}

/**
 * Gather and display the currently used standard and custom material list
 *
 * @param instance The instance that owns the property handle
 * @param handle The property handle this materials list is for
 *
 * @return necessary size for the dialog
 */
QSize MaterialRefView::refreshMaterials(int instance, int handle)
{
    clear(); // clear old material list

    m_instance = instance;
    m_handle = handle;
    qt3dsdm::Qt3DSDMInstanceHandle refInstance = getRefInstance();

    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    const auto propertySystem = doc->GetStudioSystem()->GetPropertySystem();
    auto bridge = doc->GetStudioSystem()->GetClientDataModelBridge();
    static const QPixmap pixMaterialNormal = QPixmap(":/images/Objects-Material-Normal.png");

    QVector<qt3dsdm::Qt3DSDMInstanceHandle> mats;
    doc->getSceneMaterials(doc->GetSceneInstance(), mats);

    // add freshly collected material list
    for (auto matInstance : qAsConst(mats)) {
        qt3dsdm::SValue v;
        propertySystem->GetInstancePropertyValue(matInstance,
                                bridge->GetObjectDefinitions().m_Named.m_NameProp, v);
        QString matName = qt3dsdm::get<QString>(v);

        // get the material's object name (parent)
        qt3dsdm::Qt3DSDMInstanceHandle parentInstance = bridge->GetParentInstance(matInstance);
        qt3dsdm::SValue vParent;
        propertySystem->GetInstancePropertyValue(parentInstance,
                                bridge->GetObjectDefinitions().m_Named.m_NameProp, vParent);
        QString objName = qt3dsdm::get<QString>(vParent);
        matName.append(QLatin1String(" (") + objName + QLatin1String(")"));

        QListWidgetItem *matItem = new QListWidgetItem(this);
        matItem->setData(Qt::DisplayRole, matName);
        matItem->setData(Qt::DecorationRole, pixMaterialNormal);
        matItem->setData(Qt::UserRole, QVariant(matInstance));

        if (matInstance == refInstance)
            setCurrentItem(matItem);
    }

    if (count() == 0) {
        // Show an unselectable dummy item
        static const QPixmap pixWarning = QPixmap(":/images/warning.png");
        QListWidgetItem *matItem = new QListWidgetItem(this);
        matItem->setData(Qt::DisplayRole, tr("No animatable materials found"));
        matItem->setData(Qt::DecorationRole, pixWarning);
        matItem->setData(Qt::UserRole, -1);
        setSelectionMode(QAbstractItemView::NoSelection);
    } else {
        setSelectionMode(QAbstractItemView::SingleSelection);
    }

    QSize widgetSize(CStudioPreferences::valueWidth(),
                     qMin(10, count()) * CStudioPreferences::controlBaseHeight());

    return widgetSize;
}

void MaterialRefView::updateSelection()
{
    int refInstance = getRefInstance();
    for (int i = 0, itemCount = count(); i < itemCount; ++i) {
        int matInstance = item(i)->data(Qt::UserRole).toInt();
        if (matInstance == refInstance) {
            setCurrentRow(i);
            break;
        }
    }
}

bool MaterialRefView::isFocused() const
{
    return hasFocus();
}

int MaterialRefView::getRefInstance() const
{
    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    const auto propertySystem = doc->GetStudioSystem()->GetPropertySystem();

    qt3dsdm::SValue value;
    propertySystem->GetInstancePropertyValue(m_instance, m_handle, value);
    return doc->GetDataModelObjectReferenceHelper()->Resolve(value, m_instance);
}

void MaterialRefView::focusInEvent(QFocusEvent *event)
{
    QAbstractItemView::focusInEvent(event);
    emit focusChanged();
}

void MaterialRefView::focusOutEvent(QFocusEvent *event)
{
    QAbstractItemView::focusOutEvent(event);
    emit focusChanged();
    QTimer::singleShot(0, this, &QAbstractItemView::close);
}