NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 /*
35  * sscFileSelectWidget.cpp
36  *
37  * Created on: May 6, 2011
38  * Author: christiana
39  */
40 
41 #include "cxFileSelectWidget.h"
42 #include <QtWidgets>
43 
44 #include "cxTypeConversions.h"
45 #include <iostream>
46 #include <QHBoxLayout>
47 #include <QtWidgets>
48 
49 namespace cx
50 {
51 
53 {
54  mNameFilters << "*.mhd";
55  mFolderDepth = 2;
56 
57  QHBoxLayout* dataLayout = new QHBoxLayout(this);
58  dataLayout->setMargin(0);
59  mDataComboBox = new QComboBox(this);
60  connect(mDataComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(currentDataComboIndexChanged(int)));
61 
62  mSelectDataAction = new QAction(QIcon(":/icons/open.png"), tr("&Select data"), this);
63  connect(mSelectDataAction, SIGNAL(triggered()), this, SLOT(selectData()));
64  mSelectDataButton = new QToolButton(this);
65  mSelectDataButton->setDefaultAction(mSelectDataAction);
66 
67  dataLayout->addWidget(mDataComboBox);
68  dataLayout->addWidget(mSelectDataButton);
69 }
70 
72 {
73  mFolderDepth = depth;
74 }
75 
77 {
78  return mFilename;
79 }
80 
82 {
83  mFilename = name;
84 
85  if (QFileInfo(mFilename).isDir())
86  {
87  mRootPaths.clear();
88  mRootPaths << QFileInfo(mFilename).dir().absolutePath();
89  }
90  else
91  {
92  if (mRootPaths.isEmpty())
93  mRootPaths << QFileInfo(mFilename).dir().absolutePath();
94  }
95 
96 
97 
98  this->refresh();
99 }
100 
101 void FileSelectWidget::setNameFilter(QStringList filter)
102 {
103  mNameFilters = filter;
104 }
105 
106 void FileSelectWidget::setPath(QString path)
107 {
108  this->setPaths(QStringList() << path);
109 // mRootPath = path;
110 // // std::cout << "FileSelectWidget::setPath root" << mRootPath << std::endl;
111 // this->refresh();
112 }
113 
114 void FileSelectWidget::setPaths(QStringList paths)
115 {
116  mRootPaths = paths;
117  this->refresh();
118 }
119 
120 void FileSelectWidget::selectData()
121 {
122  QString filter = mNameFilters.join(";;");
123  QString folder;
124  if (!mRootPaths.isEmpty())
125  folder = mRootPaths.front();
126  QString filename = QFileDialog::getOpenFileName(this, QString(tr("Select file")), folder, filter);
127 
128  if (filename.isEmpty())
129  return;
130 
131  mFilename = filename;
132  // std::cout << this << " selectData " << mFilename << std::endl;
133 
134  this->refresh();
135  emit fileSelected(mFilename);
136 }
137 
139 {
140  QStringList files;
141  for (int i=0; i<mRootPaths.size(); ++i)
142  files << this->getAllFiles(mRootPaths[i], mFolderDepth);
143  return files;
144 }
145 
146 
147 QStringList FileSelectWidget::getAllFiles(QString folder, int depth)
148 {
149  QDir dir(folder);
150  QStringList files = dir.entryList(mNameFilters, QDir::Files);
151 
152  QStringList retval;
153  for (int i = 0; i < files.size(); ++i)
154  {
155  retval << (dir.absolutePath() + "/" + files[i]);
156  }
157  QStringList folders = dir.entryList(QStringList(), QDir::AllDirs | QDir::NoDotAndDotDot);
158 
159  if (depth>0)
160  {
161  for (int i = 0; i < folders.size(); ++i)
162  {
163  files = this->getAllFiles(folder + "/" + folders[i], depth-1);
164  retval.append(files);
165  }
166  }
167 
168  return retval;
169 }
170 
172 {
173  this->updateComboBox();
174 }
175 
176 void FileSelectWidget::updateComboBox()
177 {
178  mDataComboBox->blockSignals(true);
179  mDataComboBox->clear();
180 
181  QStringList files;
182  for (int i=0; i<mRootPaths.size(); ++i)
183  files << this->getAllFiles(mRootPaths[i], mFolderDepth);
184 
185  for (int i = 0; i < files.size(); ++i)
186  {
187  mDataComboBox->addItem(QFileInfo(files[i]).fileName(), files[i]);
188  }
189  mDataComboBox->setCurrentIndex(-1);
190  for (int i = 0; i < mDataComboBox->count(); ++i)
191  {
192  if (mDataComboBox->itemData(i) == mFilename)
193  mDataComboBox->setCurrentIndex(i);
194  }
195 
196  if (!mFilename.isEmpty() && mDataComboBox->currentIndex() < 0 && !files.contains(mFilename))
197  {
198  mDataComboBox->addItem(QFileInfo(mFilename).fileName(), mFilename);
199  mDataComboBox->setCurrentIndex(mDataComboBox->count() - 1);
200  }
201 
202  mDataComboBox->setToolTip(mFilename);
203 
204  mDataComboBox->blockSignals(false);
205 }
206 
207 void FileSelectWidget::currentDataComboIndexChanged(int index)
208 {
209  if (index < 0)
210  return;
211 
212  mFilename = mDataComboBox->itemData(index).toString();
213  emit fileSelected(mFilename);
214 }
215 
216 }
void fileSelected(QString name)
void setNameFilter(QStringList filter)
void setFolderDepth(int depth)
QString getFilename() const
void setFilename(QString name)
void setPath(QString path)
void setPaths(QStringList paths)
FileSelectWidget(QWidget *parent)