ResourceNodes: Add copy to clipboard actions.

One for :/prefix/file paths and one as a url qrc:///prefix/file

Task-number: QTCREATORBUG-11776

Change-Id: I98cc2fccf98a6bf07487352e9b10ae29e8347a45
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
Daniel Teske
2014-03-31 14:26:03 +02:00
parent a5c77222d8
commit 589945b7d3
5 changed files with 74 additions and 5 deletions

View File

@@ -51,6 +51,9 @@ const char C_RENAME_FILE[] = "ResourceEditor.RenameFile";
const char C_OPEN_EDITOR[] = "ResourceEditor.OpenEditor";
const char C_OPEN_TEXT_EDITOR[] = "ResourceEditor.OpenTextEditor";
const char C_COPY_PATH[] = "ResourceEditor.CopyPath";
const char C_COPY_URL[] = "ResourceEditor.CopyUrl";
} // namespace Constants
} // namespace ResourceEditor

View File

@@ -48,6 +48,7 @@
#include <projectexplorer/projectnodes.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/parameteraction.h>
#include <utils/qtcassert.h>
#include <QtPlugin>
@@ -58,9 +59,14 @@
#include <QMessageBox>
#include <QFormLayout>
#include <QDialogButtonBox>
#include <QClipboard>
#include <QApplication>
using namespace ResourceEditor::Internal;
static const char resourcePrefix[] = ":";
static const char urlPrefix[] = "qrc://";
class PrefixLangDialog : public QDialog
{
Q_OBJECT
@@ -145,6 +151,8 @@ bool ResourceEditorPlugin::initialize(const QStringList &arguments, QString *err
Core::Context projectTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE);
Core::ActionContainer *folderContextMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FOLDERCONTEXT);
Core::ActionContainer *fileContextMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FILECONTEXT);
Core::Command *command = 0;
m_addPrefix = new QAction(tr("Add Prefix..."), this);
@@ -182,6 +190,18 @@ bool ResourceEditorPlugin::initialize(const QStringList &arguments, QString *err
folderContextMenu->addAction(command, ProjectExplorer::Constants::G_FOLDER_FILES);
connect(m_openInTextEditor, SIGNAL(triggered()), this, SLOT(openTextEditorContextMenu()));
m_copyPath = new Utils::ParameterAction(QString(), tr("Copy path \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
command = Core::ActionManager::registerAction(m_copyPath, Constants::C_COPY_PATH, projectTreeContext);
command->setAttribute(Core::Command::CA_UpdateText);
fileContextMenu->addAction(command, ProjectExplorer::Constants::G_FILE_OTHER);
connect(m_copyPath, SIGNAL(triggered()), this, SLOT(copyPathContextMenu()));
m_copyUrl = new Utils::ParameterAction(QString(), tr("Copy url \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
command = Core::ActionManager::registerAction(m_copyUrl, Constants::C_COPY_URL, projectTreeContext);
command->setAttribute(Core::Command::CA_UpdateText);
fileContextMenu->addAction(command, ProjectExplorer::Constants::G_FILE_OTHER);
connect(m_copyUrl, SIGNAL(triggered()), this, SLOT(copyUrlContextMenu()));
m_addPrefix->setEnabled(false);
m_removePrefix->setEnabled(false);
m_renamePrefix->setEnabled(false);
@@ -268,6 +288,18 @@ void ResourceEditorPlugin::openTextEditorContextMenu()
Core::EditorManager::openEditor(path, Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
}
void ResourceEditorPlugin::copyPathContextMenu()
{
ResourceFileNode *node = static_cast<ResourceFileNode *>(ProjectExplorer::ProjectExplorerPlugin::instance()->currentNode());
QApplication::clipboard()->setText(QLatin1String(resourcePrefix) + node->qrcPath());
}
void ResourceEditorPlugin::copyUrlContextMenu()
{
ResourceFileNode *node = static_cast<ResourceFileNode *>(ProjectExplorer::ProjectExplorerPlugin::instance()->currentNode());
QApplication::clipboard()->setText(QLatin1String(urlPrefix) + node->qrcPath());
}
void ResourceEditorPlugin::renamePrefixContextMenu()
{
ResourceFolderNode *rfn = static_cast<ResourceFolderNode *>(ProjectExplorer::ProjectExplorerPlugin::instance()->currentNode());
@@ -314,6 +346,18 @@ void ResourceEditorPlugin::updateContextActions(ProjectExplorer::Node *node, Pro
m_renamePrefix->setEnabled(isResourceFolder);
m_renamePrefix->setVisible(isResourceFolder);
bool isResourceFile = qobject_cast<ResourceFileNode *>(node);
m_copyPath->setEnabled(isResourceFile);
m_copyPath->setVisible(isResourceFile);
m_copyUrl->setEnabled(isResourceFile);
m_copyUrl->setVisible(isResourceFile);
if (isResourceFile) {
ResourceFileNode *fileNode = static_cast<ResourceFileNode *>(node);
QString qrcPath = fileNode->qrcPath();
m_copyPath->setParameter(QLatin1String(resourcePrefix) + qrcPath);
m_copyUrl->setParameter(QLatin1String(urlPrefix) + qrcPath);
}
}
void ResourceEditorPlugin::onUndoStackChanged(ResourceEditorW const *editor,

View File

@@ -41,6 +41,8 @@ class Node;
class Project;
}
namespace Utils { class ParameterAction; }
namespace ResourceEditor {
namespace Internal {
@@ -74,6 +76,9 @@ private slots:
void openEditorContextMenu();
void openTextEditorContextMenu();
void copyPathContextMenu();
void copyUrlContextMenu();
void updateContextActions(ProjectExplorer::Node*,ProjectExplorer::Project*);
public:
@@ -97,6 +102,10 @@ private:
QAction *m_openInEditor;
QAction *m_openInTextEditor;
// file context menu
Utils::ParameterAction *m_copyPath;
Utils::ParameterAction *m_copyUrl;
};
} // namespace Internal

View File

@@ -150,14 +150,19 @@ void ResourceTopLevelNode::update()
int filecount = file.fileCount(i);
for (int j = 0; j < filecount; ++j) {
const QString &fileName = file.file(i, j);
QString alias = file.alias(i, j);
if (alias.isEmpty()) {
alias = QFileInfo(path()).absoluteDir().relativeFilePath(fileName);
}
if (fileNames.contains(fileName)) {
// The file name is duplicated, skip it
// Note: this is wrong, but the qrceditor doesn't allow it either
// only aliases need to be unique
} else {
const QString qrcPath = QDir::cleanPath(prefix + QLatin1Char('/') + alias);
fileNames.insert(fileName);
filesToAdd[qMakePair(prefix, lang)]
<< new ResourceFileNode(fileName, this);
<< new ResourceFileNode(fileName, qrcPath, this);
}
}
@@ -478,9 +483,10 @@ bool ResourceFileWatcher::reload(QString *errorString, ReloadFlag flag, ChangeTy
return true;
}
ResourceFileNode::ResourceFileNode(const QString &filePath, ResourceTopLevelNode *topLevel)
ResourceFileNode::ResourceFileNode(const QString &filePath, const QString &qrcPath, ResourceTopLevelNode *topLevel)
: ProjectExplorer::FileNode(filePath, ProjectExplorer::UnknownFileType, false),
m_topLevel(topLevel)
m_topLevel(topLevel),
m_qrcPath(qrcPath)
{
QString baseDir = QFileInfo(topLevel->path()).absolutePath();
@@ -491,3 +497,8 @@ QString ResourceFileNode::displayName() const
{
return m_displayName;
}
QString ResourceFileNode::qrcPath() const
{
return m_qrcPath;
}

View File

@@ -101,13 +101,15 @@ class ResourceFileNode : public ProjectExplorer::FileNode
{
Q_OBJECT
public:
ResourceFileNode(const QString &filePath, ResourceTopLevelNode *topLevel);
ResourceFileNode(const QString &filePath, const QString &qrcPath, ResourceTopLevelNode *topLevel);
QString displayName() const;
QString qrcPath() const;
private:
QString m_displayName;
ResourceTopLevelNode *m_topLevel;
QString m_displayName;
QString m_qrcPath;
};
class ResourceFileWatcher : public Core::IDocument