1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #ifndef BASEFILEWIZARD_H
35 #define BASEFILEWIZARD_H
37 #include "core_global.h"
39 #include <coreplugin/dialogs/iwizard.h>
41 #include <QtCore/QSharedDataPointer>
42 #include <QtCore/QList>
58 class IFileWizardExtension;
60 class BaseFileWizardParameterData;
61 struct BaseFileWizardPrivate;
62 class GeneratedFilePrivate;
65 * Represents a file generated by a wizard. The Wizard class will check for
66 * each file whether it already exists and will report any errors that may
67 * occur during creation of the files.
69 class CORE_EXPORT GeneratedFile
72 enum Attribute { // Open this file in editor
73 OpenEditorAttribute = 0x01,
75 OpenProjectAttribute = 0x02,
76 /* File is generated by external scripts, do not write out,
77 * see BaseFileWizard::writeFiles() */
78 CustomGeneratorAttribute = 0x4
80 Q_DECLARE_FLAGS(Attributes, Attribute)
83 explicit GeneratedFile(const QString &path);
84 GeneratedFile(const GeneratedFile &);
85 GeneratedFile &operator=(const GeneratedFile &);
88 // Full path of the file should be created, or the suggested file name
90 void setPath(const QString &p);
92 // Contents of the file (UTF8)
93 QString contents() const;
94 void setContents(const QString &c);
96 QByteArray binaryContents() const;
97 void setBinaryContents(const QByteArray &c);
99 // Defaults to false (Text file).
100 bool isBinary() const;
101 void setBinary(bool b);
103 // Id of editor to open the file with
104 QString editorId() const;
105 void setEditorId(const QString &k);
107 bool write(QString *errorMessage) const;
109 Attributes attributes() const;
110 void setAttributes(Attributes a);
113 QSharedDataPointer<GeneratedFilePrivate> m_d;
116 typedef QList<GeneratedFile> GeneratedFiles;
118 /* Parameter class for passing parameters to instances of class Wizard
119 * containing name, icon and such. */
121 class CORE_EXPORT BaseFileWizardParameters
124 explicit BaseFileWizardParameters(IWizard::WizardKind kind = IWizard::FileWizard);
125 BaseFileWizardParameters(const BaseFileWizardParameters &);
126 BaseFileWizardParameters &operator=(const BaseFileWizardParameters&);
127 ~BaseFileWizardParameters();
131 IWizard::WizardKind kind() const;
132 void setKind(IWizard::WizardKind k);
135 void setIcon(const QIcon &icon);
137 QString description() const;
138 void setDescription(const QString &description);
140 QString displayName() const;
141 void setDisplayName(const QString &name);
144 void setId(const QString &id);
146 QString category() const;
147 void setCategory(const QString &category);
149 QString displayCategory() const;
150 void setDisplayCategory(const QString &trCategory);
153 QSharedDataPointer<BaseFileWizardParameterData> m_d;
156 CORE_EXPORT QDebug operator<<(QDebug d, const BaseFileWizardParameters &);
158 /* A generic wizard for creating files.
160 * The abstract methods:
162 * createWizardDialog() : Called to create the QWizard dialog to be shown
163 * generateFiles() : Generate file content
165 * must be implemented. The behaviour can be further customized by overwriting
166 * the virtual method:
167 * postGenerateFiles() : Called after generating the files.
170 class CORE_EXPORT BaseFileWizard : public IWizard
172 Q_DISABLE_COPY(BaseFileWizard)
176 virtual ~BaseFileWizard();
179 virtual WizardKind kind() const;
180 virtual QIcon icon() const;
181 virtual QString description() const;
182 virtual QString displayName() const;
183 virtual QString id() const;
185 virtual QString category() const;
186 virtual QString displayCategory() const;
188 virtual void runWizard(const QString &path, QWidget *parent);
190 // Build a file name, adding the extension unless baseName already has one
191 static QString buildFileName(const QString &path, const QString &baseName, const QString &extension);
193 // Sets some standard options on a QWizard
194 static void setupWizard(QWizard *);
196 // Read "shortTitle" dynamic property of the pageId and apply it as the title of corresponding progress item
197 static void applyExtensionPageShortTitle(Utils::Wizard *wizard, int pageId);
200 typedef QList<QWizardPage *> WizardPageList;
202 explicit BaseFileWizard(const BaseFileWizardParameters ¶meters, QObject *parent = 0);
204 // Overwrite to create the wizard dialog on the parent, adding
205 // the extension pages.
206 virtual QWizard *createWizardDialog(QWidget *parent,
207 const QString &defaultPath,
208 const WizardPageList &extensionPages) const = 0;
210 // Overwrite to query the parameters from the dialog and generate the files.
211 virtual GeneratedFiles generateFiles(const QWizard *w,
212 QString *errorMessage) const = 0;
214 /* Physically write files. Re-implement (calling the base implementation)
215 * to create files with CustomGeneratorAttribute set. */
216 virtual bool writeFiles(const GeneratedFiles &files, QString *errorMessage);
218 /* Overwrite to perform steps to be done after files are actually created.
219 * The default implementation opens editors with the newly generated files. */
220 virtual bool postGenerateFiles(const QWizard *w, const GeneratedFiles &l, QString *errorMessage);
222 // Utility that returns the preferred suffix for a mime type
223 static QString preferredSuffix(const QString &mimeType);
225 // Utility that performs an overwrite check on a set of files. It checks if
226 // the file exists, can be overwritten at all and prompts the user.
227 enum OverwriteResult { OverwriteOk, OverwriteError, OverwriteCanceled };
228 OverwriteResult promptOverwrite(const QStringList &files,
229 QString *errorMessage) const;
231 // Utility to open the editors for the files whose attribute is set accordingly.
232 static bool postGenerateOpenEditors(const GeneratedFiles &l, QString *errorMessage = 0);
235 BaseFileWizardPrivate *m_d;
238 // StandardFileWizard convenience class for creating one file. It uses
239 // Utils::FileWizardDialog and introduces a new virtual to generate the
240 // files from path and name.
242 class CORE_EXPORT StandardFileWizard : public BaseFileWizard
244 Q_DISABLE_COPY(StandardFileWizard)
248 explicit StandardFileWizard(const BaseFileWizardParameters ¶meters, QObject *parent = 0);
250 // Implemented to create a Utils::FileWizardDialog
251 virtual QWizard *createWizardDialog(QWidget *parent,
252 const QString &defaultPath,
253 const WizardPageList &extensionPages) const;
254 // Implemented to retrieve path and name and call generateFilesFromPath()
255 virtual GeneratedFiles generateFiles(const QWizard *w,
256 QString *errorMessage) const;
258 // Newly introduced virtual that creates a file from a path
259 virtual GeneratedFiles generateFilesFromPath(const QString &path,
261 QString *errorMessage) const = 0;
266 Q_DECLARE_OPERATORS_FOR_FLAGS(Core::GeneratedFile::Attributes)
268 #endif // BASEFILEWIZARD_H