Skip to content

Commit

Permalink
Merge pull request commontk#505 from kislinsk/504-qt5-fixes
Browse files Browse the repository at this point in the history
Make CTK compatible with Qt5 again.
  • Loading branch information
saschazelzer committed Oct 13, 2014
2 parents 2fc5562 + b9393da commit 29715cb
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 120 deletions.
16 changes: 13 additions & 3 deletions CMake/ctkMacroSetupPlugins.cmake
Expand Up @@ -113,9 +113,19 @@ macro(ctkMacroSetupPlugins )

# Set the variable QT_INSTALLED_LIBRARY_DIR that contains all
# Qt shared libraries
set(QT_INSTALLED_LIBRARY_DIR ${QT_LIBRARY_DIR})
if(WIN32)
get_filename_component(QT_INSTALLED_LIBRARY_DIR ${QT_QMAKE_EXECUTABLE} PATH)
if (CTK_QT_VERSION VERSION_GREATER "4")
if(WIN32)
get_target_property(_qt5_moc_executable Qt5::moc LOCATION)
get_filename_component(QT_INSTALLED_LIBRARY_DIR ${_qt5_moc_executable} PATH)
else()
get_target_property(_qt5_core_lib Qt5::Core LOCATION)
get_filename_component(QT_INSTALLED_LIBRARY_DIR ${_qt5_core_lib} PATH)
endif()
else()
set(QT_INSTALLED_LIBRARY_DIR ${QT_LIBRARY_DIR})
if(WIN32)
get_filename_component(QT_INSTALLED_LIBRARY_DIR ${QT_QMAKE_EXECUTABLE} PATH)
endif()
endif()

set(plugin_symbolic_names )
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -375,7 +375,7 @@ if(MSVC)
set(msvc_suppressed_warnings
"/wd4290" # C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
)
set(CTK_CXX_FLAGS "${CTK_CXX_FLAGS} ${msvc_suppressed_warnings}")
set(CTK_CXX_FLAGS "${CTK_CXX_FLAGS} ${msvc_suppressed_warnings} -DNOMINMAX")
endif()

#-----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Libs/Core/Testing/Cpp/ctkHighPrecisionTimerTest.cpp
Expand Up @@ -30,7 +30,7 @@ int ctkHighPrecisionTimerTest(int /*argc*/, char* /*argv*/[])
{
ctkHighPrecisionTimer timer;
timer.start();
QTest::qSleep(200);
QTest::qSleep(250);
qint64 millis = timer.elapsedMilli();
qint64 micros = timer.elapsedMicro();
if (millis < 200 || millis > 300 ||
Expand Down
2 changes: 1 addition & 1 deletion Libs/Core/ctkErrorLogQtMessageHandler.cpp
Expand Up @@ -97,7 +97,7 @@ void ctkErrorLogModelQtMessageOutput(QtMsgType type, const QMessageLogContext& c
// // }
handler->handleMessage(
ctk::qtHandleToString(QThread::currentThreadId()),
level, handler->handlerPrettyName(), msg);
level, handler->handlerPrettyName(), ctkErrorLogContext(msg), msg);
}
}
#else
Expand Down
9 changes: 8 additions & 1 deletion Libs/Widgets/CMakeLists.txt
Expand Up @@ -98,6 +98,8 @@ set(KIT_SRCS
ctkHistogram.h
ctkIconEnginePlugin.cpp
ctkIconEnginePlugin.h
ctkIconEnginePlugin_qt4.h
ctkIconEnginePlugin_qt5.h
ctkLanguageComboBox.cpp
ctkLanguageComboBox.h
ctkLayoutFactory.cpp
Expand Down Expand Up @@ -250,7 +252,6 @@ set(KIT_MOC_SRCS
ctkFlowLayout.h
ctkFontButton.h
ctkHistogram.h
ctkIconEnginePlugin.h
ctkLanguageComboBox.h
ctkLayoutFactory.h
ctkLayoutManager.h
Expand Down Expand Up @@ -304,6 +305,12 @@ set(KIT_MOC_SRCS
ctkWorkflowWidgetStep_p.h
)

if(CTK_QT_VERSION VERSION_GREATER "4")
list(APPEND KIT_MOC_SRCS ctkIconEnginePlugin_qt5.h)
else()
list(APPEND KIT_MOC_SRCS ctkIconEnginePlugin_qt4.h)
endif()

# Headers that should run through moc without adding
# the generated cpp file to the source list
set(KIT_GENERATE_MOC_SRCS
Expand Down
119 changes: 9 additions & 110 deletions Libs/Widgets/ctkIconEnginePlugin.h
Expand Up @@ -21,117 +21,16 @@
#ifndef __ctkIconEnginePlugin_h
#define __ctkIconEnginePlugin_h

// Qt includes
#if QT_VERSION >= 0x050000
# include <QIconEngine>
# include <QIconEnginePlugin>
#include <QtGlobal>

// The MOC is not able to parse "#if QT_VERSION >= 0x..." in Qt 5 and
// "#if QT_VERSION >= QT_VERSION_CHECK(...)" in Qt4. As a workaround,
// we check for the Qt version here (not parsed by MOC) and provide two
// versions of the actual header file according to the used Qt version.
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
# include <ctkIconEnginePlugin_qt5.h>
#else
# include <QIconEngineV2>
# include <QIconEnginePluginV2>
# include <ctkIconEnginePlugin_qt4.h>
#endif

// CTK includes
#include "ctkPimpl.h"
#include "ctkPixmapIconEngine.h"
#include "ctkWidgetsExport.h"

class ctkIconEnginePluginPrivate;
class ctkIconEnginePrivate;

/// \ingroup Widgets
/// ctkIconEnginePlugin must be loaded when starting the application.
/// \code
/// QApplication myApp;
/// QCoreApplication::addLibraryPath("MyApp-build/plugins");
/// \endcode
/// where the plugin must be located in "MyApp-build/plugins/iconengines"
/// don't forget to declare in the cpp file:
/// Q_EXPORT_PLUGIN2(yourpluginName, ctkIconEnginePlugin)
class CTK_WIDGETS_EXPORT ctkIconEnginePlugin
// Can't use QT_VERSION_CHECK macro for Qt 4 moc parsing
#if QT_VERSION >= 0x050000
: public QIconEnginePlugin
#else
: public QIconEnginePluginV2
#endif
{
Q_OBJECT;
public:
ctkIconEnginePlugin(QObject* parent = 0);
virtual ~ctkIconEnginePlugin();

#if QT_VERSION >= 0x050000
virtual QIconEngine* create(const QString& filename=QString());
#else
virtual QIconEngineV2* create(const QString& filename=QString());
#endif
/// Support all the Qt image formats by default
virtual QStringList keys()const;

/// Directory list given to the created icon engines
/// Subdirectories where the icons should be searched, typically:
/// "Small", "Medium", "Large", "XLarge" or
/// "16x16", "32x32", "64x64", "128x128" or
/// "LowDef", "HighDef"
/// \sa ctkIconEnginePlugin::setSizeDirectories
void setSizeDirectories(const QStringList& sizeDirectories);
QStringList sizeDirectories()const;
protected:
QScopedPointer<ctkIconEnginePluginPrivate> d_ptr;

private:
Q_DECLARE_PRIVATE(ctkIconEnginePlugin);
Q_DISABLE_COPY(ctkIconEnginePlugin);
};

//------------------------------------------------------------------------------
/// \ingroup Widgets
/// ctkIconEngine is an icon engine that behaves like the default Qt icon engine
/// QPixmapIconEngine(ctkPixmapIconEngine)), but can automatically support icons
/// in multiple size. When adding a file to an icon, it will automatically check
/// if the same file name exists in a different directory. This allows the
/// application to contains icons in different size,e.g. :/Icons/Small/edit.png
/// and :/Icons/Large/edit.png.
/// Without ctkIconEngine, QIcon already support mutltiple files:
/// \code
/// QIcon editIcon;
/// editIcon.addFile(":/Icons/Small/edit.png");
/// editIcon.addFile(":/Icons/Large/edit.png");
/// \endcode
/// Using ctkIconEngine, adding a file to an icon will automatically search for
/// any icon in a different directory:
/// \code
/// ctkIconEngine* autoIconEngine;
/// autoIconEngine->setSizeDirectories(QStringList() << "Large" << "Small";
/// QIcon editIcon(autoIconEngine);
/// editIcon.addFile(":/Icons/Small/edit.png");
/// \endcode
/// where the large version of the icon is automatically added.
/// It is mostly useful when using the designer, where only 1 icon file can
/// be specified. It must be used with ctkIconEnginePlugin
/// TODO: support more than just files in ressources.
class CTK_WIDGETS_EXPORT ctkIconEngine: public ctkPixmapIconEngine
{
public:
typedef ctkPixmapIconEngine Superclass;
ctkIconEngine();
virtual ~ctkIconEngine();
virtual void addFile(const QString& fileName, const QSize& size,
QIcon::Mode mode, QIcon::State state);
/// Subdirectories where the icons should be searched, typically:
/// "Small", "Medium", "Large", "XLarge" or
/// "16x16", "32x32", "64x64", "128x128" or
/// "LowDef", "HighDef"
void setSizeDirectories(const QStringList& sizeDirectories);
QStringList sizeDirectories()const;

virtual QString key()const;

protected:
QScopedPointer<ctkIconEnginePrivate> d_ptr;

private:
Q_DECLARE_PRIVATE(ctkIconEngine);
Q_DISABLE_COPY(ctkIconEngine);
};
#endif
125 changes: 125 additions & 0 deletions Libs/Widgets/ctkIconEnginePlugin_qt4.h
@@ -0,0 +1,125 @@
/*=========================================================================
Library: CTK
Copyright (c) Kitware Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

#ifndef __ctkIconEnginePlugin_qt4_h
#define __ctkIconEnginePlugin_qt4_h

// Qt includes
# include <QIconEngineV2>
# include <QIconEnginePluginV2>

// CTK includes
#include "ctkPimpl.h"
#include "ctkPixmapIconEngine.h"
#include "ctkWidgetsExport.h"

class ctkIconEnginePluginPrivate;
class ctkIconEnginePrivate;

/// \ingroup Widgets
/// ctkIconEnginePlugin must be loaded when starting the application.
/// \code
/// QApplication myApp;
/// QCoreApplication::addLibraryPath("MyApp-build/plugins");
/// \endcode
/// where the plugin must be located in "MyApp-build/plugins/iconengines"
/// don't forget to declare in the cpp file:
/// Q_EXPORT_PLUGIN2(yourpluginName, ctkIconEnginePlugin)
class CTK_WIDGETS_EXPORT ctkIconEnginePlugin
: public QIconEnginePluginV2
{
Q_OBJECT;
public:
ctkIconEnginePlugin(QObject* parent = 0);
virtual ~ctkIconEnginePlugin();

virtual QIconEngineV2* create(const QString& filename=QString());

/// Support all the Qt image formats by default
virtual QStringList keys()const;

/// Directory list given to the created icon engines
/// Subdirectories where the icons should be searched, typically:
/// "Small", "Medium", "Large", "XLarge" or
/// "16x16", "32x32", "64x64", "128x128" or
/// "LowDef", "HighDef"
/// \sa ctkIconEnginePlugin::setSizeDirectories
void setSizeDirectories(const QStringList& sizeDirectories);
QStringList sizeDirectories()const;

protected:
QScopedPointer<ctkIconEnginePluginPrivate> d_ptr;

private:
Q_DECLARE_PRIVATE(ctkIconEnginePlugin);
Q_DISABLE_COPY(ctkIconEnginePlugin);
};

//------------------------------------------------------------------------------
/// \ingroup Widgets
/// ctkIconEngine is an icon engine that behaves like the default Qt icon engine
/// QPixmapIconEngine(ctkPixmapIconEngine)), but can automatically support icons
/// in multiple size. When adding a file to an icon, it will automatically check
/// if the same file name exists in a different directory. This allows the
/// application to contains icons in different size,e.g. :/Icons/Small/edit.png
/// and :/Icons/Large/edit.png.
/// Without ctkIconEngine, QIcon already support mutltiple files:
/// \code
/// QIcon editIcon;
/// editIcon.addFile(":/Icons/Small/edit.png");
/// editIcon.addFile(":/Icons/Large/edit.png");
/// \endcode
/// Using ctkIconEngine, adding a file to an icon will automatically search for
/// any icon in a different directory:
/// \code
/// ctkIconEngine* autoIconEngine;
/// autoIconEngine->setSizeDirectories(QStringList() << "Large" << "Small";
/// QIcon editIcon(autoIconEngine);
/// editIcon.addFile(":/Icons/Small/edit.png");
/// \endcode
/// where the large version of the icon is automatically added.
/// It is mostly useful when using the designer, where only 1 icon file can
/// be specified. It must be used with ctkIconEnginePlugin
/// TODO: support more than just files in ressources.
class CTK_WIDGETS_EXPORT ctkIconEngine: public ctkPixmapIconEngine
{
public:
typedef ctkPixmapIconEngine Superclass;
ctkIconEngine();
virtual ~ctkIconEngine();
virtual void addFile(const QString& fileName, const QSize& size,
QIcon::Mode mode, QIcon::State state);
/// Subdirectories where the icons should be searched, typically:
/// "Small", "Medium", "Large", "XLarge" or
/// "16x16", "32x32", "64x64", "128x128" or
/// "LowDef", "HighDef"
void setSizeDirectories(const QStringList& sizeDirectories);
QStringList sizeDirectories()const;

virtual QString key()const;

protected:
QScopedPointer<ctkIconEnginePrivate> d_ptr;

private:
Q_DECLARE_PRIVATE(ctkIconEngine);
Q_DISABLE_COPY(ctkIconEngine);
};
#endif

0 comments on commit 29715cb

Please sign in to comment.