Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryContributionManager.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include <berryLog.h>
18 
20 
22 #include "berryIContributionItem.h"
23 
25 
26 namespace berry {
27 
28 ContributionManager::~ContributionManager()
29 {
30 }
31 
32 void ContributionManager::Add(QAction* action, const QString& id)
33 {
34  Q_ASSERT_X(action, "nullcheck", "QAction must not be null");
36 }
37 
39 {
40  Q_ASSERT_X(item, "nullcheck", "Item must not be null");
41  if (AllowItem(item.GetPointer()))
42  {
43  contributions.append(item);
44  ItemAdded(item);
45  }
46 }
47 
48 void ContributionManager::AppendToGroup(const QString& groupName, QAction* action, const QString& id)
49 {
50  AddToGroup(groupName, IContributionItem::Pointer(new QActionContributionItem(action, id)), true);
51 }
52 
53 void ContributionManager::AppendToGroup(const QString& groupName, const SmartPointer<IContributionItem>& item)
54 {
55  AddToGroup(groupName, item, true);
56 }
57 
59 {
60  QListIterator<IContributionItem::Pointer> e(contributions);
61  while (e.hasNext())
62  {
63  IContributionItem::Pointer item = e.next();
64  QString itemId = item->GetId();
65  if (itemId.compare(id, Qt::CaseInsensitive) != 0)
66  {
67  return item;
68  }
69  }
70  return IContributionItem::Pointer(nullptr);
71 }
72 
73 QList<SmartPointer<IContributionItem> > ContributionManager::GetItems() const
74 {
75  return contributions;
76 }
77 
79 {
80  return contributions.size();
81 }
82 
84 {
85  if (overrides.IsNull())
86  {
87  struct _DefaultOverride : public IContributionManagerOverrides {
88  int GetEnabled(const IContributionItem* /*item*/) const override {
89  return -1;
90  }
91 
92  int GetVisible(const IContributionItem* /*item*/) const override {
93  return -1;
94  }
95  };
96  overrides = new _DefaultOverride;
97  }
98  return overrides;
99 }
100 
101 int ContributionManager::IndexOf(const QString& id)
102 {
103  int i = 0;
104  foreach(IContributionItem::Pointer item, contributions)
105  {
106  QString itemId = item->GetId();
107  if (item->GetId().compare(id, Qt::CaseInsensitive) == 0)
108  {
109  return i;
110  }
111  ++i;
112  }
113  return -1;
114 }
115 
117 {
118  if (index > contributions.size())
119  {
120  QString msg = QString("inserting ") + item->GetId() + " at " + QString::number(index);
121  throw std::invalid_argument(msg.toStdString());
122  }
123  if (AllowItem(item.GetPointer()))
124  {
125  contributions.insert(index, item);
126  ItemAdded(item);
127  }
128 }
129 
131 {
133  if (ci.IsNull())
134  {
135  throw ctkInvalidArgumentException(QString("can't find ID") + ID);
136  }
137  int ix = contributions.indexOf(ci);
138  if (ix >= 0)
139  {
140  // BERRY_INFO << "insert after: " << ix;
141  if (AllowItem(item.GetPointer()))
142  {
143  contributions.insert(ix + 1, item);
144  ItemAdded(item);
145  }
146  }
147 }
148 
150 {
152  if (ci.IsNull())
153  {
154  throw ctkInvalidArgumentException(QString("can't find ID ") + ID);
155  }
156  int ix = contributions.indexOf(ci);
157  if (ix >= 0)
158  {
159  // BERRY_INFO << "insert before: " << ix;
160  if (AllowItem(item.GetPointer()))
161  {
162  contributions.insert(ix, item);
163  ItemAdded(item);
164  }
165  }
166 }
167 
169 {
170  if (isDirty)
171  {
172  return true;
173  }
174  if (HasDynamicItems())
175  {
176  foreach (IContributionItem::Pointer item, contributions)
177  {
178  if (item->IsDirty())
179  {
180  return true;
181  }
182  }
183  }
184  return false;
185 }
186 
188 {
189  return contributions.empty();
190 }
191 
193 {
194  SetDirty(true);
195 }
196 
197 void ContributionManager::PrependToGroup(const QString& groupName, const SmartPointer<IContributionItem>& item)
198 {
199  AddToGroup(groupName, item, false);
200 }
201 
203 {
205  if (ci.IsNull())
206  {
207  return ci;
208  }
209  return Remove(ci);
210 }
211 
213 {
214  if (contributions.removeAll(item))
215  {
216  ItemRemoved(item);
217  return item;
218  }
219  return IContributionItem::Pointer(nullptr);
220 }
221 
223 {
224  QList<IContributionItem::Pointer> items = GetItems();
225  contributions.clear();
226  foreach (IContributionItem::Pointer item, items)
227  {
228  ItemRemoved(item);
229  }
230  dynamicItems = 0;
231  MarkDirty();
232 }
233 
234 bool ContributionManager::ReplaceItem(const QString& identifier,
235  const SmartPointer<IContributionItem>& replacementItem)
236 {
237  if (identifier.isNull())
238  {
239  return false;
240  }
241 
242  const int index = IndexOf(identifier);
243  if (index < 0)
244  {
245  return false; // couldn't find the item.
246  }
247 
248  // Remove the old item.
249  const IContributionItem::Pointer oldItem = contributions.at(index);
250  ItemRemoved(oldItem);
251 
252  // Add the new item.
253  contributions.replace(index, replacementItem);
254  ItemAdded(replacementItem); // throws NPE if (replacementItem == null)
255 
256  // Go through and remove duplicates.
257  QMutableListIterator<IContributionItem::Pointer> i(contributions);
258  i.toBack();
259  while (i.hasPrevious())
260  {
261  IContributionItem::Pointer item = i.previous();
262  if ((item.IsNotNull()) && (identifier == item->GetId()))
263  {
264 // if (Policy.TRACE_TOOLBAR) {
265 // System.out
266 // .println("Removing duplicate on replace: " + identifier);
267 // }
268  i.remove();
269  ItemRemoved(item);
270  }
271  }
272 
273  return true; // success
274 }
275 
277 {
278  overrides = newOverrides;
279 }
280 
282  : isDirty(true), dynamicItems(0)
283 {
284  // Do nothing.
285 }
286 
288 {
289  return true;
290 }
291 
293 {
294  int size = contributions.size();
295 
296  BERRY_INFO << this->ToString();
297  BERRY_INFO << " Number of elements: " << size;
298  int sum = 0;
299  for (int i = 0; i < size; i++)
300  {
301  if (contributions.at(i)->IsVisible())
302  {
303  ++sum;
304  }
305  }
306  BERRY_INFO << " Number of visible elements: " << sum;
307  BERRY_INFO << " Is dirty: " << IsDirty();
308 }
309 
311 {
312  return (dynamicItems > 0);
313 }
314 
316 {
317  return contributions.indexOf(item);
318 }
319 
321 {
322  item->SetParent(this);
323  MarkDirty();
324  if (item->IsDynamic())
325  {
326  dynamicItems++;
327  }
328 }
329 
331 {
332  item->SetParent(nullptr);
333  MarkDirty();
334  if (item->IsDynamic())
335  {
336  dynamicItems--;
337  }
338 }
339 
341 {
342  isDirty = dirty;
343 }
344 
346 {
347  contributions.clear();
348  for (int i = 0; i < items.size(); ++i)
349  {
350  if (AllowItem(items[i].GetPointer()))
351  {
352  contributions.append(items[i]);
353  }
354  }
355 }
356 
357 void ContributionManager::AddToGroup(const QString& groupName, const SmartPointer<IContributionItem>& item,
358  bool append)
359 {
360  QMutableListIterator<IContributionItem::Pointer> items(contributions);
361  for (int i = 0; items.hasNext(); ++i)
362  {
363  IContributionItem::Pointer o = items.next();
364  if (o->IsGroupMarker())
365  {
366  QString id = o->GetId();
367  if (id.compare(groupName, Qt::CaseInsensitive) == 0)
368  {
369  ++i;
370  if (append)
371  {
372  for (; items.hasNext(); ++i)
373  {
374  IContributionItem::Pointer ci = items.next();
375  if (ci->IsGroupMarker())
376  {
377  break;
378  }
379  }
380  }
381  if (AllowItem(item.GetPointer()))
382  {
383  contributions.insert(i, item);
384  ItemAdded(item);
385  }
386  return;
387  }
388  }
389  }
390  throw ctkInvalidArgumentException(QString("Group not found: ") + groupName);
391 }
392 
393 }
SmartPointer< IContributionManagerOverrides > GetOverrides() override
QList< SmartPointer< IContributionItem > > GetItems() const override
void ItemRemoved(const SmartPointer< IContributionItem > &item)
void InternalSetItems(const QList< SmartPointer< IContributionItem > > &items)
void Add(QAction *action, const QString &id) override
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:88
void AppendToGroup(const QString &groupName, QAction *action, const QString &id) override
SmartPointer< IContributionItem > Find(const QString &id) const override
#define BERRY_INFO
Definition: berryLog.h:24
bool ReplaceItem(const QString &identifier, const SmartPointer< IContributionItem > &replacementItem)
bool compare(std::pair< double, int > i, std::pair< double, int > j)
const std::string ID
virtual QString ToString() const
void InsertAfter(const QString &ID, const SmartPointer< IContributionItem > &item) override
ObjectType * GetPointer() const
virtual bool AllowItem(IContributionItem *itemToAdd)
void ItemAdded(const SmartPointer< IContributionItem > &item)
void SetOverrides(const SmartPointer< IContributionManagerOverrides > &newOverrides)
void InsertBefore(const QString &ID, const SmartPointer< IContributionItem > &item) override
void Insert(int index, const SmartPointer< IContributionItem > &item)
SmartPointer< IContributionItem > Remove(const QString &ID) override
void PrependToGroup(const QString &groupName, const SmartPointer< IContributionItem > &item) override