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:
Eike Ziller
2014-03-06 17:50:28 +01:00
committed by Friedemann Kleint
parent ca6efdcedc
commit 0d511290f5
4 changed files with 91 additions and 79 deletions

View File

@@ -40,10 +40,14 @@
#include "settingspage.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/mimedatabase.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/designmode.h>
#include <cpptools/cpptoolsconstants.h>
#include <QDebug>
#include <QLibraryInfo>
@@ -55,6 +59,7 @@ using namespace Designer::Internal;
using namespace Designer::Constants;
FormEditorPlugin::FormEditorPlugin()
: m_actionSwitchSource(new QAction(tr("Switch Source/Form"), this))
{
}
@@ -99,6 +104,18 @@ void FormEditorPlugin::extensionsInitialized()
{
DesignMode::instance()->setDesignModeIsRequired();
// 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
}
// 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)

View File

@@ -32,6 +32,10 @@
#include <extensionsystem/iplugin.h>
QT_BEGIN_NAMESPACE
class QAction;
QT_END_NAMESPACE
namespace Designer {
namespace Internal {
@@ -48,14 +52,17 @@ public:
bool initialize(const QStringList &arguments, QString *errorMessage = 0);
void extensionsInitialized();
private:
void initializeTemplates();
private slots:
#ifdef WITH_TESTS
void test_gotoslot();
void test_gotoslot_data();
#endif
void switchSourceForm();
private:
void initializeTemplates();
QAction *m_actionSwitchSource;
};
} // namespace Internal

View File

@@ -50,7 +50,6 @@
#include <coreplugin/mimedatabase.h>
#include <coreplugin/outputpane.h>
#include <texteditor/texteditorsettings.h>
#include <cpptools/cpptoolsconstants.h>
#include <utils/qtcassert.h>
#include <QDesignerFormEditorPluginInterface>
@@ -134,7 +133,6 @@ FormEditorW::FormEditorW() :
m_actionGroupPreviewInStyle(0),
m_previewInStyleMenu(0),
m_actionAboutPlugins(0),
m_actionSwitchSource(0),
m_shortcutMapper(new QSignalMapper(this)),
m_context(0),
m_modeWidget(0),
@@ -401,11 +399,7 @@ void FormEditorW::setupActions()
{
//menus
ActionContainer *medit = ActionManager::actionContainer(Core::Constants::M_EDIT);
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
ActionContainer *mformtools = ActionManager::createMenu(M_FORMEDITOR);
mformtools->menu()->setTitle(tr("For&m Editor"));
mtools->addMenu(mformtools);
ActionContainer *mformtools = ActionManager::actionContainer(M_FORMEDITOR);
//overridden actions
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);
m_actionSwitchSource = new QAction(tr("Switch Source/Form"), this);
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);
mformtools->addSeparator(m_contexts, Core::Constants::G_DEFAULT_THREE);
#if QT_VERSION >= 0x050000
QAction *actionFormSettings = m_fwm->action(QDesignerFormWindowManagerInterface::FormWindowSettingsDialogAction);
#else
QAction *actionFormSettings = m_fwm->actionShowFormWindowSettingsDialog();
#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->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,
#if QT_VERSION >= 0x050000
SLOT(showPluginDialog())
@@ -669,14 +657,15 @@ QAction *FormEditorW::createEditModeAction(QActionGroup *ag,
// Create a tool action
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);
if (!keySequence.isEmpty())
command->setDefaultKeySequence(QKeySequence(keySequence));
if (!a->isSeparator())
bindShortcut(command, a);
c1->addAction(command);
c1->addAction(command, groupId);
return command;
}
@@ -848,57 +837,5 @@ void FormEditorW::print()
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 Designer

View File

@@ -127,7 +127,6 @@ private slots:
void toolChanged(int);
void print();
void setPreviewMenuEnabled(bool e);
void switchSourceForm();
void updateShortcut(QObject *command);
void closeFormEditorsForXmlEditors(QList<Core::IEditor*> editors);
@@ -160,7 +159,8 @@ private:
const QString &keySequence = QString());
Core::Command *addToolAction(QAction *a,
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;
static FormEditorW *m_self;
@@ -187,7 +187,6 @@ private:
QActionGroup *m_actionGroupPreviewInStyle;
QMenu *m_previewInStyleMenu;
QAction *m_actionAboutPlugins;
QAction *m_actionSwitchSource;
QSignalMapper *m_shortcutMapper;
DesignerContext *m_context;