Developer Documentation
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 //== INCLUDES =================================================================
45 #include <QHBoxLayout>
46 #include <QLineEdit>
47 #include <QPushButton>
48 
49 #include "filenameWidget.hh"
50 
51 //== NAMESPACES ===============================================================
52 namespace VSI {
53 
54 //=============================================================================
55 //
56 // CLASS VSI::FilenameWidget - IMPLEMENTATION
57 //
58 //=============================================================================
59 
61 FilenameWidget::FilenameWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
62  TypeWidget (_hints, _typeName, _parent),
63  default_ (""),
64  line_ (0),
65  selectButton_ (0),
66  mode_ (QFileDialog::AcceptOpen),
67  filter_ ("Any File (*)"),
68  defaultSuffix_ (""),
69  dirOnly_ (false)
70 {
71  QHBoxLayout *hL = new QHBoxLayout;
72 
73  line_ = new QLineEdit;
74 
75  if (_hints.contains ("default"))
76  default_ = _hints["default"];
77 
78  line_->setText (default_);
79 
80  hL->addWidget (line_);
81 
82  selectButton_ = new QPushButton ("Select");
83 
84  connect (selectButton_, SIGNAL (pressed ()), this, SLOT (showDialog ()));
85  hL->addWidget (selectButton_);
86 
87  setLayout (hL);
88 
89  if (_typeName == "Directory")
90  dirOnly_ = true;
91 
92  if (_hints.contains ("mode") && _hints["mode"] == "Save")
93  mode_ = QFileDialog::AcceptSave;
94 
95  if (_hints.contains ("filter"))
96  filter_ = _hints["filter"];
97 
98  if (_hints.contains ("default_suffix"))
99  defaultSuffix_ = _hints["default_suffix"];
100 
101 }
102 
103 //------------------------------------------------------------------------------
104 
106 FilenameWidget::~ FilenameWidget()
107 {
108 }
109 
110 //------------------------------------------------------------------------------
111 
114 {
115  return "\"" + line_->text () + "\"";
116 }
117 
118 //------------------------------------------------------------------------------
119 
121 void FilenameWidget::fromValue(QString _from)
122 {
123  if (_from.isEmpty ())
124  {
125  line_->clear ();
126  return;
127  }
128 
129  _from.remove (0, 1);
130  _from.remove (_from.length () - 1, 1);
131 
132  line_->setText (_from);
133 }
134 
135 //------------------------------------------------------------------------------
136 
139 {
140  line_->setText (default_);
141 }
142 
143 //------------------------------------------------------------------------------
144 
145 // Show file dialog
146 void FilenameWidget::showDialog()
147 {
148 
149  QFileDialog d (this, tr("Select File"), QString() , filter_);
150  d.setAcceptMode (mode_);
151  d.setDefaultSuffix (defaultSuffix_);
152  if (dirOnly_)
153  {
154  d.setFileMode (QFileDialog::Directory);
155  d.setOption (QFileDialog::ShowDirsOnly, true);
156  }
157 
158  if (QDialog::Accepted == d.exec ())
159  line_->setText (d.selectedFiles ()[0]);
160 }
161 
162 //------------------------------------------------------------------------------
163 }
FilenameWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
void fromValue(QString _from)
Read value from string.
QString toValue()
Convert current value to string.
void toDefault()
Reset to default.