NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxFileInputWidget.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 #include "cxFileInputWidget.h"
34 #include <QtWidgets>
35 
36 #include "cxTypeConversions.h"
37 #include <iostream>
38 
39 namespace cx
40 {
41 
42 FileInputWidget::FileInputWidget(QWidget* parent) : QWidget(parent)
43 {
44  mBasePath = "~";
45  mUseRelativePath = false;
46  mDescription = NULL;
47  mBrowseButton = NULL;
48  mFilenameEdit = NULL;
49 
50  mLayout = new QGridLayout(this);
51  mLayout->setMargin(0);
52 
53  mFilenameEdit = new QLineEdit(this);
54  mFilenameEdit->setToolTip("File name");
55  connect(mFilenameEdit, SIGNAL(editingFinished()), this, SIGNAL(fileChanged()));
56  connect(mFilenameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateColor()));
57  mLayout->addWidget(mFilenameEdit, 0, 1);
58 
59  mBrowseAction = new QAction(QIcon(":/icons/open.png"), "Browse", this);
60  mBrowseAction->setStatusTip("Browse the file system");
61  connect(mBrowseAction, SIGNAL(triggered()), this, SLOT(browse()));
62 
63  mBrowseButton = new QToolButton();
64  mBrowseButton->setDefaultAction(mBrowseAction);
65  mLayout->addWidget(mBrowseButton, 0, 2);
66 }
67 
69 {
70  if (!mDescription)
71  {
72  mDescription = new QLabel(this);
73  mLayout->addWidget(mDescription, 0,0);
74  }
75 
76  mDescription->setText(text);
77 }
78 
79 void FileInputWidget::setFilename(QString text)
80 {
81  if (text==mFilenameEdit->text())
82  return;
83  mFilenameEdit->setText(text);
84  this->widgetHasBeenChanged();
85 }
86 
87 void FileInputWidget::widgetHasBeenChanged()
88 {
89  this->updateHelpInternal();
90  this->updateColor();
91  emit fileChanged();
92 }
93 
94 void FileInputWidget::setHelp(QString text)
95 {
96  mBaseHelp = text;
97  this->updateHelpInternal();
98 }
99 
100 void FileInputWidget::updateHelpInternal()
101 {
102  QString text = QString("%1\n%2").arg(this->getAbsoluteFilename()).arg(mBaseHelp);
103  mFilenameEdit->setToolTip(text);
104  mFilenameEdit->setStatusTip(text);
105 }
106 
108 {
109  mBrowseAction->setToolTip(text);
110  mBrowseAction->setStatusTip(text);
111 }
112 
114 {
115  mBasePath = path;
116  this->widgetHasBeenChanged();
117 }
118 
119 void FileInputWidget::browse()
120 {
121  QString text = "Select file";
122  if (mUseRelativePath)
123  text = QString("Select file relative to %1").arg(mBasePath);
124 
125  QString filename = QFileDialog::getOpenFileName(this, text, mBasePath);
126  if (filename.isEmpty())
127  return;
128 
129  if (mUseRelativePath)
130  filename = QDir(mBasePath).relativeFilePath(filename);
131 
132  this->setFilename(filename);
133 }
134 
136 {
137  return mFilenameEdit->text();
138 }
139 
141 {
142  QString absolute = QDir(mBasePath).absoluteFilePath(this->getFilename().trimmed());
143  QString cleaned = QDir().cleanPath(absolute);
144  return cleaned;
145 }
146 
148 {
149  mUseRelativePath = on;
150  this->widgetHasBeenChanged();
151 }
152 
153 void FileInputWidget::updateColor()
154 {
155  QColor color = QColor("black");
156  if (!QFileInfo(this->getAbsoluteFilename()).exists())
157  color = QColor("red");
158 
159  QPalette p = mFilenameEdit->palette();
160  p.setColor(QPalette::Text, color);
161  mFilenameEdit->setPalette(p);
162 }
163 
164 
165 
166 
167 
168 } // namespace cx
void setDescription(QString text)
void setBasePath(QString path)
void setFilename(QString text)
void setUseRelativePath(bool on)
void setHelp(QString text)
FileInputWidget(QWidget *parent=0)
void setBrowseHelp(QString text)
QString getAbsoluteFilename() const
QString getFilename() const