CustusX  15.3.3-beta
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 
138 QStringList FileSelectWidget::getAllFiles(QString folder, int depth)
139 {
140  QDir dir(folder);
141  QStringList files = dir.entryList(mNameFilters, QDir::Files);
142 
143  QStringList retval;
144  for (int i = 0; i < files.size(); ++i)
145  {
146  retval << (dir.absolutePath() + "/" + files[i]);
147  }
148  QStringList folders = dir.entryList(QStringList(), QDir::AllDirs | QDir::NoDotAndDotDot);
149 
150  if (depth>0)
151  {
152  for (int i = 0; i < folders.size(); ++i)
153  {
154  files = this->getAllFiles(folder + "/" + folders[i], depth-1);
155  retval.append(files);
156  }
157  }
158 
159  return retval;
160 }
161 
163 {
164  this->updateComboBox();
165 }
166 
167 void FileSelectWidget::updateComboBox()
168 {
169  mDataComboBox->blockSignals(true);
170  mDataComboBox->clear();
171 
172  QStringList files;
173  for (int i=0; i<mRootPaths.size(); ++i)
174  files << this->getAllFiles(mRootPaths[i], mFolderDepth);
175 
176  for (int i = 0; i < files.size(); ++i)
177  {
178  mDataComboBox->addItem(QFileInfo(files[i]).fileName(), files[i]);
179  }
180  mDataComboBox->setCurrentIndex(-1);
181  for (int i = 0; i < mDataComboBox->count(); ++i)
182  {
183  if (mDataComboBox->itemData(i) == mFilename)
184  mDataComboBox->setCurrentIndex(i);
185  }
186 
187  if (!mFilename.isEmpty() && mDataComboBox->currentIndex() < 0 && !files.contains(mFilename))
188  {
189  mDataComboBox->addItem(QFileInfo(mFilename).fileName(), mFilename);
190  mDataComboBox->setCurrentIndex(mDataComboBox->count() - 1);
191  }
192 
193  mDataComboBox->setToolTip(mFilename);
194 
195  mDataComboBox->blockSignals(false);
196 }
197 
198 void FileSelectWidget::currentDataComboIndexChanged(int index)
199 {
200  if (index < 0)
201  return;
202 
203  mFilename = mDataComboBox->itemData(index).toString();
204  emit fileSelected(mFilename);
205 }
206 
207 }
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)