browser.cpp 7.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * space group browser
 * @author Tobias Weber <tweber@ill.fr>
 * @date Apr-2018
 * @license GPLv3, see 'LICENSE' file
 * @desc The present version was forked on 8-Nov-2018 from the privately developed "magtools" project (https://github.com/t-weber/magtools).
 */

#include "browser.h"
#include <sstream>

#include <QtWidgets/QMenuBar>


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


SgBrowserDlg::SgBrowserDlg(QWidget* pParent, QSettings *pSett)
	: QDialog{pParent}, m_pSettings{pSett}
{
	// ------------------------------------------------------------------------
	// setup UI
	this->setupUi(this);
	auto *pLayout = this->layout();

	// menu
	auto *pMenuBar = new QMenuBar(this);
	auto *pMenuOptions = new QMenu("Options", pMenuBar);

	auto *pShowBNS = new QAction("Show BNS", pMenuOptions);
	pShowBNS->setCheckable(true);
	pShowBNS->setChecked(true);
	pMenuOptions->addAction(pShowBNS);

	pMenuBar->addMenu(pMenuOptions);
	pLayout->setMenuBar(pMenuBar);
	// ------------------------------------------------------------------------


	// ------------------------------------------------------------------------
	// restore settings
	if(m_pSettings)
	{
		if(m_pSettings->contains("sgbrowser/geo"))
			this->restoreGeometry(m_pSettings->value("sgbrowser/geo").toByteArray());
		//if(m_pSettings->contains("sgbrowser/state"))
		//	this->restoreState(m_pSettings->value("sgbrowser/state").toByteArray());
	}
	// ------------------------------------------------------------------------


	// load data
	SetupSpaceGroups();


	// ------------------------------------------------------------------------
	// connections
	connect(m_pTree, &QTreeWidget::currentItemChanged, this, &SgBrowserDlg::SpaceGroupSelected);
	connect(pShowBNS, &QAction::toggled, this, &SgBrowserDlg::SwitchToBNS);
	// ------------------------------------------------------------------------
}


// ----------------------------------------------------------------------------
/**
 * load space group list
 */
void SgBrowserDlg::SetupSpaceGroups()
{
	std::cerr << "Loading space groups ... ";
Tobias WEBER's avatar
Tobias WEBER committed
71
	m_sgs.Load("magsg.info");
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
	std::cerr << "Done." << std::endl;

	const auto *pSgs = m_sgs.GetSpacegroups();
	if(pSgs)
	{
		for(const auto& sg : *pSgs)
			SetupSpaceGroup(sg);
	}
}


/**
 * add space group to tree widget
 */
void SgBrowserDlg::SetupSpaceGroup(const Spacegroup<t_mat_sg, t_vec_sg>& sg)
{
	int iNrStruct = sg.GetStructNumber();
	int iNrMag = sg.GetMagNumber();

	// find top-level item with given structural sg number
	auto get_topsg = [](QTreeWidget *pTree, int iStructNr) -> QTreeWidgetItem*
	{
		for(int item=0; item<pTree->topLevelItemCount(); ++item)
		{
			QTreeWidgetItem *pItem = pTree->topLevelItem(item);
			if(!pItem) continue;

			if(pItem->data(0, Qt::UserRole) == iStructNr)
				return pItem;
		}

		return nullptr;
	};


	// find existing top-level structural space group or insert new one
	auto *pTopItem = get_topsg(m_pTree, iNrStruct);
	if(!pTopItem)
	{
		QString structname = ("(" + std::to_string(iNrStruct) + ") " + sg.GetName()).c_str();

		pTopItem = new QTreeWidgetItem();
		pTopItem->setText(0, structname);
		pTopItem->setData(0, Qt::UserRole, iNrStruct);
		pTopItem->setData(0, Qt::UserRole+1, iNrMag);
		m_pTree->addTopLevelItem(pTopItem);
	}


	// create magnetic group and add it as sub-item for the corresponding structural group
	QString magname = ("(" + sg.GetNumber() + ") " + sg.GetName()).c_str();

	auto *pSubItem = new QTreeWidgetItem();
	pSubItem->setText(0, magname);
	pSubItem->setData(0, Qt::UserRole, iNrStruct);
	pSubItem->setData(0, Qt::UserRole+1, iNrMag);
	pTopItem->addChild(pSubItem);
}
// ----------------------------------------------------------------------------


/**
 * switched to show BNS (otherwise OG)
 */
void SgBrowserDlg::SwitchToBNS(bool bBNS)
{
	m_showBNS = bBNS;
	SpaceGroupSelected(m_pTree->currentItem());
}


/**
 * space group selected
 */
void SgBrowserDlg::SpaceGroupSelected(QTreeWidgetItem *pItem)
{
	if(!pItem) return;

	// clean up
	m_pSymOps->clear();
	m_pWyc->clear();

	int iNrStruct = pItem->data(0, Qt::UserRole).toInt();
	int iNrMag = pItem->data(0, Qt::UserRole+1).toInt();

	const auto* pSg = m_sgs.GetSpacegroupByNumber(iNrStruct, iNrMag);
	if(!pSg) return;


	// print a symmetry operator
	auto print_sym = [](const t_mat_sg& mat, const t_vec_sg& vec, t_real_sg inv) -> std::string
	{
		std::ostringstream ostr;

		for(std::size_t i=0; i<mat.size1(); ++i)
		{
			// rotation matrix
			ostr << "( ";
			for(std::size_t j=0; j<mat.size2(); ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << mat(i,j);
			ostr << " )";

			// translation vector
			ostr << std::setw(ostr.precision()*2) << std::right << "( ";
			ostr << std::setw(ostr.precision()*1.5) << std::right << vec[i]; 
			ostr << " )";

			// time inversion
			if(i==mat.size1()/2)
			{
				ostr << std::setw(ostr.precision()*2) << std::right << "t = ";
				ostr << inv;
			}

			if(i < mat.size1()-1)
				ostr << "\n";
		}

		return ostr.str();
	};


	// print a wyckoff position
	auto print_wyc = [](const t_mat_sg& mat, const t_mat_sg& matRot,  const t_vec_sg& vec) -> std::string
	{
		std::ostringstream ostr;

		for(std::size_t i=0; i<mat.size1(); ++i)
		{
			// rotation matrices
			ostr << "( ";
			for(std::size_t j=0; j<mat.size2(); ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << mat(i,j);

			ostr  << " | ";
			for(std::size_t j=0; j<matRot.size2(); ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << matRot(i,j);
			ostr << " )";

			// translation vector
			ostr << std::setw(ostr.precision()*2) << std::right << "( ";
			ostr << std::setw(ostr.precision()*1.5) << std::right << vec[i]; 
			ostr << " )";

			if(i < mat.size1()-1)
				ostr << "\n";
		}

		return ostr.str();
	};


	// iterate over symmetries
	const auto *symms = pSg->GetSymmetries(m_showBNS);
	if(symms)
	{
		for(std::size_t iOp=0; iOp<symms->GetRotations().size(); ++iOp)
		{
			const auto& rot = symms->GetRotations()[iOp];
			const auto& trans = symms->GetTranslations()[iOp];
			const auto inv = symms->GetInversions()[iOp];

			auto *pOpItem = new QListWidgetItem();
			pOpItem->setText(print_sym(rot, trans, inv).c_str());
			m_pSymOps->addItem(pOpItem);
		}
	}


	// iterate over wyckoff positions
	const auto *wycs = pSg->GetWycPositions(m_showBNS);
	if(wycs)
	{
		for(const auto &wyc : *wycs)
		{
			auto *pWycItem = new QTreeWidgetItem();
			pWycItem->setText(0, wyc.GetName().c_str());

			// iterate over trafos
			for(std::size_t iOp=0; iOp<wyc.GetRotations().size(); ++iOp)
			{
				const auto& rot = wyc.GetRotations()[iOp];
				const auto& rotMag = wyc.GetRotationsMag()[iOp];
				const auto& trans = wyc.GetTranslations()[iOp];

				auto *pSubItem = new QTreeWidgetItem();
				pSubItem->setText(0, print_wyc(rot, rotMag, trans).c_str());
				pWycItem->addChild(pSubItem);
			}

			m_pWyc->addTopLevelItem(pWycItem);
			pWycItem->setExpanded(true);
		}
	}
}


void SgBrowserDlg::showEvent(QShowEvent *pEvt)
{
	QDialog::showEvent(pEvt);
}


void SgBrowserDlg::hideEvent(QHideEvent *pEvt)
{
	QDialog::hideEvent(pEvt);
}


void SgBrowserDlg::closeEvent(QCloseEvent *pEvt)
{
	if(m_pSettings)
	{
		m_pSettings->setValue("sgbrowser/geo", this->saveGeometry());
		//m_pSettings->setValue("sgbrowser/state", this->saveState());
	}

	QDialog::closeEvent(pEvt);
}


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