icore.cpp 15.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** 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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
con's avatar
con committed
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
con's avatar
con committed
30

con's avatar
con committed
31
#include "icore.h"
32
#include "windowsupport.h"
con's avatar
con committed
33

34
#include <app/app_version.h>
Tobias Hunger's avatar
Tobias Hunger committed
35
36
#include <extensionsystem/pluginmanager.h>

37
#include <QSysInfo>
38
#include <QApplication>
39

con's avatar
con committed
40
41
42
/*!
    \namespace Core
    \brief The Core namespace contains all classes that make up the Core plugin
Leena Miettinen's avatar
Leena Miettinen committed
43
    which constitute the basic functionality of \QC.
con's avatar
con committed
44
45
46
47
48
49
50
51
52
*/

/*!
    \namespace Core::Internal
    \internal
*/

/*!
    \class Core::ICore
53
54
    \brief The ICore class allows access to the different parts that make up
    the basic functionality of \QC.
con's avatar
con committed
55
56
57

    You should never create a subclass of this interface. The one and only
    instance is created by the Core plugin. You can access this instance
58
    from your plugin through \c{Core::instance()}.
con's avatar
con committed
59
60
61
62
63

    \mainclass
*/

/*!
64
    \fn void ICore::showNewItemDialog(const QString &title,
con's avatar
con committed
65
                                      const QList<IWizard *> &wizards,
66
67
                                      const QString &defaultLocation = QString(),
                                      const QVariantMap &extraVariables = QVariantMap())
con's avatar
con committed
68

Leena Miettinen's avatar
Leena Miettinen committed
69
70
71
72
73
    Opens a dialog where the user can choose from a set of \a wizards that
    create new files, classes, or projects.

    The \a title argument is shown as the dialog title. The path where the
    files will be created (if the user does not change it) is set
con's avatar
con committed
74
75
76
    in \a defaultLocation. It defaults to the path of the file manager's
    current file.

77
    \sa Core::DocumentManager
con's avatar
con committed
78
79
80
*/

/*!
81
    \fn bool ICore::showOptionsDialog(Id group, Id page, QWidget *parent = 0);
Leena Miettinen's avatar
Leena Miettinen committed
82
83
84

    Opens the application \gui Options (or \gui Preferences) dialog with preselected
    \a page in the specified \a group.
con's avatar
con committed
85
86
87
88

    The arguments refer to the string IDs of the corresponding IOptionsPage.
*/

89
90
91
/*!
    \fn bool ICore::showWarningWithOptions(const QString &title, const QString &text,
                                   const QString &details = QString(),
92
93
94
                                   Id settingsCategory = Id(),
                                   Id settingsId = Id(),
                                   QWidget *parent = 0)
95

Leena Miettinen's avatar
Leena Miettinen committed
96
    Shows a warning message with a button that opens a settings page.
97
98

    Should be used to display configuration errors and point users to the setting.
Leena Miettinen's avatar
Leena Miettinen committed
99
    Returns \c true if the settings dialog was accepted.
100
101
102
*/


con's avatar
con committed
103
/*!
104
    \fn QSettings *ICore::settings(QSettings::Scope scope = QSettings::UserScope)
Leena Miettinen's avatar
Leena Miettinen committed
105
106

    Returns the application's main settings object.
con's avatar
con committed
107
108
109

    You can use it to retrieve or set application wide settings
    (in contrast to session or project specific settings).
110

Leena Miettinen's avatar
Leena Miettinen committed
111
    If \a scope is \c QSettings::UserScope (the default), the
112
    users settings will be read from the users settings, with
Leena Miettinen's avatar
Leena Miettinen committed
113
    a fallback to global settings provided with \QC.
114

Leena Miettinen's avatar
Leena Miettinen committed
115
116
    If \a scope is \c QSettings::SystemScope, only the system settings
    shipped with the current version of \QC will be read. This
117
118
    functionality exists for internal purposes only.

119
120
121
122
    \see settingsDatabase()
*/

/*!
123
    \fn SettingsDatabase *ICore::settingsDatabase()
Leena Miettinen's avatar
Leena Miettinen committed
124
125

    Returns the application's settings database.
126
127
128
129
130
131

    The settings database is meant as an alternative to the regular settings
    object. It is more suitable for storing large amounts of data. The settings
    are application wide.

    \see SettingsDatabase
con's avatar
con committed
132
133
134
*/

/*!
135
    \fn QPrinter *ICore::printer()
Leena Miettinen's avatar
Leena Miettinen committed
136
137

    Returns the application's printer object.
con's avatar
con committed
138
139

    Always use this printer object for printing, so the different parts of the
140
    application re-use its settings.
con's avatar
con committed
141
142
143
*/

/*!
144
    \fn QString ICore::resourcePath()
Leena Miettinen's avatar
Leena Miettinen committed
145
146

    Returns the absolute path that is used for resources like
con's avatar
con committed
147
148
149
    project templates and the debugger macros.

    This abstraction is needed to avoid platform-specific code all over
Leena Miettinen's avatar
Leena Miettinen committed
150
151
    the place, since on Mac OS X, for example, the resources are part of the
    application bundle.
con's avatar
con committed
152
153
*/

154
155

/*!
156
    \fn QString ICore::userResourcePath()
Leena Miettinen's avatar
Leena Miettinen committed
157
158

    Returns the absolute path in the users directory that is used for
159
160
    resources like project templates.

161
    Use this function for finding the place for resources that the user may
Leena Miettinen's avatar
Leena Miettinen committed
162
    write to, for example, to allow for custom palettes or templates.
163
164
*/

con's avatar
con committed
165
/*!
166
    \fn QWidget *ICore::mainWindow()
con's avatar
con committed
167

Leena Miettinen's avatar
Leena Miettinen committed
168
    Returns the main application window.
con's avatar
con committed
169

170
    For dialog parents use \c dialogParent().
con's avatar
con committed
171
172
*/

173
174
175
176
177
178
/*!
    \fn QWidget *ICore::dialogParent()

    Returns a widget pointer suitable to use as parent for QDialogs.
 */

con's avatar
con committed
179
/*!
180
    \fn IContext *ICore::currentContextObject()
Leena Miettinen's avatar
Leena Miettinen committed
181
182

    Returns the context object of the current main context.
con's avatar
con committed
183

184
    \sa ICore::updateAdditionalContexts()
con's avatar
con committed
185
186
187
188
    \sa ICore::addContextObject()
*/

/*!
189
    \fn void ICore::updateAdditionalContexts(const Context &remove, const Context &add)
Leena Miettinen's avatar
Leena Miettinen committed
190
    Changes the currently active additional contexts.
con's avatar
con committed
191

192
193
    Removes the list of additional contexts specified by \a remove and adds the
    list of additional contexts specified by \a add.
con's avatar
con committed
194
195
196
197
198
199

    \sa ICore::hasContext()
*/

/*!
    \fn bool ICore::hasContext(int context) const
Leena Miettinen's avatar
Leena Miettinen committed
200
    Returns whether the given \a context is currently one of the active contexts.
con's avatar
con committed
201

202
    \sa ICore::updateAdditionalContexts()
con's avatar
con committed
203
204
205
206
207
    \sa ICore::addContextObject()
*/

/*!
    \fn void ICore::addContextObject(IContext *context)
Leena Miettinen's avatar
Leena Miettinen committed
208
    Registers an additional \a context object.
con's avatar
con committed
209
210

    After registration this context object gets automatically the
211
    current context object whenever its widget gets focus.
con's avatar
con committed
212
213

    \sa ICore::removeContextObject()
214
    \sa ICore::updateAdditionalContexts()
con's avatar
con committed
215
216
217
218
219
    \sa ICore::currentContextObject()
*/

/*!
    \fn void ICore::removeContextObject(IContext *context)
Leena Miettinen's avatar
Leena Miettinen committed
220
    Unregisters a \a context object from the list of know contexts.
con's avatar
con committed
221
222

    \sa ICore::addContextObject()
223
    \sa ICore::updateAdditionalContexts()
con's avatar
con committed
224
225
226
227
228
    \sa ICore::currentContextObject()
}
*/

/*!
229
    \fn void ICore::openFiles(const QStringList &fileNames, OpenFilesFlags flags = None)
Leena Miettinen's avatar
Leena Miettinen committed
230
231
232
    Opens all files from a list of \a fileNames like it would be
    done if they were given to \QC on the command line, or
    they were opened via \gui File > \gui Open.
con's avatar
con committed
233
234
235
*/

/*!
236
    \fn ICore::ICore(Internal::MainWindow *mw)
con's avatar
con committed
237
238
239
240
241
242
243
244
245
246
    \internal
*/

/*!
    \fn ICore::~ICore()
    \internal
*/

/*!
    \fn void ICore::coreOpened()
Leena Miettinen's avatar
Leena Miettinen committed
247
    Indicates that all plugins have been loaded and the main window is shown.
con's avatar
con committed
248
249
250
251
*/

/*!
    \fn void ICore::saveSettingsRequested()
Leena Miettinen's avatar
Leena Miettinen committed
252
    Signals that the user has requested that the global settings
con's avatar
con committed
253
254
255
256
257
258
259
    should be saved to disk.

    At the moment that happens when the application is closed, and on \gui{Save All}.
*/

/*!
    \fn void ICore::optionsDialogRequested()
Leena Miettinen's avatar
Leena Miettinen committed
260
    Enables plugins to perform actions just before the \gui Tools > \gui Options
con's avatar
con committed
261
262
263
264
265
    dialog is shown.
*/

/*!
    \fn void ICore::coreAboutToClose()
Leena Miettinen's avatar
Leena Miettinen committed
266
    Enables plugins to perform some pre-end-of-life actions.
con's avatar
con committed
267
268

    The application is guaranteed to shut down after this signal is emitted.
269
    It is there as an addition to the usual plugin lifecycle functions, namely
Leena Miettinen's avatar
Leena Miettinen committed
270
    \c IPlugin::aboutToShutdown(), just for convenience.
con's avatar
con committed
271
272
273
*/

/*!
274
    \fn void ICore::contextAboutToChange(const QList<Core::IContext *> &context)
Leena Miettinen's avatar
Leena Miettinen committed
275
276
    Indicates that a new \a context will shortly become the current context
    (meaning that its widget got focus).
con's avatar
con committed
277
278
279
*/

/*!
280
    \fn void ICore::contextChanged(Core::IContext *context, const Core::Context &additionalContexts)
Leena Miettinen's avatar
Leena Miettinen committed
281
282
283
    Indicates that a new \a context just became the current context
    (meaning that its widget got focus), or that the additional context ids
    specified by \a additionalContexts changed.
con's avatar
con committed
284
*/
hjk's avatar
hjk committed
285
286

#include "mainwindow.h"
287
#include "documentmanager.h"
hjk's avatar
hjk committed
288

289
290
#include <utils/hostosinfo.h>

291
292
#include <QCoreApplication>
#include <QDebug>
293
#include <QDir>
294
#include <QStatusBar>
hjk's avatar
hjk committed
295

296
using namespace Core::Internal;
297
using namespace ExtensionSystem;
298

hjk's avatar
hjk committed
299
300
301
302
303
304
305
306
307
308
309
namespace Core {

// The Core Singleton
static ICore *m_instance = 0;
static MainWindow *m_mainwindow;

ICore *ICore::instance()
{
    return m_instance;
}

310
311
312
313
314
bool ICore::isNewItemDialogRunning()
{
    return m_mainwindow->isNewItemDialogRunning();
}

hjk's avatar
hjk committed
315
316
317
318
ICore::ICore(MainWindow *mainwindow)
{
    m_instance = this;
    m_mainwindow = mainwindow;
Tobias Hunger's avatar
Tobias Hunger committed
319
    // Save settings once after all plugins are initialized:
320
    connect(PluginManager::instance(), SIGNAL(initializationDone()),
Eike Ziller's avatar
Eike Ziller committed
321
            this, SLOT(saveSettings()));
322
323
    connect(m_mainwindow, SIGNAL(newItemDialogRunningChanged()),
            this, SIGNAL(newItemDialogRunningChanged()));
hjk's avatar
hjk committed
324
325
326
327
328
}

ICore::~ICore()
{
    m_instance = 0;
hjk's avatar
hjk committed
329
    m_mainwindow = 0;
hjk's avatar
hjk committed
330
331
332
}

void ICore::showNewItemDialog(const QString &title,
Tobias Hunger's avatar
Tobias Hunger committed
333
                              const QList<IWizardFactory *> &factories,
334
335
                              const QString &defaultLocation,
                              const QVariantMap &extraVariables)
hjk's avatar
hjk committed
336
{
Tobias Hunger's avatar
Tobias Hunger committed
337
    m_mainwindow->showNewItemDialog(title, factories, defaultLocation, extraVariables);
hjk's avatar
hjk committed
338
339
}

340
bool ICore::showOptionsDialog(const Id page, QWidget *parent)
hjk's avatar
hjk committed
341
{
342
    return m_mainwindow->showOptionsDialog(page, parent);
hjk's avatar
hjk committed
343
344
}

345
346
347
348
349
QString ICore::msgShowOptionsDialog()
{
    return QCoreApplication::translate("Core", "Configure...", "msgShowOptionsDialog");
}

350
351
352
353
354
355
356
357
358
359
QString ICore::msgShowOptionsDialogToolTip()
{
    if (Utils::HostOsInfo::isMacHost())
        return QCoreApplication::translate("Core", "Open Preferences dialog.",
                                           "msgShowOptionsDialogToolTip (mac version)");
    else
        return QCoreApplication::translate("Core", "Open Options dialog.",
                                           "msgShowOptionsDialogToolTip (non-mac version)");
}

hjk's avatar
hjk committed
360
bool ICore::showWarningWithOptions(const QString &title, const QString &text,
361
                                   const QString &details, Id settingsId, QWidget *parent)
hjk's avatar
hjk committed
362
{
363
    return m_mainwindow->showWarningWithOptions(title, text, details, settingsId, parent);
hjk's avatar
hjk committed
364
365
366
367
}

QSettings *ICore::settings(QSettings::Scope scope)
{
368
369
370
371
    if (scope == QSettings::UserScope)
        return PluginManager::settings();
    else
        return PluginManager::globalSettings();
hjk's avatar
hjk committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
}

SettingsDatabase *ICore::settingsDatabase()
{
    return m_mainwindow->settingsDatabase();
}

QPrinter *ICore::printer()
{
    return m_mainwindow->printer();
}

QString ICore::userInterfaceLanguage()
{
    return qApp->property("qtc_locale").toString();
}

QString ICore::resourcePath()
{
391
392
393
    const QString sharePath = QLatin1String(Utils::HostOsInfo::isMacHost()
                                            ? "/../Resources" : "/../share/qtcreator");
    return QDir::cleanPath(QCoreApplication::applicationDirPath() + sharePath);
hjk's avatar
hjk committed
394
395
396
397
398
399
400
401
}

QString ICore::userResourcePath()
{
    // Create qtcreator dir if it doesn't yet exist
    const QString configDir = QFileInfo(settings(QSettings::UserScope)->fileName()).path();
    const QString urp = configDir + QLatin1String("/qtcreator");

402
    if (!QFileInfo::exists(urp + QLatin1Char('/'))) {
hjk's avatar
hjk committed
403
404
405
406
407
408
409
410
        QDir dir;
        if (!dir.mkpath(urp))
            qWarning() << "could not create" << urp;
    }

    return urp;
}

411
412
413
414
415
416
417
QString ICore::documentationPath()
{
    const QString docPath = QLatin1String(Utils::HostOsInfo::isMacHost()
                                            ? "/../Resources/doc" : "/../share/doc/qtcreator");
    return QDir::cleanPath(QCoreApplication::applicationDirPath() + docPath);
}

418
419
420
421
422
423
424
425
426
427
428
/*!
    Returns the path to the command line tools that are shipped with \QC (corresponding
    to the IDE_LIBEXEC_PATH qmake variable).
 */
QString ICore::libexecPath()
{
    const QString libexecPath = QLatin1String(Utils::HostOsInfo::isMacHost()
                                            ? "/../Resources" : "");
    return QDir::cleanPath(QCoreApplication::applicationDirPath() + libexecPath);
}

429
430
431
432
433
434
435
436
437
438
439
440
static QString compilerString()
{
#if defined(Q_CC_CLANG) // must be before GNU, because clang claims to be GNU too
    QString isAppleString;
#if defined(__apple_build_version__) // Apple clang has other version numbers
    isAppleString = QLatin1String(" (Apple)");
#endif
    return QLatin1String("Clang " ) + QString::number(__clang_major__) + QLatin1Char('.')
            + QString::number(__clang_minor__) + isAppleString;
#elif defined(Q_CC_GNU)
    return QLatin1String("GCC " ) + QLatin1String(__VERSION__);
#elif defined(Q_CC_MSVC)
441
442
443
    if (_MSC_VER >= 1800) // 1800: MSVC 2013 (yearly release cycle)
        return QLatin1String("MSVC ") + QString::number(2008 + ((_MSC_VER / 100) - 13));
    if (_MSC_VER >= 1500) // 1500: MSVC 2008, 1600: MSVC 2010, ... (2-year release cycle)
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
        return QLatin1String("MSVC ") + QString::number(2008 + 2 * ((_MSC_VER / 100) - 15));
#endif
    return QLatin1String("<unknown compiler>");
}

QString ICore::versionString()
{
    QString ideVersionDescription;
#ifdef IDE_VERSION_DESCRIPTION
    ideVersionDescription = tr(" (%1)").arg(QLatin1String(Constants::IDE_VERSION_DESCRIPTION_STR));
#endif
    return tr("Qt Creator %1%2").arg(QLatin1String(Constants::IDE_VERSION_LONG),
                                     ideVersionDescription);
}

QString ICore::buildCompatibilityString()
{
461
462
463
    return tr("Based on Qt %1 (%2, %3 bit)").arg(QLatin1String(qVersion()),
                                                 compilerString(),
                                                 QString::number(QSysInfo::WordSize));
464
465
}

hjk's avatar
hjk committed
466
467
468
469
470
471
IContext *ICore::currentContextObject()
{
    return m_mainwindow->currentContextObject();
}


472
QWidget *ICore::mainWindow()
hjk's avatar
hjk committed
473
474
475
476
{
    return m_mainwindow;
}

477
478
479
480
481
482
QWidget *ICore::dialogParent()
{
    QWidget *active = QApplication::activeModalWidget();
    return active ? active : m_mainwindow;
}

hjk's avatar
hjk committed
483
484
485
486
487
QStatusBar *ICore::statusBar()
{
    return m_mainwindow->statusBar();
}

488
489
490
491
492
493
494
495
496
497
498
499
500
void ICore::raiseWindow(QWidget *widget)
{
    if (!widget)
        return;
    QWidget *window = widget->window();
    if (window == m_mainwindow) {
        m_mainwindow->raiseWindow();
    } else {
        window->raise();
        window->activateWindow();
    }
}

hjk's avatar
hjk committed
501
502
503
504
505
void ICore::updateAdditionalContexts(const Context &remove, const Context &add)
{
    m_mainwindow->updateAdditionalContexts(remove, add);
}

506
507
508
509
510
511
512
513
514
515
void ICore::addAdditionalContext(const Context &context)
{
    m_mainwindow->updateAdditionalContexts(Context(), context);
}

void ICore::removeAdditionalContext(const Context &context)
{
    m_mainwindow->updateAdditionalContexts(context, Context());
}

hjk's avatar
hjk committed
516
517
518
519
520
521
522
523
524
525
void ICore::addContextObject(IContext *context)
{
    m_mainwindow->addContextObject(context);
}

void ICore::removeContextObject(IContext *context)
{
    m_mainwindow->removeContextObject(context);
}

526
527
528
529
530
void ICore::registerWindow(QWidget *window, const Context &context)
{
    new WindowSupport(window, context); // deletes itself when widget is destroyed
}

hjk's avatar
hjk committed
531
532
533
534
535
void ICore::openFiles(const QStringList &arguments, ICore::OpenFilesFlags flags)
{
    m_mainwindow->openFiles(arguments, flags);
}

Tobias Hunger's avatar
Tobias Hunger committed
536
537
538
539
540
541
542
543
void ICore::saveSettings()
{
    emit m_instance->saveSettingsRequested();

    ICore::settings(QSettings::SystemScope)->sync();
    ICore::settings(QSettings::UserScope)->sync();
}

hjk's avatar
hjk committed
544
} // namespace Core