1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "gettingstartedwelcomepage.h"
#include "exampleslistmodel.h"
#include "screenshotcropper.h"
#include <utils/pathchooser.h>
#ifdef Q_OS_WIN
#include <utils/winutils.h>
#endif
#include <coreplugin/coreconstants.h>
#include <coreplugin/documentmanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/helpmanager.h>
#include <coreplugin/modemanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <QMutex>
#include <QThread>
#include <QMutexLocker>
#include <QPointer>
#include <QWaitCondition>
#include <QDir>
#include <QBuffer>
#include <QImage>
#include <QImageReader>
#include <QGridLayout>
#include <QLabel>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QMessageBox>
#include <QApplication>
#include <QDeclarativeImageProvider>
#include <QDeclarativeEngine>
#include <QDeclarativeContext>
#include <QDesktopServices>
using namespace Utils;
namespace QtSupport {
namespace Internal {
const char C_FALLBACK_ROOT[] = "ProjectsFallbackRoot";
QPointer<ExamplesListModel> &examplesModelStatic()
{
static QPointer<ExamplesListModel> s_examplesModel;
return s_examplesModel;
}
class Fetcher : public QObject
{
Q_OBJECT
public:
Fetcher() : QObject(), m_shutdown(false)
{
connect(Core::ICore::instance(), SIGNAL(coreAboutToClose()), this, SLOT(shutdown()));
}
void wait()
{
if (QThread::currentThread() == QApplication::instance()->thread())
return;
if (m_shutdown)
return;
m_waitcondition.wait(&m_mutex, 4000);
}
QByteArray data()
{
QMutexLocker lock(&m_dataMutex);
return m_fetchedData;
}
void clearData()
{
QMutexLocker lock(&m_dataMutex);
m_fetchedData.clear();
}
bool asynchronousFetchData(const QUrl &url)
{
QMutexLocker lock(&m_mutex);
if (!QMetaObject::invokeMethod(this,
"fetchData",
Qt::AutoConnection,
Q_ARG(QUrl, url))) {
return false;
}
wait();
return true;
}
public slots:
void fetchData(const QUrl &url)
{
if (m_shutdown)
return;
QMutexLocker lock(&m_mutex);
if (Core::HelpManager::instance()) {
QMutexLocker dataLock(&m_dataMutex);
m_fetchedData = Core::HelpManager::instance()->fileData(url);
}
m_waitcondition.wakeAll();
}
private slots:
void shutdown()
{
m_shutdown = true;
}
public:
QByteArray m_fetchedData;
QWaitCondition m_waitcondition;
QMutex m_mutex; //This mutex synchronises the wait() and wakeAll() on the wait condition.
//We have to ensure that wakeAll() is called always after wait().
QMutex m_dataMutex; //This mutex synchronises the access of m_fectedData.
//If the wait condition timeouts we otherwise get a race condition.
bool m_shutdown;
};
class HelpImageProvider : public QDeclarativeImageProvider
{
public:
HelpImageProvider()
: QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
{
}
// gets called by declarative in separate thread
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
Q_UNUSED(size)
QMutexLocker lock(&m_mutex);
QUrl url = QUrl::fromEncoded(id.toLatin1());
if (!m_fetcher.asynchronousFetchData(url))
return QImage();
if (m_fetcher.data().isEmpty())
return QImage();
QByteArray data = m_fetcher.data();
QBuffer imgBuffer(&data);
imgBuffer.open(QIODevice::ReadOnly);
QImageReader reader(&imgBuffer);
QImage img = reader.read();
m_fetcher.clearData();
return ScreenshotCropper::croppedImage(img, id, requestedSize);
}
private:
Fetcher m_fetcher;
QMutex m_mutex;
};
GettingStartedWelcomePage::GettingStartedWelcomePage() : m_engine(0)
{
}
QUrl GettingStartedWelcomePage::pageLocation() const
{
QString resourcePath = Core::ICore::resourcePath();
#ifdef Q_OS_WIN
// normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
resourcePath = Utils::normalizePathName(resourcePath);
#endif
return QUrl::fromLocalFile(resourcePath + QLatin1String("/welcomescreen/gettingstarted.qml"));
}
QString GettingStartedWelcomePage::title() const
{
return tr("Getting Started");
}
int GettingStartedWelcomePage::priority() const
{
return 4;
}
void GettingStartedWelcomePage::facilitateQml(QDeclarativeEngine *engine)
{
m_engine = engine;
}
GettingStartedWelcomePage::Id GettingStartedWelcomePage::id() const
{
return GettingStarted;
}
ExamplesWelcomePage::ExamplesWelcomePage()
: m_engine(0), m_showExamples(false)
{
}
void ExamplesWelcomePage::setShowExamples(bool showExamples)
{
m_showExamples = showExamples;
}
QString ExamplesWelcomePage::title() const
{
if (m_showExamples)
return tr("Examples");
else
return tr("Tutorials");
}
int ExamplesWelcomePage::priority() const
{
if (m_showExamples)
return 30;
else
return 40;
}
bool ExamplesWelcomePage::hasSearchBar() const
{
if (m_showExamples)
return true;
else
return false;
}
QUrl ExamplesWelcomePage::pageLocation() const
{
QString resourcePath = Core::ICore::resourcePath();
#ifdef Q_OS_WIN
// normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows
resourcePath = Utils::normalizePathName(resourcePath);
#endif
if (m_showExamples)
return QUrl::fromLocalFile(resourcePath + QLatin1String("/welcomescreen/examples.qml"));
else
return QUrl::fromLocalFile(resourcePath + QLatin1String("/welcomescreen/tutorials.qml"));
}
void ExamplesWelcomePage::facilitateQml(QDeclarativeEngine *engine)
{
m_engine = engine;
m_engine->addImageProvider(QLatin1String("helpimage"), new HelpImageProvider);
connect (examplesModel(), SIGNAL(tagsUpdated()), SLOT(updateTagsModel()));
ExamplesListModelFilter *proxy = new ExamplesListModelFilter(examplesModel(), this);
proxy->setDynamicSortFilter(true);
proxy->sort(0);
proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
QDeclarativeContext *rootContenxt = m_engine->rootContext();
if (m_showExamples) {
proxy->setShowTutorialsOnly(false);
rootContenxt->setContextProperty(QLatin1String("examplesModel"), proxy);
rootContenxt->setContextProperty(QLatin1String("qtVersionModel"), proxy->qtVersionModel());
} else {
rootContenxt->setContextProperty(QLatin1String("tutorialsModel"), proxy);
}
rootContenxt->setContextProperty(QLatin1String("gettingStarted"), this);
}
ExamplesWelcomePage::Id ExamplesWelcomePage::id() const
{
return m_showExamples ? Examples : Tutorials;
}
void ExamplesWelcomePage::openSplitHelp(const QUrl &help)
{
Core::ICore::helpManager()->handleHelpRequest(help.toString()+QLatin1String("?view=split"));
}
void ExamplesWelcomePage::openHelp(const QUrl &help)
{
Core::ICore::helpManager()->handleHelpRequest(help.toString());
}
void ExamplesWelcomePage::openUrl(const QUrl &url)
{
QDesktopServices::openUrl(url);
}
QStringList ExamplesWelcomePage::tagList() const
{
return examplesModel()->tags();
}
QString ExamplesWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileInfo, QStringList &filesToOpen, const QStringList& dependencies)
{
const QString projectDir = proFileInfo.canonicalPath();
QDialog d(Core::ICore::mainWindow());
QGridLayout *lay = new QGridLayout(&d);
QLabel *descrLbl = new QLabel;
d.setWindowTitle(tr("Copy Project to writable Location?"));
descrLbl->setTextFormat(Qt::RichText);
descrLbl->setWordWrap(false);
const QString nativeProjectDir = QDir::toNativeSeparators(projectDir);
descrLbl->setText(QString::fromLatin1("<blockquote>%1</blockquote>").arg(nativeProjectDir));
descrLbl->setMinimumWidth(descrLbl->sizeHint().width());
descrLbl->setWordWrap(true);
descrLbl->setText(tr("<p>The project you are about to open is located in the "
"write-protected location:</p><blockquote>%1</blockquote>"
"<p>Please select a writable location below and click \"Copy Project and Open\" "
"to open a modifiable copy of the project or click \"Keep Project and Open\" "
"to open the project in location.</p><p><b>Note:</b> You will not "
"be able to alter or compile your project in the current location.</p>")
.arg(nativeProjectDir));
lay->addWidget(descrLbl, 0, 0, 1, 2);
QLabel *txt = new QLabel(tr("&Location:"));
PathChooser *chooser = new PathChooser;
txt->setBuddy(chooser);
chooser->setExpectedKind(PathChooser::ExistingDirectory);
QSettings *settings = Core::ICore::settings();
chooser->setPath(settings->value(QString::fromLatin1(C_FALLBACK_ROOT),
Core::DocumentManager::projectsDirectory()).toString());
lay->addWidget(txt, 1, 0);
lay->addWidget(chooser, 1, 1);
QDialogButtonBox *bb = new QDialogButtonBox;
connect(bb, SIGNAL(accepted()), &d, SLOT(accept()));
connect(bb, SIGNAL(rejected()), &d, SLOT(reject()));
QPushButton *copyBtn = bb->addButton(tr("&Copy Project and Open"), QDialogButtonBox::AcceptRole);
copyBtn->setDefault(true);
bb->addButton(tr("&Keep Project and Open"), QDialogButtonBox::RejectRole);
lay->addWidget(bb, 2, 0, 1, 2);
connect(chooser, SIGNAL(validChanged(bool)), copyBtn, SLOT(setEnabled(bool)));
if (d.exec() == QDialog::Accepted) {
QString exampleDirName = proFileInfo.dir().dirName();
QString destBaseDir = chooser->path();
settings->setValue(QString::fromLatin1(C_FALLBACK_ROOT), destBaseDir);
QDir toDirWithExamplesDir(destBaseDir);
if (toDirWithExamplesDir.cd(exampleDirName)) {
toDirWithExamplesDir.cdUp(); // step out, just to not be in the way
QMessageBox::warning(Core::ICore::mainWindow(), tr("Cannot Use Location"),
tr("The specified location already exists. "
"Please specify a valid location."),
QMessageBox::Ok, QMessageBox::NoButton);
return QString();
} else {
QString error;
QString targetDir = destBaseDir + QLatin1Char('/') + exampleDirName;
if (FileUtils::copyRecursively(FileName::fromString(projectDir),
FileName::fromString(targetDir), &error)) {
// set vars to new location
const QStringList::Iterator end = filesToOpen.end();
for (QStringList::Iterator it = filesToOpen.begin(); it != end; ++it)
it->replace(projectDir, targetDir);
foreach (const QString &dependency, dependencies) {
FileName targetFile = FileName::fromString(targetDir);
targetFile.appendPath(QDir(dependency).dirName());
if (!FileUtils::copyRecursively(FileName::fromString(dependency), targetFile,
&error)) {
QMessageBox::warning(Core::ICore::mainWindow(), tr("Cannot Copy Project"), error);
// do not fail, just warn;
}
}
return targetDir + QLatin1Char('/') + proFileInfo.fileName();
} else {
QMessageBox::warning(Core::ICore::mainWindow(), tr("Cannot Copy Project"), error);
}
}
}
return QString();
}
void ExamplesWelcomePage::openProject(const QString &projectFile, const QStringList &additionalFilesToOpen,
const QUrl &help, const QStringList &dependencies, const QStringList &platforms)
{
QString proFile = projectFile;
if (proFile.isEmpty())
return;
QStringList filesToOpen = additionalFilesToOpen;
QFileInfo proFileInfo(proFile);
if (!proFileInfo.exists())
return;
// If the Qt is a distro Qt on Linux, it will not be writable, hence compilation will fail
if (!proFileInfo.isWritable())
proFile = copyToAlternativeLocation(proFileInfo, filesToOpen, dependencies);
// don't try to load help and files if loading the help request is being cancelled
QString errorMessage;
ProjectExplorer::ProjectExplorerPlugin *peplugin = ProjectExplorer::ProjectExplorerPlugin::instance();
if (proFile.isEmpty())
return;
if (ProjectExplorer::Project *project = peplugin->openProject(proFile, &errorMessage)) {
Core::ICore::openFiles(filesToOpen);
if (project->needsConfiguration())
project->configureAsExampleProject(platforms);
Core::ModeManager::activateModeType(Core::Constants::MODE_EDIT_TYPE);
if (help.isValid())
Core::ICore::helpManager()->handleHelpRequest(help.toString() + QLatin1String("?view=split"));
}
if (!errorMessage.isEmpty())
QMessageBox::critical(Core::ICore::mainWindow(), tr("Failed to Open Project"), errorMessage);
}
void ExamplesWelcomePage::updateTagsModel()
{
m_engine->rootContext()->setContextProperty(QLatin1String("tagsList"), examplesModel()->tags());
emit tagsUpdated();
}
ExamplesListModel *ExamplesWelcomePage::examplesModel() const
{
if (examplesModelStatic())
return examplesModelStatic().data();
examplesModelStatic() = new ExamplesListModel(const_cast<ExamplesWelcomePage*>(this));
return examplesModelStatic().data();
}
} // namespace Internal
} // namespace QtSupport
#include "gettingstartedwelcomepage.moc"
|