首页 Qt Qt Creator 插件开发(20):添加新的工程类型(续)

Qt Creator 插件开发(20):添加新的工程类型(续)

0 1.6K

预定义的IWizard实现:Core::BaseFileWizard

Qt Creator 已经提供了一个默认的IWizard接口的实现,也就是Core::BaseFileWizard类。这个类提供了
IWizard接口的所有函数的默认实现,并且增加了自己特有的虚函数。为了利用这个类,我们需要继承它,并且重写其中的一个或多个函数。

Core::GeneratedFileCore::GeneratedFiles

通常说,一个新的向导(IWizard的实现)能够提供用户多种提示,并且最终生成一个或者多个文件。Core::GeneratedFile类帮助我们对将要生成的文件进行抽象。很快,我们就可以了解到,在Core::BaseFileWizard的子类中,我们可以为每一个自动生成的文件创建Core::GeneratedFile类的实例。Core::GeneratedFile在 coreplugin/basefilewizard.h 中声明:

class CORE_EXPORT GeneratedFile
{
public:
    enum Attribute { // Open this file in editor
                     OpenEditorAttribute = 0x01,
                     // Open project
                     OpenProjectAttribute = 0x02,
                     /* File is generated by external scripts, do not write out,
                      * see BaseFileWizard::writeFiles() */
                     CustomGeneratorAttribute = 0x4
                   };
    Q_DECLARE_FLAGS(Attributes, Attribute)

    GeneratedFile();
    explicit GeneratedFile(const QString &path);
    GeneratedFile(const GeneratedFile &);
    GeneratedFile &operator=(const GeneratedFile &);
    ~GeneratedFile();

    // Full path of the file should be created, or the suggested file name
    QString path() const;
    void setPath(const QString &p);

    // Contents of the file (UTF8)
    QString contents() const;
    void setContents(const QString &c);

    QByteArray binaryContents() const;
    void setBinaryContents(const QByteArray &c);

    // Defaults to false (Text file).
    bool isBinary() const;
    void setBinary(bool b);

    // Id of editor to open the file with
    QString editorId() const;
    void setEditorId(const QString &k);

    bool write(QString *errorMessage) const;

    Attributes attributes() const;
    void setAttributes(Attributes a);

private:
    QSharedDataPointer m_d;
};

typedef QList GeneratedFiles;

需要由Core::BaseFileWizard子类生成的文件由Core::GeneratedFile类对象表示。这个类包含了需要生成的文件的三个关键属性:

  1. 文件名(及绝对路径)
  2. 用于编辑文件所需要的编辑器类型,合法值为:
    • CppEditor::Constants::CPPEDITOR_KIND
    • GenericProjectManager::Constants::PROJECT_KIND
    • Git::Constants:: GIT_COMMAND_LOG_EDITOR_KIND
    • Git::Constants:: C_GIT_COMMAND_LOG_EDITOR
  3. 文件内容

假设我们需要生成如下内容的 C++ 文件:

#include <iostream>

int main()
{
    cout << "Hello World\n";

    return 0;
}

我们按照下述代码使用Core::GeneratedFile

#include <coreplugin/basefilewizard.h>
#include <cppeditor/cppeditorconstants.h>

Core::GeneratedFile genFile("C:/Path/To/Source.cpp");
genFile.setEditorId(CppEditor::Constants::CPPEDITOR_ID);
genFile.setContents(
                     "#include <iostream>\n"
                     "\n"
                     "int main()\n"
                     "{\n"
                     "    cout << \"Hello World\n\";\n"
                     "    \n"
                     "    return 0;\n"
                     "}"
                    );
genFile.write(NULL);

发表评论

关于我

devbean

devbean

豆子,生于山东,定居南京。毕业于山东大学软件工程专业。软件工程师,主要关注于 Qt、Angular 等界面技术。

主题 Salodad 由 PenciDesign 提供 | 静态文件存储由又拍云存储提供 | 苏ICP备13027999号-2