USGS

Isis 3.0 Object Programmers' Reference

Home

FileDialog.cpp
1 #include "FileDialog.h"
2 
3 #include <iostream>
4 
5 #include <QFile>
6 
7 #include "FileName.h"
8 #include "IString.h"
9 
10 namespace Isis {
11  FileDialog::FileDialog(QString title, QStringList &filterList, QDir &directory, QWidget *parent) :
12  QFileDialog(parent), p_filterList(filterList), p_dir(directory) {
13 
14  p_parent = parent;
15 
16  this->setWindowTitle(title);
17  this->setFileMode(QFileDialog::ExistingFiles);
18  if(parent != 0) {
19  parent->installEventFilter(this);
20  p_appName = parent->windowTitle();
21  }
22  connect(this, SIGNAL(accepted()), this, SLOT(sendSignal()));
23 
24  //this->setFilters(p_filterList);
25  this->setNameFilters(p_filterList);
26 
27  this->setViewMode(QFileDialog::Detail);
28  if(directory.exists()) {
29  this->setDirectory(directory);
30  }
31  else {
32  this->setDirectory(QDir::current());
33  }
34 
35  p_comboBoxes = this->findChildren<QComboBox *>();
36  p_comboBoxes[0]->setEditable(true);
37  p_comboBoxes[1]->setEditable(true);
38 
39  QLineEdit *lineEdit = p_comboBoxes[1]->lineEdit();
40  //p_comboBoxes[1]->setEditText("Choose a filter or create your own.");
41  disconnect(lineEdit, 0, 0, 0);
42  connect(lineEdit, SIGNAL(textChanged(const QString &)), p_comboBoxes[1],
43  SIGNAL(activated(const QString &)));
44  connect(lineEdit, SIGNAL(editingFinished()), this,
45  SLOT(saveFilter()));
46 
47 
48  p_allPButtons = this->findChildren<QPushButton *>();
49  for(int i = 0; i < p_allPButtons.size(); i++) {
50  //Disconnecting both buttons from all their old connection so we have complete control.
51  disconnect(p_allPButtons[i], 0, 0, 0);
52  if(p_allPButtons[i]->text().contains("Open", Qt::CaseInsensitive)) {
53  connect(p_allPButtons[i], SIGNAL(clicked()), this, SLOT(done()));
54  }
55  if(p_allPButtons[i]->text() == "Cancel") {
57  //done method from QDialog which is what this used to be connected to.
58  connect(p_allPButtons[i], SIGNAL(clicked()), this, SLOT(cancel()));
59  }
60  }
61 
62  readSettings();
63  }
64 
65 
72  p_allPButtons[0]->setDefault(false);
73  if(!p_filterList.contains(p_comboBoxes[1]->currentText())) {
74  p_filterList.insert(0, p_comboBoxes[1]->currentText());
75  this->setNameFilters(p_filterList);
76  }
77 
78  }
79 
80 
87  p_dir = this->directory();
88  QStringList fileList = this->selectedFiles();
89  QStringList::const_iterator it = fileList.begin();
90  while(it != fileList.end()) {
91  if(!(*it).isEmpty()) {
92  emit fileSelected(*it);
93  ++it;
94  }
95  }
96  //p_fileList = fileList;
97  }
98 
99 
106  void FileDialog::closeEvent(QCloseEvent *event) {
107  writeSettings();
108 
109  }
110 
111 
117  close();
118  sendSignal();
119 
120  }
121 
122 
128  close();
129  p_dir = this->directory();
130  }
131 
138  if(p_appName == "") {
139  p_appName = this->windowTitle();
140  }
141 
142  QString instanceName = this->windowTitle();
143  // FileName is a QFileDialog enum for DialogLabel
144  Isis::FileName config("$HOME/.Isis/" + p_appName + "/" + instanceName + ".config");
145  QSettings settings(config.expanded(), QSettings::NativeFormat);
146  QPoint pos = settings.value("pos", QPoint(300, 100)).toPoint();
147  QSize size = settings.value("size", QSize(355, 350)).toSize();
148  resize(size);
149  move(pos);
150  }
151 
159  /*We do not want to write the settings unless the window is
160  visible at the time of closing the application*/
161  if(!this->isVisible()) return;
162 
163  if(p_appName == "") {
164  p_appName = this->windowTitle();
165  }
166 
167  QString instanceName = this->windowTitle();
168  // FileName is a QFileDialog enum for DialogLabel
169  Isis::FileName config("$HOME/.Isis/" + p_appName + "/" + instanceName + ".config");
170  QSettings settings(config.expanded(), QSettings::NativeFormat);
171  settings.setValue("pos", pos());
172  settings.setValue("size", size());
173 
174  }
175 
187  bool FileDialog::eventFilter(QObject *o, QEvent *e) {
188 
189  switch(e->type()) {
190  case QEvent::Close: {
191  writeSettings();
192 
193  }
194  case QEvent::Hide: {
195  writeSettings();
196 
197  }
198  default: {
199  return false;
200  }
201  }
202  }
203 
204 }
205