Isis 3 Programmer Reference
FileDialog.cpp
1 #include "FileDialog.h"
2 
3 #include <iostream>
4 
5 #include <QComboBox>
6 #include <QFile>
7 #include <QLineEdit>
8 #include <QPushButton>
9 
10 #include "FileName.h"
11 #include "IString.h"
12 
13 namespace Isis {
14  FileDialog::FileDialog(QString title, QStringList &filterList, QDir &directory, QWidget *parent) :
15  QFileDialog(parent), p_filterList(filterList), p_dir(directory) {
16 
17  p_parent = parent;
18 
19  // We want to use Qt's dialog so we can find the children widgets
20  this->setOptions(QFileDialog::DontUseNativeDialog);
21 
22  this->setWindowTitle(title);
23  this->setFileMode(QFileDialog::ExistingFiles);
24  if(parent != 0) {
25  parent->installEventFilter(this);
26  p_appName = parent->windowTitle();
27  }
28  connect(this, SIGNAL(accepted()), this, SLOT(sendSignal()));
29 
30  //this->setFilters(p_filterList);
31  this->setNameFilters(p_filterList);
32 
33  this->setViewMode(QFileDialog::Detail);
34  if(directory.exists()) {
35  this->setDirectory(directory);
36  }
37  else {
38  this->setDirectory(QDir::current());
39  }
40 
41  p_comboBoxes = this->findChildren<QComboBox *>();
42  p_comboBoxes[0]->setEditable(true);
43  p_comboBoxes[1]->setEditable(true);
44 
45  QLineEdit *lineEdit = p_comboBoxes[1]->lineEdit();
46  //p_comboBoxes[1]->setEditText("Choose a filter or create your own.");
47  disconnect(lineEdit, 0, 0, 0);
48  connect(lineEdit, SIGNAL(textChanged(const QString &)), p_comboBoxes[1],
49  SIGNAL(activated(const QString &)));
50  connect(lineEdit, SIGNAL(editingFinished()), this,
51  SLOT(saveFilter()));
52 
53 
54  p_allPButtons = this->findChildren<QPushButton *>();
55  for(int i = 0; i < p_allPButtons.size(); i++) {
56  //Disconnecting both buttons from all their old connection so we have complete control.
57  disconnect(p_allPButtons[i], 0, 0, 0);
58  if(p_allPButtons[i]->text().contains("Open", Qt::CaseInsensitive)) {
59  connect(p_allPButtons[i], SIGNAL(clicked()), this, SLOT(done()));
60  }
61  if(p_allPButtons[i]->text().contains("Cancel", Qt::CaseInsensitive)) {
63  //done method from QDialog which is what this used to be connected to.
64  connect(p_allPButtons[i], SIGNAL(clicked()), this, SLOT(cancel()));
65  }
66  }
67 
68  readSettings();
69  }
70 
71 
78  p_allPButtons[0]->setDefault(false);
79  if(!p_filterList.contains(p_comboBoxes[1]->currentText())) {
80  p_filterList.insert(0, p_comboBoxes[1]->currentText());
81  this->setNameFilters(p_filterList);
82  }
83 
84  }
85 
86 
93  p_dir = this->directory();
94  QStringList fileList = this->selectedFiles();
95  QStringList::const_iterator it = fileList.begin();
96  while(it != fileList.end()) {
97  if(!(*it).isEmpty()) {
98  emit fileSelected(*it);
99  ++it;
100  }
101  }
102  //p_fileList = fileList;
103  }
104 
105 
112  void FileDialog::closeEvent(QCloseEvent *event) {
113  writeSettings();
114 
115  }
116 
117 
123  close();
124  sendSignal();
125  }
126 
131  void FileDialog::done(int r) {
132  QFileDialog::done(r);
133  }
134 
135 
141  close();
142  p_dir = this->directory();
143  }
144 
151  if(p_appName == "") {
152  p_appName = this->windowTitle();
153  }
154 
155  QString instanceName = this->windowTitle();
156  // FileName is a QFileDialog enum for DialogLabel
157  Isis::FileName config("$HOME/.Isis/" + p_appName + "/" + instanceName + ".config");
158  QSettings settings(config.expanded(), QSettings::NativeFormat);
159  QPoint pos = settings.value("pos", QPoint(300, 100)).toPoint();
160  QSize size = settings.value("size", QSize(355, 350)).toSize();
161  resize(size);
162  move(pos);
163  }
164 
172  /*We do not want to write the settings unless the window is
173  visible at the time of closing the application*/
174  if(!this->isVisible()) return;
175 
176  if(p_appName == "") {
177  p_appName = this->windowTitle();
178  }
179 
180  QString instanceName = this->windowTitle();
181  // FileName is a QFileDialog enum for DialogLabel
182  Isis::FileName config("$HOME/.Isis/" + p_appName + "/" + instanceName + ".config");
183  QSettings settings(config.expanded(), QSettings::NativeFormat);
184  settings.setValue("pos", pos());
185  settings.setValue("size", size());
186 
187  }
188 
200  bool FileDialog::eventFilter(QObject *o, QEvent *e) {
201 
202  switch(e->type()) {
203  case QEvent::Close: {
204  writeSettings();
205 
206  }
207  case QEvent::Hide: {
208  writeSettings();
209 
210  }
211  default: {
212  return false;
213  }
214  }
215  }
216 
217 }
void closeEvent(QCloseEvent *event)
This method is overridden so that we can be sure to write the current settings of the Main window...
Definition: FileDialog.cpp:112
bool eventFilter(QObject *o, QEvent *e)
This event filter is installed on the parent of this window.
Definition: FileDialog.cpp:200
File name manipulation and expansion.
Definition: FileName.h:116
void writeSettings()
This method is called when the File Dialog is closed or hidden to write the size and location setting...
Definition: FileDialog.cpp:171
void done()
Called when the user presses OK.
Definition: FileDialog.cpp:122
void sendSignal()
This saves the directory that the user selected the file from so it can open to this directory next t...
Definition: FileDialog.cpp:92
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
FileDialog(QString title, QStringList &filterList, QDir &directory, QWidget *parent=0)
Definition: FileDialog.cpp:14
void saveFilter()
This is where we actually set the user editable filters and remember them.
Definition: FileDialog.cpp:77
void cancel()
Called when user presses cancel.
Definition: FileDialog.cpp:140
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void readSettings()
This method is called from the constructor so that when the Main window is created, it know&#39;s it&#39;s size and location.
Definition: FileDialog.cpp:150