Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
berryContributionManager.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 
13 #include <berryLog.h>
14 
16 
18 #include "berryIContributionItem.h"
19 
21 
22 namespace berry {
23 
24 ContributionManager::~ContributionManager()
25 {
26 }
27 
28 void ContributionManager::Add(QAction* action, const QString& id)
29 {
30  Q_ASSERT_X(action, "nullcheck", "QAction must not be null");
32 }
33 
35 {
36  Q_ASSERT_X(item, "nullcheck", "Item must not be null");
37  if (AllowItem(item.GetPointer()))
38  {
39  contributions.append(item);
40  ItemAdded(item);
41  }
42 }
43 
44 void ContributionManager::AppendToGroup(const QString& groupName, QAction* action, const QString& id)
45 {
46  AddToGroup(groupName, IContributionItem::Pointer(new QActionContributionItem(action, id)), true);
47 }
48 
49 void ContributionManager::AppendToGroup(const QString& groupName, const SmartPointer<IContributionItem>& item)
50 {
51  AddToGroup(groupName, item, true);
52 }
53 
55 {
56  QListIterator<IContributionItem::Pointer> e(contributions);
57  while (e.hasNext())
58  {
59  IContributionItem::Pointer item = e.next();
60  QString itemId = item->GetId();
61  if (itemId.compare(id, Qt::CaseInsensitive) != 0)
62  {
63  return item;
64  }
65  }
66  return IContributionItem::Pointer(nullptr);
67 }
68 
69 QList<SmartPointer<IContributionItem> > ContributionManager::GetItems() const
70 {
71  return contributions;
72 }
73 
75 {
76  return contributions.size();
77 }
78 
80 {
81  if (overrides.IsNull())
82  {
83  struct _DefaultOverride : public IContributionManagerOverrides {
84  int GetEnabled(const IContributionItem* /*item*/) const override {
85  return -1;
86  }
87 
88  int GetVisible(const IContributionItem* /*item*/) const override {
89  return -1;
90  }
91  };
92  overrides = new _DefaultOverride;
93  }
94  return overrides;
95 }
96 
97 int ContributionManager::IndexOf(const QString& id)
98 {
99  int i = 0;
100  foreach(IContributionItem::Pointer item, contributions)
101  {
102  QString itemId = item->GetId();
103  if (item->GetId().compare(id, Qt::CaseInsensitive) == 0)
104  {
105  return i;
106  }
107  ++i;
108  }
109  return -1;
110 }
111 
113 {
114  if (index > contributions.size())
115  {
116  QString msg = QString("inserting ") + item->GetId() + " at " + QString::number(index);
117  throw std::invalid_argument(msg.toStdString());
118  }
119  if (AllowItem(item.GetPointer()))
120  {
121  contributions.insert(index, item);
122  ItemAdded(item);
123  }
124 }
125 
127 {
129  if (ci.IsNull())
130  {
131  throw ctkInvalidArgumentException(QString("can't find ID") + ID);
132  }
133  int ix = contributions.indexOf(ci);
134  if (ix >= 0)
135  {
136  // BERRY_INFO << "insert after: " << ix;
137  if (AllowItem(item.GetPointer()))
138  {
139  contributions.insert(ix + 1, item);
140  ItemAdded(item);
141  }
142  }
143 }
144 
146 {
148  if (ci.IsNull())
149  {
150  throw ctkInvalidArgumentException(QString("can't find ID ") + ID);
151  }
152  int ix = contributions.indexOf(ci);
153  if (ix >= 0)
154  {
155  // BERRY_INFO << "insert before: " << ix;
156  if (AllowItem(item.GetPointer()))
157  {
158  contributions.insert(ix, item);
159  ItemAdded(item);
160  }
161  }
162 }
163 
165 {
166  if (isDirty)
167  {
168  return true;
169  }
170  if (HasDynamicItems())
171  {
172  foreach (IContributionItem::Pointer item, contributions)
173  {
174  if (item->IsDirty())
175  {
176  return true;
177  }
178  }
179  }
180  return false;
181 }
182 
184 {
185  return contributions.empty();
186 }
187 
189 {
190  SetDirty(true);
191 }
192 
193 void ContributionManager::PrependToGroup(const QString& groupName, const SmartPointer<IContributionItem>& item)
194 {
195  AddToGroup(groupName, item, false);
196 }
197 
199 {
201  if (ci.IsNull())
202  {
203  return ci;
204  }
205  return Remove(ci);
206 }
207 
209 {
210  if (contributions.removeAll(item))
211  {
212  ItemRemoved(item);
213  return item;
214  }
215  return IContributionItem::Pointer(nullptr);
216 }
217 
219 {
220  QList<IContributionItem::Pointer> items = GetItems();
221  contributions.clear();
222  foreach (IContributionItem::Pointer item, items)
223  {
224  ItemRemoved(item);
225  }
226  dynamicItems = 0;
227  MarkDirty();
228 }
229 
230 bool ContributionManager::ReplaceItem(const QString& identifier,
231  const SmartPointer<IContributionItem>& replacementItem)
232 {
233  if (identifier.isNull())
234  {
235  return false;
236  }
237 
238  const int index = IndexOf(identifier);
239  if (index < 0)
240  {
241  return false; // couldn't find the item.
242  }
243 
244  // Remove the old item.
245  const IContributionItem::Pointer oldItem = contributions.at(index);
246  ItemRemoved(oldItem);
247 
248  // Add the new item.
249  contributions.replace(index, replacementItem);
250  ItemAdded(replacementItem); // throws NPE if (replacementItem == null)
251 
252  // Go through and remove duplicates.
253  QMutableListIterator<IContributionItem::Pointer> i(contributions);
254  i.toBack();
255  while (i.hasPrevious())
256  {
257  IContributionItem::Pointer item = i.previous();
258  if ((item.IsNotNull()) && (identifier == item->GetId()))
259  {
260 // if (Policy.TRACE_TOOLBAR) {
261 // System.out
262 // .println("Removing duplicate on replace: " + identifier);
263 // }
264  i.remove();
265  ItemRemoved(item);
266  }
267  }
268 
269  return true; // success
270 }
271 
273 {
274  overrides = newOverrides;
275 }
276 
278  : isDirty(true), dynamicItems(0)
279 {
280  // Do nothing.
281 }
282 
284 {
285  return true;
286 }
287 
289 {
290  int size = contributions.size();
291 
292  BERRY_INFO << this->ToString();
293  BERRY_INFO << " Number of elements: " << size;
294  int sum = 0;
295  for (int i = 0; i < size; i++)
296  {
297  if (contributions.at(i)->IsVisible())
298  {
299  ++sum;
300  }
301  }
302  BERRY_INFO << " Number of visible elements: " << sum;
303  BERRY_INFO << " Is dirty: " << IsDirty();
304 }
305 
307 {
308  return (dynamicItems > 0);
309 }
310 
312 {
313  return contributions.indexOf(item);
314 }
315 
317 {
318  item->SetParent(this);
319  MarkDirty();
320  if (item->IsDynamic())
321  {
322  dynamicItems++;
323  }
324 }
325 
327 {
328  item->SetParent(nullptr);
329  MarkDirty();
330  if (item->IsDynamic())
331  {
332  dynamicItems--;
333  }
334 }
335 
337 {
338  isDirty = dirty;
339 }
340 
342 {
343  contributions.clear();
344  for (int i = 0; i < items.size(); ++i)
345  {
346  if (AllowItem(items[i].GetPointer()))
347  {
348  contributions.append(items[i]);
349  }
350  }
351 }
352 
353 void ContributionManager::AddToGroup(const QString& groupName, const SmartPointer<IContributionItem>& item,
354  bool append)
355 {
356  QMutableListIterator<IContributionItem::Pointer> items(contributions);
357  for (int i = 0; items.hasNext(); ++i)
358  {
359  IContributionItem::Pointer o = items.next();
360  if (o->IsGroupMarker())
361  {
362  QString id = o->GetId();
363  if (id.compare(groupName, Qt::CaseInsensitive) == 0)
364  {
365  ++i;
366  if (append)
367  {
368  for (; items.hasNext(); ++i)
369  {
370  IContributionItem::Pointer ci = items.next();
371  if (ci->IsGroupMarker())
372  {
373  break;
374  }
375  }
376  }
377  if (AllowItem(item.GetPointer()))
378  {
379  contributions.insert(i, item);
380  ItemAdded(item);
381  }
382  return;
383  }
384  }
385  }
386  throw ctkInvalidArgumentException(QString("Group not found: ") + groupName);
387 }
388 
389 }
SmartPointer< IContributionManagerOverrides > GetOverrides() override
QList< SmartPointer< IContributionItem > > GetItems() const override
void ItemRemoved(const SmartPointer< IContributionItem > &item)
void InternalSetItems(const QList< SmartPointer< IContributionItem > > &items)
virtual QString ToString() const
void Add(QAction *action, const QString &id) override
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:82
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:20
bool ReplaceItem(const QString &identifier, const SmartPointer< IContributionItem > &replacementItem)
bool compare(std::pair< double, int > i, std::pair< double, int > j)
void InsertAfter(const QString &ID, const SmartPointer< IContributionItem > &item) override
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
ObjectType * GetPointer() const