YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
newapplicationwizard.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
7#include <QGridLayout>
8#include <QFileDialog>
9#include <QFileInfo>
10#include <QMessageBox>
11
12#include <dirent.h>
14
15using namespace yarp::manager;
16
17inline bool absolute(const char *path) { //copied from yarp_OS ResourceFinder.cpp
18 if (path[0]=='/'||path[0]=='\\') {
19 return true;
20 }
21 std::string str(path);
22 if (str.length()>1) {
23 if (str[1]==':') {
24 return true;
25 }
26 }
27 return false;
28}
29
30
31NewApplicationWizard::NewApplicationWizard(yarp::os::Property *config, bool _saveAs):alreadyExists(false), saveAs(_saveAs)
32{
33 auto* page = new CustomWizardPage;
34
35 this->m_config = config;
36 page->setTitle("Application Properties");
37 page->setFinalPage(true);
38
39 nameLbl = new QLabel("Name: ");
40 nameLbl->setWordWrap(true);
41
42 nameEdit = new QLineEdit();
43 nameEdit->setPlaceholderText("ApplicationName");
44
45 descrLbl = new QLabel("Description: ");
46 descrLbl->setWordWrap(true);
47
48 descrEdit = new QLineEdit();
49 descrEdit->setPlaceholderText("Description");
50
51 versionLbl = new QLabel("Version: ");
52 versionLbl->setWordWrap(true);
53 versionEdit = new QLineEdit();
54 versionEdit->setPlaceholderText("1.0");
55
56 authLbl = new QLabel("Authors: ");
57 authLbl->setWordWrap(true);
58
59 authEdit = new QLineEdit();
60
61 fileLbl = new QLabel("File: ");
62 fileLbl->setWordWrap(true);
63
64 fileEdit = new QLineEdit();
65 fileEdit->setReadOnly(true);
66
67
68 folderLbl = new QLabel("Folder: ");
69 folderLbl->setWordWrap(true);
70
71 folderCombo = new QComboBox();
72 folderCombo->setEditable(false);
73 folderCombo->setDisabled(saveAs);
74
75
76
77 browseBtn = new QPushButton("...");
78 browseBtn->setDisabled(saveAs);
79
80
81
82
83 auto* layout = new QGridLayout;
84 layout->addWidget(nameLbl,0,0);
85 layout->addWidget(nameEdit,0,1);
86
87 layout->addWidget(descrLbl,1,0);
88 layout->addWidget(descrEdit,1,1);
89
90 layout->addWidget(versionLbl,2,0);
91 layout->addWidget(versionEdit,2,1);
92
93 layout->addWidget(authLbl,3,0);
94 layout->addWidget(authEdit,3,1);
95
96 layout->addWidget(fileLbl,4,0);
97 layout->addWidget(fileEdit,4,1);
98
99 layout->addWidget(folderLbl,5,0);
100 layout->addWidget(folderCombo,5,1);
101 layout->addWidget(browseBtn,5,2);
102
103 page->setLayout(layout);
104
105 page->registerField("fileName*",fileEdit);
106 addPage(page);
107
108
109
110 const std::string directorySeparator{yarp::conf::filesystem::preferred_separator};
111 if(m_config->check("apppath")){
112 std::string basepath=m_config->check("ymanagerini_dir", yarp::os::Value("")).asString();
113 std::string appPaths(m_config->find("apppath").asString());
114 std::string strPath;
115
116 do
117 {
118 std::string::size_type pos=appPaths.find(';');
119 strPath=appPaths.substr(0, pos);
120 trimString(strPath);
121 if (!absolute(strPath.c_str())) {
122 strPath = basepath + strPath;
123 }
124
125 if ((strPath.rfind(directorySeparator) == std::string::npos) || (strPath.rfind(directorySeparator) != strPath.size() - 1)) {
126 strPath = strPath + std::string(directorySeparator);
127 }
128 folderCombo->addItem(QString("%1").arg(strPath.c_str()));
129
130 if (pos == std::string::npos) {
131 break;
132 }
133 appPaths=appPaths.substr(pos+1);
134 }
135 while (appPaths!="");
136 }
137 if (m_config->check("yarpdatahome")){
138 std::string appPaths(m_config->find("apppath").asString());
139 std::string homePath=m_config->find("yarpdatahome").asString();
140
141 homePath += std::string(directorySeparator) + std::string("applications");
142
143 if (appPaths.find(homePath) == std::string::npos) {
144 folderCombo->addItem(QString("%1").arg(homePath.c_str()));
145 }
146 }
147
148 if(folderCombo->count() <= 0){
149 folderCombo->addItem(QDir::homePath());
150 }
151
152
153 connect(browseBtn,SIGNAL(clicked()),this,SLOT(onBrowse()));
154 connect(nameEdit,SIGNAL(textChanged(QString)),this,SLOT(onNameChanged(QString)));
155 connect(folderCombo, SIGNAL(activated(int)), this, SLOT(onSwitchCall()));
156
157}
158
159void NewApplicationWizard::checkFileAlreadyExists(){
160 buildFileName();
161 if (fileExists(this->fileName))
162 {
163 fileEdit->setStyleSheet("color: #FF0000");
164 alreadyExists = true;
165 }
166 else
167 {
168 fileEdit->setStyleSheet("color: #000000");
169 alreadyExists = false;
170 }
171
172}
173
174
175void NewApplicationWizard::onNameChanged(QString name)
176{
177 if (!name.isEmpty())
178 {
179 fileEdit->setText(QString("%1.xml").arg(name.toLatin1().data()));
180 checkFileAlreadyExists();
181 }
182 else
183 {
184 fileEdit->setText("");
185 }
186}
187
188void NewApplicationWizard::onSwitchCall()
189{
190 checkFileAlreadyExists();
191}
192
193bool NewApplicationWizard::fileExists(QString path) {
194 QFileInfo check_file(path);
195 // check if file exists
196 if (check_file.exists() && check_file.isFile())
197 {
198 return true;
199 }
200 else
201 {
202 return false;
203 }
204}
205
206void NewApplicationWizard::buildFileName(){
207 QString sep="";
208 //checking if the path terminate with / or not
209 if (folderCombo->currentText().at(folderCombo->currentText().size()-1) != '/')
210 {
212 }
213 this->fileName = QString("%1"+sep+"%2").arg(folderCombo->currentText().toLatin1().data()).arg(fileEdit->text().toLatin1().data());
214}
215
216
217void NewApplicationWizard::onBrowse( )
218{
219 QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
220 folderCombo->currentText(),
221 QFileDialog::ShowDirsOnly
222 | QFileDialog::DontResolveSymlinks);
223
224
225 if(!dir.isEmpty()){
226 folderCombo->addItem(dir);
227 folderCombo->setCurrentText(dir);
228 emit folderCombo->activated(folderCombo->count());
229 }
230
231}
232
233
235{
236 if (nameEdit->text().isEmpty()){
237 name = nameEdit->placeholderText();
238 }else{
239 name = nameEdit->text();
240 }
241 if (descrEdit->text().isEmpty()){
242 description = descrEdit->placeholderText();
243 }else{
244 description = descrEdit->text();
245 }
246 if (versionEdit->text().isEmpty()){
247 version = versionEdit->placeholderText();
248 }else{
249 version = versionEdit->text();
250 }
251 buildFileName();
252 if (alreadyExists)
253 {
254 QMessageBox::StandardButton reply;
255 reply = QMessageBox::question(this, "Quit", "The file chosen already exists, do you want to overwrite it?", QMessageBox::Yes|QMessageBox::No);
256 if (reply == QMessageBox::No)
257 {
258 QDialog::reject();
259 return;
260 }
261 }
262 QDialog::accept();
263}
int SIGNAL(int pid, int signum)
constexpr auto sep
Definition Run.cpp:99
NewApplicationWizard(yarp::os::Property *config, bool _saveAs=false)
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
A single value (typically within a Bottle).
Definition Value.h:43
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
static constexpr value_type preferred_separator
Definition filesystem.h:21
void trimString(std::string &str)
Definition utility.cpp:403
bool absolute(const char *path)