genericprojectwizard.cpp 7.51 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

Roberto Raggi's avatar
Roberto Raggi committed
30
#include "genericprojectwizard.h"
31
#include "filesselectionwizardpage.h"
Roberto Raggi's avatar
Roberto Raggi committed
32 33 34

#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
35
#include <projectexplorer/projectexplorerconstants.h>
36
#include <projectexplorer/customwizard/customwizard.h>
Roberto Raggi's avatar
Roberto Raggi committed
37

38
#include <utils/filewizardpage.h>
Roberto Raggi's avatar
Roberto Raggi committed
39

40
#include <QApplication>
hjk's avatar
hjk committed
41
#include <QDebug>
42 43
#include <QDir>
#include <QFileInfo>
hjk's avatar
hjk committed
44 45 46
#include <QPainter>
#include <QPixmap>
#include <QStyle>
Roberto Raggi's avatar
Roberto Raggi committed
47

hjk's avatar
hjk committed
48 49
namespace GenericProjectManager {
namespace Internal {
Roberto Raggi's avatar
Roberto Raggi committed
50

51 52 53 54 55
static const char *const ConfigFileTemplate =
        "// Add predefined macros for your project here. For example:\n"
        "// #define THE_ANSWER 42\n"
        ;

Roberto Raggi's avatar
Roberto Raggi committed
56
//////////////////////////////////////////////////////////////////////////////
hjk's avatar
hjk committed
57
//
Roberto Raggi's avatar
Roberto Raggi committed
58
// GenericProjectWizardDialog
hjk's avatar
hjk committed
59
//
Roberto Raggi's avatar
Roberto Raggi committed
60 61
//////////////////////////////////////////////////////////////////////////////

Tobias Hunger's avatar
Tobias Hunger committed
62 63
GenericProjectWizardDialog::GenericProjectWizardDialog(QWidget *parent) :
    Core::BaseFileWizard(parent)
Roberto Raggi's avatar
Roberto Raggi committed
64
{
65
    setWindowTitle(tr("Import Existing Project"));
Roberto Raggi's avatar
Roberto Raggi committed
66 67

    // first page
hjk's avatar
hjk committed
68
    m_firstPage = new Utils::FileWizardPage;
69
    m_firstPage->setTitle(tr("Project Name and Location"));
70
    m_firstPage->setFileNameLabel(tr("Project name:"));
71
    m_firstPage->setPathLabel(tr("Location:"));
72
    addPage(m_firstPage);
Roberto Raggi's avatar
Roberto Raggi committed
73

74 75 76
    // second page
    m_secondPage = new FilesSelectionWizardPage(this);
    m_secondPage->setTitle(tr("File Selection"));
77
    addPage(m_secondPage);
Roberto Raggi's avatar
Roberto Raggi committed
78 79
}

Roberto Raggi's avatar
Roberto Raggi committed
80
QString GenericProjectWizardDialog::path() const
81 82 83 84
{
    return m_firstPage->path();
}

85 86 87 88 89 90 91 92 93 94
QStringList GenericProjectWizardDialog::selectedPaths() const
{
    return m_secondPage->selectedPaths();
}

QStringList GenericProjectWizardDialog::selectedFiles() const
{
    return m_secondPage->selectedFiles();
}

95 96 97 98 99
void GenericProjectWizardDialog::setPath(const QString &path)
{
    m_firstPage->setPath(path);
}

100 101
QString GenericProjectWizardDialog::projectName() const
{
102
    return m_firstPage->fileName();
103
}
Roberto Raggi's avatar
Roberto Raggi committed
104

hjk's avatar
hjk committed
105 106 107 108 109 110
//////////////////////////////////////////////////////////////////////////////
//
// GenericProjectWizard
//
//////////////////////////////////////////////////////////////////////////////

Roberto Raggi's avatar
Roberto Raggi committed
111
GenericProjectWizard::GenericProjectWizard()
Roberto Raggi's avatar
Roberto Raggi committed
112
{
hjk's avatar
hjk committed
113
    setWizardKind(ProjectWizard);
con's avatar
con committed
114 115 116 117 118 119
    // TODO do something about the ugliness of standard icons in sizes different than 16, 32, 64, 128
    {
        QPixmap icon(22, 22);
        icon.fill(Qt::transparent);
        QPainter p(&icon);
        p.drawPixmap(3, 3, 16, 16, qApp->style()->standardIcon(QStyle::SP_DirIcon).pixmap(16));
hjk's avatar
hjk committed
120
        setIcon(icon);
con's avatar
con committed
121
    }
hjk's avatar
hjk committed
122 123 124 125 126 127
    setDisplayName(tr("Import Existing Project"));
    setId(QLatin1String("Z.Makefile"));
    setDescription(tr("Imports existing projects that do not use qmake, CMake or Autotools. "
                      "This allows you to use Qt Creator as a code editor."));
    setCategory(QLatin1String(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY));
    setDisplayCategory(QLatin1String(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY_DISPLAY));
Tobias Hunger's avatar
Tobias Hunger committed
128
    setFlags(Core::IWizardFactory::PlatformIndependent);
Roberto Raggi's avatar
Roberto Raggi committed
129 130
}

Tobias Hunger's avatar
Tobias Hunger committed
131
Core::BaseFileWizard *GenericProjectWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
Roberto Raggi's avatar
Roberto Raggi committed
132
{
Roberto Raggi's avatar
Roberto Raggi committed
133
    GenericProjectWizardDialog *wizard = new GenericProjectWizardDialog(parent);
Roberto Raggi's avatar
Roberto Raggi committed
134

135
    wizard->setPath(parameters.defaultPath());
136

137
    foreach (QWizardPage *p, parameters.extensionPages())
138
        wizard->addPage(p);
Roberto Raggi's avatar
Roberto Raggi committed
139

Roberto Raggi's avatar
Roberto Raggi committed
140 141
    return wizard;
}
Roberto Raggi's avatar
Roberto Raggi committed
142 143 144 145

Core::GeneratedFiles GenericProjectWizard::generateFiles(const QWizard *w,
                                                         QString *errorMessage) const
{
146 147
    Q_UNUSED(errorMessage)

Roberto Raggi's avatar
Roberto Raggi committed
148 149 150
    const GenericProjectWizardDialog *wizard = qobject_cast<const GenericProjectWizardDialog *>(w);
    const QString projectPath = wizard->path();
    const QDir dir(projectPath);
151
    const QString projectName = wizard->projectName();
152 153 154 155
    const QString creatorFileName = QFileInfo(dir, projectName + QLatin1String(".creator")).absoluteFilePath();
    const QString filesFileName = QFileInfo(dir, projectName + QLatin1String(".files")).absoluteFilePath();
    const QString includesFileName = QFileInfo(dir, projectName + QLatin1String(".includes")).absoluteFilePath();
    const QString configFileName = QFileInfo(dir, projectName + QLatin1String(".config")).absoluteFilePath();
156
    const QStringList paths = wizard->selectedPaths();
Roberto Raggi's avatar
Roberto Raggi committed
157

hjk's avatar
hjk committed
158
    Core::MimeType headerTy = Core::MimeDatabase::findByType(QLatin1String("text/x-chdr"));
Roberto Raggi's avatar
Roberto Raggi committed
159 160

    QStringList nameFilters;
161
    foreach (const Core::MimeGlobPattern &gp, headerTy.globPatterns())
162
        nameFilters.append(gp.pattern());
Roberto Raggi's avatar
Roberto Raggi committed
163 164 165

    QStringList includePaths;
    foreach (const QString &path, paths) {
166
        QFileInfo fileInfo(path);
Roberto Raggi's avatar
Roberto Raggi committed
167 168
        QDir thisDir(fileInfo.absoluteFilePath());

169 170 171
        if (! thisDir.entryList(nameFilters, QDir::Files).isEmpty()) {
            QString relative = dir.relativeFilePath(path);
            if (relative.isEmpty())
172
                relative = QLatin1Char('.');
173 174
            includePaths.append(relative);
        }
Roberto Raggi's avatar
Roberto Raggi committed
175
    }
Roberto Raggi's avatar
Roberto Raggi committed
176

177 178
    Core::GeneratedFile generatedCreatorFile(creatorFileName);
    generatedCreatorFile.setContents(QLatin1String("[General]\n"));
179
    generatedCreatorFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
Roberto Raggi's avatar
Roberto Raggi committed
180

181 182 183 184
    QStringList sources = wizard->selectedFiles();
    for (int i = 0; i < sources.length(); ++i)
        sources[i] = dir.relativeFilePath(sources[i]);

185
    Core::GeneratedFile generatedFilesFile(filesFileName);
hjk's avatar
hjk committed
186
    generatedFilesFile.setContents(sources.join(QLatin1Char('\n')));
187 188

    Core::GeneratedFile generatedIncludesFile(includesFileName);
hjk's avatar
hjk committed
189
    generatedIncludesFile.setContents(includePaths.join(QLatin1Char('\n')));
190 191

    Core::GeneratedFile generatedConfigFile(configFileName);
192
    generatedConfigFile.setContents(QLatin1String(ConfigFileTemplate));
Roberto Raggi's avatar
Roberto Raggi committed
193

Roberto Raggi's avatar
Roberto Raggi committed
194
    Core::GeneratedFiles files;
195 196 197 198
    files.append(generatedFilesFile);
    files.append(generatedIncludesFile);
    files.append(generatedConfigFile);
    files.append(generatedCreatorFile);
Roberto Raggi's avatar
Roberto Raggi committed
199 200 201 202

    return files;
}

203
bool GenericProjectWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage)
Roberto Raggi's avatar
Roberto Raggi committed
204
{
205
    Q_UNUSED(w);
206
    return ProjectExplorer::CustomProjectWizard::postGenerateOpen(l, errorMessage);
Roberto Raggi's avatar
Roberto Raggi committed
207
}
hjk's avatar
hjk committed
208 209 210

} // namespace Internal
} // namespace GenericProjectManager