OSDN Git Service

Add "Add Library" action to project explorer's context menu
authorJarek Kobus <jkobus@trolltech.com>
Wed, 13 Apr 2011 11:54:15 +0000 (13:54 +0200)
committerJarek Kobus <jkobus@trolltech.com>
Wed, 13 Apr 2011 11:56:27 +0000 (13:56 +0200)
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
Task-number: QTCREATORBUG-4127

src/plugins/qt4projectmanager/profileeditor.cpp
src/plugins/qt4projectmanager/profileeditor.h
src/plugins/qt4projectmanager/qt4projectmanager.cpp
src/plugins/qt4projectmanager/qt4projectmanager.h
src/plugins/qt4projectmanager/qt4projectmanagerplugin.cpp
src/plugins/qt4projectmanager/qt4projectmanagerplugin.h

index 6bb066c..b341331 100644 (file)
@@ -37,7 +37,6 @@
 #include "qt4projectmanager.h"
 #include "qt4projectmanagerconstants.h"
 #include "profileeditorfactory.h"
-#include "addlibrarywizard.h"
 
 #include <coreplugin/icore.h>
 #include <coreplugin/actionmanager/actionmanager.h>
@@ -244,26 +243,6 @@ void ProFileEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
     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()
 {
     openLink(findLinkAt(textCursor()));
index b1c9f69..b746672 100644 (file)
@@ -94,7 +94,6 @@ protected:
 
 public slots:
     virtual void setFontSettings(const TextEditor::FontSettings &);
-    void addLibrary();
     void jumpToFile();
 
 private:
index 88b108e..fa7e0c1 100644 (file)
 #include "qt4project.h"
 #include "qt4target.h"
 #include "profilereader.h"
+#include "profileeditor.h"
 #include "qmakestep.h"
 #include "qt4buildconfiguration.h"
+#include "addlibrarywizard.h"
 #include "wizards/qtquickapp.h"
 #include "wizards/html5app.h"
 
@@ -285,6 +287,52 @@ ProjectExplorer::Project *Qt4Manager::contextProject() const
     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()
 {
     runQMake(projectExplorer()->startupProject(), 0);
index d77b44f..a3e9f97 100644 (file)
@@ -93,6 +93,8 @@ public:
     enum Action { BUILD, REBUILD, CLEAN };
 
 public slots:
+    void addLibrary();
+    void addLibraryContextMenu();
     void runQMake();
     void runQMakeContextMenu();
     void buildSubDirContextMenu();
@@ -108,6 +110,7 @@ private slots:
 private:
     QList<Qt4Project *> m_projects;
     void handleSubDirContexMenu(Action action);
+    void addLibrary(const QString &fileName, Internal::ProFileEditorWidget *editor = 0);
     void runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node);
 
     Internal::Qt4ProjectManagerPlugin *m_plugin;
index 90ab088..9f46028 100644 (file)
@@ -109,6 +109,7 @@ bool Qt4ProjectManagerPlugin::initialize(const QStringList &arguments, QString *
 {
     Q_UNUSED(arguments)
     const Core::Context projectContext(Qt4ProjectManager::Constants::PROJECT_ID);
+    Core::Context projecTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE);
 
     ProFileParser::initialize();
     ProFileEvaluator::initialize();
@@ -243,35 +244,43 @@ bool Qt4ProjectManagerPlugin::initialize(const QStringList &arguments, QString *
             this, SLOT(buildStateChanged(ProjectExplorer::Project *)));
     connect(m_projectExplorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project *)),
             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::Command *cmd;
-
     Core::Context proFileEditorContext = Core::Context(Qt4ProjectManager::Constants::C_PROFILEEDITOR);
 
     QAction *jumpToFile = new QAction(tr("Jump to File Under Cursor"), this);
-    cmd = am->registerAction(jumpToFile,
+    command = am->registerAction(jumpToFile,
         Constants::JUMP_TO_FILE, proFileEditorContext);
-    cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
+    command->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
     connect(jumpToFile, SIGNAL(triggered()),
             this, SLOT(jumpToFile()));
-    contextMenu->addAction(cmd);
+    contextMenu->addAction(command);
 
-    QAction *addLibrary = new QAction(tr("Add Library..."), this);
-    cmd = am->registerAction(addLibrary,
+    m_addLibraryAction = new QAction(tr("Add Library..."), this);
+    command = am->registerAction(m_addLibraryAction,
         Constants::ADDLIBRARY, proFileEditorContext);
-    connect(addLibrary, SIGNAL(triggered()),
-            this, SLOT(addLibrary()));
-    contextMenu->addAction(cmd);
+    connect(m_addLibraryAction, SIGNAL(triggered()),
+            m_qt4ProjectManager, SLOT(addLibrary()));
+    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);
     separator->setSeparator(true);
     contextMenu->addAction(am->registerAction(separator,
                   Core::Id(Constants::SEPARATOR), proFileEditorContext));
 
-    cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
-    contextMenu->addAction(cmd);
+    command = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
+    contextMenu->addAction(command);
 
     return true;
 }
@@ -317,6 +326,11 @@ void Qt4ProjectManagerPlugin::currentProjectChanged()
     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)
 {
     ProjectExplorer::Project *currentProject = m_projectExplorer->currentProject();
@@ -326,14 +340,6 @@ void Qt4ProjectManagerPlugin::buildStateChanged(ProjectExplorer::Project *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()
 {
     Core::EditorManager *em = Core::EditorManager::instance();
index 40d4252..100fb73 100644 (file)
@@ -69,8 +69,8 @@ private slots:
     void updateContextMenu(ProjectExplorer::Project *project,
                            ProjectExplorer::Node *node);
     void currentProjectChanged();
+    void currentNodeChanged(ProjectExplorer::Node *node);
     void buildStateChanged(ProjectExplorer::Project *pro);
-    void addLibrary();
     void jumpToFile();
 
 #ifdef WITH_TESTS
@@ -98,6 +98,8 @@ private:
     QAction *m_buildSubProjectContextMenu;
     QAction *m_rebuildSubProjectContextMenu;
     QAction *m_cleanSubProjectContextMenu;
+    QAction *m_addLibraryAction;
+    QAction *m_addLibraryActionContextMenu;
     GettingStartedWelcomePage *m_welcomePage;
     Core::Context m_projectContext;
 };