Skip to content

Commit

Permalink
QmlDesigner: Implement renaming assets folders
Browse files Browse the repository at this point in the history
Fixes: QDS-6123
Change-Id: I1923f78fe2a6739f1cf32fd86c0919965190d8ca
Reviewed-by: <[email protected]>
Reviewed-by: Miikka Heikkinen <[email protected]>
Reviewed-by: Samuel Ghinet <[email protected]>
Reviewed-by: Thomas Hartmann <[email protected]>
  • Loading branch information
mbadri-qt committed Feb 3, 2022
1 parent 2396881 commit 10fe715
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
105 changes: 105 additions & 0 deletions share/qtcreator/qmldesigner/itemLibraryQmlSources/Assets.qml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,103 @@ Item {
selectedAssetsChanged()
}

RegExpValidator {
id: folderNameValidator
regExp: /^(\w[^*/><?\\|:]*)$/
}

Dialog {
id: renameFolderDialog

title: qsTr("Rename folder")
anchors.centerIn: parent
closePolicy: Popup.CloseOnEscape
implicitWidth: 280
modal: true

property bool renameError: false

contentItem: Column {
spacing: 2

StudioControls.TextField {
id: folderRename

actionIndicator.visible: false
translationIndicator.visible: false
width: renameFolderDialog.width - 12
validator: folderNameValidator

onEditChanged: renameFolderDialog.renameError = false
Keys.onEnterPressed: btnRename.onClicked()
Keys.onReturnPressed: btnRename.onClicked()
}

Text {
text: qsTr("Folder Name cannot be empty.")
color: "#ff0000"
visible: folderRename.text === "" && !renameFolderDialog.renameError
}

Text {
text: qsTr("Could not rename directory. Make sure no folder with the same name exists.")
wrapMode: Text.WordWrap
width: renameFolderDialog.width
color: "#ff0000"
visible: renameFolderDialog.renameError
}

Item { // spacer
width: 1
height: 10
}

Text {
text: qsTr("If the folder has assets in use, renaming it might cause the project to not work correctly.")
color: StudioTheme.Values.themeTextColor
wrapMode: Text.WordWrap
width: renameFolderDialog.width
leftPadding: 10
rightPadding: 10
}

Item { // spacer
width: 1
height: 20
}

Row {
anchors.right: parent.right

Button {
id: btnRename

text: qsTr("Rename")
enabled: folderRename.text !== ""
onClicked: {
var success = assetsModel.renameFolder(contextDir.dirPath, folderRename.text)
if (success)
renameFolderDialog.accept()

renameFolderDialog.renameError = !success
}
}

Button {
text: qsTr("Cancel")
onClicked: renameFolderDialog.reject()
}
}
}

onOpened: {
folderRename.text = contextDir.dirName
folderRename.selectAll()
folderRename.forceActiveFocus()
renameFolderDialog.renameError = false
}
}

Dialog {
id: newFolderDialog

Expand All @@ -113,6 +210,7 @@ Item {

actionIndicator.visible: false
translationIndicator.visible: false
validator: folderNameValidator

Keys.onEnterPressed: btnCreate.onClicked()
Keys.onReturnPressed: btnCreate.onClicked()
Expand Down Expand Up @@ -261,6 +359,13 @@ Item {
height: visible ? StudioTheme.Values.border : 0
}

StudioControls.MenuItem {
text: qsTr("Rename Folder")
visible: isDirContextMenu
height: visible ? implicitHeight : 0
onTriggered: renameFolderDialog.open()
}

StudioControls.MenuItem {
text: qsTr("New Folder")
onTriggered: newFolderDialog.open()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ void ItemLibraryAssetsModel::deleteFile(const QString &filePath)
}
}

bool ItemLibraryAssetsModel::renameFolder(const QString &folderPath, const QString &newName)
{
QDir dir{folderPath};
QString oldName = dir.dirName();

if (oldName == newName)
return true;

dir.cdUp();
return dir.rename(oldName, newName);
}

void ItemLibraryAssetsModel::addNewFolder(const QString &folderPath)
{
QString iterPath = folderPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ItemLibraryAssetsModel : public QAbstractListModel
Q_INVOKABLE void toggleExpandAll(bool expand);
Q_INVOKABLE DirExpandState getAllExpandedState() const;
Q_INVOKABLE void deleteFile(const QString &filePath);
Q_INVOKABLE bool renameFolder(const QString &folderPath, const QString &newName);
Q_INVOKABLE void addNewFolder(const QString &folderPath);
Q_INVOKABLE void deleteFolder(const QString &folderPath);
Q_INVOKABLE QObject *rootDir() const;
Expand Down

0 comments on commit 10fe715

Please sign in to comment.