-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy icon when editing cached remote PROTO (#5414)
* Copy icon when editing web PROTO * Fix cppcheck error
- Loading branch information
1 parent
9a55b85
commit 420730c
Showing
6 changed files
with
152 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 1996-2022 Cyberbotics Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "WbProtoIcon.hpp" | ||
|
||
#include "WbDownloadManager.hpp" | ||
#include "WbDownloader.hpp" | ||
#include "WbFileUtil.hpp" | ||
#include "WbNetwork.hpp" | ||
#include "WbStandardPaths.hpp" | ||
#include "WbUrl.hpp" | ||
|
||
#include <QtCore/QDir> | ||
#include <QtCore/QUrl> | ||
|
||
WbProtoIcon::WbProtoIcon(const QString &modelName, const QString &protoPath, QObject *parent) : | ||
QObject(parent), | ||
mPath(QString("%1icons/%2.png").arg(QUrl(protoPath).adjusted(QUrl::RemoveFilename).toString()).arg(modelName)), | ||
mModelName(modelName), | ||
mDownloader(NULL), | ||
mReady(true) { | ||
if (WbUrl::isWeb(mPath)) { | ||
if (WbNetwork::instance()->isCachedWithMapUpdate(mPath)) | ||
mPath = WbNetwork::instance()->get(mPath); | ||
else { | ||
mReady = false; | ||
mDownloader = WbDownloadManager::instance()->createDownloader(QUrl(mPath), this); | ||
connect(mDownloader, &WbDownloader::complete, this, &WbProtoIcon::updateIcon); | ||
mDownloader->download(); | ||
} | ||
} else if (WbUrl::isLocalUrl(mPath)) | ||
mPath = QDir::cleanPath(mPath.replace("webots://", WbStandardPaths::webotsHomePath())); | ||
} | ||
|
||
void WbProtoIcon::updateIcon() { | ||
assert(mDownloader); | ||
if (mDownloader->error().isEmpty()) | ||
mPath = WbNetwork::instance()->get(mDownloader->url().toString()); | ||
else | ||
mPath = QString(); | ||
// else failure downloading or file does not exist (404) | ||
|
||
mReady = true; | ||
emit iconReady(mPath); | ||
} | ||
|
||
void WbProtoIcon::duplicate(QDir destinationDir) { | ||
assert(mReady); | ||
if (!QFile::exists(mPath)) | ||
return; | ||
|
||
if (destinationDir.exists("icons") || destinationDir.mkdir("icons")) | ||
WbFileUtil::forceCopy(mPath, QString("%1/icons/%2.png").arg(destinationDir.absolutePath()).arg(mModelName)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 1996-2022 Cyberbotics Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef WB_PROTO_ICON_HPP | ||
#define WB_PROTO_ICON_HPP | ||
|
||
// | ||
// Description: object representing the PROTO icon and handling its download | ||
// | ||
|
||
#include <QtCore/QObject> | ||
|
||
class WbDownloader; | ||
|
||
class QDir; | ||
|
||
class WbProtoIcon : public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
explicit WbProtoIcon(const QString &modelName, const QString &protoPath, QObject *parent = NULL); | ||
|
||
const QString &path() const { return mPath; } | ||
bool isReady() const { return mReady; } | ||
|
||
void duplicate(QDir destinationDir); | ||
|
||
signals: | ||
void iconReady(const QString &path); | ||
|
||
private: | ||
QString mPath; | ||
const QString mModelName; | ||
WbDownloader *mDownloader; | ||
bool mReady; | ||
|
||
private slots: | ||
void updateIcon(); | ||
}; | ||
|
||
#endif |