forked from qt-creator/qt-creator
FormEditor: Make it possible to switch from source to form
before designer is fully initialized. The action actually doesn't depend on designer being initialized, so it can be registered directly at start up. Task-number: QTCREATORBUG-11244 Change-Id: I275517befa4dc94714b94a6cd665eb0361a3c45b Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
committed by
Friedemann Kleint
parent
ca6efdcedc
commit
0d511290f5
@@ -40,10 +40,14 @@
|
|||||||
#include "settingspage.h"
|
#include "settingspage.h"
|
||||||
#include "qtdesignerformclasscodegenerator.h"
|
#include "qtdesignerformclasscodegenerator.h"
|
||||||
|
|
||||||
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||||
|
#include <coreplugin/actionmanager/actionmanager.h>
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <coreplugin/mimedatabase.h>
|
#include <coreplugin/mimedatabase.h>
|
||||||
#include <coreplugin/coreconstants.h>
|
#include <coreplugin/coreconstants.h>
|
||||||
#include <coreplugin/designmode.h>
|
#include <coreplugin/designmode.h>
|
||||||
|
#include <cpptools/cpptoolsconstants.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QLibraryInfo>
|
#include <QLibraryInfo>
|
||||||
@@ -55,6 +59,7 @@ using namespace Designer::Internal;
|
|||||||
using namespace Designer::Constants;
|
using namespace Designer::Constants;
|
||||||
|
|
||||||
FormEditorPlugin::FormEditorPlugin()
|
FormEditorPlugin::FormEditorPlugin()
|
||||||
|
: m_actionSwitchSource(new QAction(tr("Switch Source/Form"), this))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,6 +104,18 @@ void FormEditorPlugin::extensionsInitialized()
|
|||||||
{
|
{
|
||||||
DesignMode::instance()->setDesignModeIsRequired();
|
DesignMode::instance()->setDesignModeIsRequired();
|
||||||
// 4) test and make sure everything works (undo, saving, editors, opening/closing multiple files, dirtiness etc)
|
// 4) test and make sure everything works (undo, saving, editors, opening/closing multiple files, dirtiness etc)
|
||||||
|
|
||||||
|
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
||||||
|
ActionContainer *mformtools = ActionManager::createMenu(M_FORMEDITOR);
|
||||||
|
mformtools->menu()->setTitle(tr("For&m Editor"));
|
||||||
|
mtools->addMenu(mformtools);
|
||||||
|
|
||||||
|
connect(m_actionSwitchSource, SIGNAL(triggered()), this, SLOT(switchSourceForm()));
|
||||||
|
Core::Context context(Designer::Constants::C_FORMEDITOR, Core::Constants::C_EDITORMANAGER);
|
||||||
|
Core::Command *cmd = Core::ActionManager::registerAction(m_actionSwitchSource,
|
||||||
|
"FormEditor.FormSwitchSource", context);
|
||||||
|
cmd->setDefaultKeySequence(tr("Shift+F4"));
|
||||||
|
mformtools->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////
|
////////////////////////////////////////////////////
|
||||||
@@ -134,4 +151,56 @@ void FormEditorPlugin::initializeTemplates()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find out current existing editor file
|
||||||
|
static QString currentFile()
|
||||||
|
{
|
||||||
|
if (const IDocument *document = EditorManager::currentDocument()) {
|
||||||
|
const QString fileName = document->filePath();
|
||||||
|
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();
|
||||||
|
const MimeType currentMimeType = MimeDatabase::findByFile(current);
|
||||||
|
if (!currentMimeType)
|
||||||
|
return QString();
|
||||||
|
// Determine potential suffixes of candidate files
|
||||||
|
// 'ui' -> 'cpp', 'cpp/h' -> 'ui'.
|
||||||
|
QStringList candidateSuffixes;
|
||||||
|
if (currentMimeType.type() == QLatin1String(FORM_MIMETYPE)) {
|
||||||
|
candidateSuffixes += MimeDatabase::findByType(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)).suffixes();
|
||||||
|
} else if (currentMimeType.type() == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)
|
||||||
|
|| currentMimeType.type() == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)) {
|
||||||
|
candidateSuffixes += MimeDatabase::findByType(QLatin1String(FORM_MIMETYPE)).suffixes();
|
||||||
|
} 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())
|
||||||
|
Core::EditorManager::openEditor(fileToOpen);
|
||||||
|
}
|
||||||
|
|
||||||
Q_EXPORT_PLUGIN(FormEditorPlugin)
|
Q_EXPORT_PLUGIN(FormEditorPlugin)
|
||||||
|
|||||||
@@ -32,6 +32,10 @@
|
|||||||
|
|
||||||
#include <extensionsystem/iplugin.h>
|
#include <extensionsystem/iplugin.h>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QAction;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace Designer {
|
namespace Designer {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
@@ -48,14 +52,17 @@ public:
|
|||||||
bool initialize(const QStringList &arguments, QString *errorMessage = 0);
|
bool initialize(const QStringList &arguments, QString *errorMessage = 0);
|
||||||
void extensionsInitialized();
|
void extensionsInitialized();
|
||||||
|
|
||||||
private:
|
|
||||||
void initializeTemplates();
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
#ifdef WITH_TESTS
|
#ifdef WITH_TESTS
|
||||||
void test_gotoslot();
|
void test_gotoslot();
|
||||||
void test_gotoslot_data();
|
void test_gotoslot_data();
|
||||||
#endif
|
#endif
|
||||||
|
void switchSourceForm();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void initializeTemplates();
|
||||||
|
|
||||||
|
QAction *m_actionSwitchSource;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
@@ -50,7 +50,6 @@
|
|||||||
#include <coreplugin/mimedatabase.h>
|
#include <coreplugin/mimedatabase.h>
|
||||||
#include <coreplugin/outputpane.h>
|
#include <coreplugin/outputpane.h>
|
||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
#include <cpptools/cpptoolsconstants.h>
|
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <QDesignerFormEditorPluginInterface>
|
#include <QDesignerFormEditorPluginInterface>
|
||||||
@@ -134,7 +133,6 @@ FormEditorW::FormEditorW() :
|
|||||||
m_actionGroupPreviewInStyle(0),
|
m_actionGroupPreviewInStyle(0),
|
||||||
m_previewInStyleMenu(0),
|
m_previewInStyleMenu(0),
|
||||||
m_actionAboutPlugins(0),
|
m_actionAboutPlugins(0),
|
||||||
m_actionSwitchSource(0),
|
|
||||||
m_shortcutMapper(new QSignalMapper(this)),
|
m_shortcutMapper(new QSignalMapper(this)),
|
||||||
m_context(0),
|
m_context(0),
|
||||||
m_modeWidget(0),
|
m_modeWidget(0),
|
||||||
@@ -401,11 +399,7 @@ void FormEditorW::setupActions()
|
|||||||
{
|
{
|
||||||
//menus
|
//menus
|
||||||
ActionContainer *medit = ActionManager::actionContainer(Core::Constants::M_EDIT);
|
ActionContainer *medit = ActionManager::actionContainer(Core::Constants::M_EDIT);
|
||||||
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
ActionContainer *mformtools = ActionManager::actionContainer(M_FORMEDITOR);
|
||||||
|
|
||||||
ActionContainer *mformtools = ActionManager::createMenu(M_FORMEDITOR);
|
|
||||||
mformtools->menu()->setTitle(tr("For&m Editor"));
|
|
||||||
mtools->addMenu(mformtools);
|
|
||||||
|
|
||||||
//overridden actions
|
//overridden actions
|
||||||
bindShortcut(ActionManager::registerAction(m_fwm->actionUndo(), Core::Constants::UNDO, m_contexts), m_fwm->actionUndo());
|
bindShortcut(ActionManager::registerAction(m_fwm->actionUndo(), Core::Constants::UNDO, m_contexts), m_fwm->actionUndo());
|
||||||
@@ -528,26 +522,20 @@ void FormEditorW::setupActions()
|
|||||||
|
|
||||||
mformtools->addSeparator(m_contexts);
|
mformtools->addSeparator(m_contexts);
|
||||||
|
|
||||||
m_actionSwitchSource = new QAction(tr("Switch Source/Form"), this);
|
mformtools->addSeparator(m_contexts, Core::Constants::G_DEFAULT_THREE);
|
||||||
connect(m_actionSwitchSource, SIGNAL(triggered()), this, SLOT(switchSourceForm()));
|
|
||||||
|
|
||||||
// Switch form/source in editor/design contexts.
|
|
||||||
Context switchContexts = m_contexts;
|
|
||||||
switchContexts.add(Core::Constants::C_EDITORMANAGER);
|
|
||||||
addToolAction(m_actionSwitchSource, switchContexts, "FormEditor.FormSwitchSource", mformtools, tr("Shift+F4"));
|
|
||||||
|
|
||||||
mformtools->addSeparator(m_contexts);
|
|
||||||
#if QT_VERSION >= 0x050000
|
#if QT_VERSION >= 0x050000
|
||||||
QAction *actionFormSettings = m_fwm->action(QDesignerFormWindowManagerInterface::FormWindowSettingsDialogAction);
|
QAction *actionFormSettings = m_fwm->action(QDesignerFormWindowManagerInterface::FormWindowSettingsDialogAction);
|
||||||
#else
|
#else
|
||||||
QAction *actionFormSettings = m_fwm->actionShowFormWindowSettingsDialog();
|
QAction *actionFormSettings = m_fwm->actionShowFormWindowSettingsDialog();
|
||||||
#endif
|
#endif
|
||||||
addToolAction(actionFormSettings, m_contexts, "FormEditor.FormSettings", mformtools);
|
addToolAction(actionFormSettings, m_contexts, "FormEditor.FormSettings", mformtools,
|
||||||
|
QString(), Core::Constants::G_DEFAULT_THREE);
|
||||||
|
|
||||||
mformtools->addSeparator(m_contexts);
|
mformtools->addSeparator(m_contexts, Core::Constants::G_DEFAULT_THREE);
|
||||||
m_actionAboutPlugins = new QAction(tr("About Qt Designer Plugins..."), this);
|
m_actionAboutPlugins = new QAction(tr("About Qt Designer Plugins..."), this);
|
||||||
m_actionAboutPlugins->setMenuRole(QAction::NoRole);
|
m_actionAboutPlugins->setMenuRole(QAction::NoRole);
|
||||||
addToolAction(m_actionAboutPlugins, m_contexts, "FormEditor.AboutPlugins", mformtools);
|
addToolAction(m_actionAboutPlugins, m_contexts, "FormEditor.AboutPlugins", mformtools,
|
||||||
|
QString(), Core::Constants::G_DEFAULT_THREE);
|
||||||
connect(m_actionAboutPlugins, SIGNAL(triggered()), m_fwm,
|
connect(m_actionAboutPlugins, SIGNAL(triggered()), m_fwm,
|
||||||
#if QT_VERSION >= 0x050000
|
#if QT_VERSION >= 0x050000
|
||||||
SLOT(showPluginDialog())
|
SLOT(showPluginDialog())
|
||||||
@@ -669,14 +657,15 @@ QAction *FormEditorW::createEditModeAction(QActionGroup *ag,
|
|||||||
|
|
||||||
// Create a tool action
|
// Create a tool action
|
||||||
Command *FormEditorW::addToolAction(QAction *a, const Context &context, const Id &id,
|
Command *FormEditorW::addToolAction(QAction *a, const Context &context, const Id &id,
|
||||||
ActionContainer *c1, const QString &keySequence)
|
ActionContainer *c1, const QString &keySequence,
|
||||||
|
Core::Id groupId)
|
||||||
{
|
{
|
||||||
Command *command = ActionManager::registerAction(a, id, context);
|
Command *command = ActionManager::registerAction(a, id, context);
|
||||||
if (!keySequence.isEmpty())
|
if (!keySequence.isEmpty())
|
||||||
command->setDefaultKeySequence(QKeySequence(keySequence));
|
command->setDefaultKeySequence(QKeySequence(keySequence));
|
||||||
if (!a->isSeparator())
|
if (!a->isSeparator())
|
||||||
bindShortcut(command, a);
|
bindShortcut(command, a);
|
||||||
c1->addAction(command);
|
c1->addAction(command, groupId);
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -848,57 +837,5 @@ void FormEditorW::print()
|
|||||||
printer->setOrientation(oldOrientation);
|
printer->setOrientation(oldOrientation);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find out current existing editor file
|
|
||||||
static QString currentFile()
|
|
||||||
{
|
|
||||||
if (const IDocument *document = EditorManager::currentDocument()) {
|
|
||||||
const QString fileName = document->filePath();
|
|
||||||
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();
|
|
||||||
const MimeType currentMimeType = MimeDatabase::findByFile(current);
|
|
||||||
if (!currentMimeType)
|
|
||||||
return QString();
|
|
||||||
// Determine potential suffixes of candidate files
|
|
||||||
// 'ui' -> 'cpp', 'cpp/h' -> 'ui'.
|
|
||||||
QStringList candidateSuffixes;
|
|
||||||
if (currentMimeType.type() == QLatin1String(FORM_MIMETYPE)) {
|
|
||||||
candidateSuffixes += MimeDatabase::findByType(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)).suffixes();
|
|
||||||
} else if (currentMimeType.type() == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)
|
|
||||||
|| currentMimeType.type() == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)) {
|
|
||||||
candidateSuffixes += MimeDatabase::findByType(QLatin1String(FORM_MIMETYPE)).suffixes();
|
|
||||||
} 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 FormEditorW::switchSourceForm()
|
|
||||||
{
|
|
||||||
const QString fileToOpen = otherFile();
|
|
||||||
if (!fileToOpen.isEmpty())
|
|
||||||
EditorManager::openEditor(fileToOpen);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace Designer
|
} // namespace Designer
|
||||||
|
|||||||
@@ -127,7 +127,6 @@ private slots:
|
|||||||
void toolChanged(int);
|
void toolChanged(int);
|
||||||
void print();
|
void print();
|
||||||
void setPreviewMenuEnabled(bool e);
|
void setPreviewMenuEnabled(bool e);
|
||||||
void switchSourceForm();
|
|
||||||
void updateShortcut(QObject *command);
|
void updateShortcut(QObject *command);
|
||||||
void closeFormEditorsForXmlEditors(QList<Core::IEditor*> editors);
|
void closeFormEditorsForXmlEditors(QList<Core::IEditor*> editors);
|
||||||
|
|
||||||
@@ -160,7 +159,8 @@ private:
|
|||||||
const QString &keySequence = QString());
|
const QString &keySequence = QString());
|
||||||
Core::Command *addToolAction(QAction *a,
|
Core::Command *addToolAction(QAction *a,
|
||||||
const Core::Context &context, const Core::Id &id,
|
const Core::Context &context, const Core::Id &id,
|
||||||
Core::ActionContainer *c1, const QString &keySequence = QString());
|
Core::ActionContainer *c1, const QString &keySequence = QString(),
|
||||||
|
Core::Id groupId = Core::Id());
|
||||||
QToolBar *createEditorToolBar() const;
|
QToolBar *createEditorToolBar() const;
|
||||||
|
|
||||||
static FormEditorW *m_self;
|
static FormEditorW *m_self;
|
||||||
@@ -187,7 +187,6 @@ private:
|
|||||||
QActionGroup *m_actionGroupPreviewInStyle;
|
QActionGroup *m_actionGroupPreviewInStyle;
|
||||||
QMenu *m_previewInStyleMenu;
|
QMenu *m_previewInStyleMenu;
|
||||||
QAction *m_actionAboutPlugins;
|
QAction *m_actionAboutPlugins;
|
||||||
QAction *m_actionSwitchSource;
|
|
||||||
QSignalMapper *m_shortcutMapper;
|
QSignalMapper *m_shortcutMapper;
|
||||||
|
|
||||||
DesignerContext *m_context;
|
DesignerContext *m_context;
|
||||||
|
|||||||
Reference in New Issue
Block a user