NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
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) 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 #include "cxFileInputWidget.h"
13 #include <QtWidgets>
14 
15 #include "cxTypeConversions.h"
16 #include <iostream>
17 
18 namespace cx
19 {
20 
21 FileInputWidget::FileInputWidget(QWidget* parent) : QWidget(parent)
22 {
23  mBasePath = "~";
24  mUseRelativePath = false;
25  mDescription = NULL;
26  mBrowseButton = NULL;
27  mFilenameEdit = NULL;
28 
29  mLayout = new QGridLayout(this);
30  mLayout->setMargin(0);
31 
32  mFilenameEdit = new QLineEdit(this);
33  mFilenameEdit->setToolTip("File name");
34  connect(mFilenameEdit, SIGNAL(editingFinished()), this, SIGNAL(fileChanged()));
35  connect(mFilenameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateColor()));
36  mLayout->addWidget(mFilenameEdit, 0, 1);
37 
38  mBrowseAction = new QAction(QIcon(":/icons/open.png"), "Browse", this);
39  mBrowseAction->setStatusTip("Browse the file system");
40  connect(mBrowseAction, SIGNAL(triggered()), this, SLOT(browse()));
41 
42  mBrowseButton = new QToolButton();
43  mBrowseButton->setDefaultAction(mBrowseAction);
44  mLayout->addWidget(mBrowseButton, 0, 2);
45 }
46 
48 {
49  if (!mDescription)
50  {
51  mDescription = new QLabel(this);
52  mLayout->addWidget(mDescription, 0,0);
53  }
54 
55  mDescription->setText(text);
56 }
57 
58 void FileInputWidget::setFilename(QString text)
59 {
60  if (text==mFilenameEdit->text())
61  return;
62  mFilenameEdit->setText(text);
63  this->widgetHasBeenChanged();
64 }
65 
66 void FileInputWidget::widgetHasBeenChanged()
67 {
68  this->updateHelpInternal();
69  this->updateColor();
70  emit fileChanged();
71 }
72 
73 void FileInputWidget::setHelp(QString text)
74 {
75  mBaseHelp = text;
76  this->updateHelpInternal();
77 }
78 
79 void FileInputWidget::updateHelpInternal()
80 {
81  QString text = QString("%1\n%2").arg(this->getAbsoluteFilename()).arg(mBaseHelp);
82  mFilenameEdit->setToolTip(text);
83  mFilenameEdit->setStatusTip(text);
84 }
85 
87 {
88  mBrowseAction->setToolTip(text);
89  mBrowseAction->setStatusTip(text);
90 }
91 
92 void FileInputWidget::setBasePath(QString path)
93 {
94  mBasePath = path;
95  this->widgetHasBeenChanged();
96 }
97 
98 void FileInputWidget::browse()
99 {
100  QString text = "Select file";
101  if (mUseRelativePath)
102  text = QString("Select file relative to %1").arg(mBasePath);
103 
104  QString filename = QFileDialog::getOpenFileName(this, text, mBasePath);
105  if (filename.isEmpty())
106  return;
107 
108  if (mUseRelativePath)
109  filename = QDir(mBasePath).relativeFilePath(filename);
110 
111  this->setFilename(filename);
112 }
113 
115 {
116  return mFilenameEdit->text();
117 }
118 
120 {
121  QString absolute = QDir(mBasePath).absoluteFilePath(this->getFilename().trimmed());
122  QString cleaned = QDir().cleanPath(absolute);
123  return cleaned;
124 }
125 
127 {
128  mUseRelativePath = on;
129  this->widgetHasBeenChanged();
130 }
131 
132 void FileInputWidget::updateColor()
133 {
134  QColor color = QColor("black");
135  if (!QFileInfo(this->getAbsoluteFilename()).exists())
136  color = QColor("red");
137 
138  QPalette p = mFilenameEdit->palette();
139  p.setColor(QPalette::Text, color);
140  mFilenameEdit->setPalette(p);
141 }
142 
143 
144 
145 
146 
147 } // namespace cx
cxFileInputWidget.h
cx::FileInputWidget::setUseRelativePath
void setUseRelativePath(bool on)
Definition: cxFileInputWidget.cpp:126
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::FileInputWidget::getFilename
QString getFilename() const
Definition: cxFileInputWidget.cpp:114
cx::FileInputWidget::fileChanged
void fileChanged()
cx::FileInputWidget::setHelp
void setHelp(QString text)
Definition: cxFileInputWidget.cpp:73
cx::FileInputWidget::setBasePath
void setBasePath(QString path)
Definition: cxFileInputWidget.cpp:92
cx::FileInputWidget::setBrowseHelp
void setBrowseHelp(QString text)
Definition: cxFileInputWidget.cpp:86
cxTypeConversions.h
cx::FileInputWidget::getAbsoluteFilename
QString getAbsoluteFilename() const
Definition: cxFileInputWidget.cpp:119
cx::FileInputWidget::FileInputWidget
FileInputWidget(QWidget *parent=0)
Definition: cxFileInputWidget.cpp:21
cx::FileInputWidget::setFilename
void setFilename(QString text)
Definition: cxFileInputWidget.cpp:58
cx::FileInputWidget::setDescription
void setDescription(QString text)
Definition: cxFileInputWidget.cpp:47