formeditorplugin.cpp 7.85 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** 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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
32
33
#include "formeditorplugin.h"
#include "formeditorfactory.h"
#include "formeditorw.h"
34
#include "formtemplatewizardpage.h"
35
#include "formwindoweditor.h"
con's avatar
con committed
36
37

#ifdef CPP_ENABLED
38
#  include "cpp/formclasswizard.h"
con's avatar
con committed
39
40
#endif

41
#include "settingspage.h"
42
#include "qtdesignerformclasscodegenerator.h"
con's avatar
con committed
43

44
45
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
46
#include <coreplugin/actionmanager/command.h>
47
#include <coreplugin/editormanager/editormanager.h>
con's avatar
con committed
48
#include <coreplugin/icore.h>
49
#include <coreplugin/idocument.h>
con's avatar
con committed
50
#include <coreplugin/coreconstants.h>
51
#include <coreplugin/designmode.h>
52
#include <cpptools/cpptoolsconstants.h>
53
#include <projectexplorer/jsonwizard/jsonwizardfactory.h>
Eike Ziller's avatar
Eike Ziller committed
54
#include <utils/mimetypes/mimedatabase.h>
55
56

#include <QAction>
57
#include <QApplication>
58
#include <QDebug>
Eike Ziller's avatar
Eike Ziller committed
59
#include <QFileInfo>
60
#include <QLibraryInfo>
61
#include <QMenu>
62
63
#include <QTranslator>
#include <QtPlugin>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
64

hjk's avatar
hjk committed
65
using namespace Core;
con's avatar
con committed
66
67
68
using namespace Designer::Internal;
using namespace Designer::Constants;

69
FormEditorPlugin::FormEditorPlugin()
70
    : m_actionSwitchSource(new QAction(tr("Switch Source/Form"), this))
con's avatar
con committed
71
72
73
74
75
76
77
78
79
80
81
82
83
{
}

FormEditorPlugin::~FormEditorPlugin()
{
    FormEditorW::deleteInstance();
}

////////////////////////////////////////////////////
//
// INHERITED FROM ExtensionSystem::Plugin
//
////////////////////////////////////////////////////
84
bool FormEditorPlugin::initialize(const QStringList &arguments, QString *error)
con's avatar
con committed
85
{
86
    Q_UNUSED(arguments)
87

88
89
#ifdef CPP_ENABLED
    IWizardFactory::registerFactoryCreator(
90
                [this]() -> QList<IWizardFactory *> {
91
92
93
94
95
96
97
98
99
100
101
102
                    IWizardFactory *wizard = new FormClassWizard;
                    wizard->setWizardKind(IWizardFactory::FileWizard);
                    wizard->setCategory(QLatin1String(Core::Constants::WIZARD_CATEGORY_QT));
                    wizard->setDisplayCategory(QCoreApplication::translate("Core", Core::Constants::WIZARD_TR_CATEGORY_QT));
                    wizard->setDisplayName(tr("Qt Designer Form Class"));
                    wizard->setId("C.FormClass");
                    wizard->setDescription(tr("Creates a Qt Designer form along with a matching class (C++ header and source file) "
                    "for implementation purposes. You can add the form and class to an existing Qt Widget Project."));

                    return QList<IWizardFactory *>() << wizard;
                });
#endif
con's avatar
con committed
103

104
    ProjectExplorer::JsonWizardFactory::registerPageFactory(new Internal::FormPageFactory);
105
    addAutoReleasedObject(new FormEditorFactory);
106
    addAutoReleasedObject(new SettingsPageProvider);
107
    addAutoReleasedObject(new QtDesignerFormClassCodeGenerator);
108
    // Ensure that loading designer translations is done before FormEditorW is instantiated
hjk's avatar
hjk committed
109
    const QString locale = ICore::userInterfaceLanguage();
110
111
112
    if (!locale.isEmpty()) {
        QTranslator *qtr = new QTranslator(this);
        const QString &creatorTrPath =
hjk's avatar
hjk committed
113
                ICore::resourcePath() + QLatin1String("/translations");
114
115
116
117
118
        const QString &qtTrPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
        const QString &trFile = QLatin1String("designer_") + locale;
        if (qtr->load(trFile, qtTrPath) || qtr->load(trFile, creatorTrPath))
            qApp->installTranslator(qtr);
    }
119
    error->clear();
con's avatar
con committed
120
121
122
123
124
    return true;
}

void FormEditorPlugin::extensionsInitialized()
{
hjk's avatar
hjk committed
125
    DesignMode::instance()->setDesignModeIsRequired();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
126
    // 4) test and make sure everything works (undo, saving, editors, opening/closing multiple files, dirtiness etc)
127
128
129
130
131
132

    ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
    ActionContainer *mformtools = ActionManager::createMenu(M_FORMEDITOR);
    mformtools->menu()->setTitle(tr("For&m Editor"));
    mtools->addMenu(mformtools);

Montel Laurent's avatar
Montel Laurent committed
133
    connect(m_actionSwitchSource, &QAction::triggered, this, &FormEditorPlugin::switchSourceForm);
134
135
    Context context(C_FORMEDITOR, Core::Constants::C_EDITORMANAGER);
    Command *cmd = ActionManager::registerAction(m_actionSwitchSource,
136
137
138
                                                             "FormEditor.FormSwitchSource", context);
    cmd->setDefaultKeySequence(tr("Shift+F4"));
    mformtools->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
con's avatar
con committed
139
140
141
142
}

////////////////////////////////////////////////////
//
143
// PRIVATE functions
con's avatar
con committed
144
145
146
//
////////////////////////////////////////////////////

147
148
149
150
// Find out current existing editor file
static QString currentFile()
{
    if (const IDocument *document = EditorManager::currentDocument()) {
151
        const QString fileName = document->filePath().toString();
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        if (!fileName.isEmpty() && QFileInfo(fileName).isFile())
            return fileName;
    }
    return QString();
}

// Switch between form ('ui') and source file ('cpp'):
// Find corresponding 'other' file, simply assuming it is in the same directory.
static QString otherFile()
{
    // Determine mime type of current file.
    const QString current = currentFile();
    if (current.isEmpty())
        return QString();
Eike Ziller's avatar
Eike Ziller committed
166
167
168
    Utils::MimeDatabase mdb;
    const Utils::MimeType currentMimeType = mdb.mimeTypeForFile(current);
    if (!currentMimeType.isValid())
169
170
171
172
        return QString();
    // Determine potential suffixes of candidate files
    // 'ui' -> 'cpp', 'cpp/h' -> 'ui'.
    QStringList candidateSuffixes;
Eike Ziller's avatar
Eike Ziller committed
173
174
175
176
177
178
    if (currentMimeType.matchesName(QLatin1String(FORM_MIMETYPE))) {
        candidateSuffixes += mdb.mimeTypeForName(
                    QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)).suffixes();
    } else if (currentMimeType.matchesName(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE))
               || currentMimeType.matchesName(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE))) {
        candidateSuffixes += mdb.mimeTypeForName(QLatin1String(FORM_MIMETYPE)).suffixes();
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    } else {
        return QString();
    }
    // Try to find existing file with desired suffix
    const QFileInfo currentFI(current);
    const QString currentBaseName = currentFI.path() + QLatin1Char('/')
            + currentFI.baseName() + QLatin1Char('.');
    foreach (const QString &candidateSuffix, candidateSuffixes) {
        const QFileInfo fi(currentBaseName + candidateSuffix);
        if (fi.isFile())
            return fi.absoluteFilePath();
    }
    return QString();
}

void FormEditorPlugin::switchSourceForm()
{
    const QString fileToOpen = otherFile();
    if (!fileToOpen.isEmpty())
198
        EditorManager::openEditor(fileToOpen);
199
}