OSDN Git Service

eb225a2501cd61f1b70d03957f94f32f85777523
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qtsupport / gettingstartedwelcomepage.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "gettingstartedwelcomepage.h"
34
35 #include "exampleslistmodel.h"
36
37 #include <utils/pathchooser.h>
38 #include <utils/fileutils.h>
39
40 #include <coreplugin/coreplugin.h>
41 #include <coreplugin/helpmanager.h>
42 #include <projectexplorer/projectexplorer.h>
43
44 #include <QtGui/QGraphicsProxyWidget>
45 #include <QtGui/QScrollBar>
46 #include <QtGui/QSortFilterProxyModel>
47 #include <QtSql/QSqlQueryModel>
48 #include <QtSql/QSqlQuery>
49 #include <QtDeclarative>
50 #include <QDebug>
51
52 namespace QtSupport {
53 namespace Internal {
54
55 const char *C_FALLBACK_ROOT = "ProjectsFallbackRoot";
56
57 class HelpImageProvider : public QDeclarativeImageProvider
58 {
59 public:
60     HelpImageProvider()
61         : QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
62     {
63     }
64
65     QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
66     {
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);
75         return img;
76     }
77 };
78
79 GettingStartedWelcomePage::GettingStartedWelcomePage()
80     : m_examplesModel(0), m_engine(0)
81 {
82 }
83
84 void GettingStartedWelcomePage::facilitateQml(QDeclarativeEngine *engine)
85 {
86     m_engine = 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);
93     proxy->sort(0);
94     proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
95
96     QDeclarativeContext *rootContenxt = m_engine->rootContext();
97     rootContenxt->setContextProperty(QLatin1String("examplesModel"), proxy);
98     rootContenxt->setContextProperty(QLatin1String("gettingStarted"), this);
99 }
100
101 void GettingStartedWelcomePage::openSplitHelp(const QUrl &help)
102 {
103     Core::ICore::instance()->helpManager()->handleHelpRequest(help.toString()+QLatin1String("?view=split"));
104 }
105
106 QStringList GettingStartedWelcomePage::tagList() const
107 {
108     return m_examplesModel->tags();
109 }
110
111 QString GettingStartedWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileInfo, QStringList &filesToOpen)
112 {
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);
156             return QString();
157         } else {
158             QString error;
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);
165
166                 return targetDir+ '/' + proFileInfo.fileName();
167             } else {
168                 QMessageBox::warning(Core::ICore::instance()->mainWindow(), tr("Cannot Copy Project"), error);
169             }
170
171         }
172     }
173     return QString();
174
175 }
176
177 void GettingStartedWelcomePage::openProject(const QString &projectFile, const QStringList &additionalFilesToOpen, const QUrl &help)
178 {
179     QString proFile = projectFile;
180     if (proFile.isEmpty())
181         return;
182
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);
188
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"));
193     }
194 }
195
196 void GettingStartedWelcomePage::updateTagsModel()
197 {
198     m_engine->rootContext()->setContextProperty(QLatin1String("tagsList"), m_examplesModel->tags());
199     emit tagsUpdated();
200 }
201
202 } // namespace Internal
203 } // namespace QtSupport
204