forked from qt-creator/qt-creator
Read external tools from resources and show them in menu.
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "coreplugin/core_global.h"
|
||||
#include "coreplugin/uniqueidmanager.h"
|
||||
#include <coreplugin/actionmanager/command.h>
|
||||
#include "coreplugin/icontext.h"
|
||||
|
||||
#include <QtCore/QObject>
|
||||
@@ -50,7 +51,6 @@ QT_END_NAMESPACE
|
||||
namespace Core {
|
||||
|
||||
class ActionContainer;
|
||||
class Command;
|
||||
|
||||
class CORE_EXPORT ActionManager : public QObject
|
||||
{
|
||||
|
||||
@@ -91,6 +91,7 @@ const char * const M_FILE_RECENTFILES = "QtCreator.Menu.File.RecentFiles";
|
||||
const char * const M_EDIT = "QtCreator.Menu.Edit";
|
||||
const char * const M_EDIT_ADVANCED = "QtCreator.Menu.Edit.Advanced";
|
||||
const char * const M_TOOLS = "QtCreator.Menu.Tools";
|
||||
const char * const M_TOOLS_EXTERNAL = "QtCreator.Menu.Tools.External";
|
||||
const char * const M_WINDOW = "QtCreator.Menu.Window";
|
||||
const char * const M_WINDOW_PANES = "QtCreator.Menu.Window.Panes";
|
||||
const char * const M_WINDOW_VIEWS = "QtCreator.Menu.Window.Views";
|
||||
|
||||
@@ -28,11 +28,21 @@
|
||||
**************************************************************************/
|
||||
|
||||
#include "externaltool.h"
|
||||
#include "actionmanager.h"
|
||||
#include "actioncontainer.h"
|
||||
#include "command.h"
|
||||
#include "coreconstants.h"
|
||||
|
||||
#include <QtCore/QXmlStreamReader>
|
||||
#include <QtCore/QDir>
|
||||
#include <QtCore/QFile>
|
||||
#include <QtGui/QMenu>
|
||||
#include <QtGui/QMenuItem>
|
||||
#include <QtGui/QAction>
|
||||
|
||||
#include <QtDebug>
|
||||
|
||||
using namespace Core;
|
||||
using namespace Core::Internal;
|
||||
|
||||
namespace {
|
||||
@@ -53,6 +63,8 @@ namespace {
|
||||
const char * const kOutputReloadDocument = "reloaddocument";
|
||||
}
|
||||
|
||||
// #pragma mark -- ExternalTool
|
||||
|
||||
ExternalTool::ExternalTool() :
|
||||
m_order(-1),
|
||||
m_outputHandling(ShowInPane)
|
||||
@@ -147,7 +159,7 @@ static void localizedText(const QStringList &locales, QXmlStreamReader *reader,
|
||||
}
|
||||
}
|
||||
|
||||
ExternalTool * ExternalTool::createFromXml(const QString &xml, QString *errorMessage, const QString &locale)
|
||||
ExternalTool * ExternalTool::createFromXml(const QByteArray &xml, QString *errorMessage, const QString &locale)
|
||||
{
|
||||
int descriptionLocale = -1;
|
||||
int nameLocale = -1;
|
||||
@@ -220,3 +232,80 @@ ExternalTool * ExternalTool::createFromXml(const QString &xml, QString *errorMes
|
||||
}
|
||||
return tool;
|
||||
}
|
||||
|
||||
// #pragma mark -- ExternalToolManager
|
||||
|
||||
ExternalToolManager::ExternalToolManager(Core::ICore *core)
|
||||
: QObject(core), m_core(core)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
ExternalToolManager::~ExternalToolManager()
|
||||
{
|
||||
qDeleteAll(m_tools);
|
||||
}
|
||||
|
||||
void ExternalToolManager::initialize()
|
||||
{
|
||||
ActionManager *am = m_core->actionManager();
|
||||
ActionContainer *mexternaltools = am->createMenu(Id(Constants::M_TOOLS_EXTERNAL));
|
||||
mexternaltools->menu()->setTitle(tr("External"));
|
||||
ActionContainer *mtools = am->actionContainer(Constants::M_TOOLS);
|
||||
Command *cmd;
|
||||
|
||||
QAction *sep = new QAction(this);
|
||||
sep->setSeparator(true);
|
||||
cmd = am->registerAction(sep, Id("Tools.Separator"), Context(Constants::C_GLOBAL));
|
||||
mtools->addAction(cmd, Constants::G_DEFAULT_THREE);
|
||||
mtools->addMenu(mexternaltools, Constants::G_DEFAULT_THREE);
|
||||
|
||||
QMap<QString, ActionContainer *> categoryMenus;
|
||||
QDir dir(m_core->resourcePath() + QLatin1String("/externaltools"),
|
||||
QLatin1String("*.xml"), QDir::Unsorted, QDir::Files | QDir::Readable);
|
||||
foreach (const QFileInfo &info, dir.entryInfoList()) {
|
||||
QFile file(info.absoluteFilePath());
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
const QByteArray &bytes = file.readAll();
|
||||
file.close();
|
||||
QString error;
|
||||
ExternalTool *tool = ExternalTool::createFromXml(bytes, &error, m_core->userInterfaceLanguage());
|
||||
if (!tool) {
|
||||
// TODO error handling
|
||||
qDebug() << tr("Error while parsing external tool %1: %2").arg(file.fileName(), error);
|
||||
continue;
|
||||
}
|
||||
if (m_tools.contains(tool->id())) {
|
||||
// TODO error handling
|
||||
qDebug() << tr("Error: External tool in %1 has duplicate id").arg(file.fileName());
|
||||
delete tool;
|
||||
continue;
|
||||
}
|
||||
m_tools.insert(tool->id(), tool);
|
||||
|
||||
// category menus
|
||||
ActionContainer *container;
|
||||
if (tool->displayCategory().isEmpty())
|
||||
container = mexternaltools;
|
||||
else
|
||||
container = categoryMenus.value(tool->displayCategory());
|
||||
if (!container) {
|
||||
container = am->createMenu(Id("Tools.External.Category." + tool->displayCategory()));
|
||||
container->menu()->setTitle(tool->displayCategory());
|
||||
mexternaltools->addMenu(container, Constants::G_DEFAULT_ONE);
|
||||
}
|
||||
|
||||
// tool action
|
||||
QAction *action = new QAction(tool->displayName(), this);
|
||||
action->setToolTip(tool->description());
|
||||
action->setWhatsThis(tool->description());
|
||||
action->setData(tool->id());
|
||||
cmd = am->registerAction(action, Id("Tools.External." + tool->id()), Context(Constants::C_GLOBAL));
|
||||
container->addAction(cmd, Constants::G_DEFAULT_TWO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExternalToolManager::menuActivated()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -30,6 +30,9 @@
|
||||
#ifndef EXTERNALTOOL_H
|
||||
#define EXTERNALTOOL_H
|
||||
|
||||
#include "icore.h"
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
@@ -58,7 +61,7 @@ public:
|
||||
QString arguments() const;
|
||||
QString workingDirectory() const;
|
||||
|
||||
static ExternalTool *createFromXml(const QString &xml, QString *errorMessage = 0, const QString &locale = QString());
|
||||
static ExternalTool *createFromXml(const QByteArray &xml, QString *errorMessage = 0, const QString &locale = QString());
|
||||
|
||||
private:
|
||||
QString m_id;
|
||||
@@ -72,6 +75,24 @@ private:
|
||||
OutputHandling m_outputHandling;
|
||||
};
|
||||
|
||||
class ExternalToolManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ExternalToolManager(Core::ICore *core);
|
||||
~ExternalToolManager();
|
||||
|
||||
void initialize();
|
||||
|
||||
private slots:
|
||||
void menuActivated();
|
||||
|
||||
private:
|
||||
Core::ICore *m_core;
|
||||
QMap<QString, ExternalTool *> m_tools;
|
||||
};
|
||||
|
||||
} // Internal
|
||||
} // Core
|
||||
|
||||
|
||||
@@ -38,10 +38,12 @@
|
||||
#include "coreimpl.h"
|
||||
#include "coreconstants.h"
|
||||
#include "editormanager.h"
|
||||
#include "externaltool.h"
|
||||
#include "fancytabwidget.h"
|
||||
#include "filemanager.h"
|
||||
#include "generalsettings.h"
|
||||
#include "helpmanager.h"
|
||||
#include "ieditor.h"
|
||||
#include "ifilefactory.h"
|
||||
#include "messagemanager.h"
|
||||
#include "modemanager.h"
|
||||
@@ -54,7 +56,6 @@
|
||||
#include "progressview.h"
|
||||
#include "shortcutsettings.h"
|
||||
#include "vcsmanager.h"
|
||||
#include "ieditor.h"
|
||||
|
||||
#include "scriptmanager_p.h"
|
||||
#include "settingsdialog.h"
|
||||
@@ -205,6 +206,7 @@ MainWindow::MainWindow() :
|
||||
m_messageManager = new MessageManager;
|
||||
m_editorManager = new EditorManager(m_coreImpl, this);
|
||||
m_editorManager->hide();
|
||||
new ExternalToolManager(m_coreImpl);
|
||||
setCentralWidget(m_modeStack);
|
||||
|
||||
connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*,QWidget*)),
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
using namespace Core::Internal;
|
||||
|
||||
static const char * const TEST_XML1 =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<externaltool id=\"lupdate\">"
|
||||
" <description>Synchronizes translator's ts files with the program code</description>"
|
||||
" <description xml:lang=\"de\">Synchronisiert die ts-Übersetzungsdateien mit dem Programmcode</description>"
|
||||
@@ -24,6 +25,7 @@ static const char * const TEST_XML1 =
|
||||
;
|
||||
|
||||
static const char * const TEST_XML2 =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<externaltool id=\"sort\">"
|
||||
" <description>Sorts the selected text</description>"
|
||||
" <description xml:lang=\"de\">Sortiert den ausgewählten Text</description>"
|
||||
@@ -39,6 +41,7 @@ static const char * const TEST_XML2 =
|
||||
"</externaltool>";
|
||||
|
||||
static const char * const TEST_XML3 =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<externaltool id=\"vi\">"
|
||||
" <description>Opens the current file in vi</description>"
|
||||
" <description xml:lang=\"de\">Öffnet die aktuelle Datei in vi</description>"
|
||||
@@ -54,6 +57,7 @@ static const char * const TEST_XML3 =
|
||||
"</externaltool>";
|
||||
|
||||
static const char * const TEST_XML_LANG =
|
||||
"<?xml version=\"1.0\" encoding=\"Latin-1\"?>"
|
||||
"<externaltool id=\"temp\">"
|
||||
" <description>Hi</description>"
|
||||
" <description xml:lang=\"de\">Hallo</description>"
|
||||
@@ -83,7 +87,7 @@ private Q_SLOTS:
|
||||
void ExternaltoolTest::testRead1()
|
||||
{
|
||||
QString error;
|
||||
ExternalTool *tool = ExternalTool::createFromXml(QLatin1String(TEST_XML1), &error);
|
||||
ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML1), &error);
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->id(), QString::fromLatin1("lupdate"));
|
||||
@@ -103,7 +107,7 @@ void ExternaltoolTest::testRead1()
|
||||
void ExternaltoolTest::testRead2()
|
||||
{
|
||||
QString error;
|
||||
ExternalTool *tool = ExternalTool::createFromXml(QLatin1String(TEST_XML2), &error);
|
||||
ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML2), &error);
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->id(), QString::fromLatin1("sort"));
|
||||
@@ -122,7 +126,7 @@ void ExternaltoolTest::testRead2()
|
||||
void ExternaltoolTest::testRead3()
|
||||
{
|
||||
QString error;
|
||||
ExternalTool *tool = ExternalTool::createFromXml(QLatin1String(TEST_XML3), &error);
|
||||
ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML3), &error);
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->id(), QString::fromLatin1("vi"));
|
||||
@@ -143,7 +147,7 @@ void ExternaltoolTest::testReadLocale()
|
||||
QString error;
|
||||
ExternalTool *tool;
|
||||
|
||||
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error);
|
||||
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error);
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->description(), QString::fromLatin1("Hi"));
|
||||
@@ -151,7 +155,7 @@ void ExternaltoolTest::testReadLocale()
|
||||
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi"));
|
||||
delete tool;
|
||||
|
||||
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("uk"));
|
||||
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("uk"));
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->description(), QString::fromLatin1("Hi"));
|
||||
@@ -159,7 +163,7 @@ void ExternaltoolTest::testReadLocale()
|
||||
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi"));
|
||||
delete tool;
|
||||
|
||||
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("de_DE.UTF-8"));
|
||||
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("de_DE.UTF-8"));
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->description(), QString::fromLatin1("Hallo"));
|
||||
@@ -167,7 +171,7 @@ void ExternaltoolTest::testReadLocale()
|
||||
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hallo"));
|
||||
delete tool;
|
||||
|
||||
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("de_CH"));
|
||||
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("de_CH"));
|
||||
QVERIFY(tool != 0);
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(tool->description(), QString::fromLatin1("Grüezi"));
|
||||
|
||||
Reference in New Issue
Block a user