Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
filenameWidget.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //== INCLUDES =================================================================
51 #include <QHBoxLayout>
52 #include <QLineEdit>
53 #include <QPushButton>
54 
55 #include "filenameWidget.hh"
56 
57 //== NAMESPACES ===============================================================
58 namespace VSI {
59 
60 //=============================================================================
61 //
62 // CLASS VSI::FilenameWidget - IMPLEMENTATION
63 //
64 //=============================================================================
65 
67 FilenameWidget::FilenameWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
68  TypeWidget (_hints, _typeName, _parent),
69  default_ (""),
70  line_ (0),
71  selectButton_ (0),
72  mode_ (QFileDialog::AcceptOpen),
73  filter_ ("Any File (*)"),
74  defaultSuffix_ (""),
75  dirOnly_ (false)
76 {
77  QHBoxLayout *hL = new QHBoxLayout;
78 
79  line_ = new QLineEdit;
80 
81  if (_hints.contains ("default"))
82  default_ = _hints["default"];
83 
84  line_->setText (default_);
85 
86  hL->addWidget (line_);
87 
88  selectButton_ = new QPushButton ("Select");
89 
90  connect (selectButton_, SIGNAL (pressed ()), this, SLOT (showDialog ()));
91  hL->addWidget (selectButton_);
92 
93  setLayout (hL);
94 
95  if (_typeName == "Directory")
96  dirOnly_ = true;
97 
98  if (_hints.contains ("mode") && _hints["mode"] == "Save")
99  mode_ = QFileDialog::AcceptSave;
100 
101  if (_hints.contains ("filter"))
102  filter_ = _hints["filter"];
103 
104  if (_hints.contains ("default_suffix"))
105  defaultSuffix_ = _hints["default_suffix"];
106 
107 }
108 
109 //------------------------------------------------------------------------------
110 
113 {
114 }
115 
116 //------------------------------------------------------------------------------
117 
120 {
121  return "\"" + line_->text () + "\"";
122 }
123 
124 //------------------------------------------------------------------------------
125 
127 void FilenameWidget::fromValue(QString _from)
128 {
129  if (_from.isEmpty ())
130  {
131  line_->clear ();
132  return;
133  }
134 
135  _from.remove (0, 1);
136  _from.remove (_from.length () - 1, 1);
137 
138  line_->setText (_from);
139 }
140 
141 //------------------------------------------------------------------------------
142 
145 {
146  line_->setText (default_);
147 }
148 
149 //------------------------------------------------------------------------------
150 
151 // Show file dialog
152 void FilenameWidget::showDialog()
153 {
154 
155  QFileDialog d (this, tr("Select File"), QString() , filter_);
156  d.setAcceptMode (mode_);
157  d.setDefaultSuffix (defaultSuffix_);
158  if (dirOnly_)
159  {
160  d.setFileMode (QFileDialog::Directory);
161  d.setOption (QFileDialog::ShowDirsOnly, true);
162  }
163 
164  if (QDialog::Accepted == d.exec ())
165  line_->setText (d.selectedFiles ()[0]);
166 }
167 
168 //------------------------------------------------------------------------------
169 }
FilenameWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
QString toValue()
Convert current value to string.
void toDefault()
Reset to default.
void fromValue(QString _from)
Read value from string.
~FilenameWidget()
Destructor.