OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / helloworld / helloworldplugin.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "helloworldplugin.h"
35
36 #include <coreplugin/actionmanager/actionmanager.h>
37 #include <coreplugin/actionmanager/actioncontainer.h>
38 #include <coreplugin/coreconstants.h>
39 #include <coreplugin/icore.h>
40 #include <coreplugin/imode.h>
41 #include <coreplugin/modemanager.h>
42 #include <coreplugin/uniqueidmanager.h>
43
44 #include <QtCore/QDebug>
45 #include <QtCore/QtPlugin>
46 #include <QtGui/QAction>
47 #include <QtGui/QMenu>
48 #include <QtGui/QMessageBox>
49 #include <QtGui/QPushButton>
50
51 namespace HelloWorld {
52 namespace Internal {
53
54 /*!  A mode with a push button based on BaseMode.  */
55
56 class HelloMode : public Core::IMode
57 {
58 public:
59     HelloMode() : m_widget(new QPushButton(tr("Hello World PushButton!"))) {}
60
61     QString displayName() const { return tr("Hello world!"); }
62     QIcon icon() const { return QIcon(); }
63     int priority() const { return 0; }
64     QWidget *widget() { return m_widget; }
65     QString id() const { return QLatin1String("HelloWorld.HelloWorldMode"); }
66     QString type() const { return QLatin1String("HelloWorld.HelloWorldMode"); }
67     Core::Context context() const { return Core::Context("HelloWorld.MainView"); };
68     QString contextHelpId() const { return QString(); }
69
70 private:
71     QWidget *m_widget;
72 };
73
74
75 /*! Constructs the Hello World plugin. Normally plugins don't do anything in
76     their constructor except for initializing their member variables. The
77     actual work is done later, in the initialize() and extensionsInitialized()
78     methods.
79 */
80 HelloWorldPlugin::HelloWorldPlugin()
81 {
82 }
83
84 /*! Plugins are responsible for deleting objects they created on the heap, and
85     to unregister objects from the plugin manager that they registered there.
86 */
87 HelloWorldPlugin::~HelloWorldPlugin()
88 {
89 }
90
91 /*! Initializes the plugin. Returns true on success.
92     Plugins want to register objects with the plugin manager here.
93
94     \a error_message can be used to pass an error message to the plugin system,
95        if there was any.
96 */
97 bool HelloWorldPlugin::initialize(const QStringList &arguments, QString *error_message)
98 {
99     Q_UNUSED(arguments)
100     Q_UNUSED(error_message)
101
102     // Get the primary access point to the workbench.
103     Core::ICore *core = Core::ICore::instance();
104
105     // Create a unique context for our own view, that will be used for the
106     // menu entry later.
107     Core::Context context("HelloWorld.MainView");
108
109     // Create an action to be triggered by a menu entry
110     QAction *helloWorldAction = new QAction(tr("Say \"&Hello World!\""), this);
111     connect(helloWorldAction, SIGNAL(triggered()), SLOT(sayHelloWorld()));
112
113     // Register the action with the action manager
114     Core::ActionManager *actionManager = core->actionManager();
115     Core::Command *command =
116             actionManager->registerAction(
117                     helloWorldAction, "HelloWorld.HelloWorldAction", context);
118
119     // Create our own menu to place in the Tools menu
120     Core::ActionContainer *helloWorldMenu =
121             actionManager->createMenu("HelloWorld.HelloWorldMenu");
122     QMenu *menu = helloWorldMenu->menu();
123     menu->setTitle(tr("&Hello World"));
124     menu->setEnabled(true);
125
126     // Add the Hello World action command to the menu
127     helloWorldMenu->addAction(command);
128
129     // Request the Tools menu and add the Hello World menu to it
130     Core::ActionContainer *toolsMenu =
131             actionManager->actionContainer(Core::Constants::M_TOOLS);
132     toolsMenu->addMenu(helloWorldMenu);
133
134     // Add a mode with a push button based on BaseMode. Like the BaseView,
135     // it will unregister itself from the plugin manager when it is deleted.
136     Core::IMode *helloMode = new HelloMode;
137     addAutoReleasedObject(helloMode);
138
139     // Add the Hello World action command to the mode manager (with 0 priority)
140     Core::ModeManager *modeManager = core->modeManager();
141     modeManager->addAction(command, 0);
142
143     return true;
144 }
145
146 /*! Notification that all extensions that this plugin depends on have been
147     initialized. The dependencies are defined in the plugins .pluginspec file.
148
149     Normally this method is used for things that rely on other plugins to have
150     added objects to the plugin manager, that implement interfaces that we're
151     interested in. These objects can now be requested through the
152     PluginManagerInterface.
153
154     The HelloWorldPlugin doesn't need things from other plugins, so it does
155     nothing here.
156 */
157 void HelloWorldPlugin::extensionsInitialized()
158 {
159 }
160
161 void HelloWorldPlugin::sayHelloWorld()
162 {
163     // When passing 0 for the parent, the message box becomes an
164     // application-global modal dialog box
165     QMessageBox::information(
166             0, tr("Hello World!"), tr("Hello World! Beautiful day today, isn't it?"));
167 }
168
169 } // namespace Internal
170 } // namespace HelloWorld
171
172 Q_EXPORT_PLUGIN(HelloWorld::Internal::HelloWorldPlugin)