source: src/UIElements/QT4/QTDialog.cpp@ 992fd7

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 992fd7 was 992fd7, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Repaired some QT4 related memory leaks

  • Property mode set to 100644
File size: 10.1 KB
Line 
1/*
2 * QTDialog.cpp
3 *
4 * Created on: Jan 18, 2010
5 * Author: crueger
6 */
7
8#include "UIElements/QT4/QTDialog.hpp"
9
10#include <string>
11#include <sstream>
12#include <limits>
13
14#include <Qt/qboxlayout.h>
15#include <Qt/qlabel.h>
16#include <Qt/qspinbox.h>
17#include <QtGui/QDoubleSpinBox>
18#include <Qt/qlineedit.h>
19#include <Qt/qdialogbuttonbox.h>
20#include <Qt/qpushbutton.h>
21#include <Qt/qcombobox.h>
22
23#include "World.hpp"
24#include "periodentafel.hpp"
25#include "atom.hpp"
26#include "element.hpp"
27#include "molecule.hpp"
28#include "Helpers/MemDebug.hpp"
29
30
31using namespace std;
32
33QTDialog::QTDialog() :
34 QDialog(0)
35{
36 // creating and filling of the Dialog window
37 mainLayout = new QVBoxLayout();
38 inputLayout = new QVBoxLayout();
39 buttonLayout = new QVBoxLayout();
40 setLayout(mainLayout);
41 mainLayout->addLayout(inputLayout);
42 mainLayout->addLayout(buttonLayout);
43 buttons = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
44 buttonLayout->addWidget(buttons);
45
46 // Disable the ok button until something was entered
47 buttons->button(QDialogButtonBox::Ok)->setEnabled(false);
48
49 // connect the buttons to their appropriate slots
50 connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
51 connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
52}
53
54QTDialog::~QTDialog()
55{
56}
57
58bool QTDialog::display(){
59 // Button state might have changed by some update that
60 // was done during query construction. To make sure
61 // the state is correct, we just call update one more time.
62 update();
63 if(exec()) {
64 setAll();
65 return true;
66 }
67 else {
68 return false;
69 }
70}
71
72void QTDialog::update(){
73 buttons->button(QDialogButtonBox::Ok)->setEnabled(checkAll());
74}
75
76/************************** Query Infrastructure ************************/
77
78void QTDialog::queryInt(const char *title, int *target)
79{
80 registerQuery(new IntQTQuery(title,target,inputLayout,this));
81}
82
83void QTDialog::queryDouble(const char* title, double* target){
84 registerQuery(new DoubleQTQuery(title,target,inputLayout,this));
85}
86
87void QTDialog::queryString(const char* title, std::string *target)
88{
89 registerQuery(new StringQTQuery(title,target,inputLayout,this));
90}
91
92void QTDialog::queryMolecule(const char *title,molecule **target,MoleculeListClass *molecules)
93{
94 registerQuery(new MoleculeQTQuery(title,target,molecules,inputLayout,this));
95}
96
97void QTDialog::queryVector(const char* title, Vector *target,const double *const cellSize, bool check) {
98 registerQuery(new VectorQTQuery(title,target,cellSize,check,inputLayout,this));
99}
100
101void QTDialog::queryElement(const char* title, const element **target){
102 registerQuery(new ElementQTQuery(title,target,inputLayout,this));
103}
104
105/************************** Query Objects *******************************/
106
107QTDialog::IntQTQuery::IntQTQuery(string _title,int *_target,QBoxLayout *_parent,QTDialog *_dialog) :
108 Dialog::IntQuery(_title,_target),
109 parent(_parent)
110{
111 thisLayout = new QHBoxLayout();
112 titleLabel = new QLabel(QString(getTitle().c_str()));
113 inputBox = new QSpinBox();
114 inputBox->setValue(0);
115 parent->addLayout(thisLayout);
116 thisLayout->addWidget(titleLabel);
117 thisLayout->addWidget(inputBox);
118
119 pipe = new IntQTQueryPipe(&tmp,_dialog);
120 pipe->update(inputBox->value());
121 connect(inputBox,SIGNAL(valueChanged(int)),pipe,SLOT(update(int)));
122}
123
124QTDialog::IntQTQuery::~IntQTQuery()
125{
126 delete pipe;
127}
128
129// Handling is easy since the GUI makes it impossible to enter invalid values
130bool QTDialog::IntQTQuery::handle()
131{
132 return true;
133}
134
135QTDialog::DoubleQTQuery::DoubleQTQuery(string title,double *_target,QBoxLayout *_parent,QTDialog *_dialog) :
136 Dialog::DoubleQuery(title,_target),
137 parent(_parent)
138{
139 thisLayout = new QHBoxLayout();
140 titleLabel = new QLabel(QString(getTitle().c_str()));
141 inputBox = new QDoubleSpinBox();
142 inputBox->setValue(0);
143 inputBox->setRange(-numeric_limits<double>::max(),numeric_limits<double>::max());
144 inputBox->setDecimals(3);
145 parent->addLayout(thisLayout);
146 thisLayout->addWidget(titleLabel);
147 thisLayout->addWidget(inputBox);
148
149 pipe = new DoubleQTQueryPipe(&tmp,_dialog);
150 pipe->update(inputBox->value());
151 connect(inputBox,SIGNAL(valueChanged(double)),pipe,SLOT(update(double)));
152}
153
154QTDialog::DoubleQTQuery::~DoubleQTQuery()
155{
156 delete pipe;
157}
158
159bool QTDialog::DoubleQTQuery::handle() {
160 return true;
161}
162
163
164QTDialog::StringQTQuery::StringQTQuery(string _title,string *_target,QBoxLayout *_parent,QTDialog *_dialog) :
165 Dialog::StringQuery(_title,_target),
166 parent(_parent)
167{
168 thisLayout = new QHBoxLayout();
169 titleLabel = new QLabel(QString(getTitle().c_str()));
170 inputBox = new QLineEdit();
171 parent->addLayout(thisLayout);
172 thisLayout->addWidget(titleLabel);
173 thisLayout->addWidget(inputBox);
174
175 pipe = new StringQTQueryPipe(&tmp,_dialog);
176 pipe->update(inputBox->text());
177 connect(inputBox,SIGNAL(textChanged(const QString&)),pipe,SLOT(update(const QString&)));
178}
179
180QTDialog::StringQTQuery::~StringQTQuery()
181{
182 delete pipe;
183}
184
185// All values besides the empty string are valid
186bool QTDialog::StringQTQuery::handle()
187{
188 return tmp!="";
189}
190
191QTDialog::MoleculeQTQuery::MoleculeQTQuery(string _title, molecule **_target, MoleculeListClass *_molecules, QBoxLayout *_parent,QTDialog *_dialog) :
192 Dialog::MoleculeQuery(_title,_target,_molecules),
193 parent(_parent)
194{
195 MoleculeList::iterator iter;
196 thisLayout = new QHBoxLayout();
197 titleLabel = new QLabel(QString(getTitle().c_str()));
198 inputBox = new QComboBox();
199 // add all molecules to the combo box
200 for(iter = molecules->ListOfMolecules.begin();
201 iter != molecules->ListOfMolecules.end();
202 ++iter) {
203 stringstream sstr;
204 sstr << (*iter)->IndexNr << "\t" << (*iter)->getName();
205 inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter)->IndexNr));
206 }
207 parent->addLayout(thisLayout);
208 thisLayout->addWidget(titleLabel);
209 thisLayout->addWidget(inputBox);
210
211 pipe = new MoleculeQTQueryPipe(&tmp,_dialog,inputBox,_molecules);
212 pipe->update(inputBox->currentIndex());
213 connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
214}
215
216QTDialog::MoleculeQTQuery::~MoleculeQTQuery()
217{
218 delete pipe;
219}
220
221// Handling is easy, since the GUI makes it impossible to select invalid values
222bool QTDialog::MoleculeQTQuery::handle()
223{
224 return true;
225}
226
227QTDialog::VectorQTQuery::VectorQTQuery(std::string title, Vector *_target, const double *const _cellSize, bool _check,QBoxLayout *_parent,QTDialog *_dialog) :
228 Dialog::VectorQuery(title,_target,_cellSize,_check),
229 parent(_parent)
230{
231 // About the j: I don't know why it was done this way, but it was used this way in Vector::AskPosition, so I reused it
232 int j = -1;
233 const char *coords[3] = {"x:","y:","z:"};
234 mainLayout= new QHBoxLayout();
235 titleLabel = new QLabel(QString(getTitle().c_str()));
236 mainLayout->addWidget(titleLabel);
237 subLayout = new QVBoxLayout();
238 mainLayout->addLayout(subLayout);
239 for(int i=0; i<3; i++) {
240 j+=i+1;
241 coordLayout[i] = new QHBoxLayout();
242 subLayout->addLayout(coordLayout[i]);
243 coordLabel[i] = new QLabel(QString(coords[i]));
244 coordLayout[i]->addWidget(coordLabel[i]);
245 coordInput[i] = new QDoubleSpinBox();
246 coordInput[i]->setRange(0,cellSize[j]);
247 coordInput[i]->setDecimals(3);
248 coordLayout[i]->addWidget(coordInput[i]);
249 pipe[i] = new DoubleQTQueryPipe(&((*tmp)[i]),_dialog);
250 pipe[i]->update(coordInput[i]->value());
251 connect(coordInput[i],SIGNAL(valueChanged(double)),pipe[i],SLOT(update(double)));
252
253 }
254 parent->addLayout(mainLayout);
255}
256
257QTDialog::VectorQTQuery::~VectorQTQuery()
258{}
259
260bool QTDialog::VectorQTQuery::handle() {
261 return true;
262}
263
264
265QTDialog::ElementQTQuery::ElementQTQuery(std::string _title, const element **_target, QBoxLayout *_parent, QTDialog *_dialog) :
266 Dialog::ElementQuery(_title,_target),
267 parent(_parent)
268{
269 periodentafel *periode = World::getInstance().getPeriode();
270 thisLayout = new QHBoxLayout();
271 titleLabel = new QLabel(QString(getTitle().c_str()));
272 inputBox = new QComboBox();
273 for(periodentafel::const_iterator iter = periode->begin();
274 iter!=periode->end();
275 ++iter)
276 {
277 stringstream sstr;
278 sstr << (*iter).first << "\t" << (*iter).second->name;
279 inputBox->addItem(QString(sstr.str().c_str()),QVariant((*iter).first));
280 }
281 parent->addLayout(thisLayout);
282 thisLayout->addWidget(titleLabel);
283 thisLayout->addWidget(inputBox);
284
285 pipe = new ElementQTQueryPipe(&tmp,_dialog,inputBox);
286 pipe->update(inputBox->currentIndex());
287 connect(inputBox,SIGNAL(currentIndexChanged(int)),pipe,SLOT(update(int)));
288}
289
290QTDialog::ElementQTQuery::~ElementQTQuery()
291{
292 delete pipe;
293}
294
295bool QTDialog::ElementQTQuery::handle(){
296 return true;
297}
298
299/*************************** Plumbing *******************************/
300
301StringQTQueryPipe::StringQTQueryPipe(string *_content, QTDialog *_dialog) :
302 content(_content),
303 dialog(_dialog)
304{}
305
306StringQTQueryPipe::~StringQTQueryPipe()
307{}
308
309void StringQTQueryPipe::update(const QString& newText) {
310 content->assign(newText.toStdString());
311 dialog->update();
312}
313
314IntQTQueryPipe::IntQTQueryPipe(int *_content, QTDialog *_dialog) :
315 content(_content),
316 dialog(_dialog)
317{}
318
319IntQTQueryPipe::~IntQTQueryPipe()
320{}
321
322void IntQTQueryPipe::update(int newInt) {
323 (*content) = newInt;
324 dialog->update();
325}
326
327DoubleQTQueryPipe::DoubleQTQueryPipe(double *_content, QTDialog *_dialog) :
328 content(_content),
329 dialog(_dialog)
330{}
331
332DoubleQTQueryPipe::~DoubleQTQueryPipe()
333{}
334
335void DoubleQTQueryPipe::update(double newDbl) {
336 (*content) = newDbl;
337 dialog->update();
338}
339
340MoleculeQTQueryPipe::MoleculeQTQueryPipe(molecule **_content, QTDialog *_dialog, QComboBox *_theBox, MoleculeListClass *_molecules) :
341 content(_content),
342 dialog(_dialog),
343 theBox(_theBox),
344 molecules(_molecules)
345{}
346
347MoleculeQTQueryPipe::~MoleculeQTQueryPipe()
348{}
349
350void MoleculeQTQueryPipe::update(int newIndex) {
351 QVariant data = theBox->itemData(newIndex);
352 int idx = data.toInt();
353 (*content) = molecules->ReturnIndex(idx);
354 dialog->update();
355}
356
357ElementQTQueryPipe::ElementQTQueryPipe(const element **_content, QTDialog *_dialog, QComboBox *_theBox) :
358 content(_content),
359 dialog(_dialog),
360 theBox(_theBox)
361{}
362
363ElementQTQueryPipe::~ElementQTQueryPipe()
364{}
365
366void ElementQTQueryPipe::update(int newIndex) {
367 QVariant data = theBox->itemData(newIndex);
368 int idx = data.toInt();
369 (*content) = World::getInstance().getPeriode()->FindElement(idx);
370 dialog->update();
371}
372
Note: See TracBrowser for help on using the repository browser.