BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemIDFactory.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/utils/ItemIDFactory.cpp
6 //! @brief Implements class ItemIDFactory
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 
18 {
19  static ItemIDFactory instance;
20  return instance;
21 }
22 
23 QString ItemIDFactory::createID(SessionItem* toBeInsertedItem)
24 {
25 
26  QUuid id = QUuid::createUuid();
27  QString id_String = id.toString();
28 
29  // prevent duplicates (very improbable that this ever happens though)
30  while (instance().IDtoItemMap.contains(id_String)) {
31  id = QUuid::createUuid();
32  id_String = id.toString();
33  }
34  instance().ItemtoIDMap.insert(toBeInsertedItem, id_String);
35  instance().IDtoItemMap.insert(id_String, toBeInsertedItem);
36 
37  return id_String;
38 }
39 
40 QString ItemIDFactory::getID(SessionItem* existingItem)
41 {
42  if (instance().ItemtoIDMap.contains(existingItem))
43  return instance().ItemtoIDMap.value(existingItem);
44  else
45  return "";
46 }
47 
48 SessionItem* ItemIDFactory::getItem(QString existingID)
49 {
50  if (instance().IDtoItemMap.contains(existingID))
51  return instance().IDtoItemMap.value(existingID);
52  else
53  return nullptr;
54 }
55 
57 {
58  static QUuid id = QUuid::createUuid();
59  return id.toString().size();
60 }
Defines class ItemIDFactory.
static int IDSize()
QMap< QString, SessionItem * > IDtoItemMap
Definition: ItemIDFactory.h:44
static QString getID(SessionItem *existingItem)
QMap< SessionItem *, QString > ItemtoIDMap
Definition: ItemIDFactory.h:45
static SessionItem * getItem(QString existingID)
static ItemIDFactory & instance()
static QString createID(SessionItem *toBeInsertedItem)
QString createUuid()
Definition: GUIHelpers.cpp:242