File: DesignerHelper.cpp

package info (click to toggle)
bornagain 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,800 kB
  • sloc: cpp: 469,684; python: 38,920; xml: 805; awk: 630; sh: 286; ansic: 37; makefile: 25
file content (290 lines) | stat: -rw-r--r-- 8,569 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// ************************************************************************** //
//
//  BornAgain: simulate and fit scattering at grazing incidence
//
//! @file      GUI/coregui/Views/SampleDesigner/DesignerHelper.cpp
//! @brief     Implements class DesignerHelper
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************** //

#include "GUI/coregui/Views/SampleDesigner/DesignerHelper.h"
#include "GUI/coregui/utils/StyleUtils.h"
#include <QPainter>
#include <QtGlobal>
#include <cmath>
#include <iostream>

namespace
{
double m_current_zoom_level = 1.0;
}

QGradient DesignerHelper::getLayerGradient(const QColor& color, const QRectF& rect)
{
    QColor c = color;
    c.setAlpha(160);
    QLinearGradient result(rect.topLeft(), rect.bottomRight());
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
    result.setColorAt(0, c.darker(150));
    result.setColorAt(0.5, c.lighter(200));
    result.setColorAt(1, c.darker(150));
#else
    result.setColorAt(0, c.dark(150));
    result.setColorAt(0.5, c.light(200));
    result.setColorAt(1, c.dark(150));
#endif
    return std::move(result);
}

QGradient DesignerHelper::getDecorationGradient(const QColor& color, const QRectF& rect)
{
    QColor c = color;
    // c.setAlpha(200);
    QLinearGradient result(rect.x() + rect.width() / 2, rect.y(), rect.x() + rect.width() / 2,
                           rect.y() + rect.height());
    result.setColorAt(0, c);
    result.setColorAt(0.5, c.lighter(150));
    result.setColorAt(1, c);
    return std::move(result);
}

QPixmap DesignerHelper::getSceneBackground()
{
    const int size = 10;
    QPixmap result(size, size);
    result.fill(QColor(250, 250, 250));
    QPainter tilePainter(&result);
    QColor color(220, 220, 220);
    tilePainter.fillRect(0.0, 0.0, 2, 2, color);
    tilePainter.end();

    return result;
}

QPixmap DesignerHelper::getPixmapLayer()
{
    QRect rect(0, 0, layerWidth(), layerHeight());
    QPixmap pixmap(rect.width() + 1, rect.height() + 1);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(Qt::black);
    painter.setBrush(getLayerGradient(Qt::green, rect));
    painter.drawRect(rect);
    return pixmap;
}

QPixmap DesignerHelper::getPixmapMultiLayer()
{
    auto rect = getDefaultMultiLayerRect();
    QPixmap pixmap(rect.width() + 1, rect.height() + 1);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(Qt::black);
    painter.setBrush(getLayerGradient(QColor(75, 157, 249), rect));
    painter.drawRect(rect);
    painter.setPen(Qt::DashLine);
    painter.drawLine(0, layerHeight() * 0.3, rect.width(), layerHeight() * 0.3);
    painter.drawLine(0, layerHeight() * 0.6, rect.width(), layerHeight() * 0.6);
    return pixmap;
}

QPixmap DesignerHelper::getPixmapParticleLayout()
{
    auto rect = getParticleLayoutBoundingRect();
    QPixmap pixmap(rect.width() + 1, rect.height() + 1);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(Qt::black);
    painter.setBrush(getDecorationGradient(QColor(135, 206, 50), rect));
    painter.drawRoundedRect(rect, 3, 3);
    return pixmap;
}

QPixmap DesignerHelper::getPixmapInterferenceFunction()
{
    auto rect = getInterferenceFunctionBoundingRect();
    QPixmap pixmap(rect.width() + 1, rect.height() + 1);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(Qt::black);
    painter.setBrush(getDecorationGradient(Qt::lightGray, rect));
    painter.drawRoundedRect(rect, 3, 3);
    return pixmap;
}

QPixmap DesignerHelper::getPixmapParticle()
{
    auto rect = getParticleBoundingRect();
    QPixmap pixmap(rect.width() + 1, rect.height() + 1);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(Qt::black);
    painter.setBrush(getDecorationGradient(getDefaultParticleColor(), rect));
    painter.drawRoundedRect(rect, 5, 5);
    return pixmap;
}

QColor DesignerHelper::getRandomColor()
{
    return QColor(qrand() % 256, qrand() % 256, qrand() % 256);
}

bool DesignerHelper::sort_layers(QGraphicsItem* left, QGraphicsItem* right)
{
    return left->y() < right->y();
}

// non-linear conversion of layer's thickness in nanometers to screen size to have reasonable
// graphics representation
int DesignerHelper::nanometerToScreen(double nanometer)
{
    const int ymin(layerHeight());
    const int ymax(500);
    int result(ymin);
    if (nanometer > 0)
        result = qBound(ymin, ymin + (int)std::pow(nanometer, 0.9), ymax);
    return result;
}

QRectF DesignerHelper::getDefaultBoundingRect(const QString& name)
{
    if (name == "MultiLayer") {
        return getDefaultMultiLayerRect();
    } else if (name == "Layer") {
        return QRectF(0, 0, layerWidth(), layerHeight());
    } else if (name == "ParticleLayout") {
        return getParticleLayoutBoundingRect();
    } else if (name == "Rotation") {
        return getTransformationBoundingRect();
    } else if (name.startsWith("FormFactor") || name == "Particle" || name == "ParticleCoreShell"
               || name == "ParticleDistribution") {
        return getParticleBoundingRect();
    } else if (name.startsWith("Interference")) {
        return getInterferenceFunctionBoundingRect();
    } else {
        return QRectF(0, 0, 50, 50);
    }
}

QColor DesignerHelper::getDefaultColor(const QString& name)
{
    if (name == "MultiLayer") {
        // return QColor(Qt::blue);
        return QColor(51, 116, 255);
    } else if (name == "Layer") {
        // return QColor(Qt::green);
        return QColor(26, 156, 9);
    } else if (name == "ParticleLayout") {
        return QColor(135, 206, 50);
    } else if (name.startsWith("FormFactor") || name == "Particle" || name == "ParticleCoreShell") {
        return QColor(210, 223, 237);
    } else if (name.startsWith("InterferenceFunction")) {
        return QColor(255, 236, 139);
    } else if (name == "Transparant red") {
        return QColor(0xFF, 0, 0, 0x80);
    } else if (name == "Transparant blue") {
        return QColor(0, 0, 0xFF, 0x80);
    } else {
        return QColor(Qt::lightGray);
    }
}

QPixmap DesignerHelper::getMimePixmap(const QString& name)
{
    QRectF default_rect = getDefaultBoundingRect(name);
    QRectF mime_rect(0, 0, default_rect.width() * m_current_zoom_level,
                     default_rect.height() * m_current_zoom_level);

    QPixmap pixmap(mime_rect.width() + 1, mime_rect.height() + 1);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(Qt::black);
    painter.setBrush(getDecorationGradient(getDefaultColor(name), mime_rect));
    painter.drawRoundedRect(mime_rect, 1, 1);
    return pixmap;
}

int DesignerHelper::getHeaderFontSize()
{
    return StyleUtils::SystemPointSize() * 1.5;
}

int DesignerHelper::layerWidth()
{
    return StyleUtils::SizeOfLetterM().width() * 18;
}

int DesignerHelper::layerHeight()
{
    return StyleUtils::SizeOfLetterM().height() * 2;
}

QColor DesignerHelper::getDefaultLayerColor()
{
    return QColor(Qt::lightGray);
}

QRectF DesignerHelper::getDefaultMultiLayerRect()
{
    return QRectF(0, 0, layerWidth() * 1.15, layerHeight());
}

QRectF DesignerHelper::getParticleLayoutBoundingRect()
{
    return QRectF(0, 0, layerHeight() * 3.5, layerHeight() * 4.5);
}

QRectF DesignerHelper::getInterferenceFunctionBoundingRect()
{
    return QRectF(0, 0, layerHeight() * 4.5, layerHeight() * 4);
}

QColor DesignerHelper::getDefaultParticleColor()
{
    return QColor(210, 223, 237);
}

QRectF DesignerHelper::getParticleBoundingRect()
{
    return QRectF(0, 0, layerHeight() * 3.5, layerHeight() * 4);
}

QColor DesignerHelper::getDefaultTransformationColor()
{
    return QColor(145, 50, 220);
}

QRectF DesignerHelper::getTransformationBoundingRect()
{
    return QRectF(0, 0, layerHeight() * 4, layerHeight() * 2);
}

QColor DesignerHelper::getDefaultMaterialColor()
{
    return QColor(qrand() % 256, qrand() % 256, qrand() % 256);
}

int DesignerHelper::getSectionFontSize()
{
    return StyleUtils::SystemPointSize() * 1.2;
}

int DesignerHelper::getLabelFontSize()
{
    return StyleUtils::SystemPointSize() * 0.9;
}

int DesignerHelper::getPortFontSize()
{
    return StyleUtils::SystemPointSize() * 0.7;
}

int DesignerHelper::getPythonEditorFontSize()
{
    return StyleUtils::SystemPointSize();
}