Add "Add Library" action to project explorer's context menu

Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
Task-number: QTCREATORBUG-4127
This commit is contained in:
Jarek Kobus
2011-04-13 13:54:15 +02:00
parent 52f52ffde3
commit d34c81a2a6
6 changed files with 80 additions and 43 deletions

View File

@@ -37,7 +37,6 @@
#include "qt4projectmanager.h" #include "qt4projectmanager.h"
#include "qt4projectmanagerconstants.h" #include "qt4projectmanagerconstants.h"
#include "profileeditorfactory.h" #include "profileeditorfactory.h"
#include "addlibrarywizard.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
@@ -244,26 +243,6 @@ void ProFileEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
highlighter->rehighlight(); highlighter->rehighlight();
} }
void ProFileEditorWidget::addLibrary()
{
AddLibraryWizard wizard(file()->fileName(), this);
if (wizard.exec() != QDialog::Accepted)
return;
TextEditor::BaseTextEditor *editable = editor();
const int endOfDoc = editable->position(TextEditor::ITextEditor::EndOfDoc);
editable->setCursorPosition(endOfDoc);
QString snippet = wizard.snippet();
// add extra \n in case the last line is not empty
int line, column;
editable->convertPosition(endOfDoc, &line, &column);
if (!editable->textAt(endOfDoc - column, column).simplified().isEmpty())
snippet = QLatin1Char('\n') + snippet;
editable->insert(snippet);
}
void ProFileEditorWidget::jumpToFile() void ProFileEditorWidget::jumpToFile()
{ {
openLink(findLinkAt(textCursor())); openLink(findLinkAt(textCursor()));

View File

@@ -94,7 +94,6 @@ protected:
public slots: public slots:
virtual void setFontSettings(const TextEditor::FontSettings &); virtual void setFontSettings(const TextEditor::FontSettings &);
void addLibrary();
void jumpToFile(); void jumpToFile();
private: private:

View File

@@ -39,8 +39,10 @@
#include "qt4project.h" #include "qt4project.h"
#include "qt4target.h" #include "qt4target.h"
#include "profilereader.h" #include "profilereader.h"
#include "profileeditor.h"
#include "qmakestep.h" #include "qmakestep.h"
#include "qt4buildconfiguration.h" #include "qt4buildconfiguration.h"
#include "addlibrarywizard.h"
#include "wizards/qtquickapp.h" #include "wizards/qtquickapp.h"
#include "wizards/html5app.h" #include "wizards/html5app.h"
@@ -285,6 +287,52 @@ ProjectExplorer::Project *Qt4Manager::contextProject() const
return m_contextProject; return m_contextProject;
} }
void Qt4Manager::addLibrary()
{
Core::EditorManager *em = Core::EditorManager::instance();
ProFileEditorWidget *editor = qobject_cast<ProFileEditorWidget*>(em->currentEditor()->widget());
if (editor)
addLibrary(editor->file()->fileName(), editor);
}
void Qt4Manager::addLibraryContextMenu()
{
ProjectExplorer::Node *node = ProjectExplorer::ProjectExplorerPlugin::instance()->currentNode();
if (qobject_cast<Qt4ProFileNode *>(node))
addLibrary(node->path());
}
void Qt4Manager::addLibrary(const QString &fileName, ProFileEditorWidget *editor)
{
AddLibraryWizard wizard(fileName, Core::EditorManager::instance());
if (wizard.exec() != QDialog::Accepted)
return;
TextEditor::BaseTextEditor *editable = 0;
if (editor) {
editable = editor->editor();
} else {
Core::EditorManager *em = Core::EditorManager::instance();
editable = qobject_cast<TextEditor::BaseTextEditor *>
(em->openEditor(fileName, Qt4ProjectManager::Constants::PROFILE_EDITOR_ID));
}
if (!editable)
return;
const int endOfDoc = editable->position(TextEditor::ITextEditor::EndOfDoc);
editable->setCursorPosition(endOfDoc);
QString snippet = wizard.snippet();
// add extra \n in case the last line is not empty
int line, column;
editable->convertPosition(endOfDoc, &line, &column);
if (!editable->textAt(endOfDoc - column, column).simplified().isEmpty())
snippet = QLatin1Char('\n') + snippet;
editable->insert(snippet);
}
void Qt4Manager::runQMake() void Qt4Manager::runQMake()
{ {
runQMake(projectExplorer()->startupProject(), 0); runQMake(projectExplorer()->startupProject(), 0);

View File

@@ -93,6 +93,8 @@ public:
enum Action { BUILD, REBUILD, CLEAN }; enum Action { BUILD, REBUILD, CLEAN };
public slots: public slots:
void addLibrary();
void addLibraryContextMenu();
void runQMake(); void runQMake();
void runQMakeContextMenu(); void runQMakeContextMenu();
void buildSubDirContextMenu(); void buildSubDirContextMenu();
@@ -108,6 +110,7 @@ private slots:
private: private:
QList<Qt4Project *> m_projects; QList<Qt4Project *> m_projects;
void handleSubDirContexMenu(Action action); void handleSubDirContexMenu(Action action);
void addLibrary(const QString &fileName, Internal::ProFileEditorWidget *editor = 0);
void runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node); void runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node);
Internal::Qt4ProjectManagerPlugin *m_plugin; Internal::Qt4ProjectManagerPlugin *m_plugin;

View File

@@ -109,6 +109,7 @@ bool Qt4ProjectManagerPlugin::initialize(const QStringList &arguments, QString *
{ {
Q_UNUSED(arguments) Q_UNUSED(arguments)
const Core::Context projectContext(Qt4ProjectManager::Constants::PROJECT_ID); const Core::Context projectContext(Qt4ProjectManager::Constants::PROJECT_ID);
Core::Context projecTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE);
ProFileParser::initialize(); ProFileParser::initialize();
ProFileEvaluator::initialize(); ProFileEvaluator::initialize();
@@ -243,35 +244,43 @@ bool Qt4ProjectManagerPlugin::initialize(const QStringList &arguments, QString *
this, SLOT(buildStateChanged(ProjectExplorer::Project *))); this, SLOT(buildStateChanged(ProjectExplorer::Project *)));
connect(m_projectExplorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project *)), connect(m_projectExplorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project *)),
this, SLOT(currentProjectChanged())); this, SLOT(currentProjectChanged()));
connect(m_projectExplorer, SIGNAL(currentNodeChanged(ProjectExplorer::Node*,ProjectExplorer::Project*)),
this, SLOT(currentNodeChanged(ProjectExplorer::Node*)));
Core::ActionContainer *contextMenu = am->createMenu(Qt4ProjectManager::Constants::M_CONTEXT); Core::ActionContainer *contextMenu = am->createMenu(Qt4ProjectManager::Constants::M_CONTEXT);
Core::Command *cmd;
Core::Context proFileEditorContext = Core::Context(Qt4ProjectManager::Constants::C_PROFILEEDITOR); Core::Context proFileEditorContext = Core::Context(Qt4ProjectManager::Constants::C_PROFILEEDITOR);
QAction *jumpToFile = new QAction(tr("Jump to File Under Cursor"), this); QAction *jumpToFile = new QAction(tr("Jump to File Under Cursor"), this);
cmd = am->registerAction(jumpToFile, command = am->registerAction(jumpToFile,
Constants::JUMP_TO_FILE, proFileEditorContext); Constants::JUMP_TO_FILE, proFileEditorContext);
cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2)); command->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
connect(jumpToFile, SIGNAL(triggered()), connect(jumpToFile, SIGNAL(triggered()),
this, SLOT(jumpToFile())); this, SLOT(jumpToFile()));
contextMenu->addAction(cmd); contextMenu->addAction(command);
QAction *addLibrary = new QAction(tr("Add Library..."), this); m_addLibraryAction = new QAction(tr("Add Library..."), this);
cmd = am->registerAction(addLibrary, command = am->registerAction(m_addLibraryAction,
Constants::ADDLIBRARY, proFileEditorContext); Constants::ADDLIBRARY, proFileEditorContext);
connect(addLibrary, SIGNAL(triggered()), connect(m_addLibraryAction, SIGNAL(triggered()),
this, SLOT(addLibrary())); m_qt4ProjectManager, SLOT(addLibrary()));
contextMenu->addAction(cmd); contextMenu->addAction(command);
m_addLibraryActionContextMenu = new QAction(tr("Add Library..."), this);
command = am->registerAction(m_addLibraryActionContextMenu,
Constants::ADDLIBRARY, projecTreeContext);
connect(m_addLibraryActionContextMenu, SIGNAL(triggered()),
m_qt4ProjectManager, SLOT(addLibraryContextMenu()));
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_FILES);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_FILES);
QAction *separator = new QAction(this); QAction *separator = new QAction(this);
separator->setSeparator(true); separator->setSeparator(true);
contextMenu->addAction(am->registerAction(separator, contextMenu->addAction(am->registerAction(separator,
Core::Id(Constants::SEPARATOR), proFileEditorContext)); Core::Id(Constants::SEPARATOR), proFileEditorContext));
cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION); command = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
contextMenu->addAction(cmd); contextMenu->addAction(command);
return true; return true;
} }
@@ -317,6 +326,11 @@ void Qt4ProjectManagerPlugin::currentProjectChanged()
m_runQMakeAction->setEnabled(!m_projectExplorer->buildManager()->isBuilding(m_projectExplorer->currentProject())); m_runQMakeAction->setEnabled(!m_projectExplorer->buildManager()->isBuilding(m_projectExplorer->currentProject()));
} }
void Qt4ProjectManagerPlugin::currentNodeChanged(ProjectExplorer::Node *node)
{
m_addLibraryActionContextMenu->setEnabled(qobject_cast<Qt4ProFileNode *>(node));
}
void Qt4ProjectManagerPlugin::buildStateChanged(ProjectExplorer::Project *pro) void Qt4ProjectManagerPlugin::buildStateChanged(ProjectExplorer::Project *pro)
{ {
ProjectExplorer::Project *currentProject = m_projectExplorer->currentProject(); ProjectExplorer::Project *currentProject = m_projectExplorer->currentProject();
@@ -326,14 +340,6 @@ void Qt4ProjectManagerPlugin::buildStateChanged(ProjectExplorer::Project *pro)
m_runQMakeActionContextMenu->setEnabled(!m_projectExplorer->buildManager()->isBuilding(pro)); m_runQMakeActionContextMenu->setEnabled(!m_projectExplorer->buildManager()->isBuilding(pro));
} }
void Qt4ProjectManagerPlugin::addLibrary()
{
Core::EditorManager *em = Core::EditorManager::instance();
ProFileEditorWidget *editor = qobject_cast<ProFileEditorWidget*>(em->currentEditor()->widget());
if (editor)
editor->addLibrary();
}
void Qt4ProjectManagerPlugin::jumpToFile() void Qt4ProjectManagerPlugin::jumpToFile()
{ {
Core::EditorManager *em = Core::EditorManager::instance(); Core::EditorManager *em = Core::EditorManager::instance();

View File

@@ -69,8 +69,8 @@ private slots:
void updateContextMenu(ProjectExplorer::Project *project, void updateContextMenu(ProjectExplorer::Project *project,
ProjectExplorer::Node *node); ProjectExplorer::Node *node);
void currentProjectChanged(); void currentProjectChanged();
void currentNodeChanged(ProjectExplorer::Node *node);
void buildStateChanged(ProjectExplorer::Project *pro); void buildStateChanged(ProjectExplorer::Project *pro);
void addLibrary();
void jumpToFile(); void jumpToFile();
#ifdef WITH_TESTS #ifdef WITH_TESTS
@@ -98,6 +98,8 @@ private:
QAction *m_buildSubProjectContextMenu; QAction *m_buildSubProjectContextMenu;
QAction *m_rebuildSubProjectContextMenu; QAction *m_rebuildSubProjectContextMenu;
QAction *m_cleanSubProjectContextMenu; QAction *m_cleanSubProjectContextMenu;
QAction *m_addLibraryAction;
QAction *m_addLibraryActionContextMenu;
GettingStartedWelcomePage *m_welcomePage; GettingStartedWelcomePage *m_welcomePage;
Core::Context m_projectContext; Core::Context m_projectContext;
}; };