BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DesignerMimeData.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/SampleDesigner/DesignerMimeData.cpp
6 //! @brief Implements class DesignerMimeData
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 
17 #include <QBitmap>
18 #include <QByteArray>
19 #include <QDataStream>
20 #include <QDrag>
21 #include <QPainter>
22 #include <QPixmap>
23 #include <QWidget>
24 #include <QXmlStreamReader>
25 #include <iostream>
26 
27 #if QT_VERSION < 0x050000
28 #define QStringLiteral QString
29 #endif
30 
31 DesignerMimeData::DesignerMimeData(const QString& entryname, const QString& xmldescr, QDrag* drag)
32  : m_entryname(entryname), m_xmldescr(xmldescr)
33 {
34  drag->setMimeData(this);
35 
37 
38  QByteArray itemData;
39  QDataStream dataStream(&itemData, QIODevice::WriteOnly);
40  dataStream << xmldescr;
41  setData("bornagain/widget", itemData);
42 
43  drag->setPixmap(getPixmap(m_classname));
44  drag->setHotSpot(QPoint(drag->pixmap().width() / 2, drag->pixmap().height() / 2));
45 }
46 
47 void DesignerMimeData::read_xmldescr(const QString& xmldescr)
48 {
49  QXmlStreamReader reader(xmldescr);
50 
51  bool widget_found = false;
52  while (!reader.atEnd()) {
53  if (reader.readNext() == QXmlStreamReader::StartElement) {
54  const QStringRef name = reader.name();
55  if (widget_found)
56  reader.raiseError("Unexpected element <" + name.toString() + ">");
57  if (name.compare(QStringLiteral("widget"), Qt::CaseInsensitive) == 0)
58  read_widget(reader);
59  else
60  reader.raiseError("Unexpected element <" + name.toString() + ">");
61  }
62  }
63  if (reader.hasError()) {
64  QString errorMessage = tr("A parse error occurred at line %1, column %2 of the XML code "
65  "specified for the widget %3: %4\n%5")
66  .arg(reader.lineNumber())
67  .arg(reader.columnNumber())
68  .arg(m_entryname)
69  .arg(reader.errorString())
70  .arg(xmldescr);
71  std::cout << "DesignerMimeData::read_xmldescr() -> Error during parse." << std::endl;
72  std::cout << errorMessage.toStdString() << std::endl;
73  }
74 }
75 
76 // extract class name and skip the rest
77 void DesignerMimeData::read_widget(QXmlStreamReader& reader)
78 {
79  for (const QXmlStreamAttribute& attribute : reader.attributes()) {
80  QStringRef name = attribute.name();
81  if (name == "class") {
82  m_classname = attribute.value().toString();
83  continue;
84  }
85  reader.raiseError("Unexpected attribute " + name.toString());
86  }
87  reader.skipCurrentElement();
88 }
89 
90 // Execute a drag and drop operation.
91 Qt::DropAction DesignerMimeData::execDrag(const QString& name, const QString& xmldescr,
92  QWidget* dragSource)
93 {
94  if (xmldescr.size() == 0)
95  return Qt::IgnoreAction;
96 
97  QDrag* drag = new QDrag(dragSource);
98  DesignerMimeData* mimeData = new DesignerMimeData(name, xmldescr, drag);
99  Q_UNUSED(mimeData);
100 
101  const Qt::DropAction executedAction = drag->exec(Qt::CopyAction);
102 
103  return executedAction;
104 }
105 
106 QPixmap DesignerMimeData::getPixmap(const QString& name)
107 {
109 }
Defines class DesignerHelper.
#define QStringLiteral
Defines class DesignerMimeData.
static QPixmap getMimePixmap(const QString &name)
returns Mime pixmap for givew IView name
Mime data for use with SampleDesigner drag and drop operations.
void read_xmldescr(const QString &xmldescr)
QPixmap getPixmap(const QString &name)
void read_widget(QXmlStreamReader &reader)
DesignerMimeData(const QString &name, const QString &xmldescr, QDrag *drag)
static Qt::DropAction execDrag(const QString &name, const QString &xmldescr, QWidget *dragSource)
Execute a drag and drop operation.
QString const & name(EShape k)
Definition: particles.cpp:21