NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxFileSelectWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 /*
14  * sscFileSelectWidget.cpp
15  *
16  * Created on: May 6, 2011
17  * Author: christiana
18  */
19 
20 #include "cxFileSelectWidget.h"
21 #include <QtWidgets>
22 
23 #include "cxTypeConversions.h"
24 #include <iostream>
25 #include <QHBoxLayout>
26 #include <QtWidgets>
27 
28 namespace cx
29 {
30 
32 {
33  mNameFilters << "*.mhd";
34  mFolderDepth = 2;
35 
36  QHBoxLayout* dataLayout = new QHBoxLayout(this);
37  dataLayout->setMargin(0);
38  mDataComboBox = new QComboBox(this);
39  connect(mDataComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(currentDataComboIndexChanged(int)));
40 
41  mSelectDataAction = new QAction(QIcon(":/icons/open.png"), tr("&Select data"), this);
42  connect(mSelectDataAction, SIGNAL(triggered()), this, SLOT(selectData()));
43  mSelectDataButton = new QToolButton(this);
44  mSelectDataButton->setDefaultAction(mSelectDataAction);
45 
46  dataLayout->addWidget(mDataComboBox);
47  dataLayout->addWidget(mSelectDataButton);
48 }
49 
51 {
52  mFolderDepth = depth;
53 }
54 
56 {
57  return mFilename;
58 }
59 
61 {
62  mFilename = name;
63 
64  if (QFileInfo(mFilename).isDir())
65  {
66  mRootPaths.clear();
67  mRootPaths << QFileInfo(mFilename).dir().absolutePath();
68  }
69  else
70  {
71  if (mRootPaths.isEmpty())
72  mRootPaths << QFileInfo(mFilename).dir().absolutePath();
73  }
74 
75 
76 
77  this->refresh();
78 }
79 
80 void FileSelectWidget::setNameFilter(QStringList filter)
81 {
82  mNameFilters = filter;
83 }
84 
85 void FileSelectWidget::setPath(QString path)
86 {
87  this->setPaths(QStringList() << path);
88 // mRootPath = path;
89 // // std::cout << "FileSelectWidget::setPath root" << mRootPath << std::endl;
90 // this->refresh();
91 }
92 
93 void FileSelectWidget::setPaths(QStringList paths)
94 {
95  mRootPaths = paths;
96  this->refresh();
97 }
98 
99 void FileSelectWidget::selectData()
100 {
101  QString filter = mNameFilters.join(";;");
102  QString folder;
103  if (!mRootPaths.isEmpty())
104  folder = mRootPaths.front();
105  QString filename = QFileDialog::getOpenFileName(this, QString(tr("Select file")), folder, filter);
106 
107  if (filename.isEmpty())
108  return;
109 
110  mFilename = filename;
111  // std::cout << this << " selectData " << mFilename << std::endl;
112 
113  this->refresh();
114  emit fileSelected(mFilename);
115 }
116 
118 {
119  QStringList files;
120  for (int i=0; i<mRootPaths.size(); ++i)
121  files << this->getAllFiles(mRootPaths[i], mFolderDepth);
122  return files;
123 }
124 
125 
126 QStringList FileSelectWidget::getAllFiles(QString folder, int depth)
127 {
128  QDir dir(folder);
129  QStringList files = dir.entryList(mNameFilters, QDir::Files);
130 
131  QStringList retval;
132  for (int i = 0; i < files.size(); ++i)
133  {
134  retval << (dir.absolutePath() + "/" + files[i]);
135  }
136  QStringList folders = dir.entryList(QStringList(), QDir::AllDirs | QDir::NoDotAndDotDot);
137 
138  if (depth>0)
139  {
140  for (int i = 0; i < folders.size(); ++i)
141  {
142  files = this->getAllFiles(folder + "/" + folders[i], depth-1);
143  retval.append(files);
144  }
145  }
146 
147  return retval;
148 }
149 
151 {
152  this->updateComboBox();
153 }
154 
155 void FileSelectWidget::updateComboBox()
156 {
157  mDataComboBox->blockSignals(true);
158  mDataComboBox->clear();
159 
160  QStringList files;
161  for (int i=0; i<mRootPaths.size(); ++i)
162  files << this->getAllFiles(mRootPaths[i], mFolderDepth);
163 
164  for (int i = 0; i < files.size(); ++i)
165  {
166  mDataComboBox->addItem(QFileInfo(files[i]).fileName(), files[i]);
167  }
168  mDataComboBox->setCurrentIndex(-1);
169  for (int i = 0; i < mDataComboBox->count(); ++i)
170  {
171  if (mDataComboBox->itemData(i) == mFilename)
172  mDataComboBox->setCurrentIndex(i);
173  }
174 
175  if (!mFilename.isEmpty() && mDataComboBox->currentIndex() < 0 && !files.contains(mFilename))
176  {
177  mDataComboBox->addItem(QFileInfo(mFilename).fileName(), mFilename);
178  mDataComboBox->setCurrentIndex(mDataComboBox->count() - 1);
179  }
180 
181  mDataComboBox->setToolTip(mFilename);
182 
183  mDataComboBox->blockSignals(false);
184 }
185 
186 void FileSelectWidget::currentDataComboIndexChanged(int index)
187 {
188  if (index < 0)
189  return;
190 
191  mFilename = mDataComboBox->itemData(index).toString();
192  emit fileSelected(mFilename);
193 }
194 
195 }
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::FileSelectWidget::setFilename
void setFilename(QString name)
Definition: cxFileSelectWidget.cpp:60
cx::FileSelectWidget::FileSelectWidget
FileSelectWidget(QWidget *parent)
Definition: cxFileSelectWidget.cpp:31
cx::FileSelectWidget::getAllFiles
QStringList getAllFiles()
Definition: cxFileSelectWidget.cpp:117
cxFileSelectWidget.h
cx::FileSelectWidget::setNameFilter
void setNameFilter(QStringList filter)
Definition: cxFileSelectWidget.cpp:80
cx::FileSelectWidget::setPaths
void setPaths(QStringList paths)
Definition: cxFileSelectWidget.cpp:93
cx::FileSelectWidget::setPath
void setPath(QString path)
Definition: cxFileSelectWidget.cpp:85
cxTypeConversions.h
cx::FileSelectWidget::getFilename
QString getFilename() const
Definition: cxFileSelectWidget.cpp:55
cx::FileSelectWidget::refresh
void refresh()
Definition: cxFileSelectWidget.cpp:150
cx::FileSelectWidget::fileSelected
void fileSelected(QString name)
cx::FileSelectWidget::setFolderDepth
void setFolderDepth(int depth)
Definition: cxFileSelectWidget.cpp:50