aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/basefilewizard.cpp
blob: 2a9cdef67b891feac2990332630d2ad6614a7fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "basefilewizard.h"

#include "basefilewizardfactory.h"
#include "ifilewizardextension.h"

#include <QMessageBox>

using namespace Utils;

namespace Core {

static QList<IFileWizardExtension *> g_fileWizardExtensions;

IFileWizardExtension::IFileWizardExtension()
{
    g_fileWizardExtensions.append(this);
}

IFileWizardExtension::~IFileWizardExtension()
{
    g_fileWizardExtensions.removeOne(this);
}

BaseFileWizard::BaseFileWizard(const BaseFileWizardFactory *factory,
                               const QVariantMap &extraValues,
                               QWidget *parent) :
    Wizard(parent),
    m_extraValues(extraValues),
    m_factory(factory)
{
    for (IFileWizardExtension *extension : qAsConst(g_fileWizardExtensions))
        m_extensionPages += extension->extensionPages(factory);

    if (!m_extensionPages.empty())
        m_firstExtensionPage = m_extensionPages.front();
}

void BaseFileWizard::initializePage(int id)
{
    Wizard::initializePage(id);
    if (page(id) == m_firstExtensionPage) {
        generateFileList();

        for (IFileWizardExtension *ex : qAsConst(g_fileWizardExtensions))
            ex->firstExtensionPageShown(m_files, m_extraValues);
    }
}

QList<QWizardPage *> BaseFileWizard::extensionPages()
{
    return m_extensionPages;
}

void BaseFileWizard::accept()
{
    if (m_files.isEmpty())
        generateFileList();

    QString errorMessage;

    // Compile result list and prompt for overwrite
    switch (BaseFileWizardFactory::promptOverwrite(&m_files, &errorMessage)) {
    case BaseFileWizardFactory::OverwriteCanceled:
        reject();
        return;
    case BaseFileWizardFactory::OverwriteError:
        QMessageBox::critical(nullptr, tr("Existing files"), errorMessage);
        reject();
        return;
    case BaseFileWizardFactory::OverwriteOk:
        break;
    }

    for (IFileWizardExtension *ex : qAsConst(g_fileWizardExtensions)) {
        for (int i = 0; i < m_files.count(); i++) {
            ex->applyCodeStyle(&m_files[i]);
        }
    }

    // Write
    if (!m_factory->writeFiles(m_files, &errorMessage)) {
        QMessageBox::critical(parentWidget(), tr("File Generation Failure"), errorMessage);
        reject();
        return;
    }

    bool removeOpenProjectAttribute = false;
    // Run the extensions
    for (IFileWizardExtension *ex : qAsConst(g_fileWizardExtensions)) {
        bool remove;
        if (!ex->processFiles(m_files, &remove, &errorMessage)) {
            if (!errorMessage.isEmpty())
                QMessageBox::critical(parentWidget(), tr("File Generation Failure"), errorMessage);
            reject();
            return;
        }
        removeOpenProjectAttribute |= remove;
    }

    if (removeOpenProjectAttribute) {
        for (int i = 0; i < m_files.count(); i++) {
            if (m_files[i].attributes() & GeneratedFile::OpenProjectAttribute)
                m_files[i].setAttributes(GeneratedFile::OpenEditorAttribute);
        }
    }

    // Post generation handler
    if (!m_factory->postGenerateFiles(this, m_files, &errorMessage))
        if (!errorMessage.isEmpty())
            QMessageBox::critical(nullptr, tr("File Generation Failure"), errorMessage);

    Wizard::accept();
}

void BaseFileWizard::reject()
{
    m_files.clear();
    Wizard::reject();
}

void BaseFileWizard::generateFileList()
{
    QString errorMessage;
    m_files = m_factory->generateFiles(this, &errorMessage);
    if (m_files.empty()) {
        QMessageBox::critical(parentWidget(), tr("File Generation Failure"), errorMessage);
        reject();
    }
}

} // namespace Core