Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
berryAbstractUICTKPlugin.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include "internal/berryBundleUtility.h"
16 #include "berryWorkbenchPlugin.h"
17 
18 #include "berryQtStyleManager.h"
19 #include "berryPlatformUI.h"
21 #include "berryIPreferences.h"
22 
23 #include <QApplication>
24 #include <QIcon>
25 #include <QImage>
26 #include <QString>
27 
28 namespace berry
29 {
30 
31 const QString AbstractUICTKPlugin::FN_DIALOG_SETTINGS = "dialog_settings.xml";
32 
34  : preferencesService(nullptr)
35 {
36 }
37 
38 // IDialogSettings getDialogSettings() {
39 // if (dialogSettings == null) {
40 // loadDialogSettings();
41 // }
42 // return dialogSettings;
43 // }
44 
45 
46 // ImageRegistry getImageRegistry() {
47 // if (imageRegistry == null) {
48 // imageRegistry = createImageRegistry();
49 // initializeImageRegistry(imageRegistry);
50 // }
51 // return imageRegistry;
52 // }
53 
54 
56 {
57  // Create the preference store lazily.
58  if (preferencesService == nullptr)
59  {
60  ctkServiceReference serviceRef = m_Context->getServiceReference<IPreferencesService>();
61  if (!serviceRef)
62  {
63  BERRY_ERROR << "Preferences service not available";
64  }
65  preferencesService = m_Context->getService<IPreferencesService>(serviceRef);
66  }
67  return preferencesService;
68 }
69 
71 {
72  IPreferencesService* prefService = this->GetPreferencesService();
73  if (prefService == nullptr) return IPreferences::Pointer(nullptr);
74 
75  return prefService->GetSystemPreferences();
76 }
77 
79 {
80  return PlatformUI::GetWorkbench();
81 }
82 
83 // ImageRegistry createImageRegistry()
84 // {
85 //
86 // //If we are in the UI Thread use that
87 // if (Display.getCurrent() != null)
88 // {
89 // return new ImageRegistry(Display.getCurrent());
90 // }
91 //
92 // if (PlatformUI.isWorkbenchRunning())
93 // {
94 // return new ImageRegistry(PlatformUI.getWorkbench().getDisplay());
95 // }
96 //
97 // //Invalid thread access if it is not the UI Thread
98 // //and the workbench is not created.
99 // throw new SWTError(SWT.ERROR_THREAD_INVALID_ACCESS);
100 // }
101 
102 
103 // void initializeImageRegistry(ImageRegistry reg) {
104 // // spec'ed to do nothing
105 // }
106 
107 
108 // void loadDialogSettings() {
109 // dialogSettings = new DialogSettings("Workbench"); //$NON-NLS-1$
110 //
111 // // bug 69387: The instance area should not be created (in the call to
112 // // #getStateLocation) if -data @none or -data @noDefault was used
113 // IPath dataLocation = getStateLocationOrNull();
114 // if (dataLocation != null) {
115 // // try r/w state area in the local file system
116 // String readWritePath = dataLocation.append(FN_DIALOG_SETTINGS)
117 // .toOSString();
118 // File settingsFile = new File(readWritePath);
119 // if (settingsFile.exists()) {
120 // try {
121 // dialogSettings.load(readWritePath);
122 // } catch (IOException e) {
123 // // load failed so ensure we have an empty settings
124 // dialogSettings = new DialogSettings("Workbench"); //$NON-NLS-1$
125 // }
126 //
127 // return;
128 // }
129 // }
130 //
131 // // otherwise look for bundle specific dialog settings
132 // URL dsURL = BundleUtility.find(getBundle(), FN_DIALOG_SETTINGS);
133 // if (dsURL == null) {
134 // return;
135 // }
136 //
137 // InputStream is = null;
138 // try {
139 // is = dsURL.openStream();
140 // BufferedReader reader = new BufferedReader(
141 // new InputStreamReader(is, "utf-8")); //$NON-NLS-1$
142 // dialogSettings.load(reader);
143 // } catch (IOException e) {
144 // // load failed so ensure we have an empty settings
145 // dialogSettings = new DialogSettings("Workbench"); //$NON-NLS-1$
146 // } finally {
147 // try {
148 // if (is != null) {
149 // is.close();
150 // }
151 // } catch (IOException e) {
152 // // do nothing
153 // }
154 // }
155 // }
156 
157 
158 // void refreshPluginActions() {
159 // // If the workbench is not started yet, or is no longer running, do nothing.
160 // if (!PlatformUI.isWorkbenchRunning()) {
161 // return;
162 // }
163 //
164 // // startup() is not guaranteed to be called in the UI thread,
165 // // but refreshPluginActions must run in the UI thread,
166 // // so use asyncExec. See bug 6623 for more details.
167 // Display.getDefault().asyncExec(new Runnable() {
168 // public void run() {
169 // WWinPluginAction.refreshActionList();
170 // }
171 // });
172 // }
173 
174 
175 // void saveDialogSettings() {
176 // if (dialogSettings == null) {
177 // return;
178 // }
179 //
180 // try {
181 // IPath path = getStateLocationOrNull();
182 // if(path == null) {
183 // return;
184 // }
185 // String readWritePath = path
186 // .append(FN_DIALOG_SETTINGS).toOSString();
187 // dialogSettings.save(readWritePath);
188 // } catch (IOException e) {
189 // // spec'ed to ignore problems
190 // } catch (IllegalStateException e) {
191 // // spec'ed to ignore problems
192 // }
193 // }
194 
195 
196 void AbstractUICTKPlugin::start(ctkPluginContext* context)
197 {
198  Plugin::start(context);
199 
200  // Should only attempt refreshPluginActions() once the bundle
201  // has been fully started. Otherwise, action delegates
202  // can be created while in the process of creating
203  // a triggering action delegate (if UI events are processed during startup).
204  // Also, if the start throws an exception, the bundle will be shut down.
205  // We don't want to have created any delegates if this happens.
206  // See bug 63324 for more details.
207 
208  // bundleListener = new BundleListener()
209  // {
210  // public void bundleChanged(BundleEvent event)
211  // {
212  // if (event.getBundle() == getBundle())
213  // {
214  // if (event.getType() == BundleEvent.STARTED)
215  // {
216  // // We're getting notified that the bundle has been started.
217  // // Make sure it's still active. It may have been shut down between
218  // // the time this event was queued and now.
219  // if (getBundle().getState() == Bundle.ACTIVE)
220  // {
221  // refreshPluginActions();
222  // }
223  // fc.removeBundleListener(this);
224  // }
225  // }
226  // }
227  // };
228  // context.addBundleListener(bundleListener);
229 
230  // bundleListener is removed in stop(BundleContext)
231 }
232 
233 void AbstractUICTKPlugin::stop(ctkPluginContext* context)
234 {
235  Q_UNUSED(context)
236 
237  // try
238  // {
239  // if (bundleListener != null)
240  // {
241  // context.removeBundleListener(bundleListener);
242  // }
243  // saveDialogSettings();
244  // savePreferenceStore();
245  // preferenceStore = null;
246  // if (imageRegistry != null)
247  // imageRegistry.dispose();
248  // imageRegistry = null;
249  //}
250 
251  Plugin::stop(context);
252 }
253 
255  const QString& pluginId, const QString& imageFilePath)
256 {
257  if (pluginId.isEmpty() || imageFilePath.isEmpty())
258  {
259  throw ctkInvalidArgumentException("argument cannot be empty");
260  }
261 
262  // if the plug-in is not ready then there is no image
263  QSharedPointer<ctkPlugin> plugin = BundleUtility::FindPlugin(pluginId);
264  if (!BundleUtility::IsReady(plugin.data()))
265  {
266  return QIcon();
267  }
268 
269  QByteArray imgContent = plugin->getResource(imageFilePath);
270 
271  if (imageFilePath.endsWith(".svg", Qt::CaseInsensitive))
272  return QtStyleManager::ThemeIcon(imgContent);
273 
274  QImage image = QImage::fromData(imgContent);
275  QPixmap pixmap = QPixmap::fromImage(image);
276  return QIcon(pixmap);
277 }
278 
280 {
281  return QIcon(":/org.blueberry.ui.qt/icon_missing.png");
282 }
283 
284 }
void stop(ctkPluginContext *context) override
Definition: berryPlugin.cpp:28
static QIcon ThemeIcon(const QByteArray &originalSVG)
void start(ctkPluginContext *context) override
Definition: berryPlugin.cpp:23
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
Implements transparent reference counting.
#define BERRY_ERROR
Definition: berryLog.h:22
SmartPointer< IPreferences > GetPreferences() const
IPreferencesService * GetPreferencesService() const
void start(ctkPluginContext *context) override
static IWorkbench * GetWorkbench()
void stop(ctkPluginContext *context) override
mitk::Image::Pointer image
ctkPluginContext * m_Context
Definition: berryPlugin.h:67
static QIcon ImageDescriptorFromPlugin(const QString &pluginId, const QString &imageFilePath)
berry::SmartPointer< Self > Pointer