1 /** -*- mode: c++ ; c-basic-offset: 2 -*-
2 * @file FolderParameter.cpp
3 * @author Sebastien Fourey
4 * @date Nov 2014
5 *
6 * @brief Declaration of the class AbstractParameter
7 *
8 * This file is part of the ZArt software's source code.
9 *
10 * Copyright Sebastien Fourey / GREYC Ensicaen (2010-...)
11 *
12 * https://foureys.users.greyc.fr/
13 *
14 * This software is a computer program whose purpose is to demonstrate
15 * the possibilities of the GMIC image processing language by offering the
16 * choice of several manipulations on a video stream acquired from a webcam. In
17 * other words, ZArt is a GUI for G'MIC real-time manipulations on the output
18 * of a webcam.
19 *
20 * This software is governed by the CeCILL license under French law and
21 * abiding by the rules of distribution of free software. You can use,
22 * modify and/ or redistribute the software under the terms of the CeCILL
23 * license as circulated by CEA, CNRS and INRIA at the following URL
24 * "http://www.cecill.info". See also the directory "Licence" which comes
25 * with this source code for the full text of the CeCILL license.
26 *
27 * As a counterpart to the access to the source code and rights to copy,
28 * modify and redistribute granted by the license, users are provided only
29 * with a limited warranty and the software's author, the holder of the
30 * economic rights, and the successive licensors have only limited
31 * liability.
32 *
33 * In this respect, the user's attention is drawn to the risks associated
34 * with loading, using, modifying and/or developing or reproducing the
35 * software by the user in light of its specific status of free software,
36 * that may mean that it is complicated to manipulate, and that also
37 * therefore means that it is reserved for developers and experienced
38 * professionals having in-depth computer knowledge. Users are therefore
39 * encouraged to load and test the software's suitability as regards their
40 * requirements in conditions enabling the security of their systems and/or
41 * data to be ensured and, more generally, to use and operate it in the
42 * same conditions as regards security.
43 *
44 * The fact that you are presently reading this means that you have had
45 * knowledge of the CeCILL license and that you accept its terms.
46 */
47 #include "FolderParameter.h"
48 #include <QFileDialog>
49 #include <QFileInfo>
50 #include <QFontMetrics>
51 #include <QGridLayout>
52 #include <QLabel>
53 #include <QPushButton>
54 #include <QWidget>
55 #include "Common.h"
56
FolderParameter(QDomNode node,QObject * parent)57 FolderParameter::FolderParameter(QDomNode node, QObject * parent) : AbstractParameter(parent), _node(node), _label(0), _button(0)
58 {
59 _name = node.attributes().namedItem("name").nodeValue();
60 _default = node.toElement().attribute("default", QString());
61 _value = node.toElement().attribute("savedValue", _default);
62 }
63
~FolderParameter()64 FolderParameter::~FolderParameter()
65 {
66 delete _label;
67 delete _button;
68 }
69
addTo(QWidget * widget,int row)70 void FolderParameter::addTo(QWidget * widget, int row)
71 {
72 QGridLayout * grid = dynamic_cast<QGridLayout *>(widget->layout());
73 if (!grid)
74 return;
75 delete _label;
76 delete _button;
77
78 QString buttonText;
79 if (_value.isEmpty()) {
80 buttonText = "...";
81 } else {
82 int w = widget->contentsRect().width() / 3;
83 QFontMetrics fm(widget->font());
84 buttonText = fm.elidedText(QFileInfo(_value).fileName(), Qt::ElideRight, w);
85 }
86 _button = new QPushButton(buttonText, widget);
87 grid->addWidget(_label = new QLabel(_name, widget), row, 0, 1, 1);
88 grid->addWidget(_button, row, 1, 1, 2);
89 connect(_button, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
90 }
91
textValue() const92 QString FolderParameter::textValue() const
93 {
94 if (_value.isEmpty())
95 return QString("\"\\\"\\\"\"");
96 else
97 return QString("\"%1\"").arg(_value);
98 }
99
unquotedTextValue() const100 QString FolderParameter::unquotedTextValue() const
101 {
102 return _value;
103 }
104
setValue(const QString & value)105 void FolderParameter::setValue(const QString & value)
106 {
107 _value = value;
108 if (_button) {
109 if (_value.isEmpty()) {
110 _button->setText("...");
111 } else {
112 int width = _button->contentsRect().width() - 10;
113 QFontMetrics fm(_button->font());
114 _button->setText(fm.elidedText(QFileInfo(_value).fileName(), Qt::ElideRight, width));
115 }
116 }
117 }
118
reset()119 void FolderParameter::reset()
120 {
121 _value = _default;
122 }
123
saveValueInDOM()124 void FolderParameter::saveValueInDOM()
125 {
126 _node.toElement().setAttribute("savedValue", _value);
127 }
128
onButtonPressed()129 void FolderParameter::onButtonPressed()
130 {
131 QString filename = QFileDialog::getExistingDirectory(0, "Select a directory", _value);
132 if (filename.isNull()) {
133 _value = "";
134 _button->setText("...");
135 } else {
136 _value = filename;
137 int w = _button->contentsRect().width() - 10;
138 QFontMetrics fm(_button->font());
139 _button->setText(fm.elidedText(QFileInfo(_value).fileName(), Qt::ElideRight, w));
140 }
141 emit valueChanged();
142 }
143