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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include "formmanager.h"
#include "services/pluginmanager.h"
#include "uiloader.h"
#include "common/unused.h"
#include "common/global.h"
#include <QFile>
#include <QDir>
#include <QRegularExpression>
#include <QApplication>
#include <QDebug>
FormManager::FormManager()
{
init();
}
FormManager::~FormManager()
{
if (uiLoader)
{
delete uiLoader;
uiLoader = nullptr;
}
}
QWidget* FormManager::createWidget(const QString& name)
{
if (!widgetNameToFullPath.contains(name))
{
qCritical() << "Asked for widget name which isn't managed by FormManager:" << name << ", while available widgets are:"
<< widgetNameToFullPath.keys();
return nullptr;
}
return createWidgetByFullPath(widgetNameToFullPath[name]);
}
bool FormManager::hasWidget(const QString& name)
{
return widgetNameToFullPath.contains(name);
}
QStringList FormManager::getAvailableForms() const
{
return widgetNameToFullPath.keys();
}
QStringList FormManager::getFormDirs() const
{
return formDirs;
}
QWidget* FormManager::createWidgetByFullPath(const QString& path)
{
QWidget* widget = uiLoader->load(path);
if (!widget)
{
qCritical() << "Error occured while loading ui file:" << path << ". Error message: "
<< uiLoader->errorString();
return nullptr;
}
return widget;
}
void FormManager::rescanResources(Plugin* plugin, PluginType* pluginType)
{
UNUSED(pluginType);
rescanResources(plugin->getName());
}
void FormManager::rescanResources(const QString& pluginName)
{
if (PLUGINS->isBuiltIn(pluginName))
return;
for (const QString& widgetName : resourceForms)
widgetNameToFullPath.remove(widgetName);
resourceForms.clear();
loadRecurently(":/forms", "");
}
void FormManager::pluginsAboutToMassUnload()
{
disconnect(PLUGINS, SIGNAL(loaded(Plugin*,PluginType*)), this, SLOT(rescanResources(Plugin*,PluginType*)));
disconnect(PLUGINS, SIGNAL(unloaded(QString,PluginType*)), this, SLOT(rescanResources(QString)));
}
void FormManager::pluginsInitiallyLoaded()
{
load();
connect(PLUGINS, SIGNAL(loaded(Plugin*,PluginType*)), this, SLOT(rescanResources(Plugin*,PluginType*)));
connect(PLUGINS, SIGNAL(unloaded(QString,PluginType*)), this, SLOT(rescanResources(QString)));
connect(PLUGINS, SIGNAL(aboutToQuit()), this, SLOT(pluginsAboutToMassUnload()));
disconnect(PLUGINS, SIGNAL(pluginsInitiallyLoaded()), this, SLOT(pluginsInitiallyLoaded()));
}
void FormManager::init()
{
uiLoader = new UiLoader();
if (PLUGINS->arePluginsInitiallyLoaded())
pluginsInitiallyLoaded();
else
connect(PLUGINS, SIGNAL(pluginsInitiallyLoaded()), this, SLOT(pluginsInitiallyLoaded()));
}
void FormManager::load()
{
formDirs += qApp->applicationDirPath() + "/forms";
formDirs += ":/forms";
formDirs += QDir(CFG->getConfigDir()).absoluteFilePath("forms");
QString envDirs = SQLITESTUDIO->getEnv("SQLITESTUDIO_FORMS");
if (!envDirs.isNull())
formDirs += envDirs.split(PATH_LIST_SEPARATOR);
formDirs += PLUGINS->getPluginDirs();
#ifdef FORMS_DIR
formDirs += STRINGIFY(FORMS_DIR);
#endif
for (QString dirPath : formDirs)
loadRecurently(dirPath, "");
}
void FormManager::loadRecurently(const QString& path, const QString& prefix)
{
static const QStringList fileExtensions = {"*.ui", "*.UI"};
QDir dir(path);
QString fullPath;
QString widgetName;
for (const QFileInfo& entry : dir.entryInfoList(fileExtensions, QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot|QDir::Readable))
{
fullPath = entry.absoluteFilePath();
if (entry.isDir())
{
loadRecurently(fullPath, prefix+entry.fileName()+"_");
continue;
}
qDebug().noquote() << "Loading form file:" << toNativePath(fullPath);
widgetName = getWidgetName(fullPath);
if (widgetName.isNull())
continue;
if (widgetNameToFullPath.contains(widgetName))
{
qCritical() << "Widget named" << widgetName << "was already loaded by FormManager from file" << widgetNameToFullPath[widgetName]
<< "therefore file" << fullPath << "will be ignored";
continue;
}
widgetNameToFullPath[widgetName] = fullPath;
if (fullPath.startsWith(":/"))
resourceForms << widgetName;
}
}
QString FormManager::getWidgetName(const QString& path)
{
static const QRegularExpression re(R"(<widget class\=\"\w+\" name\=\"(\w+)\">)");
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
{
qWarning() << "Could not open" << path << "for reading. Form file ignored.";
return QString();
}
QString contents = file.readAll();
file.close();
QRegularExpressionMatch match = re.match(contents);
if (!match.hasMatch())
{
qWarning() << "Could not match widget in" << path << " document. File ignored.";
return QString();
}
QString widgetName = match.captured(1);
return widgetName;
}
|