BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DesignerHelper.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/DesignerHelper.cpp
6 //! @brief Implements class DesignerHelper
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 <QPainter>
18 #include <cmath>
19 #include <iostream>
20 #include <random>
21 
22 namespace {
23 double m_current_zoom_level = 1.0;
24 }
25 
26 QGradient DesignerHelper::getLayerGradient(const QColor& color, const QRectF& rect)
27 {
28  QColor c = color;
29  c.setAlpha(160);
30  QLinearGradient result(rect.topLeft(), rect.bottomRight());
31 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
32  result.setColorAt(0, c.darker(150));
33  result.setColorAt(0.5, c.lighter(200));
34  result.setColorAt(1, c.darker(150));
35 #else
36  result.setColorAt(0, c.dark(150));
37  result.setColorAt(0.5, c.light(200));
38  result.setColorAt(1, c.dark(150));
39 #endif
40  return std::move(result);
41 }
42 
43 QGradient DesignerHelper::getDecorationGradient(const QColor& color, const QRectF& rect)
44 {
45  QColor c = color;
46  // c.setAlpha(200);
47  QLinearGradient result(rect.x() + rect.width() / 2, rect.y(), rect.x() + rect.width() / 2,
48  rect.y() + rect.height());
49  result.setColorAt(0, c);
50  result.setColorAt(0.5, c.lighter(150));
51  result.setColorAt(1, c);
52  return std::move(result);
53 }
54 
56 {
57  const int size = 10;
58  QPixmap result(size, size);
59  result.fill(QColor(250, 250, 250));
60  QPainter tilePainter(&result);
61  QColor color(220, 220, 220);
62  tilePainter.fillRect(0.0, 0.0, 2, 2, color);
63  tilePainter.end();
64 
65  return result;
66 }
67 
69 {
70  QRect rect(0, 0, layerWidth(), layerHeight());
71  QPixmap pixmap(rect.width() + 1, rect.height() + 1);
72  pixmap.fill(Qt::transparent);
73  QPainter painter(&pixmap);
74  painter.setPen(Qt::black);
75  painter.setBrush(getLayerGradient(Qt::green, rect));
76  painter.drawRect(rect);
77  return pixmap;
78 }
79 
81 {
82  auto rect = getDefaultMultiLayerRect();
83  QPixmap pixmap(rect.width() + 1, rect.height() + 1);
84  pixmap.fill(Qt::transparent);
85  QPainter painter(&pixmap);
86  painter.setPen(Qt::black);
87  painter.setBrush(getLayerGradient(QColor(75, 157, 249), rect));
88  painter.drawRect(rect);
89  painter.setPen(Qt::DashLine);
90  painter.drawLine(0, layerHeight() * 0.3, rect.width(), layerHeight() * 0.3);
91  painter.drawLine(0, layerHeight() * 0.6, rect.width(), layerHeight() * 0.6);
92  return pixmap;
93 }
94 
96 {
97  auto rect = getParticleLayoutBoundingRect();
98  QPixmap pixmap(rect.width() + 1, rect.height() + 1);
99  pixmap.fill(Qt::transparent);
100  QPainter painter(&pixmap);
101  painter.setPen(Qt::black);
102  painter.setBrush(getDecorationGradient(QColor(135, 206, 50), rect));
103  painter.drawRoundedRect(rect, 3, 3);
104  return pixmap;
105 }
106 
108 {
110  QPixmap pixmap(rect.width() + 1, rect.height() + 1);
111  pixmap.fill(Qt::transparent);
112  QPainter painter(&pixmap);
113  painter.setPen(Qt::black);
114  painter.setBrush(getDecorationGradient(Qt::lightGray, rect));
115  painter.drawRoundedRect(rect, 3, 3);
116  return pixmap;
117 }
118 
120 {
121  auto rect = getParticleBoundingRect();
122  QPixmap pixmap(rect.width() + 1, rect.height() + 1);
123  pixmap.fill(Qt::transparent);
124  QPainter painter(&pixmap);
125  painter.setPen(Qt::black);
126  painter.setBrush(getDecorationGradient(getDefaultParticleColor(), rect));
127  painter.drawRoundedRect(rect, 5, 5);
128  return pixmap;
129 }
130 
132 {
133  static std::random_device r;
134  std::default_random_engine re(r());
135  std::uniform_int_distribution<int> ru(0, 255);
136 
137  return QColor(ru(re), ru(re), ru(re));
138 }
139 
140 bool DesignerHelper::sort_layers(QGraphicsItem* left, QGraphicsItem* right)
141 {
142  return left->y() < right->y();
143 }
144 
145 // non-linear conversion of layer's thickness in nanometers to screen size to have reasonable
146 // graphics representation
148 {
149  const int ymin(layerHeight());
150  const int ymax(500);
151  int result(ymin);
152  if (nanometer > 0)
153  result = qBound(ymin, ymin + (int)std::pow(nanometer, 0.9), ymax);
154  return result;
155 }
156 
158 {
159  if (name == "MultiLayer") {
160  return getDefaultMultiLayerRect();
161  } else if (name == "Layer") {
162  return QRectF(0, 0, layerWidth(), layerHeight());
163  } else if (name == "ParticleLayout") {
165  } else if (name == "Rotation") {
167  } else if (name.startsWith("FormFactor") || name == "Particle" || name == "ParticleCoreShell"
168  || name == "ParticleDistribution") {
169  return getParticleBoundingRect();
170  } else if (name.startsWith("Interference")) {
172  } else {
173  return QRectF(0, 0, 50, 50);
174  }
175 }
176 
177 QColor DesignerHelper::getDefaultColor(const QString& name)
178 {
179  if (name == "MultiLayer") {
180  // return QColor(Qt::blue);
181  return QColor(51, 116, 255);
182  } else if (name == "Layer") {
183  // return QColor(Qt::green);
184  return QColor(26, 156, 9);
185  } else if (name == "ParticleLayout") {
186  return QColor(135, 206, 50);
187  } else if (name.startsWith("FormFactor") || name == "Particle" || name == "ParticleCoreShell") {
188  return QColor(210, 223, 237);
189  } else if (name.startsWith("InterferenceFunction")) {
190  return QColor(255, 236, 139);
191  } else if (name == "Transparant red") {
192  return QColor(0xFF, 0, 0, 0x80);
193  } else if (name == "Transparant blue") {
194  return QColor(0, 0, 0xFF, 0x80);
195  } else {
196  return QColor(Qt::lightGray);
197  }
198 }
199 
200 QPixmap DesignerHelper::getMimePixmap(const QString& name)
201 {
202  QRectF default_rect = getDefaultBoundingRect(name);
203  QRectF mime_rect(0, 0, default_rect.width() * m_current_zoom_level,
204  default_rect.height() * m_current_zoom_level);
205 
206  QPixmap pixmap(mime_rect.width() + 1, mime_rect.height() + 1);
207  pixmap.fill(Qt::transparent);
208  QPainter painter(&pixmap);
209  painter.setPen(Qt::black);
210  painter.setBrush(getDecorationGradient(getDefaultColor(name), mime_rect));
211  painter.drawRoundedRect(mime_rect, 1, 1);
212  return pixmap;
213 }
214 
216 {
217  return StyleUtils::SystemPointSize() * 1.5;
218 }
219 
221 {
222  return StyleUtils::SizeOfLetterM().width() * 18;
223 }
224 
226 {
227  return StyleUtils::SizeOfLetterM().height() * 2;
228 }
229 
231 {
232  return QColor(Qt::lightGray);
233 }
234 
236 {
237  return QRectF(0, 0, layerWidth() * 1.15, layerHeight());
238 }
239 
241 {
242  return QRectF(0, 0, layerHeight() * 3.5, layerHeight() * 4.5);
243 }
244 
246 {
247  return QRectF(0, 0, layerHeight() * 4.5, layerHeight() * 4);
248 }
249 
251 {
252  return QColor(210, 223, 237);
253 }
254 
256 {
257  return QRectF(0, 0, layerHeight() * 3.5, layerHeight() * 4);
258 }
259 
261 {
262  return QColor(145, 50, 220);
263 }
264 
266 {
267  return QRectF(0, 0, layerHeight() * 4, layerHeight() * 2);
268 }
269 
271 {
272  return getRandomColor();
273 }
274 
276 {
277  return StyleUtils::SystemPointSize() * 1.2;
278 }
279 
281 {
282  return StyleUtils::SystemPointSize() * 0.9;
283 }
284 
286 {
287  return StyleUtils::SystemPointSize() * 0.7;
288 }
289 
291 {
293 }
Defines class DesignerHelper.
DefinesStyleUtils namespace.
static QPixmap getSceneBackground()
static QRectF getParticleLayoutBoundingRect()
static QRectF getInterferenceFunctionBoundingRect()
static int getPythonEditorFontSize()
static QRectF getParticleBoundingRect()
static QPixmap getPixmapParticleLayout()
static QRectF getDefaultBoundingRect(const QString &name)
returns default bounding rectangle for given IvView name
static bool sort_layers(QGraphicsItem *left, QGraphicsItem *right)
sort graphics item according they y-coordinate
static QPixmap getPixmapInterferenceFunction()
static QColor getDefaultMaterialColor()
static QPixmap getMimePixmap(const QString &name)
returns Mime pixmap for givew IView name
static QGradient getDecorationGradient(const QColor &color, const QRectF &rect)
static QColor getDefaultParticleColor()
static int getHeaderFontSize()
returns system dependent font size
static QColor getRandomColor()
static QGradient getLayerGradient(const QColor &color, const QRectF &rect)
static int getLabelFontSize()
static int layerWidth()
static int layerHeight()
static QPixmap getPixmapParticle()
static QRectF getDefaultMultiLayerRect()
static QColor getDefaultLayerColor()
static int nanometerToScreen(double nanometer)
non-linear conversion of layer's thickness in nanometers to screen size to have reasonable graphics r...
static int getPortFontSize()
static QColor getDefaultColor(const QString &name)
returns default color for IView with given name
static QRectF getTransformationBoundingRect()
static int getSectionFontSize()
static QPixmap getPixmapLayer()
static QColor getDefaultTransformationColor()
static QPixmap getPixmapMultiLayer()
QString const & name(EShape k)
Definition: particles.cpp:21
QSize SizeOfLetterM(const QWidget *widget=nullptr)
Returns size of largest letter of default system font.
Definition: StyleUtils.cpp:110
int SystemPointSize()
Returns size in points of default system font.
Definition: StyleUtils.cpp:116
static constexpr double nanometer
Internal unit for lengths.
Definition: Units.h:33