Remove usages of Q_GLOBAL_STATIC_WITH_INITIALIZER in Qt Creator

Q_GLOBAL_STATIC_WITH_INITIALIZER will be removed in Qt5, since
it interferes with multi threading.

Change-Id: I2013091ecb0613a168cd77f56ac88edb3b97fe1d
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
This commit is contained in:
Alessandro Portale
2012-03-27 16:37:33 +02:00
committed by Friedemann Kleint
parent a3d244e970
commit 1da95bc129
5 changed files with 51 additions and 42 deletions

View File

@@ -107,8 +107,10 @@ static const FileTypeDataStorage fileTypeDataStorage[] = {
":/qt4projectmanager/images/unknown.png" } ":/qt4projectmanager/images/unknown.png" }
}; };
struct Qt4NodeStaticData { class Qt4NodeStaticData {
struct FileTypeData { public:
class FileTypeData {
public:
FileTypeData(ProjectExplorer::FileType t = ProjectExplorer::UnknownFileType, FileTypeData(ProjectExplorer::FileType t = ProjectExplorer::UnknownFileType,
const QString &tN = QString(), const QString &tN = QString(),
const QIcon &i = QIcon()) : const QIcon &i = QIcon()) :
@@ -119,16 +121,19 @@ struct Qt4NodeStaticData {
QIcon icon; QIcon icon;
}; };
Qt4NodeStaticData();
QVector<FileTypeData> fileTypeData; QVector<FileTypeData> fileTypeData;
QIcon projectIcon; QIcon projectIcon;
}; };
static void clearQt4NodeStaticData(); static void clearQt4NodeStaticData();
Q_GLOBAL_STATIC_WITH_INITIALIZER(Qt4NodeStaticData, qt4NodeStaticData, { Qt4NodeStaticData::Qt4NodeStaticData()
{
// File type data // File type data
const unsigned count = sizeof(fileTypeDataStorage)/sizeof(FileTypeDataStorage); const unsigned count = sizeof(fileTypeDataStorage)/sizeof(FileTypeDataStorage);
x->fileTypeData.reserve(count); fileTypeData.reserve(count);
// Overlay the SP_DirIcon with the custom icons // Overlay the SP_DirIcon with the custom icons
const QSize desiredSize = QSize(16, 16); const QSize desiredSize = QSize(16, 16);
@@ -141,18 +146,20 @@ Q_GLOBAL_STATIC_WITH_INITIALIZER(Qt4NodeStaticData, qt4NodeStaticData, {
QIcon folderIcon; QIcon folderIcon;
folderIcon.addPixmap(folderPixmap); folderIcon.addPixmap(folderPixmap);
const QString desc = Qt4ProjectManager::Qt4PriFileNode::tr(fileTypeDataStorage[i].typeName); const QString desc = Qt4ProjectManager::Qt4PriFileNode::tr(fileTypeDataStorage[i].typeName);
x->fileTypeData.push_back(Qt4NodeStaticData::FileTypeData(fileTypeDataStorage[i].type, fileTypeData.push_back(Qt4NodeStaticData::FileTypeData(fileTypeDataStorage[i].type,
desc, folderIcon)); desc, folderIcon));
} }
// Project icon // Project icon
const QIcon projectBaseIcon(QLatin1String(":/qt4projectmanager/images/qt_project.png")); const QIcon projectBaseIcon(QLatin1String(":/qt4projectmanager/images/qt_project.png"));
const QPixmap projectPixmap = Core::FileIconProvider::overlayIcon(QStyle::SP_DirIcon, const QPixmap projectPixmap = Core::FileIconProvider::overlayIcon(QStyle::SP_DirIcon,
projectBaseIcon, projectBaseIcon,
desiredSize); desiredSize);
x->projectIcon.addPixmap(projectPixmap); projectIcon.addPixmap(projectPixmap);
qAddPostRoutine(clearQt4NodeStaticData); qAddPostRoutine(clearQt4NodeStaticData);
}) }
Q_GLOBAL_STATIC(Qt4NodeStaticData, qt4NodeStaticData)
static void clearQt4NodeStaticData() static void clearQt4NodeStaticData()
{ {

View File

@@ -47,10 +47,7 @@ struct item
bool isDefault; bool isDefault;
}; };
typedef QVector<const item*> itemVectorType; static inline QVector<const item*> itemVector()
typedef QHash<QString, const item*> itemHashType;
const itemVectorType itemVector()
{ {
static const struct item items[] = { static const struct item items[] = {
{"core", {"core",
@@ -119,37 +116,37 @@ const itemVectorType itemVector()
false} false}
}; };
const int itemsCount = sizeof items / sizeof items[0]; const int itemsCount = sizeof items / sizeof items[0];
itemVectorType result; QVector<const item*> result;
result.reserve(itemsCount); result.reserve(itemsCount);
for (int i = 0; i < itemsCount; i++) for (int i = 0; i < itemsCount; i++)
result.append(items + i); result.append(items + i);
return result; return result;
} }
Q_GLOBAL_STATIC_WITH_INITIALIZER(itemVectorType, staticItemVector, { class StaticQtModuleInfo
*x = itemVector(); {
}) public:
StaticQtModuleInfo() : items(itemVector()) {}
Q_GLOBAL_STATIC_WITH_INITIALIZER(QStringList, staticModulesList, { const QVector<const item*> items;
const itemVectorType * const itemVector = staticItemVector(); };
for (int i = 0; i < itemVector->count(); i++)
x->append(QString::fromLatin1(itemVector->at(i)->config));
})
Q_GLOBAL_STATIC_WITH_INITIALIZER(itemHashType, staticItemHash, { Q_GLOBAL_STATIC(StaticQtModuleInfo, staticQtModuleInfo)
const itemVectorType * const itemVector = staticItemVector();
for (int i = 0; i < itemVector->count(); i++)
x->insert(QString::fromLatin1(itemVector->at(i)->config), itemVector->at(i));
})
QStringList QtModulesInfo::modules() QStringList QtModulesInfo::modules()
{ {
return *staticModulesList(); QStringList result;
foreach (const item *i, staticQtModuleInfo()->items)
result.push_back(i->config);
return result;
} }
static inline const item *itemForModule(const QString &module) static inline const item *itemForModule(const QString &module)
{ {
return staticItemHash()->value(module); foreach (const item *i, staticQtModuleInfo()->items)
if (i->config == module)
return i;
return 0;
} }
QString QtModulesInfo::moduleName(const QString &module) QString QtModulesInfo::moduleName(const QString &module)

View File

@@ -43,13 +43,20 @@
namespace QtSupport { namespace QtSupport {
namespace Internal { namespace Internal {
class AreasOfInterest {
public:
AreasOfInterest();
QMap<QString, QRect> areas;
};
AreasOfInterest::AreasOfInterest()
{
#ifdef QT_CREATOR #ifdef QT_CREATOR
Q_GLOBAL_STATIC_WITH_INITIALIZER(AreasOfInterest, areasOfInterest, { areas = ScreenshotCropper::loadAreasOfInterest(Core::ICore::resourcePath() + QLatin1String("/welcomescreen/images_areaofinterest.xml"));
*x = ScreenshotCropper::loadAreasOfInterest(Core::ICore::resourcePath() + QLatin1String("/welcomescreen/images_areaofinterest.xml"));
})
#else
Q_GLOBAL_STATIC(AreasOfInterest, areasOfInterest)
#endif // QT_CREATOR #endif // QT_CREATOR
}
Q_GLOBAL_STATIC(AreasOfInterest, welcomeScreenAreas)
static inline QString fileNameForPath(const QString &path) static inline QString fileNameForPath(const QString &path)
{ {
@@ -81,7 +88,7 @@ static QRect cropRectForAreaOfInterest(const QSize &imageSize, const QSize &crop
QImage ScreenshotCropper::croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize) QImage ScreenshotCropper::croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize)
{ {
const QRect areaOfInterest = areasOfInterest()->value(fileNameForPath(filePath)); const QRect areaOfInterest = welcomeScreenAreas()->areas.value(fileNameForPath(filePath));
if (areaOfInterest.isValid()) { if (areaOfInterest.isValid()) {
const QRect cropRect = cropRectForAreaOfInterest(sourceImage.size(), cropSize, areaOfInterest); const QRect cropRect = cropRectForAreaOfInterest(sourceImage.size(), cropSize, areaOfInterest);
@@ -113,9 +120,9 @@ static const QString xmlAttributeY = QLatin1String("y");
static const QString xmlAttributeWidth = QLatin1String("width"); static const QString xmlAttributeWidth = QLatin1String("width");
static const QString xmlAttributeHeight = QLatin1String("height"); static const QString xmlAttributeHeight = QLatin1String("height");
AreasOfInterest ScreenshotCropper::loadAreasOfInterest(const QString &areasXmlFile) QMap<QString, QRect> ScreenshotCropper::loadAreasOfInterest(const QString &areasXmlFile)
{ {
AreasOfInterest areasOfInterest; QMap<QString, QRect> areasOfInterest;
QFile xmlFile(areasXmlFile); QFile xmlFile(areasXmlFile);
if (!xmlFile.open(QIODevice::ReadOnly)) { if (!xmlFile.open(QIODevice::ReadOnly)) {
qWarning() << Q_FUNC_INFO << "Could not open file" << areasXmlFile; qWarning() << Q_FUNC_INFO << "Could not open file" << areasXmlFile;
@@ -144,7 +151,7 @@ AreasOfInterest ScreenshotCropper::loadAreasOfInterest(const QString &areasXmlFi
return areasOfInterest; return areasOfInterest;
} }
bool ScreenshotCropper::saveAreasOfInterest(const QString &areasXmlFile, AreasOfInterest &areas) bool ScreenshotCropper::saveAreasOfInterest(const QString &areasXmlFile, QMap<QString, QRect> &areas)
{ {
QFile file(areasXmlFile); QFile file(areasXmlFile);
if (!file.open(QIODevice::WriteOnly)) if (!file.open(QIODevice::WriteOnly))

View File

@@ -40,14 +40,12 @@
namespace QtSupport { namespace QtSupport {
namespace Internal { namespace Internal {
typedef QMap<QString, QRect> AreasOfInterest;
class ScreenshotCropper class ScreenshotCropper
{ {
public: public:
static QImage croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize); static QImage croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize);
static AreasOfInterest loadAreasOfInterest(const QString &areasXmlFile); static QMap<QString, QRect> loadAreasOfInterest(const QString &areasXmlFile);
static bool saveAreasOfInterest(const QString &areasXmlFile, AreasOfInterest &areas); static bool saveAreasOfInterest(const QString &areasXmlFile, QMap<QString, QRect> &areas);
}; };
} // namespace Internal } // namespace Internal

View File

@@ -58,7 +58,7 @@ public slots:
void saveData(); void saveData();
private: private:
AreasOfInterest m_areasOfInterest; QMap<QString, QRect> m_areasOfInterest;
QString m_areasOfInterestFile; QString m_areasOfInterestFile;
QString m_imagesFolder; QString m_imagesFolder;
Ui::ScreenShotCropperWindow *ui; Ui::ScreenShotCropperWindow *ui;