Files
qt-creator/src/plugins/welcome/welcomeplugin.cpp

377 lines
12 KiB
C++
Raw Normal View History

/****************************************************************************
2009-07-20 19:08:09 +02:00
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2009-07-20 19:08:09 +02:00
**
** This file is part of Qt Creator.
2009-07-20 19:08:09 +02:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
2009-07-20 19:08:09 +02:00
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
2009-07-20 19:08:09 +02:00
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2009-07-20 19:08:09 +02:00
#include "welcomeplugin.h"
2011-04-13 17:09:44 +02:00
#include <extensionsystem/pluginmanager.h>
2011-04-13 17:09:44 +02:00
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/imode.h>
2009-07-20 19:08:09 +02:00
#include <coreplugin/modemanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/dialogs/iwizard.h>
#include <projectexplorer/projectexplorer.h>
2009-07-20 19:08:09 +02:00
#include <utils/hostosinfo.h>
2011-04-13 17:09:44 +02:00
#include <utils/styledbar.h>
#include <utils/iwelcomepage.h>
#include <utils/networkaccessmanager.h>
2011-04-13 17:09:44 +02:00
#ifdef Q_OS_WIN
#include <utils/winutils.h>
#endif
#include <QScrollArea>
#include <QDesktopServices>
#include <QPainter>
#include <QVBoxLayout>
2011-04-13 17:09:44 +02:00
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
#include <QDebug>
#include <QUrl>
#include <QtPlugin>
2009-07-20 19:08:09 +02:00
#include <QtQuick/QQuickView>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlNetworkAccessManagerFactory>
enum { debug = 0 };
2009-07-20 19:08:09 +02:00
2011-04-13 17:09:44 +02:00
using namespace ExtensionSystem;
using namespace Utils;
2011-04-13 17:09:44 +02:00
static const char currentPageSettingsKeyC[] = "WelcomeTab";
2011-04-13 17:09:44 +02:00
namespace Welcome {
namespace Internal {
class NetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory
2011-04-13 17:09:44 +02:00
{
public:
NetworkAccessManagerFactory(): QQmlNetworkAccessManagerFactory() {}
QNetworkAccessManager* create(QObject *parent) { return new Utils::NetworkAccessManager(parent); }
};
2011-04-13 17:19:55 +02:00
struct WelcomeModePrivate
{
2011-04-13 17:09:44 +02:00
};
class WelcomeMode : public Core::IMode
{
Q_OBJECT
Q_PROPERTY(int activePlugin READ activePlugin WRITE setActivePlugin NOTIFY activePluginChanged)
2011-04-13 17:09:44 +02:00
public:
WelcomeMode();
~WelcomeMode();
void activated();
void initPlugins();
int activePlugin() const { return m_activePlugin; }
Q_SCRIPTABLE QString platform() const;
// bool eventFilter(QObject *, QEvent *);
public slots:
void setActivePlugin(int pos)
{
if (m_activePlugin != pos) {
m_activePlugin = pos;
emit activePluginChanged(pos);
}
}
signals:
void activePluginChanged(int pos);
2011-04-13 17:09:44 +02:00
private slots:
void welcomePluginAdded(QObject*);
private:
void facilitateQml(QQmlEngine *engine);
QWidget *m_modeWidget;
QQuickView *m_welcomePage;
QList<QObject*> m_pluginList;
int m_activePlugin;
NetworkAccessManagerFactory *m_networkAccessManagerFactory;
2011-04-13 17:09:44 +02:00
};
// --- WelcomeMode
WelcomeMode::WelcomeMode() :
m_activePlugin(0)
, m_networkAccessManagerFactory(new NetworkAccessManagerFactory)
2011-04-13 17:09:44 +02:00
{
setDisplayName(tr("Welcome"));
QIcon qtLogo;
qtLogo.addFile(QLatin1String(Core::Constants::ICON_QTLOGO_32));
qtLogo.addFile(QLatin1String(Core::Constants::ICON_QTLOGO_64));
qtLogo.addFile(QLatin1String(Core::Constants::ICON_QTLOGO_128));
setIcon(qtLogo);
2011-04-13 17:09:44 +02:00
setPriority(Core::Constants::P_MODE_WELCOME);
setId(Core::Constants::MODE_WELCOME);
2011-04-13 17:09:44 +02:00
setContextHelpId(QLatin1String("Qt Creator Manual"));
2011-04-13 17:19:55 +02:00
setContext(Core::Context(Core::Constants::C_WELCOME_MODE));
m_welcomePage = new QQuickView;
m_welcomePage->setObjectName(QLatin1String("WelcomePage"));
m_welcomePage->setResizeMode(QQuickView::SizeRootObjectToView);
// filter to forward dragEnter events
// m_welcomePage->installEventFilter(this);
// m_welcomePage->viewport()->installEventFilter(this);
m_modeWidget = new QWidget;
m_modeWidget->setObjectName(QLatin1String("WelcomePageModeWidget"));
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
layout->setSpacing(0);
2011-04-13 17:09:44 +02:00
Utils::StyledBar* styledBar = new Utils::StyledBar(m_modeWidget);
styledBar->setObjectName(QLatin1String("WelcomePageStyledBar"));
layout->addWidget(styledBar);
// QScrollArea *scrollArea = new QScrollArea(m_modeWidget);
// scrollArea->setFrameShape(QFrame::NoFrame);
// layout->addWidget(scrollArea);
// scrollArea->setWidget(m_welcomePage);
// scrollArea->setWidgetResizable(true);
QWidget *container = QWidget::createWindowContainer(m_welcomePage, m_modeWidget);
container->setMinimumSize(QSize(880, 548));
layout->addWidget(container);
m_modeWidget->setLayout(layout);
connect(PluginManager::instance(), SIGNAL(objectAdded(QObject*)), SLOT(welcomePluginAdded(QObject*)));
2011-04-13 17:09:44 +02:00
setWidget(m_modeWidget);
2011-04-13 17:09:44 +02:00
}
//bool WelcomeMode::eventFilter(QObject *, QEvent *e)
//{
// if (e->type() == QEvent::DragEnter) {
// e->ignore();
// return true;
// }
// return false;
//}
2011-04-13 17:09:44 +02:00
WelcomeMode::~WelcomeMode()
2009-07-20 19:08:09 +02:00
{
QSettings *settings = Core::ICore::settings();
settings->setValue(QLatin1String(currentPageSettingsKeyC), activePlugin());
delete m_modeWidget;
delete m_networkAccessManagerFactory;
2009-07-20 19:08:09 +02:00
}
bool sortFunction(Utils::IWelcomePage * a, Utils::IWelcomePage *b)
2011-04-13 17:09:44 +02:00
{
return a->priority() < b->priority();
}
void WelcomeMode::facilitateQml(QQmlEngine * /*engine*/)
{
}
static QString applicationDirPath()
{
#ifdef Q_OS_WIN
// normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
return Utils::normalizePathName(QCoreApplication::applicationDirPath());
#else
return QCoreApplication::applicationDirPath();
#endif
}
static QString resourcePath()
{
#ifdef Q_OS_WIN
// normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
return Utils::normalizePathName(Core::ICore::resourcePath());
#else
return Core::ICore::resourcePath();
#endif
}
2011-04-13 17:09:44 +02:00
void WelcomeMode::initPlugins()
{
QSettings *settings = Core::ICore::settings();
setActivePlugin(settings->value(QLatin1String(currentPageSettingsKeyC)).toInt());
QQmlContext *ctx = m_welcomePage->rootContext();
ctx->setContextProperty(QLatin1String("welcomeMode"), this);
QList<Utils::IWelcomePage*> duplicatePlugins = PluginManager::getObjects<Utils::IWelcomePage>();
qSort(duplicatePlugins.begin(), duplicatePlugins.end(), &sortFunction);
QList<Utils::IWelcomePage*> plugins;
QHash<Utils::IWelcomePage::Id, Utils::IWelcomePage*> pluginHash;
//avoid duplicate ids - choose by priority
foreach (Utils::IWelcomePage* plugin, duplicatePlugins) {
if (pluginHash.contains(plugin->id())) {
Utils::IWelcomePage* pluginOther = pluginHash.value(plugin->id());
if (pluginOther->priority() > plugin->priority()) {
plugins.removeAll(pluginOther);
pluginHash.remove(pluginOther->id());
plugins << plugin;
pluginHash.insert(plugin->id(), plugin);
}
} else {
plugins << plugin;
pluginHash.insert(plugin->id(), plugin);
}
}
QQmlEngine *engine = m_welcomePage->engine();
QStringList importPathList = engine->importPathList();
importPathList << resourcePath() + QLatin1String("/welcomescreen");
engine->setImportPathList(importPathList);
if (!debug)
engine->setOutputWarningsToStandardError(false);
engine->setNetworkAccessManagerFactory(m_networkAccessManagerFactory);
QString pluginPath = applicationDirPath();
if (HostOsInfo::isMacHost())
pluginPath += QLatin1String("/../PlugIns");
else
pluginPath += QLatin1String("/../" IDE_LIBRARY_BASENAME "/qtcreator");
engine->addImportPath(QDir::cleanPath(pluginPath));
facilitateQml(engine);
2011-04-13 17:09:44 +02:00
foreach (Utils::IWelcomePage *plugin, plugins) {
plugin->facilitateQml(engine);
m_pluginList.append(plugin);
2011-04-13 17:09:44 +02:00
}
ctx->setContextProperty(QLatin1String("pagesModel"), QVariant::fromValue(m_pluginList));
QString path = resourcePath() + QLatin1String("/welcomescreen/welcomescreen.qml");
// finally, load the root page
m_welcomePage->setSource(
QUrl::fromLocalFile(path));
2011-04-13 17:09:44 +02:00
}
QString WelcomeMode::platform() const
{
switch (HostOsInfo::hostOs()) {
case OsTypeWindows: return QLatin1String("windows");
case OsTypeMac: return QLatin1String("mac");
case OsTypeLinux: return QLatin1String("linux");
case OsTypeOtherUnix: return QLatin1String("unix");
default: return QLatin1String("other");
}
}
2011-04-13 17:09:44 +02:00
void WelcomeMode::welcomePluginAdded(QObject *obj)
{
QHash<Utils::IWelcomePage::Id, Utils::IWelcomePage*> pluginHash;
foreach (QObject *obj, m_pluginList) {
Utils::IWelcomePage *plugin = qobject_cast<Utils::IWelcomePage*>(obj);
pluginHash.insert(plugin->id(), plugin);
}
if (Utils::IWelcomePage *plugin = qobject_cast<Utils::IWelcomePage*>(obj)) {
//check for duplicated id
if (pluginHash.contains(plugin->id())) {
Utils::IWelcomePage* pluginOther = pluginHash.value(plugin->id());
if (pluginOther->priority() > plugin->priority())
m_pluginList.removeAll(pluginOther);
else
return;
}
int insertPos = 0;
foreach (Utils::IWelcomePage* p, PluginManager::getObjects<Utils::IWelcomePage>()) {
if (plugin->priority() < p->priority())
insertPos++;
else
break;
}
m_pluginList.insert(insertPos, plugin);
// update model through reset
QQmlContext *ctx = m_welcomePage->rootContext();
ctx->setContextProperty(QLatin1String("pagesModel"), QVariant::fromValue(m_pluginList));
2011-04-13 17:09:44 +02:00
}
}
WelcomePlugin::WelcomePlugin()
: m_welcomeMode(0)
2009-07-20 19:08:09 +02:00
{
}
/*! Initializes the plugin. Returns true on success.
Plugins want to register objects with the plugin manager here.
\a errorMessage can be used to pass an error message to the plugin system,
2009-07-20 19:08:09 +02:00
if there was any.
*/
bool WelcomePlugin::initialize(const QStringList & /* arguments */, QString * /* errorMessage */)
2009-07-20 19:08:09 +02:00
{
m_welcomeMode = new WelcomeMode;
addAutoReleasedObject(m_welcomeMode);
2009-07-20 19:08:09 +02:00
return true;
}
/*! Notification that all extensions that this plugin depends on have been
initialized. The dependencies are defined in the plugins .qwp file.
Normally this function is used for things that rely on other plugins to have
2009-07-20 19:08:09 +02:00
added objects to the plugin manager, that implement interfaces that we're
interested in. These objects can now be requested through the
PluginManagerInterface.
The WelcomePlugin doesn't need things from other plugins, so it does
nothing here.
*/
void WelcomePlugin::extensionsInitialized()
{
m_welcomeMode->initPlugins();
Core::ModeManager::activateMode(m_welcomeMode->id());
2009-07-20 19:08:09 +02:00
}
2011-04-13 17:09:44 +02:00
} // namespace Internal
} // namespace Welcome
2011-04-13 17:09:44 +02:00
Q_EXPORT_PLUGIN(Welcome::Internal::WelcomePlugin)
#include "welcomeplugin.moc"