/
elementdataviewer.cpp
557 lines (477 loc) · 17.4 KB
/
elementdataviewer.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/***************************************************************************
copyright : (C) 2004, 2005, 2006 by Carsten Niehaus
email : cniehaus@kde.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "elementdataviewer.h"
#include <element.h>
#include "prefs.h"
//Qt-Includes
#include <QDebug>
#include <QDialogButtonBox>
#include <QKeyEvent>
#include <QPen>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
#include <KActionCollection>
#include <KConfig>
#include <KConfigGroup>
#include <KHelpClient>
#include <KPlotAxis>
#include <KPlotObject>
#include <KStandardAction>
#include <KToolInvocation>
#include <KUnitConversion/Converter>
AxisData::AxisData(AXISTYPE type) : currentDataType(-1)
{
m_type = type;
}
double AxisData::value(int element) const
{
if ((element < 1) || (element > dataList.count())) {
return 0.0;
}
return dataList[element - 1];
}
ElementDataViewer::ElementDataViewer(QWidget *parent)
: QDialog(parent),
m_yData(new AxisData(AxisData::Y)),
m_xData(new AxisData(AxisData::X))
{
setWindowTitle(i18n("Plot Data"));
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Help|QDialogButtonBox::Close);
QWidget *mainWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
mainLayout->addWidget(mainWidget);
connect(buttonBox, &QDialogButtonBox::accepted, this, &ElementDataViewer::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &ElementDataViewer::reject);
mainLayout->addWidget(buttonBox);
buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
KalziumDataObject *kdo = KalziumDataObject::instance();
ui.setupUi(mainWidget);
m_timer = new QTimer(this);
m_timer->setSingleShot(true);
// setup the list of names
foreach (Element *e, kdo->ElementList) {
names << e->dataAsString(ChemicalDataObject::name);
symbols << e->dataAsString(ChemicalDataObject::symbol);
elecConfig<< e->dataAsString(ChemicalDataObject::electronicConfiguration);
block << e->dataAsString(ChemicalDataObject::periodTableBlock);
}
m_actionCollection = new KActionCollection (this);
KStandardAction::quit(this, SLOT(close()), m_actionCollection);
connect(m_timer, &QTimer::timeout,
this, &ElementDataViewer::drawPlot);
connect(ui.KCB_y, QOverload<int>::of(&KComboBox::activated),
this, &ElementDataViewer::rangeChanged);
connect(ui.KCB_x, QOverload<int>::of(&KComboBox::activated),
this, &ElementDataViewer::rangeChanged);
connect(ui.comboElementLabels, QOverload<int>::of(&KComboBox::activated),
this, &ElementDataViewer::rangeChanged);
connect(ui.comboElementType, QOverload<int>::of(&KComboBox::activated),
this, &ElementDataViewer::rangeChanged);
connect(ui.from, QOverload<int>::of(&QSpinBox::valueChanged),
this, &ElementDataViewer::rangeChanged);
connect(ui.to, QOverload<int>::of(&QSpinBox::valueChanged),
this, &ElementDataViewer::rangeChanged);
connect(buttonBox->button(QDialogButtonBox::Help), &QPushButton::clicked,
this, &ElementDataViewer::slotHelp);
connect(ui.full, &QPushButton::clicked,
this, &ElementDataViewer::fullRange);
connect(ui.swapXYAxis, &QPushButton::clicked,
this, &ElementDataViewer::swapXYAxis);
drawPlot();
resize(650, 500);
}
ElementDataViewer::~ElementDataViewer()
{
delete m_yData;
delete m_xData;
}
void ElementDataViewer::slotHelp()
{
KHelpClient::invokeHelp(QStringLiteral("tools.html#plot_data"), QStringLiteral("kalzium"));
}
void ElementDataViewer::rangeChanged()
{
if (ui.from->value() > ui.to->value()) {
ui.to->setValue(ui.from->value());
}
m_timer->stop();
m_timer->start(500);
}
void ElementDataViewer::fullRange()
{
ui.from ->setValue(1);
ui.to ->setValue(116);
}
void ElementDataViewer::swapXYAxis()
{
int x = ui.KCB_x->currentIndex();
int y = ui.KCB_y->currentIndex();
ui.KCB_x->setCurrentIndex(y);
ui.KCB_y->setCurrentIndex(x);
rangeChanged();
}
void ElementDataViewer::setLimits()
{
qDebug() << "ElementDataViewer::setLimits()";
double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0;
getMinMax(x1, x2, m_xData);
getMinMax(y1, y2, m_yData);
qDebug() << x1 << " :: " << x2 << " ----- " << y1 << " :: " << y2;
//JH: add some padding to show all points
double dx = 0.05*(x2-x1);
double dy = 0.05*(y2-y1);
x1 -= dx;
x2 += dx;
y1 -= dy;
y2 += dy;
//X // try to put a small padding to make the points on the axis visible
//X double dx = (to - from + 1) / 25 + 1.0;
//X double dy = (maxY - minY) / 10.0;
//X // in case that dy is quite small (for example, when plotting a single
//X // point)
//X if (dy < 1e-7)
//X dy = maxY / 10.0;
//X ui.plotwidget->setLimits(from - dx, to + dx, minY - dy, maxY + dy);
ui.plotwidget->setLimits(x1, x2, y1, y2);
}
void ElementDataViewer::getMinMax(double& min, double& max, AxisData * data)
{
int firstElement = ui.from->value();
int lastElement = ui.to->value();
double minValue = data->value(firstElement);
double maxValue = data->value(firstElement);
qDebug() << "Taking elements from " << firstElement << " to " << lastElement;
for (int _currentVal = firstElement; _currentVal <= lastElement; ++_currentVal) { //go over all selected elements
double v = data->value(_currentVal);
if (minValue > v) {
minValue = v;
}
if (maxValue < v) {
maxValue = v;
}
}
qDebug() << "The value are ]"<< minValue << " , " << maxValue << "[.";
min = minValue;
max = maxValue;
}
void ElementDataViewer::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Plus:
case Qt::Key_Equal:
slotZoomIn();
break;
case Qt::Key_Minus:
case Qt::Key_Underscore:
slotZoomOut();
break;
case Qt::Key_Escape:
close();
break;
}
}
void ElementDataViewer::slotZoomIn() {}
void ElementDataViewer::slotZoomOut() {}
void ElementDataViewer::setupAxisData(AxisData *data)
{
int selectedData = 0;
if (data->type() == AxisData::X) {
selectedData = ui.KCB_x->currentIndex();
} else {
selectedData = ui.KCB_y->currentIndex();
}
data->currentDataType = selectedData;
// init to _something_
ChemicalDataObject::BlueObelisk kind = ChemicalDataObject::mass;
QString caption;
int unit = KUnitConversion::NoUnit;
switch (selectedData) {
case AxisData::NUMBER:
{
kind = ChemicalDataObject::atomicNumber;
caption = i18n("Atomic Number");
break;
}
case AxisData::MASS:
{
kind = ChemicalDataObject::mass;
caption = i18n("Atomic Mass");
break;
}
case AxisData::EN:
{
kind = ChemicalDataObject::electronegativityPauling;
caption = i18n("Electronegativity");
break;
}
case AxisData::MELTINGPOINT:
{
kind = ChemicalDataObject::meltingpoint;
caption = i18n("Melting Point");
unit = Prefs::temperatureUnit();
break;
}
case AxisData::BOILINGPOINT:
{
kind = ChemicalDataObject::boilingpoint;
caption = i18n("Boiling Point");
unit = Prefs::temperatureUnit();
break;
}
case AxisData::ATOMICRADIUS:
{
kind = ChemicalDataObject::radiusVDW;
caption = i18n("Atomic Radius");
unit = Prefs::lengthUnit();
break;
}
case AxisData::COVALENTRADIUS:
{
kind = ChemicalDataObject::radiusCovalent;
caption = i18n("Covalent Radius");
unit = Prefs::lengthUnit();
break;
}
}
KalziumDataObject *kdo = KalziumDataObject::instance();
DoubleList dblDataList;
foreach (Element * element, kdo->ElementList) {
dblDataList << element->dataAsVariant(kind, unit).toDouble();
}
data->dataList.clear();
data->dataList << dblDataList;
data->kind = kind;
if (unit != KUnitConversion::NoUnit) {
QString stringUnit = KalziumDataObject::instance()->unitAsString(unit);
data->unit = QString(' ' + stringUnit);
caption.append(" [");
caption.append(stringUnit);
caption.append(']');
}
if (data->type() == AxisData::X) {
ui.plotwidget->axis(KPlotWidget::BottomAxis)->setLabel(caption);
} else {
ui.plotwidget->axis(KPlotWidget::LeftAxis)->setLabel(caption);
ui.plotwidget->axis(KPlotWidget::RightAxis)->setLabel(caption);
}
}
void ElementDataViewer::drawPlot()
{
/*
* to be 100% safe delete the old list
*/
ui.plotwidget->removeAllPlotObjects();
/*
* spare the next step in case everything is already set and done
*/
if (m_yData->currentDataType != ui.KCB_y->currentIndex()) {
initData();
}
if (m_xData->currentDataType != ui.KCB_x->currentIndex()) {
initData();
}
/*
* if the user selected the elements 20 to 30 the list-values are 19 to 29!!!
*/
const int tmpfrom = ui.from->value();
const int tmpto = ui.to->value();
const int from = qMin(tmpfrom, tmpto);
const int to = qMax(tmpfrom, tmpto);
/*
* The number of elements. #20 to 30 are 30-20+1=11 Elements
*/
int num = to - from + 1;
setLimits();
QSet<int> metals, metalloids, nonMetals;
metals << 3 << 4 << 11 << 12 << 13;
for (int i = 19; i <= 31; ++i) {
metals << i;
}
for (int i = 37; i <= 50; ++i) {
metals << i;
}
for (int i = 55; i <= 83; ++i) {
metals << i;
}
for (int i = 87; i <= 116; ++i) {
metals << i;
}
metalloids << 5 << 14 << 32 << 33 << 51 << 52 << 84 << 85;
nonMetals << 1 << 2 << 6 << 7 << 8 << 9 << 10 << 15 << 16;
nonMetals << 17 << 18 << 34 << 35 << 36 << 53 << 54 << 86;
/*
* check if the users wants to see the elementnames or not
*/
int whatShow = ui.comboElementLabels->currentIndex();
/*
* Checks what type of element, the user wants to plot.
* example, metal, non-metal.
*/
int whichType = ui.comboElementType->currentIndex();
KPlotObject* dataPointGreen = nullptr;
KPlotObject* dataPointRed = nullptr;
double av_x = 0.0;
double max_x = m_xData->value(from);
double min_x = m_xData->value(from);
double av_y = 0.0;
double max_y = m_yData->value(from);
double min_y = m_yData->value(from);
/*
* iterate for example from element 20 to 30 and construct
* the KPlotObjects
*/
dataPointGreen = new KPlotObject(
Qt::green,
KPlotObject::Points,
4,
KPlotObject::Star);
dataPointGreen->setLabelPen(QPen(Qt::blue));
dataPointRed = new KPlotObject(
Qt::red,
KPlotObject::Points,
4,
KPlotObject::Star); //Star can be replaced with a cross
dataPointRed->setLabelPen(QPen(Qt::blue));
for (int i = from; i < to + 1; ++i) {
double value_y = m_yData->value(i);
double value_x = m_xData->value(i);
bool known = ((value_y) > 0.0) ? 1 : 0;
//The element is know if its value is not zero
bool belongs = 1;
//The value of belongs is one if it belongs to the particular group
//See if the particular element belongs to the selected set or not.
//If a particular group of elements is selected,
if (whichType > 0) {
belongs = 0;
switch (whichType) {
case 1: //Plot only metals
belongs = metals . contains(i);
break;
case 2: //plot only nonmetals and metalloids
belongs = (nonMetals.contains(i) || metalloids.contains(i));
break;
case 3: //Plot s block elements
belongs = (block[i - 1]== QLatin1String("s"));
break;
case 4: //Plot p block elements
belongs = (block[i - 1]== QLatin1String("p"));
break;
case 5: //Plot d block elements
belongs = (block[i - 1]== QLatin1String("d"));
break;
case 6: //plot f block elements
belongs = (block[i - 1]== QLatin1String("f"));
break;
case 7: // Noble gases
belongs = ((elecConfig[i - 1]) . endsWith(QLatin1String("p6")));
belongs |= (i == 2); //Include Helium
break;
case 8: // Alkalie metals
belongs = ((elecConfig[i - 1]) . endsWith(QLatin1String("s1")));
belongs &= (block[i - 1]== QLatin1String("s")); //exclude chromium
belongs &= (i != 1); //exclude Hydrogen
break;
case 9: // Alkaline earth metals
belongs = ((elecConfig[i - 1]) . endsWith(QLatin1String("s2")));
belongs &= (block[i - 1]== QLatin1String("s")); //exclude chromium
belongs &= (i != 2); //exclude Helium
break;
case 10: // Lanthanides
// If element i is an f block element, with
// electronic configuration containing "f4" in it
// or the element is Lanthanum
belongs = ((block[i - 1]== QLatin1String("f")) && \
((elecConfig[i - 1]) . contains (QLatin1String("4f")))) || \
(i == 57); //Lanthanum 57
break;
case 11: //Actinides
//If element i is an f block element, with
// electronic configuration containing "f5" in it
// or the element is Actinium
belongs = (((block[i - 1]== QLatin1String("f"))) && \
((elecConfig[i - 1]) . contains (QLatin1String("5f")))) || \
(i == 89); //Actinium 89
break;
case 12: //Radio active
belongs = ((i == 43) || (i == 61) || (i > 84));
// Technitium prothomium and then polonium onwards.
break;
default:
whichType = 0;
belongs = 1;
}
}
if (belongs) {
if (known) {
av_x += value_x;
av_y += value_y;
if (value_x > max_x) {
max_x = value_x;
}
if (value_y > max_y) {
max_y = value_y;
}
if (value_x < min_x) {
min_x = value_x;
}
if (value_y < min_y) {
min_y = value_y;
}
QString lbl;
if (whatShow > 0) { //The users wants to see the labels
lbl = whatShow == 1 ? names[i-1] : symbols[i-1];
}
dataPointGreen->addPoint(value_x, value_y, lbl);
} else { //unknown value
//num is required while finding the average, if an element is not
//known it should not contribute to the average.
--num;
QString lbl;
if (whatShow > 0) { //The user wants to see the labels
lbl = whatShow == 1 ? names[i - 1] : symbols[i - 1];
}
dataPointRed->addPoint(value_x, value_y, lbl);
//For an Unknown value, use a red point to mark the data-point.
}
} else { //The element does not belong to the set
//num is required while finding average, if an element is
//not in the selected set, it should not contribute to the avg.
--num;
}
}
ui.plotwidget->addPlotObject(dataPointGreen);
ui.plotwidget->addPlotObject(dataPointRed);
if (num > 0) {
//now set the values for the min, max and average value
ui.av_x->setText(QString::number(av_x / num).append(m_xData->unit));
ui.minimum_x->setText(QString::number(min_x).append(m_xData->unit));
ui.maximum_x->setText(QString::number(max_x).append(m_xData->unit));
ui.av_y->setText(QString::number(av_y / num).append(m_yData->unit));
ui.minimum_y->setText(QString::number(min_y).append(m_yData->unit));
ui.maximum_y->setText(QString::number(max_y).append(m_yData->unit));
} else {
ui.av_x->setText(QString::number(0.0));
ui.minimum_x->setText(QString::number(0.0));
ui.maximum_x->setText(QString::number(0.0));
ui.av_y->setText(QString::number(0.0));
ui.minimum_y->setText(QString::number(0.0));
ui.maximum_y->setText(QString::number(0.0));
}
}
void ElementDataViewer::initData()
{
setupAxisData(m_xData);
setupAxisData(m_yData);
}