1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
31 **************************************************************************/
33 #include "gettingstartedwelcomepage.h"
35 #include "exampleslistmodel.h"
37 #include <utils/pathchooser.h>
38 #include <utils/fileutils.h>
40 #include <coreplugin/coreplugin.h>
41 #include <coreplugin/helpmanager.h>
42 #include <projectexplorer/projectexplorer.h>
44 #include <QtGui/QGraphicsProxyWidget>
45 #include <QtGui/QScrollBar>
46 #include <QtGui/QSortFilterProxyModel>
47 #include <QtSql/QSqlQueryModel>
48 #include <QtSql/QSqlQuery>
49 #include <QtDeclarative>
55 const char *C_FALLBACK_ROOT = "ProjectsFallbackRoot";
57 class HelpImageProvider : public QDeclarativeImageProvider
61 : QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
65 QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
67 QUrl url = QUrl::fromEncoded(id.toAscii());
68 QByteArray imgData = Core::HelpManager::instance()->fileData(url);
69 QBuffer imgBuffer(&imgData);
70 imgBuffer.open(QIODevice::ReadOnly);
71 QImageReader reader(&imgBuffer);
72 QImage img = reader.read();
73 if (size && requestedSize != *size)
74 img = img.scaled(requestedSize);
79 GettingStartedWelcomePage::GettingStartedWelcomePage()
80 : m_examplesModel(0), m_engine(0)
84 void GettingStartedWelcomePage::facilitateQml(QDeclarativeEngine *engine)
87 m_engine->addImageProvider(QLatin1String("helpimage"), new HelpImageProvider);
88 m_examplesModel = new ExamplesListModel(this);
89 connect (m_examplesModel, SIGNAL(tagsUpdated()), SLOT(updateTagsModel()));
90 ExamplesListModelFilter *proxy = new ExamplesListModelFilter(this);
91 proxy->setSourceModel(m_examplesModel);
92 proxy->setDynamicSortFilter(true);
94 proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
96 QDeclarativeContext *rootContenxt = m_engine->rootContext();
97 rootContenxt->setContextProperty(QLatin1String("examplesModel"), proxy);
98 rootContenxt->setContextProperty(QLatin1String("gettingStarted"), this);
101 void GettingStartedWelcomePage::openSplitHelp(const QUrl &help)
103 Core::ICore::instance()->helpManager()->handleHelpRequest(help.toString()+QLatin1String("?view=split"));
106 QStringList GettingStartedWelcomePage::tagList() const
108 return m_examplesModel->tags();
111 QString GettingStartedWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileInfo, QStringList &filesToOpen)
113 const QString projectDir = proFileInfo.canonicalPath();
114 QDialog d(Core::ICore::instance()->mainWindow());
115 QGridLayout *lay = new QGridLayout(&d);
116 QLabel *descrLbl = new QLabel;
117 d.setWindowTitle(tr("Copy Project to writable Location?"));
118 descrLbl->setTextFormat(Qt::RichText);
119 descrLbl->setWordWrap(true);
120 descrLbl->setText(tr("<p>The project you are about to open is located in the "
121 "write-protected location:</p><blockquote>%1</blockquote>"
122 "<p>Please select a writable location below and click \"Copy Project and Open\" "
123 "to open a modifiable copy of the project or click \"Keep Project and Open\" "
124 "to open the project in location.</p><p><b>Note:</b> You will not "
125 "be able to alter or compile your project in the current location.</p>")
126 .arg(QDir::toNativeSeparators(projectDir)));
127 lay->addWidget(descrLbl, 0, 0, 1, 2);
128 QLabel *txt = new QLabel(tr("&Location:"));
129 Utils::PathChooser *chooser = new Utils::PathChooser;
130 txt->setBuddy(chooser);
131 chooser->setExpectedKind(Utils::PathChooser::ExistingDirectory);
132 QSettings *settings = Core::ICore::instance()->settings();
133 chooser->setPath(settings->value(
134 QString::fromLatin1(C_FALLBACK_ROOT), QDir::homePath()).toString());
135 lay->addWidget(txt, 1, 0);
136 lay->addWidget(chooser, 1, 1);
137 QDialogButtonBox *bb = new QDialogButtonBox;
138 connect(bb, SIGNAL(accepted()), &d, SLOT(accept()));
139 connect(bb, SIGNAL(rejected()), &d, SLOT(reject()));
140 QPushButton *copyBtn = bb->addButton(tr("&Copy Project and Open"), QDialogButtonBox::AcceptRole);
141 copyBtn->setDefault(true);
142 bb->addButton(tr("&Keep Project and Open"), QDialogButtonBox::RejectRole);
143 lay->addWidget(bb, 2, 0, 1, 2);
144 connect(chooser, SIGNAL(validChanged(bool)), copyBtn, SLOT(setEnabled(bool)));
145 if (d.exec() == QDialog::Accepted) {
146 QString exampleDirName = proFileInfo.dir().dirName();
147 QString destBaseDir = chooser->path();
148 settings->setValue(QString::fromLatin1(C_FALLBACK_ROOT), destBaseDir);
149 QDir toDirWithExamplesDir(destBaseDir);
150 if (toDirWithExamplesDir.cd(exampleDirName)) {
151 toDirWithExamplesDir.cdUp(); // step out, just to not be in the way
152 QMessageBox::warning(Core::ICore::instance()->mainWindow(), tr("Cannot Use Location"),
153 tr("The specified location already exists. "
154 "Please specify a valid location."),
155 QMessageBox::Ok, QMessageBox::NoButton);
159 QString targetDir = destBaseDir + '/' + exampleDirName;
160 if (Utils::FileUtils::copyRecursively(projectDir, targetDir, &error)) {
161 // set vars to new location
162 QStringList::Iterator it;
163 for (it = filesToOpen.begin(); it != filesToOpen.end(); ++it)
164 it->replace(projectDir, targetDir);
166 return targetDir+ '/' + proFileInfo.fileName();
168 QMessageBox::warning(Core::ICore::instance()->mainWindow(), tr("Cannot Copy Project"), error);
177 void GettingStartedWelcomePage::openProject(const QString &projectFile, const QStringList &additionalFilesToOpen, const QUrl &help)
179 QString proFile = projectFile;
180 if (proFile.isEmpty())
183 QStringList filesToOpen = additionalFilesToOpen;
184 QFileInfo proFileInfo(proFile);
185 // If the Qt is a distro Qt on Linux, it will not be writable, hence compilation will fail
186 if (!proFileInfo.isWritable())
187 proFile = copyToAlternativeLocation(proFileInfo, filesToOpen);
189 // don't try to load help and files if loading the help request is being cancelled
190 if (!proFile.isEmpty() && ProjectExplorer::ProjectExplorerPlugin::instance()->openProject(proFile)) {
191 Core::ICore::instance()->openFiles(filesToOpen);
192 Core::ICore::instance()->helpManager()->handleHelpRequest(help.toString()+QLatin1String("?view=split"));
196 void GettingStartedWelcomePage::updateTagsModel()
198 m_engine->rootContext()->setContextProperty(QLatin1String("tagsList"), m_examplesModel->tags());
202 } // namespace Internal
203 } // namespace QtSupport