Commit e669f054 authored by Christian Kandeler's avatar Christian Kandeler Committed by hjk
Browse files

Utils: Introduce HostOsInfo class.



The class' member functions are intended to be used
instead of the Q_OS_* macros in all contexts where
the latter are not syntactically required.
This lowers the likelihood of changes made on one
platform breaking the build on another, e.g. due to
the code model missing symbols in #ifdef'ed out code
when refactoring.

Change-Id: I4a54788591b4c8f8d589b8368a6c683d4155c9fa
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent b674b59b
......@@ -29,6 +29,9 @@
**************************************************************************/
#include "contextpanewidget.h"
#include <utils/hostosinfo.h>
#include <QToolButton>
#include <QFontComboBox>
#include <QComboBox>
......@@ -49,6 +52,8 @@
#include "customcolordialog.h"
#include "colorbutton.h"
using namespace Utils;
namespace QmlEditorWidgets {
/* XPM */
......@@ -102,12 +107,12 @@ DragWidget::DragWidget(QWidget *parent) : QFrame(parent)
// TODO: The following code should be enabled for OSX
// when QTBUG-23205 is fixed
#ifndef Q_OS_MAC
m_dropShadowEffect = new QGraphicsDropShadowEffect;
m_dropShadowEffect->setBlurRadius(6);
m_dropShadowEffect->setOffset(2, 2);
setGraphicsEffect(m_dropShadowEffect);
#endif
if (!HostOsInfo::isMacHost()) {
m_dropShadowEffect = new QGraphicsDropShadowEffect;
m_dropShadowEffect->setBlurRadius(6);
m_dropShadowEffect->setOffset(2, 2);
setGraphicsEffect(m_dropShadowEffect);
}
}
void DragWidget::mousePressEvent(QMouseEvent * event)
......@@ -127,12 +132,12 @@ void DragWidget::mouseReleaseEvent(QMouseEvent *event)
m_startPos = QPoint(-1, -1);
// TODO: The following code should be enabled for OSX
// when QTBUG-23205 is fixed
#ifndef Q_OS_MAC
m_dropShadowEffect = new QGraphicsDropShadowEffect;
m_dropShadowEffect->setBlurRadius(6);
m_dropShadowEffect->setOffset(2, 2);
setGraphicsEffect(m_dropShadowEffect);
#endif
if (!HostOsInfo::isMacHost()) {
m_dropShadowEffect = new QGraphicsDropShadowEffect;
m_dropShadowEffect->setBlurRadius(6);
m_dropShadowEffect->setOffset(2, 2);
setGraphicsEffect(m_dropShadowEffect);
}
}
QFrame::mouseReleaseEvent(event);
}
......@@ -177,16 +182,14 @@ void DragWidget::protectedMoved()
void DragWidget::leaveEvent(QEvent *)
{
#ifdef Q_OS_MAC
unsetCursor();
#endif
if (HostOsInfo::isMacHost())
unsetCursor();
}
void DragWidget::enterEvent(QEvent *)
{
#ifdef Q_OS_MAC
setCursor(Qt::ArrowCursor);
#endif
if (HostOsInfo::isMacHost())
setCursor(Qt::ArrowCursor);
}
ContextPaneWidget::ContextPaneWidget(QWidget *parent) : DragWidget(parent), m_currentWidget(0)
......@@ -232,9 +235,8 @@ ContextPaneWidget::ContextPaneWidget(QWidget *parent) : DragWidget(parent), m_cu
m_disableAction->setCheckable(true);
connect(m_disableAction.data(), SIGNAL(toggled(bool)), this, SLOT(onDisable(bool)));
m_pinned = false;
#ifdef Q_OS_MAC
setCursor(Qt::ArrowCursor);
#endif
if (HostOsInfo::isMacHost())
setCursor(Qt::ArrowCursor);
}
ContextPaneWidget::~ContextPaneWidget()
......
......@@ -29,6 +29,9 @@
**************************************************************************/
#include "contextpanewidgetimage.h"
#include <utils/hostosinfo.h>
#include "ui_contextpanewidgetimage.h"
#include "ui_contextpanewidgetborderimage.h"
#include <qmljs/qmljspropertyreader.h>
......@@ -611,12 +614,12 @@ PreviewLabel::PreviewLabel(QWidget *parent)
// TODO: The following code should be enabled for OSX
// when QTBUG-23205 is fixed
#ifndef Q_OS_MAC
QGraphicsDropShadowEffect *dropShadowEffect = new QGraphicsDropShadowEffect;
dropShadowEffect->setBlurRadius(4);
dropShadowEffect->setOffset(2, 2);
m_hooverInfo->setGraphicsEffect(dropShadowEffect);
#endif
if (!Utils::HostOsInfo::isMacHost()) {
QGraphicsDropShadowEffect *dropShadowEffect = new QGraphicsDropShadowEffect;
dropShadowEffect->setBlurRadius(4);
dropShadowEffect->setOffset(2, 2);
m_hooverInfo->setGraphicsEffect(dropShadowEffect);
}
m_hooverInfo->setAutoFillBackground(true);
m_hooverInfo->raise();
}
......
......@@ -32,6 +32,8 @@
#include "huecontrol.h"
#include "colorbox.h"
#include <utils/hostosinfo.h>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
......@@ -41,6 +43,8 @@
#include <QDialogButtonBox>
#include <QGraphicsEffect>
using namespace Utils;
namespace QmlEditorWidgets {
CustomColorDialog::CustomColorDialog(QWidget *parent) : QFrame(parent )
......@@ -52,12 +56,12 @@ CustomColorDialog::CustomColorDialog(QWidget *parent) : QFrame(parent )
// TODO: The following code should be enabled for OSX
// when QTBUG-23205 is fixed
#ifndef Q_OS_MAC
QGraphicsDropShadowEffect *dropShadowEffect = new QGraphicsDropShadowEffect;
dropShadowEffect->setBlurRadius(6);
dropShadowEffect->setOffset(2, 2);
setGraphicsEffect(dropShadowEffect);
#endif
if (!HostOsInfo::isMacHost()) {
QGraphicsDropShadowEffect *dropShadowEffect = new QGraphicsDropShadowEffect;
dropShadowEffect->setBlurRadius(6);
dropShadowEffect->setOffset(2, 2);
setGraphicsEffect(dropShadowEffect);
}
setAutoFillBackground(true);
m_hueControl = new HueControl(this);
......@@ -184,16 +188,14 @@ void CustomColorDialog::setupWidgets()
void CustomColorDialog::leaveEvent(QEvent *)
{
#ifdef Q_OS_MAC
unsetCursor();
#endif
if (HostOsInfo::isMacHost())
unsetCursor();
}
void CustomColorDialog::enterEvent(QEvent *)
{
#ifdef Q_OS_MAC
setCursor(Qt::ArrowCursor);
#endif
if (HostOsInfo::isMacHost())
setCursor(Qt::ArrowCursor);
}
......
......@@ -7,5 +7,6 @@ unix:QMAKE_CXXFLAGS_DEBUG += -O3
include(../../qtcreatorlibrary.pri)
include(../qmljs/qmljs.pri)
include(../utils/utils.pri)
include(qmleditorwidgets-lib.pri)
......@@ -19,6 +19,7 @@ QtcLibrary {
Depends { name: "cpp" }
Depends { name: "Qt"; submodules: ["widgets", "quick1", "script"] }
Depends { name: "QmlJS" }
Depends { name: "Utils" }
files: [
"resources.qrc",
......
......@@ -38,8 +38,9 @@
#include <QDateTime>
#include <utils/environment.h>
#include <utils/synchronousprocess.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/synchronousprocess.h>
#include <QDesktopServices>
#include <QDebug>
......@@ -121,14 +122,13 @@ QString BuildableHelperLibrary::qtVersionForQMake(const QString &qmakePath, bool
QStringList BuildableHelperLibrary::possibleQMakeCommands()
{
// On windows no one has renamed qmake, right?
#ifdef Q_OS_WIN
return QStringList(QLatin1String("qmake.exe"));
#else
if (HostOsInfo::isWindowsHost())
return QStringList(QLatin1String("qmake.exe"));
// On unix some distributions renamed qmake to avoid clashes
QStringList result;
result << QLatin1String("qmake-qt4") << QLatin1String("qmake4") << QLatin1String("qmake");
return result;
#endif
}
// Copy helper source files to a target directory, replacing older files.
......
......@@ -33,6 +33,8 @@
#include "environment.h"
#include "qtcprocess.h"
#include <utils/hostosinfo.h>
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
......@@ -134,12 +136,14 @@ bool ConsoleProcess::start(const QString &program, const QString &args)
}
}
if (Utils::HostOsInfo::isMacHost()) {
xtermArgs << (QCoreApplication::applicationDirPath()
+ QLatin1String("/../Resources/qtcreator_process_stub"));
} else {
xtermArgs << (QCoreApplication::applicationDirPath()
+ QLatin1String("/qtcreator_process_stub"));
}
xtermArgs
#ifdef Q_OS_MAC
<< (QCoreApplication::applicationDirPath() + QLatin1String("/../Resources/qtcreator_process_stub"))
#else
<< (QCoreApplication::applicationDirPath() + QLatin1String("/qtcreator_process_stub"))
#endif
<< modeOption(d->m_mode)
<< d->m_stubServer.fullServerName()
<< msgPromptToClose()
......@@ -279,11 +283,9 @@ void ConsoleProcess::stubExited()
QString ConsoleProcess::defaultTerminalEmulator()
{
#ifdef Q_OS_MAC
return QLatin1String("/usr/X11/bin/xterm");
#else
if (Utils::HostOsInfo::isMacHost())
return QLatin1String("/usr/X11/bin/xterm");
return QLatin1String("xterm");
#endif
}
QString ConsoleProcess::terminalEmulator(const QSettings *settings)
......
......@@ -30,6 +30,7 @@
#include "detailsbutton.h"
#include <utils/hostosinfo.h>
#include <utils/stylehelper.h>
#include <QPropertyAnimation>
......@@ -83,11 +84,9 @@ QSize DetailsButton::sizeHint() const
{
// TODO: Adjust this when icons become available!
const int w = fontMetrics().width(text()) + 32;
#ifdef Q_OS_MAC
return QSize(w, 34);
#else
if (HostOsInfo::isMacHost())
return QSize(w, 34);
return QSize(w, 22);
#endif
}
bool DetailsButton::event(QEvent *e)
......@@ -120,11 +119,10 @@ void DetailsButton::paintEvent(QPaintEvent *e)
QWidget::paintEvent(e);
QPainter p(this);
#ifndef Q_OS_MAC
// draw hover animation
if (!isDown() && m_fader > 0)
if (!HostOsInfo::isMacHost() && !isDown() && m_fader > 0)
p.fillRect(rect().adjusted(1, 1, -2, -2), QColor(255, 255, 255, int(m_fader*180)));
#endif
if (isChecked()) {
if (m_checkedPixmap.isNull() || m_checkedPixmap.size() != contentsRect().size())
......
......@@ -30,6 +30,7 @@
#include "detailswidget.h"
#include "detailsbutton.h"
#include "hostosinfo.h"
#include <QStack>
#include <QPropertyAnimation>
......@@ -143,10 +144,10 @@ QPixmap DetailsWidgetPrivate::cacheBackground(const QSize &size)
QRect topRect(0, 0, size.width(), topHeight);
QRect fullRect(0, 0, size.width(), size.height());
#ifdef Q_OS_MAC
p.fillRect(fullRect, qApp->palette().window().color());
#endif
p.fillRect(fullRect, QColor(255, 255, 255, 40));
if (HostOsInfo::isMacHost())
p.fillRect(fullRect, qApp->palette().window().color());
else
p.fillRect(fullRect, QColor(255, 255, 255, 40));
QLinearGradient lg(topRect.topLeft(), topRect.bottomLeft());
lg.setColorAt(0, QColor(255, 255, 255, 130));
......@@ -188,11 +189,10 @@ void DetailsWidgetPrivate::changeHoverState(bool hovered)
{
if (!m_toolWidget)
return;
#ifdef Q_OS_MAC
m_toolWidget->setOpacity(hovered ? 1.0 : 0);
#else
m_toolWidget->fadeTo(hovered ? 1.0 : 0);
#endif
if (HostOsInfo::isMacHost())
m_toolWidget->setOpacity(hovered ? 1.0 : 0);
else
m_toolWidget->fadeTo(hovered ? 1.0 : 0);
m_hovered = hovered;
}
......@@ -381,9 +381,8 @@ void DetailsWidget::setToolWidget(Utils::FadingPanel *widget)
d->m_toolWidget->adjustSize();
d->m_grid->addWidget(d->m_toolWidget, 0, 1, 1, 1, Qt::AlignRight);
#ifdef Q_OS_MAC
d->m_toolWidget->setOpacity(1.0);
#endif
if (HostOsInfo::isMacHost())
d->m_toolWidget->setOpacity(1.0);
d->changeHoverState(d->m_hovered);
}
......
......@@ -30,6 +30,8 @@
#include "environment.h"
#include "hostosinfo.h"
#include <QDir>
#include <QProcess>
#include <QString>
......@@ -41,18 +43,18 @@ public:
SystemEnvironment()
: Environment(QProcess::systemEnvironment())
{
#ifdef Q_OS_LINUX
QString ldLibraryPath = value(QLatin1String("LD_LIBRARY_PATH"));
QDir lib(QCoreApplication::applicationDirPath());
lib.cd("../lib");
QString toReplace = lib.path();
lib.cd("qtcreator");
toReplace.append(QLatin1String(":"));
toReplace.append(lib.path());
if (ldLibraryPath.startsWith(toReplace))
set(QLatin1String("LD_LIBRARY_PATH"), ldLibraryPath.remove(0, toReplace.length()));
#endif
if (Utils::HostOsInfo::isLinuxHost()) {
QString ldLibraryPath = value(QLatin1String("LD_LIBRARY_PATH"));
QDir lib(QCoreApplication::applicationDirPath());
lib.cd("../lib");
QString toReplace = lib.path();
lib.cd("qtcreator");
toReplace.append(QLatin1String(":"));
toReplace.append(lib.path());
if (ldLibraryPath.startsWith(toReplace))
set(QLatin1String("LD_LIBRARY_PATH"), ldLibraryPath.remove(0, toReplace.length()));
}
}
};
......@@ -60,6 +62,11 @@ Q_GLOBAL_STATIC(SystemEnvironment, staticSystemEnvironment)
namespace Utils {
static QChar varSeparator()
{
return HostOsInfo::isWindowsHost() ? QLatin1Char(';') : QLatin1Char(':');
}
static bool sortEnvironmentItem(const EnvironmentItem &a, const EnvironmentItem &b)
{
return a.name < b.name;
......@@ -104,11 +111,10 @@ Environment::Environment(const QStringList &env)
foreach (const QString &s, env) {
int i = s.indexOf(QLatin1Char('='));
if (i >= 0) {
#ifdef Q_OS_WIN
m_values.insert(s.left(i).toUpper(), s.mid(i+1));
#else
m_values.insert(s.left(i), s.mid(i+1));
#endif
if (HostOsInfo::isWindowsHost())
m_values.insert(s.left(i).toUpper(), s.mid(i+1));
else
m_values.insert(s.left(i), s.mid(i+1));
}
}
}
......@@ -128,32 +134,18 @@ QStringList Environment::toStringList() const
void Environment::set(const QString &key, const QString &value)
{
#ifdef Q_OS_WIN
QString _key = key.toUpper();
#else
const QString &_key = key;
#endif
m_values.insert(_key, value);
m_values.insert(HostOsInfo::isWindowsHost() ? key.toUpper() : key, value);
}
void Environment::unset(const QString &key)
{
#ifdef Q_OS_WIN
QString _key = key.toUpper();
#else
const QString &_key = key;
#endif
m_values.remove(_key);
m_values.remove(HostOsInfo::isWindowsHost() ? key.toUpper() : key);
}
void Environment::appendOrSet(const QString &key, const QString &value, const QString &sep)
{
#ifdef Q_OS_WIN
QString _key = key.toUpper();
#else
const QString &_key = key;
#endif
QMap<QString, QString>::iterator it = m_values.find(key);
const QString &_key = HostOsInfo::isWindowsHost() ? key.toUpper() : key;
QMap<QString, QString>::iterator it = m_values.find(_key);
if (it == m_values.end()) {
m_values.insert(_key, value);
} else {
......@@ -166,12 +158,8 @@ void Environment::appendOrSet(const QString &key, const QString &value, const QS
void Environment::prependOrSet(const QString&key, const QString &value, const QString &sep)
{
#ifdef Q_OS_WIN
QString _key = key.toUpper();
#else
const QString &_key = key;
#endif
QMap<QString, QString>::iterator it = m_values.find(key);
const QString &_key = HostOsInfo::isWindowsHost() ? key.toUpper() : key;
QMap<QString, QString>::iterator it = m_values.find(_key);
if (it == m_values.end()) {
m_values.insert(_key, value);
} else {
......@@ -184,38 +172,33 @@ void Environment::prependOrSet(const QString&key, const QString &value, const QS
void Environment::appendOrSetPath(const QString &value)
{
#ifdef Q_OS_WIN
const QChar sep = QLatin1Char(';');
#else
const QChar sep = QLatin1Char(':');
#endif
appendOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(sep));
appendOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(varSeparator()));
}
void Environment::prependOrSetPath(const QString &value)
{
#ifdef Q_OS_WIN
const QChar sep = QLatin1Char(';');
#else
const QChar sep = QLatin1Char(':');
#endif
prependOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(sep));
prependOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(varSeparator()));
}
void Environment::prependOrSetLibrarySearchPath(const QString &value)
{
#ifdef Q_OS_MAC
Q_UNUSED(value);
// we could set DYLD_LIBRARY_PATH on Mac but it is unnecessary in practice
#elif defined(Q_OS_WIN)
const QChar sep = QLatin1Char(';');
const QLatin1String path("PATH");
prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
#elif defined(Q_OS_UNIX)
const QChar sep = QLatin1Char(':');
const QLatin1String path("LD_LIBRARY_PATH");
prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
#endif
switch (HostOsInfo::hostOs()) {
case HostOsInfo::HostOsWindows: {
const QChar sep = QLatin1Char(';');
const QLatin1String path("PATH");
prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
break;
}
case HostOsInfo::HostOsLinux:
case HostOsInfo::HostOsOtherUnix: {
const QChar sep = QLatin1Char(':');
const QLatin1String path("LD_LIBRARY_PATH");
prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
break;
}
default: // we could set DYLD_LIBRARY_PATH on Mac but it is unnecessary in practice
break;
}
}
Environment Environment::systemEnvironment()
......@@ -233,14 +216,15 @@ QString Environment::searchInPath(const QString &executable,
{
QStringList execs;
execs << executable;
#ifdef Q_OS_WIN
// Check all the executable extensions on windows:
QStringList extensions = value(QLatin1String("PATHEXT")).split(QLatin1Char(';'));
// .exe.bat is legal (and run when starting new.exe), so always go through the complete list once:
foreach (const QString &ext, extensions)
execs << executable + ext.toLower();
#endif
if (HostOsInfo::isWindowsHost()) {
// Check all the executable extensions on windows:
QStringList extensions = value(QLatin1String("PATHEXT")).split(QLatin1Char(';'));
// .exe.bat is legal (and run when starting new.exe), so always go through the
// complete list once:
foreach (const QString &ext, extensions)
execs << executable + ext.toLower();
}
return searchInPath(execs, additionalDirs);
}
......@@ -287,12 +271,7 @@ QString Environment::searchInPath(const QStringList &executables,
QStringList Environment::path() const
{
#ifdef Q_OS_WIN
const QChar sep = QLatin1Char(';');
#else
const QChar sep = QLatin1Char(':');
#endif
return m_values.value(QLatin1String("PATH")).split(sep, QString::SkipEmptyParts);
return m_values.value(QLatin1String("PATH")).split(varSeparator(), QString::SkipEmptyParts);
}
QString Environment::value(const QString &key) const
......@@ -425,68 +404,68 @@ QString Environment::expandVariables(const QString &input) const
{
QString result = input;
#ifdef Q_OS_WIN
for (int vStart = -1, i = 0; i < result.length(); ) {
if (result.at(i++) == QLatin1Char('%')) {
if (vStart > 0) {
const_iterator it = m_values.constFind(result.mid(vStart, i - vStart - 1).toUpper());
if (it != m_values.constEnd()) {
result.replace(vStart - 1, i - vStart + 1, *it);
i = vStart - 1 + it->length();
vStart = -1;
if (HostOsInfo::isWindowsHost()) {
for (int vStart = -1, i = 0; i < result.length(); ) {
if (result.at(i++) == QLatin1Char('%')) {
if (vStart > 0) {
const_iterator it = m_values.constFind(result.mid(vStart, i - vStart - 1).toUpper());
if (it != m_values.constEnd()) {
result.replace(vStart - 1, i - vStart + 1, *it);
i = vStart - 1 + it->length();
vStart = -1;
} else {
vStart = i;
}
} else {
vStart = i;
}
} else {
vStart = i;
}
}
}
#else
enum { BASE, OPTIONALVARIABLEBRACE, VARIABLE, BRACEDVARIABLE } state = BASE;
int vStart = -1;
for (int i = 0; i < result.length();) {
QChar c = result.at(i++);
if (state == BASE) {
if (c == QLatin1Char('$'))
state = OPTIONALVARIABLEBRACE;