From 79d6c0223d53c22fa5baf9488d9d055cb87b4c34 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 23 Jun 2011 15:13:11 +0200 Subject: [PATCH] compile .qrc action --- python/src/META-INF/python-plugin-common.xml | 4 + .../com/jetbrains/pyqt/CompileQrcAction.java | 94 +++++++++++++++++++ .../com/jetbrains/pyqt/CompileQrcDialog.form | 32 +++++++ python/src/com/jetbrains/pyqt/QtFileType.java | 54 +++++++---- 4 files changed, 164 insertions(+), 20 deletions(-) create mode 100644 python/src/com/jetbrains/pyqt/CompileQrcAction.java create mode 100644 python/src/com/jetbrains/pyqt/CompileQrcDialog.form diff --git a/python/src/META-INF/python-plugin-common.xml b/python/src/META-INF/python-plugin-common.xml index 7fd218e5df94..93bb7d80b240 100644 --- a/python/src/META-INF/python-plugin-common.xml +++ b/python/src/META-INF/python-plugin-common.xml @@ -691,6 +691,10 @@ description="Create a new directory and __init__.py inside it" icon="/nodes/packageClosed.png"> + + + + diff --git a/python/src/com/jetbrains/pyqt/CompileQrcAction.java b/python/src/com/jetbrains/pyqt/CompileQrcAction.java new file mode 100644 index 000000000000..aaeedca50aa1 --- /dev/null +++ b/python/src/com/jetbrains/pyqt/CompileQrcAction.java @@ -0,0 +1,94 @@ +package com.jetbrains.pyqt; + +import com.intellij.execution.ExecutionException; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.ui.TextFieldWithBrowseButton; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.jetbrains.django.run.Runner; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @author yole + */ +public class CompileQrcAction extends AnAction { + @Override + public void actionPerformed(AnActionEvent e) { + Project project = e.getData(PlatformDataKeys.PROJECT); + VirtualFile[] vFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY); + assert vFiles != null; + Module module = e.getData(LangDataKeys.MODULE); + String path = QtFileType.findQtTool(module, "pyrcc4"); + if (path == null) { + path = QtFileType.findQtTool(module, "pyside-rcc"); + } + if (path == null) { + Messages.showErrorDialog(project, "Could not find pyrcc4 or pyside-rcc for selected Python interpreter", "Compile .qrc file"); + return; + } + CompileQrcDialog dialog = new CompileQrcDialog(project, vFiles); + dialog.show(); + if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) { + return; + } + List args = new ArrayList(Arrays.asList(path, "-o", dialog.getOutputPath())); + for (VirtualFile vFile : vFiles) { + args.add(vFile.getPath()); + } + try { + Runner.runInPath(project, vFiles[0].getParent().getPath(), true, null, args.toArray(new String[args.size()])); + } + catch (ExecutionException ex) { + Messages.showErrorDialog(project, "Error running " + path + ": " + ex.getMessage(), "Compile .qrc file"); + } + } + + @Override + public void update(AnActionEvent e) { + Module module = e.getData(LangDataKeys.MODULE); + VirtualFile[] vFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY); + e.getPresentation().setVisible(module != null && filesAreQrc(vFiles)); + } + + private static boolean filesAreQrc(VirtualFile[] vFiles) { + if (vFiles == null || vFiles.length == 0) { + return false; + } + for (VirtualFile vFile : vFiles) { + if (!"qrc".equals(FileUtil.getExtension(vFile.getName()))) { + return false; + } + } + return true; + } + + public static class CompileQrcDialog extends DialogWrapper { + private JPanel myPanel; + private TextFieldWithBrowseButton myOutputFileField; + + protected CompileQrcDialog(Project project, VirtualFile[] vFiles) { + super(project); + init(); + } + + @Override + protected JComponent createCenterPanel() { + return myPanel; + } + + public String getOutputPath() { + return myOutputFileField.getText(); + } + } +} diff --git a/python/src/com/jetbrains/pyqt/CompileQrcDialog.form b/python/src/com/jetbrains/pyqt/CompileQrcDialog.form new file mode 100644 index 000000000000..4ce539fe4678 --- /dev/null +++ b/python/src/com/jetbrains/pyqt/CompileQrcDialog.form @@ -0,0 +1,32 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/python/src/com/jetbrains/pyqt/QtFileType.java b/python/src/com/jetbrains/pyqt/QtFileType.java index 39b64aa1b2b4..8aeeb99cc653 100644 --- a/python/src/com/jetbrains/pyqt/QtFileType.java +++ b/python/src/com/jetbrains/pyqt/QtFileType.java @@ -76,35 +76,49 @@ public abstract class QtFileType implements FileType, INativeFileType { @Override public boolean openFileInAssociatedApplication(Project project, VirtualFile file) { + String qtTool = findQtTool(ModuleUtil.findModuleForFile(file, project), getToolName()); + if (qtTool == null) { + return false; + } + try { + Runtime.getRuntime().exec(new String[] { qtTool, file.getPath() } ); + } + catch (IOException e) { + Messages.showErrorDialog(project, "Failed to run Qt Designer: " + e.getMessage(), "Error"); + } + return true; + } + + public static String findQtTool(Module module, String toolName) { if (SystemInfo.isWindows) { - Module module = ModuleUtil.findModuleForFile(file, project); if (module == null) { - return false; + return null; } Sdk sdk = PythonSdkType.findPythonSdk(module); if (sdk == null) { - return false; + return null; } - List qt4 = - ResolveImportUtil.resolveModulesInRootProvider(sdk.getRootProvider(), module, PyQualifiedName.fromComponents("PyQt4")); - for (PsiElement psiElement : qt4) { - if (psiElement instanceof PsiDirectory) { - VirtualFile designer = ((PsiDirectory)psiElement).getVirtualFile().findChild(getToolName() + ".exe"); - if (designer != null) { - try { - Runtime.getRuntime().exec(new String[] { designer.getPath(), file.getPath() } ); - } - catch (IOException e) { - Messages.showErrorDialog(project, "Failed to run Qt Designer: " + e.getMessage(), "Error"); - } - return true; - } - } + String tool = findToolInPackage(toolName, module, sdk, "PyQt4"); + if (tool != null) { + return tool; } - + return findToolInPackage(toolName, module, sdk, "PySide"); } // TODO - return true; + return null; + } + + private static String findToolInPackage(String toolName, Module module, Sdk sdk, String name) { + List elements = ResolveImportUtil.resolveModulesInRootProvider(sdk.getRootProvider(), module, PyQualifiedName.fromComponents(name)); + for (PsiElement psiElement : elements) { + if (psiElement instanceof PsiDirectory) { + VirtualFile tool = ((PsiDirectory)psiElement).getVirtualFile().findChild(toolName + ".exe"); + if (tool != null) { + return tool.getPath(); + } + } + } + return null; } protected abstract String getToolName();