Repository navigation
Expand file tree
/
Copy pathStylePlugin.cpp
More file actions
154 lines (116 loc) · 3.67 KB
/
Copy pathStylePlugin.cpp
File metadata and controls
154 lines (116 loc) · 3.67 KB
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
// Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
#include "StdAfx.h"
#include "StylePlugin.h"
#include <CryCore/Platform/platform_impl.inl>
#include <Commands/ICommandManager.h>
#include <QDir>
#include "CryString/CryPath.h"
#include "CrySerialization/Decorators/Resources.h"
#include "QFileDialog"
#include "QApplication"
#include "EditorStyleHelper.h"
#include <Serialization/Decorators/EditorActionButton.h>
#include <QFileSystemWatcher>
#include <QPixmapCache>
#include <QToolButton>
#include <QObject>
#include <QIconEngine>
#include <QtGui/private/qicon_p.h>
#include "QResource"
#include "IconPackManger.h"
CStylePreferences gStylePreferences;
static CStylePlugin* g_pStylePlugin = nullptr;
void OnStyleFileChanged() {
g_pStylePlugin->ReloadStyle();
}
#pragma optimize( "", off )
CStylePlugin::CStylePlugin()
: m_pPackManager(std::make_unique<IconPackManager>())
{
g_pStylePlugin = this;
m_pWatcher = std::make_unique<QFileSystemWatcher>();
gStylePreferences.signalSettingsChanged.Connect(this, &CStylePlugin::OnStyleSettingsChanged);
QObject::connect(&*m_pWatcher, &QFileSystemWatcher::fileChanged, OnStyleFileChanged);
}
#pragma optimize( "", on )
CStylePlugin::~CStylePlugin()
{
g_pStylePlugin = nullptr;
}
void CStylePlugin::OnStyleSettingsChanged()
{
auto& styleSheet = gStylePreferences.GetStyleSheetPath();
if (!styleSheet.empty() && styleSheet != m_styleSheetPath)
{
m_pWatcher->removePath(m_styleSheetPath.c_str());
m_styleSheetPath = styleSheet;
m_pWatcher->addPath(m_styleSheetPath.c_str());
ReloadStyle();
}
}
void CStylePlugin::OnEditorNotifyEvent(EEditorNotifyEvent event)
{
if (event == EEditorNotifyEvent::eNotify_OnInit)
{
OnStyleSettingsChanged();
}
else if (event == EEditorNotifyEvent::eNotify_OnMainFrameInitialized)
{
//Force a style reload
OnStyleSettingsChanged();
ReloadStyle();
ReloadIcons();
}
else if (event == eNotify_OnQuit)
{
gStylePreferences.signalSettingsChanged.DisconnectObject(this);
}
}
void CStylePlugin::ReloadStyle()
{
if (m_styleSheetPath.empty())
return;
QFile styleSheetFile(m_styleSheetPath.c_str());
if (styleSheetFile.open(QFile::ReadOnly))
{
qApp->setStyleSheet(styleSheetFile.readAll());
qApp->setPalette(GetStyleHelper()->palette());
}
}
void CStylePlugin::ReloadIcons()
{
m_pPackManager->ReloadIcons();
}
REGISTER_PLUGIN(CStylePlugin);
constexpr char* g_styleSheetPath = "Editor/Styles/stylesheet.qss";
constexpr char* g_iconsFolder = "Editor/Icons/";
CStylePreferences::CStylePreferences()
: SPreferencePage("Style Preferences", "General/Style")
, m_stylePath(PathUtil::Make(PathUtil::GetEnginePath(), g_styleSheetPath))
{
}
bool CStylePreferences::Serialize(yasli::Archive& ar)
{
ar(Serialization::MakeResourceSelector<>(m_stylePath, "Style"), "stylesheet", "Stylesheet");
g_pStylePlugin->GetIconPackManager()->SerializePacks(ar);
if (ar.isInput())
signalSettingsChanged();
return true;
}
REGISTER_PREFERENCES_PAGE_PTR(CStylePreferences, &gStylePreferences)
SResourceSelectionResult StyleSelector(const SResourceSelectorContext& context, const char* previousValue)
{
SResourceSelectionResult result{ false, previousValue };
// start in Sounds folder if no sound is selected
string startPath = PathUtil::GetPathWithoutFilename(previousValue);
if (startPath.empty())
startPath = PathUtil::Make(PathUtil::GetEnginePath(), "Editor/Styles");
auto fileName = QFileDialog::getOpenFileName(context.parentWidget, "Open Stylesheet", startPath.c_str(), "QT Stylesheets (*.qss)");
if (!fileName.isEmpty())
{
result.selectedResource = fileName.toStdString().c_str();
result.selectionAccepted = true;
}
return result;
}
REGISTER_RESOURCE_SELECTOR("Style", StyleSelector, "")