structfact.cpp 60.6 KB
Newer Older
1
2
3
4
5
/**
 * structure factor tool
 * @author Tobias Weber <tweber@ill.fr>
 * @date Dec-2018
 * @license GPLv3, see 'LICENSE' file
Tobias WEBER's avatar
Tobias WEBER committed
6
 * @desc The present version was forked on 28-Dec-2018 from my privately developed "misc" project (https://github.com/t-weber/misc).
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * ----------------------------------------------------------------------------
 * mag-core (part of the Takin software suite)
 * Copyright (C) 2018-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 * "misc" project
 * Copyright (C) 2017-2021  Tobias WEBER (privately developed).
 *
 * 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, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------------
27
28
29
30
 */

#include "structfact.h"

Tobias WEBER's avatar
Tobias WEBER committed
31
#include <QtCore/QDir>
32
33
34
35
36
37
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
Tobias WEBER's avatar
Tobias WEBER committed
38
39
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
40
41

#include <iostream>
Tobias WEBER's avatar
Tobias WEBER committed
42
#include <fstream>
Tobias WEBER's avatar
Tobias WEBER committed
43
44
#include <random>
#include <chrono>
Tobias WEBER's avatar
Tobias WEBER committed
45
#include <tuple>
46
47
48

#include <boost/version.hpp>
#include <boost/config.hpp>
Tobias WEBER's avatar
Tobias WEBER committed
49
50
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
Tobias WEBER's avatar
Tobias WEBER committed
51
#include <boost/algorithm/string.hpp>
52
53
#include <boost/algorithm/string/replace.hpp>
namespace algo = boost::algorithm;
Tobias WEBER's avatar
Tobias WEBER committed
54
namespace pt = boost::property_tree;
55

Tobias WEBER's avatar
Tobias WEBER committed
56
#include "loadcif.h"
57
58
#include "tlibs2/libs/algos.h"
#include "tlibs2/libs/helper.h"
59
60


Tobias WEBER's avatar
Tobias WEBER committed
61
using namespace tl2_ops;
62
63


Tobias WEBER's avatar
Tobias WEBER committed
64
65
66
constexpr t_real g_eps = 1e-6;
constexpr int g_prec = 6;

67
68
69
70
71
std::vector<std::string> StructFactDlg::g_default_colours
{{
	"#ff0000", "#0000ff", "#00ff00", "#000000",
}};

Tobias WEBER's avatar
Tobias WEBER committed
72

73
74
75
enum : int
{
	COL_NAME = 0,
Tobias WEBER's avatar
Tobias WEBER committed
76
77
78
	COL_SCATLEN_RE,
	COL_SCATLEN_IM,
	COL_X, COL_Y, COL_Z,
Tobias WEBER's avatar
Tobias WEBER committed
79
	COL_RAD,
Tobias WEBER's avatar
Tobias WEBER committed
80
	COL_COL,
Tobias WEBER's avatar
Tobias WEBER committed
81
82

	NUM_COLS
83
84
85
86
87
};


struct PowderLine
{
88
89
90
	t_real Q{};
	t_real I{};
	std::size_t num_peaks = 0;
91
92
93
94
95
96
	std::string peaks;
};


// ----------------------------------------------------------------------------
StructFactDlg::StructFactDlg(QWidget* pParent) : QDialog{pParent},
Tobias WEBER's avatar
Tobias WEBER committed
97
	m_sett{new QSettings{"takin", "structfact"}}
98
{
99
	setWindowTitle("Nuclear Structure Factors");
100
101
102
	setSizeGripEnabled(true);
	setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));

Tobias WEBER's avatar
Tobias WEBER committed
103

104
	auto tabs = new QTabWidget(this);
105

Tobias WEBER's avatar
Tobias WEBER committed
106
107
108
109
110

	{ // nuclei panel
		QWidget *nucleipanel = new QWidget(this);

		m_nuclei = new QTableWidget(nucleipanel);
111
112
113
114
115
116
117
118
119
120
		m_nuclei->setShowGrid(true);
		m_nuclei->setSortingEnabled(true);
		m_nuclei->setMouseTracking(true);
		m_nuclei->setSelectionBehavior(QTableWidget::SelectRows);
		m_nuclei->setSelectionMode(QTableWidget::ContiguousSelection);
		m_nuclei->setContextMenuPolicy(Qt::CustomContextMenu);

		m_nuclei->verticalHeader()->setDefaultSectionSize(fontMetrics().lineSpacing() + 4);
		m_nuclei->verticalHeader()->setVisible(false);

Tobias WEBER's avatar
Tobias WEBER committed
121
		m_nuclei->setColumnCount(NUM_COLS);
122
123
124
125
126
127
		m_nuclei->setHorizontalHeaderItem(COL_NAME, new QTableWidgetItem{"Name"});
		m_nuclei->setHorizontalHeaderItem(COL_SCATLEN_RE, new QTableWidgetItem{"Re{b} (fm)"});
		m_nuclei->setHorizontalHeaderItem(COL_SCATLEN_IM, new QTableWidgetItem{"Im{b} (fm)"});
		m_nuclei->setHorizontalHeaderItem(COL_X, new QTableWidgetItem{"x (frac.)"});
		m_nuclei->setHorizontalHeaderItem(COL_Y, new QTableWidgetItem{"y (frac.)"});
		m_nuclei->setHorizontalHeaderItem(COL_Z, new QTableWidgetItem{"z (frac.)"});
Tobias WEBER's avatar
Tobias WEBER committed
128
		m_nuclei->setHorizontalHeaderItem(COL_RAD, new QTableWidgetItem{"Radius"});
Tobias WEBER's avatar
Tobias WEBER committed
129
		m_nuclei->setHorizontalHeaderItem(COL_COL, new QTableWidgetItem{"Colour"});
130

131
		m_nuclei->setColumnWidth(COL_NAME, 90);
132
133
134
135
136
		m_nuclei->setColumnWidth(COL_SCATLEN_RE, 75);
		m_nuclei->setColumnWidth(COL_SCATLEN_IM, 75);
		m_nuclei->setColumnWidth(COL_X, 75);
		m_nuclei->setColumnWidth(COL_Y, 75);
		m_nuclei->setColumnWidth(COL_Z, 75);
Tobias WEBER's avatar
Tobias WEBER committed
137
		m_nuclei->setColumnWidth(COL_RAD, 75);
Tobias WEBER's avatar
Tobias WEBER committed
138
		m_nuclei->setColumnWidth(COL_COL, 75);
139

Tobias WEBER's avatar
Tobias WEBER committed
140
141
142
143
144
		QToolButton *pTabBtnAdd = new QToolButton(nucleipanel);
		QToolButton *pTabBtnDel = new QToolButton(nucleipanel);
		QToolButton *pTabBtnUp = new QToolButton(nucleipanel);
		QToolButton *pTabBtnDown = new QToolButton(nucleipanel);
		QToolButton *pTabBtnSG = new QToolButton(nucleipanel);
145
146
147
148
149
150

		m_nuclei->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Expanding});
		pTabBtnAdd->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
		pTabBtnDel->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
		pTabBtnUp->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
		pTabBtnDown->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
Tobias WEBER's avatar
Tobias WEBER committed
151
		pTabBtnSG->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
152
153
154
155
156

		pTabBtnAdd->setText("Add Nucleus");
		pTabBtnDel->setText("Delete Nuclei");
		pTabBtnUp->setText("Move Nuclei Up");
		pTabBtnDown->setText("Move Nuclei Down");
Tobias WEBER's avatar
Tobias WEBER committed
157
		pTabBtnSG->setText("Generate");
158

Tobias WEBER's avatar
Tobias WEBER committed
159
160
161
162
163
164
		m_editA = new QLineEdit("5", nucleipanel);
		m_editB = new QLineEdit("5", nucleipanel);
		m_editC = new QLineEdit("5", nucleipanel);
		m_editAlpha = new QLineEdit("90", nucleipanel);
		m_editBeta = new QLineEdit("90", nucleipanel);
		m_editGamma = new QLineEdit("90", nucleipanel);
165

Tobias WEBER's avatar
Tobias WEBER committed
166
		m_comboSG = new QComboBox(nucleipanel);
Tobias WEBER's avatar
Tobias WEBER committed
167
168
169
170
171
172


		// get space groups and symops
		for(auto [sgnum, descr, ops] : get_sgs<t_mat>())
		{
			m_comboSG->addItem(descr.c_str(), m_comboSG->count());
173
			m_SGops.emplace_back(std::move(ops));
Tobias WEBER's avatar
Tobias WEBER committed
174
175
		}

176

Tobias WEBER's avatar
Tobias WEBER committed
177
		auto pTabGrid = new QGridLayout(nucleipanel);
178
179
		pTabGrid->setSpacing(2);
		pTabGrid->setContentsMargins(4,4,4,4);
180
		int y=0;
181
182
		//pTabGrid->addWidget(m_plot.get(), y,0,1,4);
		pTabGrid->addWidget(m_nuclei, y,0,1,4);
183
184
185
186
		pTabGrid->addWidget(pTabBtnAdd, ++y,0,1,1);
		pTabGrid->addWidget(pTabBtnDel, y,1,1,1);
		pTabGrid->addWidget(pTabBtnUp, y,2,1,1);
		pTabGrid->addWidget(pTabBtnDown, y,3,1,1);
187

Tobias WEBER's avatar
Tobias WEBER committed
188
189
190
191
192
		pTabGrid->addWidget(new QLabel("Space Groups:"), ++y,0,1,1);
		pTabGrid->addWidget(m_comboSG, y,1,1,2);
		pTabGrid->addWidget(pTabBtnSG, y,3,1,1);


Tobias WEBER's avatar
Tobias WEBER committed
193
		auto sep1 = new QFrame(nucleipanel); sep1->setFrameStyle(QFrame::HLine);
194
		pTabGrid->addWidget(sep1, ++y,0, 1,4);
195

196
197
		pTabGrid->addWidget(new QLabel("Lattice (A):"), ++y,0,1,1);
		pTabGrid->addWidget(m_editA, y,1,1,1);
198
199
200
201
202
203
		pTabGrid->addWidget(m_editB, y,2,1,1);
		pTabGrid->addWidget(m_editC, y,3,1,1);
		pTabGrid->addWidget(new QLabel("Angles (deg):"), ++y,0,1,1);
		pTabGrid->addWidget(m_editAlpha, y,1,1,1);
		pTabGrid->addWidget(m_editBeta, y,2,1,1);
		pTabGrid->addWidget(m_editGamma, y,3,1,1);
204
205


206
		// table CustomContextMenu
207
208
209
		m_pTabContextMenu = new QMenu(m_nuclei);
		m_pTabContextMenu->addAction("Add Nucleus Before", this, [this]() { this->AddTabItem(-2); });
		m_pTabContextMenu->addAction("Add Nucleus After", this, [this]() { this->AddTabItem(-3); });
210
		m_pTabContextMenu->addAction("Clone Nucleus", this, [this]() { this->AddTabItem(-4); });
211
		m_pTabContextMenu->addAction("Delete Nucleus", this, [this]() { StructFactDlg::DelTabItem(); });
212
213


214
215
216
		// table CustomContextMenu in case nothing is selected
		m_pTabContextMenuNoItem = new QMenu(m_nuclei);
		m_pTabContextMenuNoItem->addAction("Add Nucleus", this, [this]() { this->AddTabItem(); });
217
		m_pTabContextMenuNoItem->addAction("Delete Nucleus", this, [this]() { StructFactDlg::DelTabItem(); });
218
219
220
		//m_pTabContextMenuNoItem->addSeparator();


221
		// signals
Tobias WEBER's avatar
Tobias WEBER committed
222
		for(auto* edit : std::vector<QLineEdit*>{{ m_editA, m_editB, m_editC, m_editAlpha, m_editBeta, m_editGamma }})
Tobias WEBER's avatar
Tobias WEBER committed
223
			connect(edit, &QLineEdit::textEdited, this, [this]() { this->CalcB(); });
Tobias WEBER's avatar
Tobias WEBER committed
224
225

		connect(pTabBtnAdd, &QToolButton::clicked, this, [this]() { this->AddTabItem(-1); });
226
		connect(pTabBtnDel, &QToolButton::clicked, this, [this]() { StructFactDlg::DelTabItem(); });
227
228
		connect(pTabBtnUp, &QToolButton::clicked, this, &StructFactDlg::MoveTabItemUp);
		connect(pTabBtnDown, &QToolButton::clicked, this, &StructFactDlg::MoveTabItemDown);
Tobias WEBER's avatar
Tobias WEBER committed
229
		connect(pTabBtnSG, &QToolButton::clicked, this, &StructFactDlg::GenerateFromSG);
230

231
232
233
234
		connect(m_nuclei, &QTableWidget::currentCellChanged, this, &StructFactDlg::TableCurCellChanged);
		connect(m_nuclei, &QTableWidget::entered, this, &StructFactDlg::TableCellEntered);
		connect(m_nuclei, &QTableWidget::itemChanged, this, &StructFactDlg::TableItemChanged);
		connect(m_nuclei, &QTableWidget::customContextMenuRequested, this, &StructFactDlg::ShowTableContextMenu);
Tobias WEBER's avatar
Tobias WEBER committed
235

Tobias WEBER's avatar
Tobias WEBER committed
236
		tabs->addTab(nucleipanel, "Nuclei");
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	}


	{	// structure factors panel
		auto sfactpanel = new QWidget(this);
		auto pGrid = new QGridLayout(sfactpanel);
		pGrid->setSpacing(4);
		pGrid->setContentsMargins(4,4,4,4);

		m_structfacts = new QPlainTextEdit(sfactpanel);
		m_structfacts->setReadOnly(true);
		m_structfacts->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

		m_maxBZ = new QSpinBox(sfactpanel);
		m_maxBZ->setMinimum(0);
		m_maxBZ->setMaximum(99);
		m_maxBZ->setValue(4);

255
256
257
258
		m_RemoveZeroes = new QCheckBox("Remove Zeroes", sfactpanel);
		m_RemoveZeroes->setChecked(true);


259
		pGrid->addWidget(m_structfacts, 0,0, 1,4);
260
		pGrid->addWidget(new QLabel("Max. Order:"), 1,0,1,1);
261
		pGrid->addWidget(m_maxBZ, 1,1, 1,1);
262
263
		pGrid->addWidget(m_RemoveZeroes, 1,2, 1,2);

264
265
266

		// signals
		connect(m_maxBZ, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [this]() { this->Calc(); });
267
		connect(m_RemoveZeroes, static_cast<void (QCheckBox::*)(int)>(&QCheckBox::stateChanged), this, [this]() { this->Calc(); });
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

		tabs->addTab(sfactpanel, "Structure Factors");
	}


	{	// powder lines panel
		auto powderpanel = new QWidget(this);
		auto pGrid = new QGridLayout(powderpanel);
		pGrid->setSpacing(4);
		pGrid->setContentsMargins(4,4,4,4);

		m_powderlines = new QPlainTextEdit(powderpanel);
		m_powderlines->setReadOnly(true);
		m_powderlines->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
		m_powderlines->setLineWrapMode(QPlainTextEdit::NoWrap);

		pGrid->addWidget(m_powderlines, 0,0, 1,4);

		tabs->addTab(powderpanel, "Powder Lines");
	}


Tobias WEBER's avatar
Tobias WEBER committed
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
	{ // find space group panel
		auto findsgpanel = new QWidget(this);

		m_nuclei_FindSG = new QTableWidget(findsgpanel);
		m_nuclei_FindSG->setShowGrid(true);
		m_nuclei_FindSG->setSortingEnabled(true);
		m_nuclei_FindSG->setMouseTracking(true);
		m_nuclei_FindSG->setSelectionBehavior(QTableWidget::SelectRows);
		m_nuclei_FindSG->setSelectionMode(QTableWidget::ContiguousSelection);
		m_nuclei_FindSG->setContextMenuPolicy(Qt::CustomContextMenu);

		m_nuclei_FindSG->verticalHeader()->setDefaultSectionSize(fontMetrics().lineSpacing() + 4);
		m_nuclei_FindSG->verticalHeader()->setVisible(false);

		m_nuclei_FindSG->setColumnCount(3);
		m_nuclei_FindSG->setHorizontalHeaderItem(0, new QTableWidgetItem{"x (frac.)"});
		m_nuclei_FindSG->setHorizontalHeaderItem(1, new QTableWidgetItem{"y (frac.)"});
		m_nuclei_FindSG->setHorizontalHeaderItem(2, new QTableWidgetItem{"z (frac.)"});

		m_nuclei_FindSG->setColumnWidth(0, 200);
		m_nuclei_FindSG->setColumnWidth(1, 200);
		m_nuclei_FindSG->setColumnWidth(2, 200);

		m_sgmatches = new QPlainTextEdit(findsgpanel);
		m_sgmatches->setReadOnly(true);
		m_sgmatches->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));


		QToolButton *pTabBtnAdd = new QToolButton(findsgpanel);
		QToolButton *pTabBtnDel = new QToolButton(findsgpanel);
		QToolButton *pTabBtnUp = new QToolButton(findsgpanel);
		QToolButton *pTabBtnDown = new QToolButton(findsgpanel);
322
		QToolButton *pBtnCalc = new QToolButton(findsgpanel);
Tobias WEBER's avatar
Tobias WEBER committed
323
324
325
326
327
328

		m_nuclei_FindSG->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Expanding});
		pTabBtnAdd->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
		pTabBtnDel->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
		pTabBtnUp->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
		pTabBtnDown->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
329
		pBtnCalc->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});
Tobias WEBER's avatar
Tobias WEBER committed
330
331
332
333
334

		pTabBtnAdd->setText("Add Nucleus");
		pTabBtnDel->setText("Delete Nuclei");
		pTabBtnUp->setText("Move Nuclei Up");
		pTabBtnDown->setText("Move Nuclei Down");
335
		pBtnCalc->setText("Find Matching Space Groups");
Tobias WEBER's avatar
Tobias WEBER committed
336
337
338
339
340
341
342
343
344
345
346
347


		auto pTabGrid = new QGridLayout(findsgpanel);
		pTabGrid->setSpacing(2);
		pTabGrid->setContentsMargins(4,4,4,4);
		int y=0;
		pTabGrid->addWidget(m_nuclei_FindSG, y,0,1,4);
		pTabGrid->addWidget(m_sgmatches, ++y,0,1,4);
		pTabGrid->addWidget(pTabBtnAdd, ++y,0,1,1);
		pTabGrid->addWidget(pTabBtnDel, y,1,1,1);
		pTabGrid->addWidget(pTabBtnUp, y,2,1,1);
		pTabGrid->addWidget(pTabBtnDown, y,3,1,1);
348
		pTabGrid->addWidget(pBtnCalc, ++y,2,1,2);
Tobias WEBER's avatar
Tobias WEBER committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369


		// table CustomContextMenu
		m_pTabContextMenu_FindSG = new QMenu(m_nuclei_FindSG);
		m_pTabContextMenu_FindSG->addAction("Add Nucleus Before", this, [this]() { this->AddTabItem_FindSG(-2); });
		m_pTabContextMenu_FindSG->addAction("Add Nucleus After", this, [this]() { this->AddTabItem_FindSG(-3); });
		m_pTabContextMenu_FindSG->addAction("Clone Nucleus", this, [this]() { this->AddTabItem_FindSG(-4); });
		m_pTabContextMenu_FindSG->addAction("Delete Nucleus", this, [this]() { StructFactDlg::DelTabItem_FindSG(); });


		// table CustomContextMenu in case nothing is selected
		m_pTabContextMenuNoItem_FindSG = new QMenu(m_nuclei_FindSG);
		m_pTabContextMenuNoItem_FindSG->addAction("Add Nucleus", this, [this]() { this->AddTabItem_FindSG(); });
		m_pTabContextMenuNoItem_FindSG->addAction("Delete Nucleus", this, [this]() { StructFactDlg::DelTabItem_FindSG(); });


		// signals
		connect(pTabBtnAdd, &QToolButton::clicked, this, [this]() { this->AddTabItem_FindSG(-1); });
		connect(pTabBtnDel, &QToolButton::clicked, this, [this]() { StructFactDlg::DelTabItem_FindSG(); });
		connect(pTabBtnUp, &QToolButton::clicked, this, &StructFactDlg::MoveTabItemUp_FindSG);
		connect(pTabBtnDown, &QToolButton::clicked, this, &StructFactDlg::MoveTabItemDown_FindSG);
370
		connect(pBtnCalc, &QToolButton::clicked, this, &StructFactDlg::FindSG);
Tobias WEBER's avatar
Tobias WEBER committed
371
372
373
374
375
376
377
378

		connect(m_nuclei_FindSG, &QTableWidget::itemChanged, this, &StructFactDlg::TableItemChanged_FindSG);
		connect(m_nuclei_FindSG, &QTableWidget::customContextMenuRequested, this, &StructFactDlg::ShowTableContextMenu_FindSG);

		tabs->addTab(findsgpanel, "Match Space Group");
	}


379
380
381
382
383
384
	{	// info panel
		auto infopanel = new QWidget(this);
		auto pGrid = new QGridLayout(infopanel);
		pGrid->setSpacing(4);
		pGrid->setContentsMargins(4,4,4,4);

385
386
387
388
389
390
391
		// table grid
		for(int i=0; i<4; ++i)
		{
			m_labelGlInfos[i] = new QLabel("", infopanel);
			m_labelGlInfos[i]->setSizePolicy(QSizePolicy::Ignored, m_labelGlInfos[i]->sizePolicy().verticalPolicy());
		}

392
393
394
395
396
397
398
		auto sep1 = new QFrame(infopanel); sep1->setFrameStyle(QFrame::HLine);
		auto sep2 = new QFrame(infopanel); sep2->setFrameStyle(QFrame::HLine);
		auto sep3 = new QFrame(infopanel); sep3->setFrameStyle(QFrame::HLine);

		std::string strBoost = BOOST_LIB_VERSION;
		algo::replace_all(strBoost, "_", ".");

399
		auto labelTitle = new QLabel("Nuclear Structure Factor Calculator", infopanel);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
		auto fontTitle = labelTitle->font();
		fontTitle.setBold(true);
		labelTitle->setFont(fontTitle);
		labelTitle->setAlignment(Qt::AlignHCenter);

		auto labelAuthor = new QLabel("Written by Tobias Weber <tweber@ill.fr>.", infopanel);
		labelAuthor->setAlignment(Qt::AlignHCenter);

		auto labelDate = new QLabel("December 2018.", infopanel);
		labelDate->setAlignment(Qt::AlignHCenter);

		int y = 0;
		pGrid->addWidget(labelTitle, y++,0, 1,1);
		pGrid->addWidget(labelAuthor, y++,0, 1,1);
		pGrid->addWidget(labelDate, y++,0, 1,1);
		pGrid->addItem(new QSpacerItem(16,16, QSizePolicy::Minimum, QSizePolicy::Fixed), y++,0, 1,1);
		pGrid->addWidget(sep1, y++,0, 1,1);
		pGrid->addWidget(new QLabel(QString("Compiler: ") + QString(BOOST_COMPILER) + ".", infopanel), y++,0, 1,1);
		pGrid->addWidget(new QLabel(QString("C++ Library: ") + QString(BOOST_STDLIB) + ".", infopanel), y++,0, 1,1);
		pGrid->addWidget(new QLabel(QString("Build Date: ") + QString(__DATE__) + ", " + QString(__TIME__) + ".", infopanel), y++,0, 1,1);
		pGrid->addWidget(sep2, y++,0, 1,1);
		pGrid->addWidget(new QLabel(QString("Qt Version: ") + QString(QT_VERSION_STR) + ".", infopanel), y++,0, 1,1);
		pGrid->addWidget(new QLabel(QString("Boost Version: ") + strBoost.c_str() + ".", infopanel), y++,0, 1,1);
		pGrid->addWidget(sep3, y++,0, 1,1);
424
425
		for(int i=0; i<4; ++i)
			pGrid->addWidget(m_labelGlInfos[i], y++,0, 1,1);
426
427
428
429
430
431
432
433
434
435
436
437
438
		pGrid->addItem(new QSpacerItem(16,16, QSizePolicy::Minimum, QSizePolicy::Expanding), y++,0, 1,1);

		tabs->addTab(infopanel, "Infos");
	}


	// main grid
	auto pmainGrid = new QGridLayout(this);
	pmainGrid->setSpacing(4);
	pmainGrid->setContentsMargins(4,4,4,4);
	pmainGrid->addWidget(tabs, 0,0, 1,1);


Tobias WEBER's avatar
Tobias WEBER committed
439
440
441
442
443
444
445
446
	// menu bar
	{
		m_menu = new QMenuBar(this);
		m_menu->setNativeMenuBar(m_sett ? m_sett->value("native_gui", false).toBool() : false);

		auto menuFile = new QMenu("File", m_menu);
		auto menuView = new QMenu("View", m_menu);

Tobias WEBER's avatar
Tobias WEBER committed
447
		auto acNew = new QAction("New", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
448
449
450
		auto acLoad = new QAction("Load...", menuFile);
		auto acSave = new QAction("Save...", menuFile);
		auto acImportCIF = new QAction("Import CIF...", menuFile);
451
		auto acImportTAZ = new QAction("Import TAZ...", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
452
		auto acExportTAZ = new QAction("Export TAZ...", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
453
		auto acExit = new QAction("Quit", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
454
455
		auto ac3DView = new QAction("3D View...", menuFile);

Tobias WEBER's avatar
Tobias WEBER committed
456
457
		menuFile->addAction(acNew);
		menuFile->addSeparator();
Tobias WEBER's avatar
Tobias WEBER committed
458
459
460
461
		menuFile->addAction(acLoad);
		menuFile->addAction(acSave);
		menuFile->addSeparator();
		menuFile->addAction(acImportCIF);
462
463
		menuFile->addSeparator();
		menuFile->addAction(acImportTAZ);
Tobias WEBER's avatar
Tobias WEBER committed
464
465
466
467
468
		menuFile->addAction(acExportTAZ);
		menuFile->addSeparator();
		menuFile->addAction(acExit);
		menuView->addAction(ac3DView);

Tobias WEBER's avatar
Tobias WEBER committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
		connect(acNew, &QAction::triggered, this,  [this]()
		{
			// clear old table
			DelTabItem(-1);

			// set some defaults
			m_comboSG->setCurrentIndex(0);
			m_editA->setText("5");
			m_editB->setText("5");
			m_editC->setText("5");
			m_editAlpha->setText("90");
			m_editBeta->setText("90");
			m_editGamma->setText("90");
		});
Tobias WEBER's avatar
Tobias WEBER committed
483
484
485
		connect(acLoad, &QAction::triggered, this, &StructFactDlg::Load);
		connect(acSave, &QAction::triggered, this, &StructFactDlg::Save);
		connect(acImportCIF, &QAction::triggered, this, &StructFactDlg::ImportCIF);
486
		connect(acImportTAZ, &QAction::triggered, this, &StructFactDlg::ImportTAZ);
Tobias WEBER's avatar
Tobias WEBER committed
487
488
489
490
491
492
493
494
495
496
		connect(acExportTAZ, &QAction::triggered, this, &StructFactDlg::ExportTAZ);
		connect(acExit, &QAction::triggered, this, &QDialog::close);
		connect(ac3DView, &QAction::triggered, this, [this]()
		{
			// plot widget
			if(!m_dlgPlot)
			{
				m_dlgPlot = new QDialog(this);
				m_dlgPlot->setWindowTitle("Unit Cell - 3D View");

Tobias WEBER's avatar
Tobias WEBER committed
497
				m_plot = std::make_shared<tl2::GlPlot>(this);
Tobias WEBER's avatar
Tobias WEBER committed
498
499
500
501
				m_plot->GetRenderer()->SetLight(0, tl2::create<t_vec3_gl>({ 5, 5, 5 }));
				m_plot->GetRenderer()->SetLight(1, tl2::create<t_vec3_gl>({ -5, -5, -5 }));
				m_plot->GetRenderer()->SetCoordMax(1.);
				m_plot->GetRenderer()->SetCamBase(tl2::create<t_mat_gl>({1,0,0,0,  0,0,1,0,  0,-1,0,-1.5,  0,0,0,1}),
Tobias WEBER's avatar
Tobias WEBER committed
502
					tl2::create<t_vec_gl>({1,0,0,0}), tl2::create<t_vec_gl>({0,0,1,0}));
Tobias WEBER's avatar
Tobias WEBER committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524


				auto labCoordSys = new QLabel("Coordinate System:", /*m_dlgPlot*/ this);
				auto comboCoordSys = new QComboBox(/*m_dlgPlot*/ this);
				m_status3D = new QLabel(/*m_dlgPlot*/ this);

				comboCoordSys->addItem("Fractional Units (rlu)");
				comboCoordSys->addItem("Lab Units (A)");


				m_plot->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Expanding});
				labCoordSys->setSizePolicy(QSizePolicy{QSizePolicy::Fixed, QSizePolicy::Fixed});

				auto grid = new QGridLayout(m_dlgPlot);
				grid->setSpacing(2);
				grid->setContentsMargins(4,4,4,4);
				grid->addWidget(m_plot.get(), 0,0,1,2);
				grid->addWidget(labCoordSys, 1,0,1,1);
				grid->addWidget(comboCoordSys, 1,1,1,1);
				grid->addWidget(m_status3D, 2,0,1,2);


Tobias WEBER's avatar
Tobias WEBER committed
525
526
527
528
				connect(m_plot.get(), &tl2::GlPlot::AfterGLInitialisation, this, &StructFactDlg::AfterGLInitialisation);
				connect(m_plot->GetRenderer(), &tl2::GlPlotRenderer::PickerIntersection, this, &StructFactDlg::PickerIntersection);
				connect(m_plot.get(), &tl2::GlPlot::MouseDown, this, &StructFactDlg::PlotMouseDown);
				connect(m_plot.get(), &tl2::GlPlot::MouseUp, this, &StructFactDlg::PlotMouseUp);
Tobias WEBER's avatar
Tobias WEBER committed
529
530
531
				connect(comboCoordSys, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this](int val)
				{
					if(this->m_plot)
Tobias WEBER's avatar
Tobias WEBER committed
532
						this->m_plot->GetRenderer()->SetCoordSys(val);
Tobias WEBER's avatar
Tobias WEBER committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
				});


				if(m_sett && m_sett->contains("geo_3dview"))
					m_dlgPlot->restoreGeometry(m_sett->value("geo_3dview").toByteArray());
				else
					m_dlgPlot->resize(500,500);
			}

			m_dlgPlot->show();
			m_dlgPlot->raise();
			m_dlgPlot->focusWidget();
		});

		m_menu->addMenu(menuFile);
		m_menu->addMenu(menuView);
		pmainGrid->setMenuBar(m_menu);
	}


Tobias WEBER's avatar
Tobias WEBER committed
553
	// restore window size and position
554
555
556
557
558
559
560
561
562
563
	if(m_sett && m_sett->contains("geo"))
		restoreGeometry(m_sett->value("geo").toByteArray());
	else
		resize(600, 500);


	m_ignoreChanges = 0;
}


Tobias WEBER's avatar
Tobias WEBER committed
564

Tobias WEBER's avatar
Tobias WEBER committed
565
566
567
// ============================================================================
// functions for nuclei tab

Tobias WEBER's avatar
Tobias WEBER committed
568
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
569

570
void StructFactDlg::AddTabItem(int row,
Tobias WEBER's avatar
Tobias WEBER committed
571
	const std::string& name, t_real bRe, t_real bIm, t_real x, t_real y, t_real z, t_real scale, const std::string& col)
572
{
573
	bool bclone = 0;
574
575
576
577
578
579
580
581
	m_ignoreChanges = 1;

	if(row == -1)	// append to end of table
		row = m_nuclei->rowCount();
	else if(row == -2 && m_iCursorRow >= 0)	// use row from member variable
		row = m_iCursorRow;
	else if(row == -3 && m_iCursorRow >= 0)	// use row from member variable +1
		row = m_iCursorRow + 1;
582
583
584
585
586
	else if(row == -4 && m_iCursorRow >= 0)	// use row from member variable +1
	{
		row = m_iCursorRow + 1;
		bclone = 1;
	}
587
588
589
590
591

	//bool sorting = m_nuclei->isSortingEnabled();
	m_nuclei->setSortingEnabled(false);
	m_nuclei->insertRow(row);

592
593
	if(bclone)
	{
Tobias WEBER's avatar
Tobias WEBER committed
594
		for(int thecol=0; thecol<NUM_COLS; ++thecol)
595
596
597
598
			m_nuclei->setItem(row, thecol, m_nuclei->item(m_iCursorRow, thecol)->clone());
	}
	else
	{
Tobias WEBER's avatar
Tobias WEBER committed
599
600
601
602
603
604
		m_nuclei->setItem(row, COL_NAME, new QTableWidgetItem(name.c_str()));
		m_nuclei->setItem(row, COL_SCATLEN_RE, new NumericTableWidgetItem<t_real>(bRe));
		m_nuclei->setItem(row, COL_SCATLEN_IM, new NumericTableWidgetItem<t_real>(bIm));
		m_nuclei->setItem(row, COL_X, new NumericTableWidgetItem<t_real>(x));
		m_nuclei->setItem(row, COL_Y, new NumericTableWidgetItem<t_real>(y));
		m_nuclei->setItem(row, COL_Z, new NumericTableWidgetItem<t_real>(z));
Tobias WEBER's avatar
Tobias WEBER committed
605
		m_nuclei->setItem(row, COL_RAD, new NumericTableWidgetItem<t_real>(scale));
Tobias WEBER's avatar
Tobias WEBER committed
606
		m_nuclei->setItem(row, COL_COL, new QTableWidgetItem(col.c_str()));
607
608
	}

609
	Add3DItem(row);
610
611
612
613
614
615
616
617
618
619
620

	m_nuclei->scrollToItem(m_nuclei->item(row, 0));
	m_nuclei->setCurrentCell(row, 0);

	m_nuclei->setSortingEnabled(/*sorting*/ true);

	m_ignoreChanges = 0;
	Calc();
}


621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
 * add 3d object
 */
void StructFactDlg::Add3DItem(int row)
{
	if(!m_plot) return;

	// add all items
	if(row < 0)
	{
		for(int row=0; row<m_nuclei->rowCount(); ++row)
			Add3DItem(row);
		return;
	}

Tobias WEBER's avatar
Tobias WEBER committed
636
	auto *itemName = m_nuclei->item(row, COL_NAME);
637
638
639
	auto *itemx = m_nuclei->item(row, COL_X);
	auto *itemy = m_nuclei->item(row, COL_Y);
	auto *itemz = m_nuclei->item(row, COL_Z);
Tobias WEBER's avatar
Tobias WEBER committed
640
	auto *itemsc = m_nuclei->item(row, COL_RAD);
Tobias WEBER's avatar
Tobias WEBER committed
641
642
	auto *itemCol = m_nuclei->item(row, COL_COL);

Tobias WEBER's avatar
Tobias WEBER committed
643
	t_real_gl posx=0, posy=0, posz=0, scale=1;
644
645
646
	std::istringstream{itemx->text().toStdString()} >> posx;
	std::istringstream{itemy->text().toStdString()} >> posy;
	std::istringstream{itemz->text().toStdString()} >> posz;
Tobias WEBER's avatar
Tobias WEBER committed
647
	std::istringstream{itemsc->text().toStdString()} >> scale;
648

Tobias WEBER's avatar
Tobias WEBER committed
649
650
651
652
	qreal r=1, g=1, b=1;
	QColor col{itemCol->text()};
	col.getRgbF(&r, &g, &b);

Tobias WEBER's avatar
Tobias WEBER committed
653
654
655
656
	auto obj = m_plot->GetRenderer()->AddLinkedObject(m_sphere, 0,0,0, r,g,b,1);
	//auto obj = m_plot->GetRenderer()->AddSphere(0.05, 0,0,0, r,g,b,1);
	m_plot->GetRenderer()->SetObjectMatrix(obj, tl2::hom_translation<t_mat_gl>(posx, posy, posz)*tl2::hom_scaling<t_mat_gl>(scale,scale,scale));
	m_plot->GetRenderer()->SetObjectLabel(obj, itemName->text().toStdString());
657
658
659
660
661
662
	m_plot->update();

	m_nuclei->item(row, COL_NAME)->setData(Qt::UserRole, unsigned(obj));
}


663
void StructFactDlg::DelTabItem(int begin, int end)
664
665
666
667
{
	m_ignoreChanges = 1;

	// if nothing is selected, clear all items
668
	if(begin == -1 || m_nuclei->selectedItems().count() == 0)
669
	{
670
671
672
673
		if(m_plot)
		{
			for(int row=0; row<m_nuclei->rowCount(); ++row)
				if(std::size_t obj = m_nuclei->item(row, COL_NAME)->data(Qt::UserRole).toUInt(); obj)
Tobias WEBER's avatar
Tobias WEBER committed
674
					m_plot->GetRenderer()->RemoveObject(obj);
675
676
			m_plot->update();
		}
Tobias WEBER's avatar
Tobias WEBER committed
677

678
679
680
		m_nuclei->clearContents();
		m_nuclei->setRowCount(0);
	}
681
	else if(begin == -2)	// clear selected
Tobias WEBER's avatar
Tobias WEBER committed
682
	{
Tobias WEBER's avatar
Tobias WEBER committed
683
684
685
		for(int row : GetSelectedRows(true))
		{
			// remove 3d object
686
687
688
			if(m_plot)
			{
				if(std::size_t obj = m_nuclei->item(row, COL_NAME)->data(Qt::UserRole).toUInt(); obj)
Tobias WEBER's avatar
Tobias WEBER committed
689
					m_plot->GetRenderer()->RemoveObject(obj);
690
691
				m_plot->update();
			}
Tobias WEBER's avatar
Tobias WEBER committed
692

Tobias WEBER's avatar
Tobias WEBER committed
693
694
			m_nuclei->removeRow(row);
		}
Tobias WEBER's avatar
Tobias WEBER committed
695
	}
696
697
698
699
700
701
702
703
	else if(begin >= 0 && end >= 0)		// clear given range
	{
		for(int row=end-1; row>=begin; --row)
		{
			// remove 3d object
			if(m_plot)
			{
				if(std::size_t obj = m_nuclei->item(row, COL_NAME)->data(Qt::UserRole).toUInt(); obj)
Tobias WEBER's avatar
Tobias WEBER committed
704
					m_plot->GetRenderer()->RemoveObject(obj);
705
706
707
708
709
710
				m_plot->update();
			}

			m_nuclei->removeRow(row);
		}
	}
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784

	m_ignoreChanges = 0;
	Calc();
}


void StructFactDlg::MoveTabItemUp()
{
	m_ignoreChanges = 1;
	m_nuclei->setSortingEnabled(false);

	auto selected = GetSelectedRows(false);
	for(int row : selected)
	{
		if(row == 0)
			continue;

		auto *item = m_nuclei->item(row, 0);
		if(!item || !item->isSelected())
			continue;

		m_nuclei->insertRow(row-1);
		for(int col=0; col<m_nuclei->columnCount(); ++col)
			m_nuclei->setItem(row-1, col, m_nuclei->item(row+1, col)->clone());
		m_nuclei->removeRow(row+1);
	}

	for(int row=0; row<m_nuclei->rowCount(); ++row)
	{
		if(auto *item = m_nuclei->item(row, 0);
			item && std::find(selected.begin(), selected.end(), row+1) != selected.end())
		{
			for(int col=0; col<m_nuclei->columnCount(); ++col)
				m_nuclei->item(row, col)->setSelected(true);
		}
	}

	m_ignoreChanges = 0;
}


void StructFactDlg::MoveTabItemDown()
{
	m_ignoreChanges = 1;
	m_nuclei->setSortingEnabled(false);

	auto selected = GetSelectedRows(true);
	for(int row : selected)
	{
		if(row == m_nuclei->rowCount()-1)
			continue;

		auto *item = m_nuclei->item(row, 0);
		if(!item || !item->isSelected())
			continue;

		m_nuclei->insertRow(row+2);
		for(int col=0; col<m_nuclei->columnCount(); ++col)
			m_nuclei->setItem(row+2, col, m_nuclei->item(row, col)->clone());
		m_nuclei->removeRow(row);
	}

	for(int row=0; row<m_nuclei->rowCount(); ++row)
	{
		if(auto *item = m_nuclei->item(row, 0);
			item && std::find(selected.begin(), selected.end(), row-1) != selected.end())
		{
			for(int col=0; col<m_nuclei->columnCount(); ++col)
				m_nuclei->item(row, col)->setSelected(true);
		}
	}

	m_ignoreChanges = 0;
}
Tobias WEBER's avatar
Tobias WEBER committed
785
786
// ----------------------------------------------------------------------------

787
788


789

Tobias WEBER's avatar
Tobias WEBER committed
790
// ----------------------------------------------------------------------------
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
std::vector<int> StructFactDlg::GetSelectedRows(bool sort_reversed) const
{
	std::vector<int> vec;
	vec.reserve(m_nuclei->selectedItems().size());

	for(int row=0; row<m_nuclei->rowCount(); ++row)
	{
		if(auto *item = m_nuclei->item(row, 0); item && item->isSelected())
			vec.push_back(row);
	}

	if(sort_reversed)
	{
		std::stable_sort(vec.begin(), vec.end(), [](int row1, int row2)
		{ return row1 > row2; });
	}

	return vec;
}


/**
 * selected a new row
 */
void StructFactDlg::TableCurCellChanged(int rowNew, int colNew, int rowOld, int colOld)
{
}


/**
 * hovered over new row
 */
void StructFactDlg::TableCellEntered(const QModelIndex& idx)
{
}


/**
 * item contents changed
 */
void StructFactDlg::TableItemChanged(QTableWidgetItem *item)
{
Tobias WEBER's avatar
Tobias WEBER committed
833
	// update associated 3d object
834
	if(item && m_plot)
Tobias WEBER's avatar
Tobias WEBER committed
835
836
837
838
	{
		int row = item->row();
		if(std::size_t obj = m_nuclei->item(row, COL_NAME)->data(Qt::UserRole).toUInt(); obj)
		{
Tobias WEBER's avatar
Tobias WEBER committed
839
			auto *itemName = m_nuclei->item(row, COL_NAME);
Tobias WEBER's avatar
Tobias WEBER committed
840
841
842
			auto *itemx = m_nuclei->item(row, COL_X);
			auto *itemy = m_nuclei->item(row, COL_Y);
			auto *itemz = m_nuclei->item(row, COL_Z);
Tobias WEBER's avatar
Tobias WEBER committed
843
			auto *itemsc = m_nuclei->item(row, COL_RAD);
Tobias WEBER's avatar
Tobias WEBER committed
844
845
			auto *itemCol = m_nuclei->item(row, COL_COL);

Tobias WEBER's avatar
Tobias WEBER committed
846
			t_real_gl posx=0, posy=0, posz=0, scale=1;
Tobias WEBER's avatar
Tobias WEBER committed
847
848
849
			std::istringstream{itemx->text().toStdString()} >> posx;
			std::istringstream{itemy->text().toStdString()} >> posy;
			std::istringstream{itemz->text().toStdString()} >> posz;
Tobias WEBER's avatar
Tobias WEBER committed
850
			std::istringstream{itemsc->text().toStdString()} >> scale;
Tobias WEBER's avatar
Tobias WEBER committed
851

Tobias WEBER's avatar
Tobias WEBER committed
852
853
854
855
			qreal r=1, g=1, b=1;
			QColor col{itemCol->text()};
			col.getRgbF(&r, &g, &b);

Tobias WEBER's avatar
Tobias WEBER committed
856
857
858
			m_plot->GetRenderer()->SetObjectMatrix(obj, tl2::hom_translation<t_mat_gl>(posx, posy, posz)*tl2::hom_scaling<t_mat_gl>(scale,scale,scale));
			m_plot->GetRenderer()->SetObjectCol(obj, r, g, b, 1);
			m_plot->GetRenderer()->SetObjectLabel(obj, itemName->text().toStdString());
Tobias WEBER's avatar
Tobias WEBER committed
859
860
861
862
			m_plot->update();
		}
	}

863
864
865
866
867
868
869
870
	if(!m_ignoreChanges)
		Calc();
}


void StructFactDlg::ShowTableContextMenu(const QPoint& pt)
{
	auto ptGlob = m_nuclei->mapToGlobal(pt);
871
872
873
874
875
876
877
878
879
880
881
882

	if(const auto* item = m_nuclei->itemAt(pt); item)
	{
		m_iCursorRow = item->row();
		ptGlob.setY(ptGlob.y() + m_pTabContextMenu->sizeHint().height()/2);
		m_pTabContextMenu->popup(ptGlob);
	}
	else
	{
		ptGlob.setY(ptGlob.y() + m_pTabContextMenuNoItem->sizeHint().height()/2);
		m_pTabContextMenuNoItem->popup(ptGlob);
	}
883
}
Tobias WEBER's avatar
Tobias WEBER committed
884
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
// ============================================================================




// ============================================================================
// functions for space group finder tab

// ----------------------------------------------------------------------------

void StructFactDlg::AddTabItem_FindSG(int row, t_real x, t_real y, t_real z)
{
	bool bclone = 0;

	if(row == -1)	// append to end of table
		row = m_nuclei_FindSG->rowCount();
	else if(row == -2 && m_iCursorRow_FindSG >= 0)	// use row from member variable
		row = m_iCursorRow_FindSG;
	else if(row == -3 && m_iCursorRow_FindSG >= 0)	// use row from member variable +1
		row = m_iCursorRow_FindSG + 1;
	else if(row == -4 && m_iCursorRow_FindSG >= 0)	// use row from member variable +1
	{
		row = m_iCursorRow_FindSG + 1;
		bclone = 1;
	}

	m_nuclei_FindSG->setSortingEnabled(false);
	m_nuclei_FindSG->insertRow(row);

	if(bclone)
	{
		for(int thecol=0; thecol<3; ++thecol)
			m_nuclei_FindSG->setItem(row, thecol, m_nuclei_FindSG->item(m_iCursorRow_FindSG, thecol)->clone());
	}
	else
	{
		m_nuclei_FindSG->setItem(row, 0, new NumericTableWidgetItem<t_real>(x));
		m_nuclei_FindSG->setItem(row, 1, new NumericTableWidgetItem<t_real>(y));
		m_nuclei_FindSG->setItem(row, 2, new NumericTableWidgetItem<t_real>(z));
	}

	m_nuclei_FindSG->scrollToItem(m_nuclei_FindSG->item(row, 0));
	m_nuclei_FindSG->setCurrentCell(row, 0);
	m_nuclei_FindSG->setSortingEnabled(true);

930
	//FindSG();
Tobias WEBER's avatar
Tobias WEBER committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
}



void StructFactDlg::DelTabItem_FindSG(int begin, int end)
{
	// if nothing is selected, clear all items
	if(begin == -1 || m_nuclei_FindSG->selectedItems().count() == 0)
	{
		m_nuclei_FindSG->clearContents();
		m_nuclei_FindSG->setRowCount(0);
	}
	else if(begin == -2)	// clear selected
	{
		for(int row : GetSelectedRows_FindSG(true))
			m_nuclei_FindSG->removeRow(row);
	}
	else if(begin >= 0 && end >= 0)		// clear given range
	{
		for(int row=end-1; row>=begin; --row)
			m_nuclei_FindSG->removeRow(row);
	}

954
	//FindSG();
Tobias WEBER's avatar
Tobias WEBER committed
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
}


void StructFactDlg::MoveTabItemUp_FindSG()
{
	m_nuclei_FindSG->setSortingEnabled(false);

	auto selected = GetSelectedRows_FindSG(false);
	for(int row : selected)
	{
		if(row == 0)
			continue;

		auto *item = m_nuclei_FindSG->item(row, 0);
		if(!item || !item->isSelected())
			continue;

		m_nuclei_FindSG->insertRow(row-1);
		for(int col=0; col<m_nuclei_FindSG->columnCount(); ++col)
			m_nuclei_FindSG->setItem(row-1, col, m_nuclei_FindSG->item(row+1, col)->clone());
		m_nuclei_FindSG->removeRow(row+1);
	}

	for(int row=0; row<m_nuclei_FindSG->rowCount(); ++row)
	{
		if(auto *item = m_nuclei_FindSG->item(row, 0);
		   item && std::find(selected.begin(), selected.end(), row+1) != selected.end())
		   {
			   for(int col=0; col<m_nuclei_FindSG->columnCount(); ++col)
				   m_nuclei_FindSG->item(row, col)->setSelected(true);
		   }
	}
}


void StructFactDlg::MoveTabItemDown_FindSG()
{
	m_nuclei_FindSG->setSortingEnabled(false);

	auto selected = GetSelectedRows_FindSG(true);
	for(int row : selected)
	{
		if(row == m_nuclei_FindSG->rowCount()-1)
			continue;

		auto *item = m_nuclei_FindSG->item(row, 0);
		if(!item || !item->isSelected())
			continue;

		m_nuclei_FindSG->insertRow(row+2);
		for(int col=0; col<m_nuclei_FindSG->columnCount(); ++col)
			m_nuclei_FindSG->setItem(row+2, col, m_nuclei_FindSG->item(row, col)->clone());
		m_nuclei_FindSG->removeRow(row);
	}

	for(int row=0; row<m_nuclei_FindSG->rowCount(); ++row)
	{
		if(auto *item = m_nuclei_FindSG->item(row, 0);
		   item && std::find(selected.begin(), selected.end(), row-1) != selected.end())
		   {
			   for(int col=0; col<m_nuclei_FindSG->columnCount(); ++col)
				   m_nuclei_FindSG->item(row, col)->setSelected(true);
		   }
	}
}
// ----------------------------------------------------------------------------



// ----------------------------------------------------------------------------
std::vector<int> StructFactDlg::GetSelectedRows_FindSG(bool sort_reversed) const
{
	std::vector<int> vec;
	vec.reserve(m_nuclei_FindSG->selectedItems().size());

	for(int row=0; row<m_nuclei_FindSG->rowCount(); ++row)
	{
		if(auto *item = m_nuclei_FindSG->item(row, 0); item && item->isSelected())
			vec.push_back(row);
	}

	if(sort_reversed)
	{
		std::stable_sort(vec.begin(), vec.end(), [](int row1, int row2)
		{ return row1 > row2; });
	}

	return vec;
}


/**
 * item contents changed
 */
void StructFactDlg::TableItemChanged_FindSG(QTableWidgetItem *item)
{
1051
	//FindSG();
Tobias WEBER's avatar
Tobias WEBER committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
}


void StructFactDlg::ShowTableContextMenu_FindSG(const QPoint& pt)
{
	auto ptGlob = m_nuclei_FindSG->mapToGlobal(pt);

	if(const auto* item = m_nuclei_FindSG->itemAt(pt); item)
	{
		m_iCursorRow_FindSG = item->row();
		ptGlob.setY(ptGlob.y() + m_pTabContextMenu_FindSG->sizeHint().height()/2);
		m_pTabContextMenu_FindSG->popup(ptGlob);
	}
	else
	{
		ptGlob.setY(ptGlob.y() + m_pTabContextMenuNoItem_FindSG->sizeHint().height()/2);
		m_pTabContextMenuNoItem_FindSG->popup(ptGlob);
	}
}
// ----------------------------------------------------------------------------



/**
 * find a matching space group for the given nucleus positions
 */
void StructFactDlg::FindSG()
{
	m_sgmatches->setPlainText("");

	std::vector<t_vec> vecFinal;
	std::ostringstream ostr;
	ostr.precision(g_prec);

	ostr << "Full set of nuclear positions to match:\n";
	for(int row=0; row<m_nuclei_FindSG->rowCount(); ++row)
	{
		auto *itemx = m_nuclei_FindSG->item(row, 0);
		auto *itemy = m_nuclei_FindSG->item(row, 1);
		auto *itemz = m_nuclei_FindSG->item(row, 2);

		if(!itemx || !itemy || !itemz)
			return;

		t_real posx=0, posy=0, posz=0;
		std::istringstream{itemx->text().toStdString()} >> posx;
		std::istringstream{itemy->text().toStdString()} >> posy;
		std::istringstream{itemz->text().toStdString()} >> posz;

		ostr << "\t(" << (row+1) << "): (" << posx << ", " << posy << ", " << posz << ")\n";
		vecFinal.emplace_back(t_vec{{posx, posy, posz}});
	}

	ostr << "\n";
	std::vector<t_vec> vecInit = vecFinal;

	while(1)
	{
		ostr << "\n--------------------------------------------------------------------------------\n";
		ostr << "Base set of nuclear positions:\n";
		std::size_t ctr = 1;
		for(const auto& pos : vecInit)
			ostr << "\t(" << ctr++ << ") " << pos << "\n";
		ostr << std::endl;

		auto matchingSGs = find_matching_sgs<t_vec, t_mat, t_real>(vecInit, vecFinal, g_eps);

		if(matchingSGs.size())
		{
			ostr << "Matching space groups:\n";
			ctr = 1;
			for(const auto& sg : matchingSGs)
				ostr << "\t(" << ctr++ << ") " << std::get<1>(sg) << "\n";
		}
		else
		{
			ostr << "No matching space groups.\n";
		}
		ostr << "--------------------------------------------------------------------------------\n\n";

		vecInit.pop_back();
		if(vecInit.size() == 0)
			break;
	}

	m_sgmatches->setPlainText(ostr.str().c_str());
}
1139

Tobias WEBER's avatar
Tobias WEBER committed
1140
// ============================================================================
1141

1142
1143


Tobias WEBER's avatar
Tobias WEBER committed
1144
// ----------------------------------------------------------------------------
1145
1146
void StructFactDlg::Load()
{
1147
1148
	m_ignoreCalc = 1;

Tobias WEBER's avatar
Tobias WEBER committed
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
	try
	{
		QString dirLast = m_sett->value("dir", "").toString();
		QString filename = QFileDialog::getOpenFileName(this, "Load File", dirLast, "XML Files (*.xml *.XML)");
		if(filename=="" || !QFile::exists(filename))
			return;
		m_sett->setValue("dir", QFileInfo(filename).path());


		pt::ptree node;

		std::ifstream ifstr{filename.toStdString()};
		pt::read_xml(ifstr, node);

		// check signature
		if(auto opt = node.get_optional<std::string>("sfact.meta.info"); !opt || *opt!=std::string{"sfact_tool"})
		{
			QMessageBox::critical(this, "Structure Factors", "Unrecognised file format.");
			return;
		}


		// clear old nuclei
1172
		DelTabItem(-1);
Tobias WEBER's avatar
Tobias WEBER committed
1173
1174
1175

		if(auto opt = node.get_optional<t_real>("sfact.xtal.a"); opt)
		{
Tobias WEBER's avatar
Tobias WEBER committed
1176
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
Tobias WEBER's avatar
Tobias WEBER committed
1177
1178
1179
1180
			m_editA->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("sfact.xtal.b"); opt)
		{
Tobias WEBER's avatar
Tobias WEBER committed
1181
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
Tobias WEBER's avatar
Tobias WEBER committed
1182
1183
1184
1185
			m_editB->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("sfact.xtal.c"); opt)
		{
Tobias WEBER's avatar
Tobias WEBER committed
1186
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
Tobias WEBER's avatar
Tobias WEBER committed
1187
1188
1189
1190
			m_editC->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("sfact.xtal.alpha"); opt)
		{
Tobias WEBER's avatar
Tobias WEBER committed
1191
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
Tobias WEBER's avatar
Tobias WEBER committed
1192
1193
1194
1195
			m_editAlpha->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("sfact.xtal.beta"); opt)
		{
Tobias WEBER's avatar
Tobias WEBER committed
1196
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
Tobias WEBER's avatar
Tobias WEBER committed
1197
1198
1199
1200
			m_editBeta->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("sfact.xtal.gamma"); opt)
		{
Tobias WEBER's avatar
Tobias WEBER committed
1201
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
Tobias WEBER's avatar
Tobias WEBER committed
1202
1203
1204
1205
1206
1207
			m_editGamma->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<int>("sfact.order"); opt)
		{
			m_maxBZ->setValue(*opt);
		}
1208
1209
1210
1211
		if(auto opt = node.get_optional<int>("sfact.removezeroes"); opt)
		{
			m_RemoveZeroes->setChecked(*opt != 0);
		}
1212
1213
1214
1215
		if(auto opt = node.get_optional<int>("sfact.sg_idx"); opt)
		{
			m_comboSG->setCurrentIndex(*opt);
		}
Tobias WEBER's avatar
Tobias WEBER committed
1216
1217
1218
1219
1220


		// nuclei
		if(auto nuclei = node.get_child_optional("sfact.nuclei"); nuclei)
		{
1221
			std::size_t nucIdx = 0;
Tobias WEBER's avatar
Tobias WEBER committed
1222
1223
1224
1225
1226
1227
1228
1229
			for(const auto &nucl : *nuclei)
			{
				auto optName = nucl.second.get<std::string>("name", "n/a");
				auto optbRe = nucl.second.get<t_real>("b_Re", 0.);
				auto optbIm = nucl.second.get<t_real>("b_Im", 0.);
				auto optX = nucl.second.get<t_real>("x", 0.);
				auto optY = nucl.second.get<t_real>("y", 0.);
				auto optZ = nucl.second.get<t_real>("z", 0.);
Tobias WEBER's avatar
Tobias WEBER committed
1230
				auto optRad = nucl.second.get<t_real>("rad", 1.);
1231
				auto optCol = nucl.second.get<std::string>("col", g_default_colours[nucIdx%g_default_colours.size()]);
Tobias WEBER's avatar
Tobias WEBER committed
1232

Tobias WEBER's avatar
Tobias WEBER committed
1233
				AddTabItem(-1, optName, optbRe, optbIm, optX,  optY, optZ, optRad, optCol);
1234
				++nucIdx;
Tobias WEBER's avatar
Tobias WEBER committed
1235
1236
1237
1238
1239
1240
1241
			}
		}
	}
	catch(const std::exception& ex)
	{
		QMessageBox::critical(this, "Structure Factors", ex.what());
	}
1242
1243
1244
1245
1246


	m_ignoreCalc = 0;
	CalcB(false);
	Calc();
1247
1248
1249
1250
1251
}


void StructFactDlg::Save()
{
Tobias WEBER's avatar
Tobias WEBER committed
1252
1253
1254
1255
1256
1257
	QString dirLast = m_sett->value("dir", "").toString();
	QString filename = QFileDialog::getSaveFileName(this, "Save File", dirLast, "XML Files (*.xml *.XML)");
	if(filename=="")
		return;
	m_sett->setValue("dir", QFileInfo(filename).path());

1258

Tobias WEBER's avatar
Tobias WEBER committed
1259
1260
1261
	pt::ptree node;
	node.put<std::string>("sfact.meta.info", "sfact_tool");
	node.put<std::string>("sfact.meta.date", tl2::epoch_to_str<t_real>(tl2::epoch<t_real>()));
1262

Tobias WEBER's avatar
Tobias WEBER committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279

	// lattice
	t_real a,b,c, alpha,beta,gamma;
	std::istringstream{m_editA->text().toStdString()} >> a;
	std::istringstream{m_editB->text().toStdString()} >> b;
	std::istringstream{m_editC->text().toStdString()} >> c;
	std::istringstream{m_editAlpha->text().toStdString()} >> alpha;
	std::istringstream{m_editBeta->text().toStdString()} >> beta;
	std::istringstream{m_editGamma->text().toStdString()} >> gamma;

	node.put<t_real>("sfact.xtal.a", a);
	node.put<t_real>("sfact.xtal.b", b);
	node.put<t_real>("sfact.xtal.c", c);
	node.put<t_real>("sfact.xtal.alpha", alpha);
	node.put<t_real>("sfact.xtal.beta", beta);
	node.put<t_real>("sfact.xtal.gamma", gamma);
	node.put<int>("sfact.order", m_maxBZ->value());
1280
	node.put<int>("sfact.removezeroes", m_RemoveZeroes->isChecked());
1281
	node.put<int>("sfact.sg_idx", m_comboSG->currentIndex());
Tobias WEBER's avatar
Tobias WEBER committed
1282
1283
1284
1285

	// nucleus list
	for(int row=0; row<m_nuclei->rowCount(); ++row)
	{
1286
		t_real bRe{},bIm{}, x{},y{},z{}, scale{};
Tobias WEBER's avatar
Tobias WEBER committed
1287
1288
1289
1290
1291
		std::istringstream{m_nuclei->item(row, COL_SCATLEN_RE)->text().toStdString()} >> bRe;
		std::istringstream{m_nuclei->item(row, COL_SCATLEN_IM)->text().toStdString()} >> bIm;
		std::istringstream{m_nuclei->item(row, COL_X)->text().toStdString()} >> x;
		std::istringstream{m_nuclei->item(row, COL_Y)->text().toStdString()} >> y;
		std::istringstream{m_nuclei->item(row, COL_Z)->text().toStdString()} >> z;
Tobias WEBER's avatar
Tobias WEBER committed
1292
		std::istringstream{m_nuclei->item(row, COL_RAD)->text().toStdString()} >> scale;
Tobias WEBER's avatar
Tobias WEBER committed
1293
1294
1295
1296
1297
1298
1299
1300

		pt::ptree itemNode;
		itemNode.put<std::string>("name", m_nuclei->item(row, COL_NAME)->text().toStdString());
		itemNode.put<t_real>("b_Re", bRe);
		itemNode.put<t_real>("b_Im", bIm);
		itemNode.put<t_real>("x", x);
		itemNode.put<t_real>("y", y);
		itemNode.put<t_real>("z", z);
Tobias WEBER's avatar
Tobias WEBER committed
1301
		itemNode.put<t_real>("rad", scale);
Tobias WEBER's avatar
Tobias WEBER committed
1302
		itemNode.put<std::string>("col", m_nuclei->item(row, COL_COL)->text().toStdString());
Tobias WEBER's avatar
Tobias WEBER committed
1303
1304
1305
1306
1307

		node.add_child("sfact.nuclei.nucleus", itemNode);
	}

	std::ofstream ofstr{filename.toStdString()};
Tobias WEBER's avatar
Tobias WEBER committed
1308
1309
1310
1311
1312
1313
	if(!ofstr)
	{
		QMessageBox::critical(this, "Structure Factors", "Cannot open file for writing.");
		return;
	}
	ofstr.precision(g_prec);
Tobias WEBER's avatar
Tobias WEBER committed
1314
1315
	pt::write_xml(ofstr, node, pt::xml_writer_make_settings('\t', 1, std::string{"utf-8"}));
}
Tobias WEBER's avatar
Tobias WEBER committed
1316
1317


1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
/**
 * load a TAZ file
 */
void StructFactDlg::ImportTAZ()
{
	m_ignoreCalc = 1;

	try
	{
		QString dirLast = m_sett->value("dir_taz", "").toString();
		QString filename = QFileDialog::getOpenFileName(this, "Load File", dirLast, "TAZ Files (*.taz *.TAZ)");
		if(filename=="" || !QFile::exists(filename))
			return;
		m_sett->setValue("dir_taz", QFileInfo(filename).path());


		pt::ptree node;

		std::ifstream ifstr{filename.toStdString()};
		pt::read_xml(ifstr, node);

		// clear old nuclei
		DelTabItem(-1);

		if(auto opt = node.get_optional<t_real>("taz.sample.a"); opt)
		{
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
			m_editA->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("taz.sample.b"); opt)
		{
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
			m_editB->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("taz.sample.c"); opt)
		{
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
			m_editC->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("taz.sample.alpha"); opt)
		{
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
			m_editAlpha->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("taz.sample.beta"); opt)
		{
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
			m_editBeta->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<t_real>("taz.sample.gamma"); opt)
		{
			std::ostringstream ostr; ostr.precision(g_prec); ostr << *opt;
			m_editGamma->setText(ostr.str().c_str());
		}
		if(auto opt = node.get_optional<std::string>("taz.sample.spacegroup"); opt)
		{
			// TODO
			//std::cout << *opt << std::endl;
		}

		// nuclei
		if(auto opt = node.get_optional<std::size_t>("taz.sample.atoms.num"); opt)
		{
			std::size_t numAtoms = *opt;
			for(std::size_t atomIdx=0; atomIdx<numAtoms; ++atomIdx)
			{
				std::string strNum = tl2::var_to_str(atomIdx);

				std::string name = node.get<std::string>("taz.sample.atoms."+strNum+".name", "n/a");
				t_real x = node.get<t_real>("taz.sample.atoms."+strNum+".x", 0.);
				t_real y = node.get<t_real>("taz.sample.atoms."+strNum+".y", 0.);
				t_real z = node.get<t_real>("taz.sample.atoms."+strNum+".z", 0.);

				// TODO
				t_real bRe = 0.;
				t_real bIm = 0.;

				t_real rad = 1.;
				std::string col = g_default_colours[atomIdx%g_default_colours.size()];

				AddTabItem(-1, name, bRe, bIm, x, y, z, rad, col);
			}
		}
	}
	catch(const std::exception& ex)
	{
		QMessageBox::critical(this, "Structure Factors", ex.what());
	}

	m_ignoreCalc = 0;
	GenerateFromSG();
	CalcB(false);
	Calc();
}


Tobias WEBER's avatar
Tobias WEBER committed
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
/**
 * save a TAZ file
 */
void StructFactDlg::ExportTAZ()
{
	QString dirLast = m_sett->value("dir_taz", "").toString();
	QString filename = QFileDialog::getSaveFileName(this, "Export TAZ", dirLast, "TAZ Files (*.taz *.TAZ)");
	if(filename=="" || !QFile::exists(filename))
		return;
	m_sett->setValue("dir_taz", QFileInfo(filename).path());

	std::ofstream ofstr{filename.toStdString()};
	if(!ofstr)
	{
		QMessageBox::critical(this, "Structure Factors", "Cannot open file for writing.");
		return;
	}
	ofstr.precision(g_prec);

	ofstr << "<taz>\n";
1434
	ofstr << "\t<meta><info>Exported from Takin/Structfact.</info></meta>\n";
Tobias WEBER's avatar
Tobias WEBER committed
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457