Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkFileChooser.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "QmitkFileChooser.h"
18 
19 #include <QApplication>
20 #include <QFileDialog>
21 #include <QHBoxLayout>
22 #include <QLineEdit>
23 #include <QPushButton>
24 #include <QVBoxLayout>
25 
26 QmitkFileChooser::QmitkFileChooser(QWidget *parent, Qt::WindowFlags f)
27  : QWidget(parent, f),
28  m_SelectDir(false),
29  m_FileMustExist(true),
30  m_SelectFile(new QPushButton("Select File")),
31  m_File(new QLineEdit)
32 {
33  m_File->setReadOnly(true);
34  this->SetHorizotalLayout(false);
35 
36  connect(m_SelectFile, SIGNAL(clicked(bool)), this, SLOT(OnSelectFileClicked(bool)));
37  connect(m_File, SIGNAL(editingFinished()), this, SLOT(OnFileEditingFinished()));
38 }
39 
40 void QmitkFileChooser::SetHorizotalLayout(bool horizontalLayout)
41 {
42  QBoxLayout *layout = nullptr;
43  if (horizontalLayout)
44  layout = new QHBoxLayout;
45  else
46  layout = new QVBoxLayout;
47 
48  layout->setContentsMargins(0, 0, 0, 0);
49  layout->addWidget(m_File);
50  layout->addWidget(m_SelectFile);
51 
52  this->setLayout(layout);
53 }
54 
55 void QmitkFileChooser::SetSelectDir(bool selectDir)
56 {
57  m_SelectDir = selectDir;
58 }
59 
60 void QmitkFileChooser::SetFileMustExist(bool fileMustExist)
61 {
62  m_FileMustExist = fileMustExist;
63 }
64 
65 void QmitkFileChooser::SetReadOnly(bool ReadOnly)
66 {
67  m_File->setReadOnly(ReadOnly);
68 }
69 
70 void QmitkFileChooser::SetFile(const std::string &file)
71 {
72  QFileInfo info(QString::fromStdString(file));
73  if (info.exists() || m_FileMustExist == false)
74  {
75  m_File->setText(QString::fromStdString(file));
76  emit NewFileSelected(file);
77  }
78 }
79 
80 void QmitkFileChooser::SetFilePattern(const std::string &filepattern)
81 {
82  m_FilePattern = QString::fromStdString(filepattern);
83 }
84 
86 {
87  QFileInfo info(m_File->text());
88  return info.exists();
89 }
90 
91 std::string QmitkFileChooser::GetFile() const
92 {
93  return m_File->text().toStdString();
94 }
95 
97 {
98  QString filename;
99  if (m_SelectDir)
100  filename = QFileDialog::getExistingDirectory(QApplication::activeWindow(), "Open directory", m_File->text());
101  else
102  {
103  if (m_FileMustExist)
104  filename = QFileDialog::getOpenFileName(QApplication::activeWindow(), "Open file", m_File->text(), m_FilePattern);
105  else
106  filename = QFileDialog::getSaveFileName(QApplication::activeWindow(), "Open file", m_File->text(), m_FilePattern);
107  }
108 
109  if (!filename.isEmpty())
110  m_File->setText(filename);
111 
112  emit NewFileSelected(filename.toStdString());
113 }
114 
116 {
117  emit NewFileSelected(m_File->text().toStdString());
118 }
QPushButton * m_SelectFile
virtual void OnSelectFileClicked(bool)
virtual std::string GetFile() const
void NewFileSelected(const std::string &)
bool IsValidFile() const
virtual void OnFileEditingFinished()
static void info(const char *fmt,...)
Definition: svm.cpp:100
void SetFilePattern(const std::string &filepattern)
void SetReadOnly(bool ReadOnly)
static const std::string filename
QmitkFileChooser(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
void SetSelectDir(bool selectDir)
void SetFile(const std::string &file)
void SetFileMustExist(bool fileMustExist)
void SetHorizotalLayout(bool horizontalLayout)