| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "spectradialog.h" |
| 7 |
|
| 8 |
#include "ui_spectradialog.h" |
| 9 |
|
| 10 |
#include <QtCore/QEvent> |
| 11 |
#include <QtCore/QFile> |
| 12 |
#include <QtCore/QSettings> |
| 13 |
|
| 14 |
#include <QtGui/QColor> |
| 15 |
#include <QtGui/QPalette> |
| 16 |
#include <QtGui/QScreen> |
| 17 |
#include <QtWidgets/QColorDialog> |
| 18 |
#include <QtWidgets/QFileDialog> |
| 19 |
|
| 20 |
#include <QtCore/QDebug> |
| 21 |
|
| 22 |
#include <avogadro/core/molecule.h> |
| 23 |
#include <avogadro/qtgui/chartwidget.h> |
| 24 |
|
| 25 |
using Avogadro::Core::Molecule; |
| 26 |
using Avogadro::QtGui::ChartWidget; |
| 27 |
|
| 28 |
namespace Avogadro::QtPlugins { |
| 29 |
|
| 30 |
// Detect dark mode: text lighter than window background |
| 31 |
static bool isDarkMode() |
| 32 |
{ |
| 33 |
const QPalette p; |
| 34 |
return p.color(QPalette::WindowText).lightness() > |
| 35 |
p.color(QPalette::Window).lightness(); |
| 36 |
} |
| 37 |
|
| 38 |
// Default colors that adapt to dark/light mode |
| 39 |
static QColor defaultBackgroundColor() |
| 40 |
{ |
| 41 |
return isDarkMode() ? QColor(0, 0, 0) : QColor(255, 255, 255); |
| 42 |
} |
| 43 |
|
| 44 |
static QColor defaultForegroundColor() |
| 45 |
{ |
| 46 |
return isDarkMode() ? QColor(255, 255, 255) : QColor(0, 0, 0); |
| 47 |
} |
| 48 |
|
| 49 |
static QColor defaultCalculatedColor() |
| 50 |
{ |
| 51 |
return isDarkMode() ? QColor(255, 255, 255) : QColor(0, 0, 0); |
| 52 |
} |
| 53 |
|
| 54 |
constexpr QColor red(255, 0, 0); |
| 55 |
constexpr QColor blue(0, 0, 255); |
| 56 |
|
| 57 |
float scaleAndBlur(float x, float peak, float intensity, float scale = 1.0, |
| 58 |
float shift = 0.0, float fwhm = 0.0) |
| 59 |
{ |
| 60 |
// return the intensity at point x, from a Gaussian centered at peak |
| 61 |
// with a width of fwhm, scaled by scale and shifted by shift |
| 62 |
float fwhm_to_sigma = 2.0 * sqrt(2.0 * log(2.0)); |
| 63 |
float sigma = fwhm / fwhm_to_sigma; |
| 64 |
|
| 65 |
// x is the absolute position, but we need to scale the peak position |
| 66 |
float scaled_peak = (peak - shift) / scale; |
| 67 |
|
| 68 |
float delta = x - scaled_peak; |
| 69 |
float exponent = -(delta * delta) / (2 * sigma * sigma); |
| 70 |
float gaussian = exp(exponent); |
| 71 |
return intensity * gaussian; |
| 72 |
} |
| 73 |
|
| 74 |
float closestTo(float x, float peak, float intensity, float scale = 1.0, |
| 75 |
float shift = 0.0, float xScale = 1.0) |
| 76 |
{ |
| 77 |
// return peak intensity if x is closer to the peak than another point |
| 78 |
// scaled by scale and shifted by shift |
| 79 |
float scaled_peak = (peak - shift) / scale; |
| 80 |
float delta = x - scaled_peak; |
| 81 |
// xScale is the reciprocal of the space between points |
| 82 |
// (i.e., used to generate many points in the loop) |
| 83 |
float peak_to_peak = 1.0 / xScale; |
| 84 |
return (fabs(delta) < peak_to_peak / 2.0) ? intensity : 0.0; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Grab the first column of a matrix for std::vector<double> conversion |
| 89 |
* @param matrix The matrix to convert |
| 90 |
* @return The first column of the matrix |
| 91 |
*/ |
| 92 |
std::vector<double> fromMatrix(const MatrixX& matrix) |
| 93 |
{ |
| 94 |
std::vector<double> result; |
| 95 |
for (auto i = 0; i < matrix.rows(); ++i) |
| 96 |
result.push_back(matrix(i, 0)); |
| 97 |
return result; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Interpolate data points at targetX (e.g., computed spectra) |
| 102 |
* from sourceX and sourceY. |
| 103 |
* |
| 104 |
* This function will linearly interpolate data points from sourceX and sourceY |
| 105 |
* and return the corresponding interpolated values at targetX. |
| 106 |
* |
| 107 |
* @param expData The experimental data points, corresponding to sourceX and |
| 108 |
* sourceY. |
| 109 |
* @param targetX The independent variable data points where the interpolation |
| 110 |
* should be performed. |
| 111 |
* |
| 112 |
* @return A vector of interpolated values, corresponding to targetX. |
| 113 |
*/ |
| 114 |
std::vector<float> interpolateData(const MatrixX& expData, |
| 115 |
const std::vector<float>& targetX) |
| 116 |
{ |
| 117 |
std::vector<float> result(targetX.size(), 0.0f); |
| 118 |
|
| 119 |
if (expData.rows() == 0 || expData.cols() < 2) |
| 120 |
return result; |
| 121 |
|
| 122 |
for (size_t i = 0; i < targetX.size(); ++i) { |
| 123 |
float x = targetX[i]; |
| 124 |
|
| 125 |
// Handle out-of-range cases |
| 126 |
if (x <= expData(0, 0)) { |
| 127 |
result[i] = expData(0, 1); |
| 128 |
continue; |
| 129 |
} |
| 130 |
if (x >= expData(expData.rows() - 1, 0)) { |
| 131 |
result[i] = expData(expData.rows() - 1, 1); |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
// Binary search for bracketing points |
| 136 |
int lower = 0; |
| 137 |
int upper = expData.rows() - 1; |
| 138 |
|
| 139 |
while (upper - lower > 1) { |
| 140 |
int mid = (lower + upper) / 2; |
| 141 |
if (expData(mid, 0) < x) |
| 142 |
lower = mid; |
| 143 |
else |
| 144 |
upper = mid; |
| 145 |
} |
| 146 |
|
| 147 |
// Linear interpolation |
| 148 |
double x1 = expData(lower, 0); |
| 149 |
double x2 = expData(upper, 0); |
| 150 |
double y1 = expData(lower, 1); |
| 151 |
double y2 = expData(upper, 1); |
| 152 |
|
| 153 |
result[i] = y1 + (y2 - y1) * (x - x1) / (x2 - x1); |
| 154 |
} |
| 155 |
|
| 156 |
return result; |
| 157 |
} |
| 158 |
|
| 159 |
SpectraDialog::SpectraDialog(QWidget* parent) |
| 160 |
: QDialog(parent), m_ui(new Ui::SpectraDialog) |
| 161 |
{ |
| 162 |
m_ui->setupUi(this); |
| 163 |
|
| 164 |
// hide the units for now |
| 165 |
m_ui->unitsLabel->hide(); |
| 166 |
m_ui->unitsCombo->hide(); |
| 167 |
|
| 168 |
// only for NMR |
| 169 |
m_ui->elementCombo->hide(); |
| 170 |
|
| 171 |
m_ui->dataTable->horizontalHeader()->setSectionResizeMode( |
| 172 |
QHeaderView::Stretch); |
| 173 |
|
| 174 |
// Hide advanced options initially |
| 175 |
m_ui->tab_widget->hide(); |
| 176 |
m_ui->dataTable->hide(); |
| 177 |
m_ui->push_exportData->hide(); |
| 178 |
|
| 179 |
readSettings(); |
| 180 |
|
| 181 |
// connections for options |
| 182 |
connect(m_ui->push_options, SIGNAL(clicked()), this, SLOT(toggleOptions())); |
| 183 |
connect(m_ui->push_export, SIGNAL(clicked()), this, SLOT(exportData())); |
| 184 |
connect(m_ui->push_exportData, SIGNAL(clicked()), this, SLOT(exportData())); |
| 185 |
connect(m_ui->push_import, SIGNAL(clicked()), this, SLOT(importData())); |
| 186 |
connect(m_ui->cb_raw, SIGNAL(toggled(bool)), this, SLOT(updatePlot())); |
| 187 |
connect(m_ui->cb_import, SIGNAL(toggled(bool)), this, SLOT(updatePlot())); |
| 188 |
connect(m_ui->push_colorBackground, SIGNAL(clicked()), this, |
| 189 |
SLOT(changeBackgroundColor())); |
| 190 |
connect(m_ui->push_colorForeground, SIGNAL(clicked()), this, |
| 191 |
SLOT(changeForegroundColor())); |
| 192 |
connect(m_ui->push_colorCalculated, SIGNAL(clicked()), this, |
| 193 |
SLOT(changeCalculatedSpectraColor())); |
| 194 |
connect(m_ui->push_colorRaw, SIGNAL(clicked()), this, |
| 195 |
SLOT(changeRawSpectraColor())); |
| 196 |
connect(m_ui->push_colorImported, SIGNAL(clicked()), this, |
| 197 |
SLOT(changeImportedSpectraColor())); |
| 198 |
connect(m_ui->fontSizeCombo, SIGNAL(currentIndexChanged(int)), this, |
| 199 |
SLOT(changeFontSize())); |
| 200 |
connect(m_ui->lineWidthSpinBox, SIGNAL(valueChanged(double)), this, |
| 201 |
SLOT(changeLineWidth())); |
| 202 |
connectOptions(); |
| 203 |
} |
| 204 |
|
| 205 |
SpectraDialog::~SpectraDialog() |
| 206 |
{ |
| 207 |
writeSettings(); |
| 208 |
} |
| 209 |
|
| 210 |
void SpectraDialog::connectOptions() |
| 211 |
{ |
| 212 |
// connect (or reconnect) anything that calls change or update plot |
| 213 |
connect(m_ui->combo_spectra, SIGNAL(currentIndexChanged(int)), this, |
| 214 |
SLOT(changeSpectra())); |
| 215 |
connect(m_ui->xAxisMinimum, SIGNAL(valueChanged(double)), this, |
| 216 |
SLOT(updatePlot())); |
| 217 |
connect(m_ui->xAxisMaximum, SIGNAL(valueChanged(double)), this, |
| 218 |
SLOT(updatePlot())); |
| 219 |
connect(m_ui->yAxisMinimum, SIGNAL(valueChanged(double)), this, |
| 220 |
SLOT(updatePlot())); |
| 221 |
connect(m_ui->yAxisMaximum, SIGNAL(valueChanged(double)), this, |
| 222 |
SLOT(updatePlot())); |
| 223 |
connect(m_ui->peakWidth, SIGNAL(valueChanged(double)), this, |
| 224 |
SLOT(updatePlot())); |
| 225 |
|
| 226 |
connect(m_ui->scaleSpinBox, SIGNAL(valueChanged(double)), this, |
| 227 |
SLOT(updatePlot())); |
| 228 |
connect(m_ui->offsetSpinBox, SIGNAL(valueChanged(double)), this, |
| 229 |
SLOT(updatePlot())); |
| 230 |
connect(m_ui->peakThreshold, SIGNAL(valueChanged(double)), this, |
| 231 |
SLOT(updatePlot())); |
| 232 |
} |
| 233 |
|
| 234 |
void SpectraDialog::disconnectOptions() |
| 235 |
{ |
| 236 |
// disconnect anything that calls change or update plot |
| 237 |
disconnect(m_ui->combo_spectra, SIGNAL(currentIndexChanged(int)), this, |
| 238 |
SLOT(changeSpectra())); |
| 239 |
disconnect(m_ui->xAxisMinimum, SIGNAL(valueChanged(double)), this, |
| 240 |
SLOT(updatePlot())); |
| 241 |
disconnect(m_ui->xAxisMaximum, SIGNAL(valueChanged(double)), this, |
| 242 |
SLOT(updatePlot())); |
| 243 |
disconnect(m_ui->yAxisMinimum, SIGNAL(valueChanged(double)), this, |
| 244 |
SLOT(updatePlot())); |
| 245 |
disconnect(m_ui->yAxisMaximum, SIGNAL(valueChanged(double)), this, |
| 246 |
SLOT(updatePlot())); |
| 247 |
disconnect(m_ui->peakWidth, SIGNAL(valueChanged(double)), this, |
| 248 |
SLOT(updatePlot())); |
| 249 |
|
| 250 |
disconnect(m_ui->scaleSpinBox, SIGNAL(valueChanged(double)), this, |
| 251 |
SLOT(updatePlot())); |
| 252 |
disconnect(m_ui->offsetSpinBox, SIGNAL(valueChanged(double)), this, |
| 253 |
SLOT(updatePlot())); |
| 254 |
disconnect(m_ui->peakThreshold, SIGNAL(valueChanged(double)), this, |
| 255 |
SLOT(updatePlot())); |
| 256 |
} |
| 257 |
|
| 258 |
void SpectraDialog::changeEvent(QEvent* event) |
| 259 |
{ |
| 260 |
if (event->type() == QEvent::PaletteChange) |
| 261 |
updatePlot(); |
| 262 |
QDialog::changeEvent(event); |
| 263 |
} |
| 264 |
|
| 265 |
void SpectraDialog::mouseDoubleClickEvent(QMouseEvent* e) |
| 266 |
{ |
| 267 |
auto* chart = chartWidget(); |
| 268 |
if (chart) |
| 269 |
chart->resetZoom(); |
| 270 |
} |
| 271 |
|
| 272 |
void SpectraDialog::updateElementCombo() |
| 273 |
{ |
| 274 |
// update the element combo box |
| 275 |
disconnect(m_ui->elementCombo, SIGNAL(currentIndexChanged(int)), this, |
| 276 |
SLOT(changeSpectra())); |
| 277 |
m_ui->elementCombo->clear(); |
| 278 |
|
| 279 |
// go through the elements in atomic number order |
| 280 |
// make a copy of the vector |
| 281 |
std::vector<unsigned char> elements = m_elements; |
| 282 |
std::sort(elements.begin(), elements.end()); |
| 283 |
|
| 284 |
// add the unique elements, with the element number as the data |
| 285 |
for (auto& element : elements) { |
| 286 |
// check to see if it's already in the combo box |
| 287 |
bool found = false; |
| 288 |
for (int i = 0; i < m_ui->elementCombo->count(); ++i) { |
| 289 |
if (m_ui->elementCombo->itemData(i).toInt() == element) { |
| 290 |
found = true; |
| 291 |
break; |
| 292 |
} |
| 293 |
} |
| 294 |
if (found) |
| 295 |
continue; |
| 296 |
|
| 297 |
switch (element) { |
| 298 |
case 1: |
| 299 |
m_ui->elementCombo->addItem("¹H", element); |
| 300 |
break; |
| 301 |
case 3: |
| 302 |
m_ui->elementCombo->addItem("⁷Li", element); |
| 303 |
break; |
| 304 |
case 5: |
| 305 |
m_ui->elementCombo->addItem("¹¹B", element); |
| 306 |
break; |
| 307 |
case 6: |
| 308 |
m_ui->elementCombo->addItem("¹³C", element); |
| 309 |
break; |
| 310 |
case 7: |
| 311 |
m_ui->elementCombo->addItem("¹⁵N", element); |
| 312 |
break; |
| 313 |
case 8: |
| 314 |
m_ui->elementCombo->addItem("¹⁷O", element); |
| 315 |
break; |
| 316 |
case 9: |
| 317 |
m_ui->elementCombo->addItem("¹⁹F", element); |
| 318 |
break; |
| 319 |
case 14: |
| 320 |
m_ui->elementCombo->addItem("²⁹Si", element); |
| 321 |
break; |
| 322 |
case 15: |
| 323 |
m_ui->elementCombo->addItem("³¹P", element); |
| 324 |
break; |
| 325 |
default: |
| 326 |
m_ui->elementCombo->addItem(QString::number(element), element); |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
// connect the element combo box |
| 332 |
connect(m_ui->elementCombo, SIGNAL(currentIndexChanged(int)), this, |
| 333 |
SLOT(changeSpectra())); |
| 334 |
changeSpectra(); // default to 1H |
| 335 |
} |
| 336 |
|
| 337 |
void SpectraDialog::changeSpectra() |
| 338 |
{ |
| 339 |
// based on the current spectra type, update the options |
| 340 |
// and prep the spectra for plotting |
| 341 |
QSettings settings; |
| 342 |
|
| 343 |
disconnectOptions(); |
| 344 |
|
| 345 |
// what type of spectra are we plotting? |
| 346 |
SpectraType type = |
| 347 |
static_cast<SpectraType>(m_ui->combo_spectra->currentData().toInt()); |
| 348 |
|
| 349 |
// only show for NMR |
| 350 |
m_ui->elementCombo->hide(); |
| 351 |
// todo: some spectra might want to swtich units |
| 352 |
|
| 353 |
m_transitions.clear(); |
| 354 |
m_intensities.clear(); |
| 355 |
|
| 356 |
switch (type) { |
| 357 |
case SpectraType::Infrared: |
| 358 |
m_transitions = fromMatrix(m_spectra["IR"].col(0)); |
| 359 |
m_intensities = fromMatrix(m_spectra["IR"].col(1)); |
| 360 |
|
| 361 |
settings.beginGroup("spectra/ir"); |
| 362 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 363 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 364 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 4000.0).toDouble()); |
| 365 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 400.0).toDouble()); |
| 366 |
m_ui->peakWidth->setValue(settings.value("fwhm", 30.0).toDouble()); |
| 367 |
settings.endGroup(); |
| 368 |
break; |
| 369 |
case SpectraType::Raman: |
| 370 |
m_transitions = fromMatrix(m_spectra["Raman"].col(0)); |
| 371 |
m_intensities = fromMatrix(m_spectra["Raman"].col(1)); |
| 372 |
|
| 373 |
settings.beginGroup("spectra/raman"); |
| 374 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 375 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 376 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 0.0).toDouble()); |
| 377 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 4000.0).toDouble()); |
| 378 |
m_ui->peakWidth->setValue(settings.value("fwhm", 30.0).toDouble()); |
| 379 |
settings.endGroup(); |
| 380 |
break; |
| 381 |
case SpectraType::VibrationalCD: |
| 382 |
m_transitions = fromMatrix(m_spectra["VibrationalCD"].col(0)); |
| 383 |
m_intensities = fromMatrix(m_spectra["VibrationalCD"].col(1)); |
| 384 |
|
| 385 |
settings.beginGroup("spectra/vcd"); |
| 386 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 387 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 388 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 0.0).toDouble()); |
| 389 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 4000.0).toDouble()); |
| 390 |
m_ui->peakWidth->setValue(settings.value("fwhm", 30.0).toDouble()); |
| 391 |
settings.endGroup(); |
| 392 |
break; |
| 393 |
case SpectraType::MagneticCD: |
| 394 |
m_transitions = fromMatrix(m_spectra["MagneticCD"].col(0)); |
| 395 |
m_intensities = fromMatrix(m_spectra["MagneticCD"].col(1)); |
| 396 |
|
| 397 |
settings.beginGroup("spectra/mcd"); |
| 398 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 399 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 400 |
m_ui->xAxisMinimum->setMaximum(30000.0); |
| 401 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 10000.0).toDouble()); |
| 402 |
m_ui->xAxisMaximum->setMaximum( |
| 403 |
std::max(settings.value("xmax", 75000.0).toDouble(), 75000.0)); |
| 404 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 75000.0).toDouble()); |
| 405 |
m_ui->peakWidth->setValue(settings.value("fwhm", 100.0).toDouble()); |
| 406 |
settings.endGroup(); |
| 407 |
break; |
| 408 |
case SpectraType::NMR: |
| 409 |
// settings handled per-element below |
| 410 |
m_ui->elementCombo->show(); |
| 411 |
break; |
| 412 |
case SpectraType::Electronic: |
| 413 |
m_transitions = fromMatrix(m_spectra["Electronic"].col(0)); |
| 414 |
m_intensities = fromMatrix(m_spectra["Electronic"].col(1)); |
| 415 |
|
| 416 |
settings.beginGroup("spectra/electronic"); |
| 417 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 418 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 419 |
// in eV |
| 420 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 5.0).toDouble()); |
| 421 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 1.0).toDouble()); |
| 422 |
m_ui->peakWidth->setValue(settings.value("fwhm", 0.1).toDouble()); |
| 423 |
settings.endGroup(); |
| 424 |
break; |
| 425 |
case SpectraType::CircularDichroism: |
| 426 |
m_transitions = fromMatrix(m_spectra["Electronic"].col(0)); |
| 427 |
// check if electronic has a third column |
| 428 |
if (m_spectra["Electronic"].cols() > 2) |
| 429 |
m_intensities = fromMatrix(m_spectra["Electronic"].col(2)); |
| 430 |
else // grab it from the CD data |
| 431 |
m_intensities = fromMatrix(m_spectra["CircularDichroism"].col(1)); |
| 432 |
|
| 433 |
settings.beginGroup("spectra/cd"); |
| 434 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 435 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 436 |
// default to eV units |
| 437 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 5.0).toDouble()); |
| 438 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 1.0).toDouble()); |
| 439 |
m_ui->peakWidth->setValue(settings.value("fwhm", 0.1).toDouble()); |
| 440 |
settings.endGroup(); |
| 441 |
break; |
| 442 |
case SpectraType::DensityOfStates: |
| 443 |
m_transitions = fromMatrix(m_spectra["DensityOfStates"].col(0)); |
| 444 |
m_intensities = fromMatrix(m_spectra["DensityOfStates"].col(1)); |
| 445 |
|
| 446 |
settings.beginGroup("spectra/dos"); |
| 447 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 448 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 449 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", -50.0).toDouble()); |
| 450 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 50.0).toDouble()); |
| 451 |
m_ui->peakWidth->setValue(settings.value("fwhm", 0.1).toDouble()); |
| 452 |
settings.endGroup(); |
| 453 |
break; |
| 454 |
} |
| 455 |
|
| 456 |
// a bunch of special work depending on the NMR element |
| 457 |
if (type == SpectraType::NMR) { |
| 458 |
// get the element |
| 459 |
int element = m_ui->elementCombo->currentData().toInt(); |
| 460 |
|
| 461 |
settings.beginGroup(QString("spectra/nmr/%1").arg(element)); |
| 462 |
m_ui->scaleSpinBox->setValue(settings.value("scale", 1.0).toDouble()); |
| 463 |
m_ui->peakWidth->setValue(settings.value("fwhm", 0.1).toDouble()); |
| 464 |
|
| 465 |
// tweak the default axis range |
| 466 |
// based on https://imserc.northwestern.edu/guide/eNMR/chem/NMRnuclei.html |
| 467 |
// offsets are approximate from a few calculations |
| 468 |
// .. to at least provide a starting point |
| 469 |
switch (element) { |
| 470 |
case 1: // 1H |
| 471 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 12.0).toDouble()); |
| 472 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 0.0).toDouble()); |
| 473 |
m_ui->offsetSpinBox->setValue( |
| 474 |
settings.value("offset", 31.876).toDouble()); |
| 475 |
break; |
| 476 |
case 3: // 7Li |
| 477 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", -16.0).toDouble()); |
| 478 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 11.0).toDouble()); |
| 479 |
// TODO: offset |
| 480 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 481 |
break; |
| 482 |
case 5: // 11B |
| 483 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 100.0).toDouble()); |
| 484 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", -120.0).toDouble()); |
| 485 |
m_ui->offsetSpinBox->setValue( |
| 486 |
settings.value("offset", 109.774).toDouble()); |
| 487 |
break; |
| 488 |
case 6: // 13C |
| 489 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 200.0).toDouble()); |
| 490 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 0.0).toDouble()); |
| 491 |
m_ui->offsetSpinBox->setValue( |
| 492 |
settings.value("offset", 192.038).toDouble()); |
| 493 |
break; |
| 494 |
case 7: // 15N |
| 495 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 800.0).toDouble()); |
| 496 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", 0.0).toDouble()); |
| 497 |
m_ui->offsetSpinBox->setValue( |
| 498 |
settings.value("offset", -106.738).toDouble()); |
| 499 |
break; |
| 500 |
case 8: // 17O |
| 501 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 1600.0).toDouble()); |
| 502 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", -50.0).toDouble()); |
| 503 |
m_ui->offsetSpinBox->setValue( |
| 504 |
settings.value("offset", 347.782).toDouble()); |
| 505 |
break; |
| 506 |
case 9: // 19F |
| 507 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 60.0).toDouble()); |
| 508 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", -300.0).toDouble()); |
| 509 |
m_ui->offsetSpinBox->setValue( |
| 510 |
settings.value("offset", 206.735).toDouble()); |
| 511 |
break; |
| 512 |
case 14: // 29Si |
| 513 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 50.0).toDouble()); |
| 514 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", -200.0).toDouble()); |
| 515 |
m_ui->offsetSpinBox->setValue( |
| 516 |
settings.value("offset", 400.876).toDouble()); |
| 517 |
break; |
| 518 |
case 15: // 31P |
| 519 |
m_ui->xAxisMinimum->setValue(settings.value("xmin", 250.0).toDouble()); |
| 520 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", -250.0).toDouble()); |
| 521 |
m_ui->offsetSpinBox->setValue( |
| 522 |
settings.value("offset", 392.841).toDouble()); |
| 523 |
break; |
| 524 |
default: |
| 525 |
m_ui->xAxisMinimum->setValue(settings.value("xmax", 100.0).toDouble()); |
| 526 |
m_ui->xAxisMaximum->setValue(settings.value("xmax", -100.0).toDouble()); |
| 527 |
m_ui->offsetSpinBox->setValue(settings.value("offset", 0.0).toDouble()); |
| 528 |
break; |
| 529 |
} |
| 530 |
settings.endGroup(); |
| 531 |
|
| 532 |
// the default NMR data has all the atoms in it, |
| 533 |
// so we need to loop through m_elements to filter |
| 534 |
MatrixX nmr = m_spectra["NMR"]; |
| 535 |
|
| 536 |
for (int i = 0; i < m_elements.size(); ++i) { |
| 537 |
if (m_elements[i] == element) { |
| 538 |
m_transitions.push_back(nmr(i, 0)); |
| 539 |
} |
| 540 |
} |
| 541 |
// fill the intensities with 1.0 |
| 542 |
m_intensities.resize(m_transitions.size(), 1.0); |
| 543 |
} |
| 544 |
// other spectra transitions and intensities are already set |
| 545 |
|
| 546 |
// update the data table |
| 547 |
double maxIntensity = 0.0; |
| 548 |
m_ui->dataTable->setRowCount(m_transitions.size()); |
| 549 |
m_ui->dataTable->setColumnCount(2); |
| 550 |
for (auto i = 0; i < m_transitions.size(); ++i) { |
| 551 |
// frequency or energy |
| 552 |
QTableWidgetItem* item = |
| 553 |
new QTableWidgetItem(QString::number(m_transitions[i], 'f', 4)); |
| 554 |
m_ui->dataTable->setItem(i, 0, item); |
| 555 |
// intensities |
| 556 |
item = new QTableWidgetItem(QString::number(m_intensities[i], 'f', 4)); |
| 557 |
m_ui->dataTable->setItem(i, 1, item); |
| 558 |
|
| 559 |
if (m_intensities[i] > maxIntensity) |
| 560 |
maxIntensity = m_intensities[i]; |
| 561 |
} |
| 562 |
|
| 563 |
// update the spin boxes |
| 564 |
m_ui->yAxisMaximum->setValue(maxIntensity); |
| 565 |
m_ui->yAxisMinimum->setMinimum(0.0); |
| 566 |
// if CD, set the minimum too |
| 567 |
if (type == SpectraType::CircularDichroism || |
| 568 |
type == SpectraType::VibrationalCD || type == SpectraType::MagneticCD) { |
| 569 |
m_ui->yAxisMinimum->setMinimum(-maxIntensity * 2.0); |
| 570 |
m_ui->yAxisMinimum->setValue(-maxIntensity); |
| 571 |
} |
| 572 |
if (type == SpectraType::Infrared) { |
| 573 |
m_ui->yAxisMaximum->setValue(102.0); // transmission |
| 574 |
} |
| 575 |
|
| 576 |
updatePlot(); |
| 577 |
connectOptions(); |
| 578 |
} |
| 579 |
|
| 580 |
void SpectraDialog::setSpectra(const std::map<std::string, MatrixX>& spectra) |
| 581 |
{ |
| 582 |
m_spectra = spectra; |
| 583 |
|
| 584 |
// update the combo box |
| 585 |
disconnect(m_ui->combo_spectra, SIGNAL(currentIndexChanged(int)), this, |
| 586 |
SLOT(changeSpectra())); |
| 587 |
|
| 588 |
m_ui->combo_spectra->clear(); |
| 589 |
for (auto& spectra : m_spectra) { |
| 590 |
QString name = QString::fromStdString(spectra.first); |
| 591 |
|
| 592 |
#ifndef NDEBUG |
| 593 |
qDebug() << " reading spectra " << name; |
| 594 |
#endif |
| 595 |
|
| 596 |
if (name == "IR") { |
| 597 |
name = tr("Infrared"); |
| 598 |
m_ui->combo_spectra->addItem(name, |
| 599 |
static_cast<int>(SpectraType::Infrared)); |
| 600 |
} else if (name == "Raman") { |
| 601 |
name = tr("Raman"); |
| 602 |
m_ui->combo_spectra->addItem(name, static_cast<int>(SpectraType::Raman)); |
| 603 |
} else if (name == "VibrationalCD") { |
| 604 |
name = tr("Vibrational CD", "vibrational circular dichroism"); |
| 605 |
m_ui->combo_spectra->addItem( |
| 606 |
name, static_cast<int>(SpectraType::VibrationalCD)); |
| 607 |
} else if (name == "MagneticCD") { |
| 608 |
name = tr("Magnetic CD", "magnetic circular dichroism"); |
| 609 |
m_ui->combo_spectra->addItem(name, |
| 610 |
static_cast<int>(SpectraType::MagneticCD)); |
| 611 |
} else if (name == "NMR") { |
| 612 |
name = tr("NMR"); |
| 613 |
m_ui->combo_spectra->addItem(name, static_cast<int>(SpectraType::NMR)); |
| 614 |
} else if (name == "Electronic") { |
| 615 |
name = tr("Electronic"); |
| 616 |
m_ui->combo_spectra->addItem(name, |
| 617 |
static_cast<int>(SpectraType::Electronic)); |
| 618 |
} else if (name == "CircularDichroism") { |
| 619 |
name = tr("Circular Dichroism"); |
| 620 |
m_ui->combo_spectra->addItem( |
| 621 |
name, static_cast<int>(SpectraType::CircularDichroism)); |
| 622 |
} else if (name == "DensityOfStates") { |
| 623 |
name = tr("Density of States"); |
| 624 |
m_ui->combo_spectra->addItem( |
| 625 |
name, static_cast<int>(SpectraType::DensityOfStates)); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
changeSpectra(); |
| 630 |
// connect again |
| 631 |
connect(m_ui->combo_spectra, SIGNAL(currentIndexChanged(int)), this, |
| 632 |
SLOT(changeSpectra())); |
| 633 |
} |
| 634 |
|
| 635 |
void SpectraDialog::writeSettings() const |
| 636 |
{ |
| 637 |
QSettings settings; |
| 638 |
|
| 639 |
settings.setValue("spectra/currentSpectra", |
| 640 |
m_ui->combo_spectra->currentIndex()); |
| 641 |
} |
| 642 |
|
| 643 |
void SpectraDialog::readSettings() |
| 644 |
{ |
| 645 |
QSettings settings; |
| 646 |
settings.beginGroup("spectra"); |
| 647 |
// update the dialog with saved settings |
| 648 |
|
| 649 |
// font size |
| 650 |
int fontSize = settings.value("fontSize", 12).toInt(); |
| 651 |
m_ui->fontSizeCombo->setCurrentText(QString::number(fontSize)); |
| 652 |
// line width |
| 653 |
float lineWidth = settings.value("lineWidth", 1.0).toFloat(); |
| 654 |
m_ui->lineWidthSpinBox->setValue(lineWidth); |
| 655 |
|
| 656 |
// TODO: other bits |
| 657 |
settings.endGroup(); |
| 658 |
} |
| 659 |
|
| 660 |
void SpectraDialog::changeBackgroundColor() |
| 661 |
{ |
| 662 |
QSettings settings; |
| 663 |
QColor current = |
| 664 |
settings.value("spectra/backgroundColor", defaultBackgroundColor()) |
| 665 |
.value<QColor>(); |
| 666 |
QColor color = |
| 667 |
QColorDialog::getColor(current, this, tr("Select Background Color"), |
| 668 |
QColorDialog::ShowAlphaChannel); |
| 669 |
if (color.isValid() && color != current) { |
| 670 |
settings.setValue("spectra/backgroundColor", color); |
| 671 |
auto* chart = chartWidget(); |
| 672 |
if (chart != nullptr) { |
| 673 |
QtGui::color4ub ubColor = { static_cast<unsigned char>(color.red()), |
| 674 |
static_cast<unsigned char>(color.green()), |
| 675 |
static_cast<unsigned char>(color.blue()), |
| 676 |
static_cast<unsigned char>(color.alpha()) }; |
| 677 |
chart->setBackgroundColor(ubColor); |
| 678 |
} |
| 679 |
updatePlot(); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
void SpectraDialog::exportData() {} |
| 684 |
|
| 685 |
void SpectraDialog::changeForegroundColor() |
| 686 |
{ |
| 687 |
QSettings settings; |
| 688 |
QColor current = |
| 689 |
settings.value("spectra/foregroundColor", defaultForegroundColor()) |
| 690 |
.value<QColor>(); |
| 691 |
QColor color = |
| 692 |
QColorDialog::getColor(current, this, tr("Select Foreground Color")); |
| 693 |
if (color.isValid() && color != current) { |
| 694 |
settings.setValue("spectra/foregroundColor", color); |
| 695 |
updatePlot(); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
void SpectraDialog::changeCalculatedSpectraColor() |
| 700 |
{ |
| 701 |
QSettings settings; |
| 702 |
QColor current = |
| 703 |
settings.value("spectra/calculatedColor", defaultCalculatedColor()) |
| 704 |
.value<QColor>(); |
| 705 |
QColor color = QColorDialog::getColor(current, this, |
| 706 |
tr("Select Calculated Spectra Color")); |
| 707 |
if (color.isValid() && color != current) { |
| 708 |
settings.setValue("spectra/calculatedColor", color); |
| 709 |
updatePlot(); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
void SpectraDialog::changeRawSpectraColor() |
| 714 |
{ |
| 715 |
QSettings settings; |
| 716 |
QColor current = settings.value("spectra/rawColor", red).value<QColor>(); |
| 717 |
QColor color = |
| 718 |
QColorDialog::getColor(current, this, tr("Select Raw Spectra Color")); |
| 719 |
if (color.isValid() && color != current) { |
| 720 |
settings.setValue("spectra/rawColor", color); |
| 721 |
updatePlot(); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
void SpectraDialog::changeImportedSpectraColor() |
| 726 |
{ |
| 727 |
QSettings settings; |
| 728 |
QColor current = |
| 729 |
settings.value("spectra/importedColor", blue).value<QColor>(); |
| 730 |
QColor color = |
| 731 |
QColorDialog::getColor(current, this, tr("Select Imported Spectra Color")); |
| 732 |
if (color.isValid() && color != current) { |
| 733 |
settings.setValue("spectra/importedColor", color); |
| 734 |
updatePlot(); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
void SpectraDialog::changeFontSize() |
| 739 |
{ |
| 740 |
int size = m_ui->fontSizeCombo->currentText().toInt(); |
| 741 |
QSettings settings; |
| 742 |
settings.setValue("spectra/fontSize", size); |
| 743 |
updatePlot(); |
| 744 |
} |
| 745 |
|
| 746 |
void SpectraDialog::changeLineWidth() |
| 747 |
{ |
| 748 |
float width = m_ui->lineWidthSpinBox->value(); |
| 749 |
QSettings settings; |
| 750 |
settings.setValue("spectra/lineWidth", width); |
| 751 |
updatePlot(); |
| 752 |
} |
| 753 |
|
| 754 |
/////////////////////// |
| 755 |
// Plot Manipulation // |
| 756 |
/////////////////////// |
| 757 |
|
| 758 |
void SpectraDialog::updatePlot() |
| 759 |
{ |
| 760 |
// disconnect the options while we update the plot |
| 761 |
disconnectOptions(); |
| 762 |
|
| 763 |
// the raw data |
| 764 |
std::vector<double> transitions, intensities; |
| 765 |
// for the plot |
| 766 |
std::vector<float> xData, yData, yStick, importedData; |
| 767 |
|
| 768 |
// determine the type to plot |
| 769 |
SpectraType type = |
| 770 |
static_cast<SpectraType>(m_ui->combo_spectra->currentData().toInt()); |
| 771 |
|
| 772 |
QSettings settings; |
| 773 |
QString windowName; |
| 774 |
QString xTitle; |
| 775 |
QString yTitle; |
| 776 |
// TODO: switch units for electronic and CD |
| 777 |
QString xWave = tr("Wavelength (nm)"); |
| 778 |
QString xFreq = tr("Frequency (Hz)"); |
| 779 |
|
| 780 |
bool transmission = false; |
| 781 |
// get the raw data from the spectra map |
| 782 |
switch (type) { |
| 783 |
case SpectraType::Infrared: |
| 784 |
windowName = tr("Vibrational Spectra"); |
| 785 |
xTitle = tr("Wavenumbers (cm⁻¹)"); |
| 786 |
yTitle = tr("Transmission"); |
| 787 |
transmission = true; |
| 788 |
|
| 789 |
settings.beginGroup("spectra/ir"); |
| 790 |
settings.setValue("xmin", float(m_ui->xAxisMinimum->value())); |
| 791 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 792 |
settings.setValue("fwhm", float(m_ui->peakWidth->value())); |
| 793 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 794 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 795 |
settings.endGroup(); |
| 796 |
break; |
| 797 |
case SpectraType::Raman: |
| 798 |
windowName = tr("Raman Spectra"); |
| 799 |
xTitle = tr("Wavenumbers (cm⁻¹)"); |
| 800 |
yTitle = tr("Intensity"); |
| 801 |
// save the plot settings |
| 802 |
settings.beginGroup("spectra/raman"); |
| 803 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 804 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 805 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 806 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 807 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 808 |
settings.endGroup(); |
| 809 |
break; |
| 810 |
case SpectraType::VibrationalCD: |
| 811 |
windowName = tr("Vibrational Circular Dichroism"); |
| 812 |
xTitle = tr("Wavenumbers (cm⁻¹)"); |
| 813 |
yTitle = tr("Intensity"); |
| 814 |
// save the plot settings |
| 815 |
settings.beginGroup("spectra/vcd"); |
| 816 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 817 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 818 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 819 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 820 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 821 |
settings.endGroup(); |
| 822 |
break; |
| 823 |
case SpectraType::MagneticCD: |
| 824 |
windowName = tr("Magnetic Circular Dichroism"); |
| 825 |
xTitle = tr("Wavenumbers (cm⁻¹)"); |
| 826 |
yTitle = tr("Intensity"); |
| 827 |
// save the plot settings |
| 828 |
settings.beginGroup("spectra/mcd"); |
| 829 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 830 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 831 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 832 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 833 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 834 |
settings.endGroup(); |
| 835 |
break; |
| 836 |
case SpectraType::NMR: |
| 837 |
windowName = tr("NMR Spectra"); |
| 838 |
xTitle = tr("Chemical Shift (ppm)"); |
| 839 |
yTitle = tr("Intensity"); |
| 840 |
// save the plot settings on a per element basis |
| 841 |
settings.beginGroup(QString("spectra/nmr/%1") |
| 842 |
.arg(m_ui->elementCombo->currentData().toInt())); |
| 843 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 844 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 845 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 846 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 847 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 848 |
settings.endGroup(); |
| 849 |
break; |
| 850 |
case SpectraType::Electronic: |
| 851 |
windowName = tr("Electronic Spectra"); |
| 852 |
xTitle = tr("Energy (eV)"); |
| 853 |
yTitle = tr("Intensity"); |
| 854 |
// save settings |
| 855 |
settings.beginGroup("spectra/electronic"); |
| 856 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 857 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 858 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 859 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 860 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 861 |
settings.endGroup(); |
| 862 |
break; |
| 863 |
case SpectraType::CircularDichroism: |
| 864 |
windowName = tr("Circular Dichroism Spectra"); |
| 865 |
xTitle = tr("Energy (eV)"); |
| 866 |
yTitle = tr("Intensity"); |
| 867 |
// save settings |
| 868 |
settings.beginGroup("spectra/cd"); |
| 869 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 870 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 871 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 872 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 873 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 874 |
settings.endGroup(); |
| 875 |
break; |
| 876 |
case SpectraType::DensityOfStates: |
| 877 |
windowName = tr("Density of States"); |
| 878 |
xTitle = tr("Energy (eV)"); |
| 879 |
yTitle = tr("Density"); |
| 880 |
// save settings |
| 881 |
settings.beginGroup("spectra/dos"); |
| 882 |
settings.setValue("xmin", m_ui->xAxisMinimum->value()); |
| 883 |
settings.setValue("xmax", m_ui->xAxisMaximum->value()); |
| 884 |
settings.setValue("fwhm", m_ui->peakWidth->value()); |
| 885 |
settings.setValue("scale", m_ui->scaleSpinBox->value()); |
| 886 |
settings.setValue("offset", m_ui->offsetSpinBox->value()); |
| 887 |
settings.endGroup(); |
| 888 |
break; |
| 889 |
} |
| 890 |
setWindowTitle(windowName); |
| 891 |
|
| 892 |
double maxIntensity = 0.0f; |
| 893 |
for (auto intensity : m_intensities) { |
| 894 |
if (intensity > maxIntensity) |
| 895 |
maxIntensity = intensity; |
| 896 |
} |
| 897 |
|
| 898 |
// if transmission for IR, set the max intensity to 100 |
| 899 |
if (type == SpectraType::Infrared) |
| 900 |
maxIntensity = 100.0; |
| 901 |
|
| 902 |
if (maxIntensity < 1.0) |
| 903 |
maxIntensity = 1.0; |
| 904 |
|
| 905 |
// now compose the plot data |
| 906 |
float scale = m_ui->scaleSpinBox->value(); |
| 907 |
float offset = m_ui->offsetSpinBox->value(); |
| 908 |
// NMR offsets should be inverted |
| 909 |
if (type == SpectraType::NMR) { |
| 910 |
scale = -scale; |
| 911 |
} |
| 912 |
|
| 913 |
float fwhm = m_ui->peakWidth->value(); |
| 914 |
|
| 915 |
float xMin = m_ui->xAxisMinimum->value(); |
| 916 |
float xMax = m_ui->xAxisMaximum->value(); |
| 917 |
|
| 918 |
float start = std::min(xMin, xMax); |
| 919 |
float end = std::max(xMin, xMax); |
| 920 |
// for some spectra, we need to take small steps, so we scale the x axis |
| 921 |
float xScale = 1.0; |
| 922 |
if (type == SpectraType::Electronic || type == SpectraType::CircularDichroism) |
| 923 |
xScale = 1.0f / 0.01f; |
| 924 |
else if (type == SpectraType::NMR) |
| 925 |
xScale = 1.0f / 0.01f; |
| 926 |
|
| 927 |
// TODO: process an experimental spectrum via interpolation |
| 928 |
for (unsigned int x = round(start * xScale); x < round(end * xScale); ++x) { |
| 929 |
float xValue = static_cast<float>(x) / xScale; |
| 930 |
xData.push_back(xValue); |
| 931 |
yData.push_back(0.0f); |
| 932 |
yStick.push_back(0.0f); |
| 933 |
|
| 934 |
// now we add up the intensity from any frequency |
| 935 |
for (auto index = 0; index < m_transitions.size(); ++index) { |
| 936 |
float freq = m_transitions[index]; |
| 937 |
float peak = m_intensities[index]; |
| 938 |
|
| 939 |
float intensity = scaleAndBlur(xValue, freq, peak, scale, offset, fwhm); |
| 940 |
float stick = closestTo(xValue, freq, peak, scale, offset, xScale); |
| 941 |
|
| 942 |
yData.back() += intensity; |
| 943 |
yStick.back() += stick; |
| 944 |
} |
| 945 |
// if transmission, we need to invert the intensity |
| 946 |
if (transmission) { |
| 947 |
float trans = 1.0f - (yData.back() / (maxIntensity * 1.25)); |
| 948 |
yData.back() = trans * 100.0; // percent |
| 949 |
trans = 1.0f - (yStick.back() / maxIntensity); |
| 950 |
yStick.back() = trans * 100.0; // percent |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
// Interpolate imported data if available |
| 955 |
if (m_importedSpectra.rows() > 0) { |
| 956 |
importedData = interpolateData(m_importedSpectra, xData); |
| 957 |
} |
| 958 |
|
| 959 |
auto* chart = chartWidget(); |
| 960 |
chart->clearPlots(); |
| 961 |
chart->setXAxisTitle(xTitle); |
| 962 |
chart->setYAxisTitle(yTitle); |
| 963 |
unsigned int fontSize = m_ui->fontSizeCombo->currentText().toInt(); |
| 964 |
chart->setFontSize(fontSize); |
| 965 |
float lineWidth = m_ui->lineWidthSpinBox->value(); |
| 966 |
chart->setLineWidth(lineWidth); |
| 967 |
// background color |
| 968 |
QColor backgroundColor = |
| 969 |
settings.value("spectra/backgroundColor", defaultBackgroundColor()) |
| 970 |
.value<QColor>(); |
| 971 |
QtGui::color4ub ubColor = { |
| 972 |
static_cast<unsigned char>(backgroundColor.red()), |
| 973 |
static_cast<unsigned char>(backgroundColor.green()), |
| 974 |
static_cast<unsigned char>(backgroundColor.blue()), |
| 975 |
static_cast<unsigned char>(backgroundColor.alpha()) |
| 976 |
}; |
| 977 |
chart->setBackgroundColor(ubColor); |
| 978 |
// axis color |
| 979 |
QColor axisColor = |
| 980 |
settings.value("spectra/foregroundColor", defaultForegroundColor()) |
| 981 |
.value<QColor>(); |
| 982 |
QtGui::color4ub axisColor4ub = { |
| 983 |
static_cast<unsigned char>(axisColor.red()), |
| 984 |
static_cast<unsigned char>(axisColor.green()), |
| 985 |
static_cast<unsigned char>(axisColor.blue()), |
| 986 |
static_cast<unsigned char>(axisColor.alpha()) |
| 987 |
}; |
| 988 |
chart->setAxisColor(QtGui::ChartWidget::Axis::x, axisColor4ub); |
| 989 |
chart->setAxisColor(QtGui::ChartWidget::Axis::y, axisColor4ub); |
| 990 |
|
| 991 |
// get the spectra color |
| 992 |
QColor spectraColor = |
| 993 |
settings.value("spectra/calculatedColor", defaultCalculatedColor()) |
| 994 |
.value<QColor>(); |
| 995 |
QtGui::color4ub calculatedColor = { |
| 996 |
static_cast<unsigned char>(spectraColor.red()), |
| 997 |
static_cast<unsigned char>(spectraColor.green()), |
| 998 |
static_cast<unsigned char>(spectraColor.blue()), |
| 999 |
static_cast<unsigned char>(spectraColor.alpha()) |
| 1000 |
}; |
| 1001 |
chart->addPlot(xData, yData, calculatedColor, xTitle, tr("Smoothed")); |
| 1002 |
// todo add hide/show raw data series |
| 1003 |
QColor rawColor = settings.value("spectra/rawColor", red).value<QColor>(); |
| 1004 |
QtGui::color4ub rawColor4ub = { static_cast<unsigned char>(rawColor.red()), |
| 1005 |
static_cast<unsigned char>(rawColor.green()), |
| 1006 |
static_cast<unsigned char>(rawColor.blue()), |
| 1007 |
static_cast<unsigned char>( |
| 1008 |
rawColor.alpha()) }; |
| 1009 |
if (m_ui->cb_raw->isChecked()) |
| 1010 |
chart->addSeries(yStick, rawColor4ub, tr("Raw")); |
| 1011 |
|
| 1012 |
QColor importedColor = |
| 1013 |
settings.value("spectra/importedColor", blue).value<QColor>(); |
| 1014 |
QtGui::color4ub importedColor4ub = { |
| 1015 |
static_cast<unsigned char>(importedColor.red()), |
| 1016 |
static_cast<unsigned char>(importedColor.green()), |
| 1017 |
static_cast<unsigned char>(importedColor.blue()), |
| 1018 |
static_cast<unsigned char>(importedColor.alpha()) |
| 1019 |
}; |
| 1020 |
if (m_ui->cb_import->isChecked() && !importedData.empty()) |
| 1021 |
chart->addSeries(importedData, importedColor4ub, tr("Imported")); |
| 1022 |
|
| 1023 |
// axis limits |
| 1024 |
float xAxisMin = m_ui->xAxisMinimum->value(); |
| 1025 |
float xAxisMax = m_ui->xAxisMaximum->value(); |
| 1026 |
float yAxisMin = m_ui->yAxisMinimum->value(); |
| 1027 |
float yAxisMax = m_ui->yAxisMaximum->value(); |
| 1028 |
|
| 1029 |
chart->setXAxisLimits(xAxisMin, xAxisMax); |
| 1030 |
if (type == SpectraType::MagneticCD) |
| 1031 |
chart->setYAxisLimits(-1.0e-4, 1.0e-4); |
| 1032 |
else |
| 1033 |
chart->setYAxisLimits(yAxisMin, yAxisMax); |
| 1034 |
|
| 1035 |
chart->setAxisDigits(QtGui::ChartWidget::Axis::x, 4); |
| 1036 |
|
| 1037 |
// set the location if needed |
| 1038 |
if (type == SpectraType::Infrared) { |
| 1039 |
chart->setLegendLocation(QtGui::ChartWidget::LegendLocation::BottomRight); |
| 1040 |
} |
| 1041 |
|
| 1042 |
// re-enable the options |
| 1043 |
connectOptions(); |
| 1044 |
raise(); |
| 1045 |
activateWindow(); |
| 1046 |
} |
| 1047 |
|
| 1048 |
QtGui::ChartWidget* SpectraDialog::chartWidget() |
| 1049 |
{ |
| 1050 |
return m_ui->plot; |
| 1051 |
} |
| 1052 |
|
| 1053 |
void SpectraDialog::importData() |
| 1054 |
{ |
| 1055 |
// get the filename to import |
| 1056 |
QString filename = QFileDialog::getOpenFileName( |
| 1057 |
this, tr("Import Data"), "", tr("CSV Files (*.csv);;TSV Files (*.tsv)")); |
| 1058 |
|
| 1059 |
QFile file(filename); |
| 1060 |
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 1061 |
return; |
| 1062 |
|
| 1063 |
std::vector<std::pair<double, double>> tempData; |
| 1064 |
|
| 1065 |
QTextStream in(&file); |
| 1066 |
// figure out if this is CSV or TSV data |
| 1067 |
QString delimiter = filename.endsWith(".tsv") ? "\t" : ","; |
| 1068 |
|
| 1069 |
// Skip header if present (detect by checking if first line contains numbers) |
| 1070 |
QString firstLine = in.readLine(); |
| 1071 |
|
| 1072 |
// Try to parse first line as numbers; if it fails, assume header |
| 1073 |
QStringList testParts = firstLine.split(delimiter); |
| 1074 |
bool hasHeader = false; |
| 1075 |
if (testParts.size() >= 2) { |
| 1076 |
bool ok1, ok2; |
| 1077 |
testParts[0].trimmed().toDouble(&ok1); |
| 1078 |
testParts[1].trimmed().toDouble(&ok2); |
| 1079 |
hasHeader = !(ok1 && ok2); |
| 1080 |
} |
| 1081 |
if (hasHeader && !in.atEnd()) |
| 1082 |
// skip the header |
| 1083 |
firstLine = in.readLine(); |
| 1084 |
|
| 1085 |
while (!in.atEnd() && !firstLine.isEmpty()) { |
| 1086 |
QStringList parts = firstLine.split(delimiter); |
| 1087 |
if (parts.size() >= 2) { |
| 1088 |
bool ok1, ok2; |
| 1089 |
double x = parts[0].trimmed().toDouble(&ok1); |
| 1090 |
double y = parts[1].trimmed().toDouble(&ok2); |
| 1091 |
|
| 1092 |
if (ok1 && ok2) { |
| 1093 |
tempData.push_back({ x, y }); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
firstLine = in.readLine(); |
| 1097 |
} |
| 1098 |
|
| 1099 |
file.close(); |
| 1100 |
|
| 1101 |
if (tempData.empty()) |
| 1102 |
return; |
| 1103 |
|
| 1104 |
// Sort by x-axis (energy/wavelength) |
| 1105 |
std::sort(tempData.begin(), tempData.end(), |
| 1106 |
[](const auto& a, const auto& b) { return a.first < b.first; }); |
| 1107 |
|
| 1108 |
// Convert to Eigen::MatrixXd (matching computed spectra format) |
| 1109 |
m_importedSpectra.resize(tempData.size(), 2); |
| 1110 |
for (size_t i = 0; i < tempData.size(); ++i) { |
| 1111 |
m_importedSpectra(i, 0) = tempData[i].first; // energy/wavelength |
| 1112 |
m_importedSpectra(i, 1) = tempData[i].second; // intensity |
| 1113 |
} |
| 1114 |
|
| 1115 |
// enable the imported data series |
| 1116 |
m_ui->cb_import->setEnabled(true); |
| 1117 |
m_ui->cb_import->setChecked(true); |
| 1118 |
|
| 1119 |
return; |
| 1120 |
} |
| 1121 |
|
| 1122 |
void SpectraDialog::toggleOptions() |
| 1123 |
{ |
| 1124 |
if (m_ui->tab_widget->isHidden()) { |
| 1125 |
m_ui->tab_widget->show(); |
| 1126 |
// todo: show the data table |
| 1127 |
// m_ui->dataTable->show(); |
| 1128 |
// m_ui->push_exportData->show(); |
| 1129 |
QSize s = size(); |
| 1130 |
s.setWidth(s.width() + m_ui->dataTable->size().width()); |
| 1131 |
s.setHeight(s.height() + m_ui->tab_widget->size().height()); |
| 1132 |
QRect rect = QGuiApplication::primaryScreen()->geometry(); |
| 1133 |
if (s.width() > rect.width() || s.height() > rect.height()) |
| 1134 |
s = rect.size() * 0.9; |
| 1135 |
resize(s); |
| 1136 |
move(rect.width() / 2 - s.width() / 2, rect.height() / 2 - s.height() / 2); |
| 1137 |
} else { |
| 1138 |
QSize s = size(); |
| 1139 |
s.setWidth(s.width() - m_ui->dataTable->size().width()); |
| 1140 |
s.setHeight(s.height() - m_ui->tab_widget->size().height()); |
| 1141 |
resize(s); |
| 1142 |
m_ui->tab_widget->hide(); |
| 1143 |
m_ui->dataTable->hide(); |
| 1144 |
m_ui->push_exportData->hide(); |
| 1145 |
QRect rect = QGuiApplication::primaryScreen()->geometry(); |
| 1146 |
move(rect.width() / 2 - s.width() / 2, rect.height() / 2 - s.height() / 2); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
} // namespace Avogadro::QtPlugins |