|
| 1 | +# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +"""VS code launch window.""" |
| 24 | +import os |
| 25 | + |
| 26 | +from PySide6 import QtCore, QtGui, QtWidgets |
| 27 | + |
| 28 | +from ansys.tools.installer.constants import ANSYS_FAVICON, USER_PATH |
| 29 | + |
| 30 | + |
| 31 | +class VSCode(QtWidgets.QWidget): |
| 32 | + """VS code launch window.""" |
| 33 | + |
| 34 | + def __init__(self, parent): |
| 35 | + """Initialize this class.""" |
| 36 | + try: |
| 37 | + super().__init__() |
| 38 | + self._parent = parent |
| 39 | + if self.is_vscode_installed(): |
| 40 | + self._parent.vscode_window = QtWidgets.QWidget() |
| 41 | + self._parent.vscode_window.move( |
| 42 | + self._parent.vscode_window.frameGeometry().center() |
| 43 | + ) |
| 44 | + vscode_window_label = QtWidgets.QLabel() |
| 45 | + vscode_window_label.setText("Configuration") |
| 46 | + vscode_window_label.setTextFormat(QtCore.Qt.TextFormat.RichText) |
| 47 | + vscode_window_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignJustify) |
| 48 | + vscode_window_label.setWordWrap(True) |
| 49 | + |
| 50 | + vscode_layout = QtWidgets.QVBoxLayout() |
| 51 | + |
| 52 | + # Group 1: Configure default Virtual Environment creation path |
| 53 | + vscode_window_path_config = QtWidgets.QGroupBox( |
| 54 | + "VS Code open directory:" |
| 55 | + ) |
| 56 | + vscode_window_path_config_layout = QtWidgets.QVBoxLayout() |
| 57 | + vscode_window_path_config_layout.setContentsMargins(10, 20, 10, 20) |
| 58 | + vscode_window_path_config.setLayout(vscode_window_path_config_layout) |
| 59 | + |
| 60 | + # ---> Add box |
| 61 | + self.vscode_window_path_config_edit = QtWidgets.QLineEdit() |
| 62 | + self.vscode_window_path_config_edit.setText(USER_PATH) |
| 63 | + vscode_window_path_config_layout.addWidget( |
| 64 | + self.vscode_window_path_config_edit |
| 65 | + ) |
| 66 | + |
| 67 | + self.vscode_warning_text = QtWidgets.QLabel() |
| 68 | + self.vscode_warning_text.setAlignment( |
| 69 | + QtCore.Qt.AlignmentFlag.AlignJustify |
| 70 | + ) |
| 71 | + self.vscode_warning_text.setWordWrap(True) |
| 72 | + vscode_window_path_config_layout.addWidget(self.vscode_warning_text) |
| 73 | + |
| 74 | + # Finally, add all the previous widgets to the global layout |
| 75 | + vscode_layout.addWidget(vscode_window_path_config) |
| 76 | + |
| 77 | + vscode_window_button_open = QtWidgets.QPushButton("Open") |
| 78 | + vscode_window_button_open.clicked.connect( |
| 79 | + lambda x: self._pop_up("Do you want to open?", self._open_vscode) |
| 80 | + ) |
| 81 | + vscode_window_button_close = QtWidgets.QPushButton("Close") |
| 82 | + vscode_window_button_close.clicked.connect( |
| 83 | + lambda x: self._pop_up("Do you want to close?", self._close_all) |
| 84 | + ) |
| 85 | + |
| 86 | + vscode_window_layout_1 = QtWidgets.QHBoxLayout() |
| 87 | + vscode_window_layout_1.addWidget(vscode_window_label) |
| 88 | + vscode_window_layout_2 = QtWidgets.QHBoxLayout() |
| 89 | + vscode_window_layout_2.addWidget(vscode_window_button_open) |
| 90 | + vscode_window_layout_2.addWidget(vscode_window_button_close) |
| 91 | + |
| 92 | + vscode_window_layout = QtWidgets.QVBoxLayout() |
| 93 | + vscode_window_layout.addLayout(vscode_window_layout_1) |
| 94 | + vscode_window_layout.addLayout(vscode_layout) |
| 95 | + vscode_window_layout.addLayout(vscode_window_layout_2) |
| 96 | + self._parent.vscode_window.setLayout(vscode_window_layout) |
| 97 | + |
| 98 | + self._parent.vscode_window.setWindowTitle("Configuration") |
| 99 | + self._parent.vscode_window.setWindowIcon(QtGui.QIcon(ANSYS_FAVICON)) |
| 100 | + self._parent.vscode_window.resize(500, 40) |
| 101 | + self._parent.vscode_window.show() |
| 102 | + else: |
| 103 | + msg = QtWidgets.QMessageBox() |
| 104 | + msg.setTextFormat(QtCore.Qt.TextFormat.RichText) |
| 105 | + msg.warning( |
| 106 | + self, |
| 107 | + "VS Code Launch Error", |
| 108 | + f"Failed to launch vscode. Try reinstalling code by following this <a href='https://code.visualstudio.com/download'>link</a>", |
| 109 | + ) |
| 110 | + |
| 111 | + except Exception as e: |
| 112 | + self._parent.show_error(str(e)) |
| 113 | + |
| 114 | + def _open_vscode(self): |
| 115 | + """Open VS code from path.""" |
| 116 | + # handle errors |
| 117 | + path = self.vscode_window_path_config_edit.text().strip() |
| 118 | + if os.path.exists(path): |
| 119 | + error_msg = "echo Failed to launch vscode. Try reinstalling code by following this link https://code.visualstudio.com/download" |
| 120 | + self._parent.launch_cmd(f"code {path} && exit 0 || {error_msg}") |
| 121 | + |
| 122 | + self.user_confirmation_form.close() |
| 123 | + self._parent.vscode_window.close() |
| 124 | + else: |
| 125 | + self.vscode_warning_text.setText( |
| 126 | + f"""{path} does not exist. Provide a valid path.""" |
| 127 | + ) |
| 128 | + self.vscode_warning_text.setStyleSheet( |
| 129 | + """ |
| 130 | + color: rgb(255, 0, 0); |
| 131 | + """ |
| 132 | + ) |
| 133 | + self.user_confirmation_form.close() |
| 134 | + |
| 135 | + def _close_all(self): |
| 136 | + """Close all the pop-up window.""" |
| 137 | + self.user_confirmation_form.close() |
| 138 | + self._parent.vscode_window.close() |
| 139 | + |
| 140 | + def _pop_up(self, message, call_back): |
| 141 | + """Launch the confirmation pop-up window.""" |
| 142 | + self.user_confirmation_form = QtWidgets.QWidget() |
| 143 | + self.user_confirmation_form.move( |
| 144 | + self.user_confirmation_form.frameGeometry().center() |
| 145 | + ) |
| 146 | + user_confirmation_label = QtWidgets.QLabel() |
| 147 | + user_confirmation_label.setText(message) |
| 148 | + user_confirmation_label.setOpenExternalLinks(True) |
| 149 | + user_confirmation_label.setTextFormat(QtCore.Qt.TextFormat.RichText) |
| 150 | + user_confirmation_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignJustify) |
| 151 | + user_confirmation_label.setWordWrap(True) |
| 152 | + |
| 153 | + user_confirmation_layout_horizontal = QtWidgets.QHBoxLayout() |
| 154 | + user_confirmation_yes_button = QtWidgets.QPushButton("Yes") |
| 155 | + user_confirmation_yes_button.clicked.connect(call_back) |
| 156 | + user_confirmation_no_button = QtWidgets.QPushButton("No") |
| 157 | + user_confirmation_no_button.clicked.connect(self.user_confirmation_form.close) |
| 158 | + user_confirmation_layout = QtWidgets.QVBoxLayout() |
| 159 | + user_confirmation_layout.addWidget(user_confirmation_label) |
| 160 | + user_confirmation_layout_horizontal.addWidget(user_confirmation_yes_button) |
| 161 | + user_confirmation_layout_horizontal.addWidget(user_confirmation_no_button) |
| 162 | + user_confirmation_layout.addLayout(user_confirmation_layout_horizontal) |
| 163 | + self.user_confirmation_form.setLayout(user_confirmation_layout) |
| 164 | + self.user_confirmation_form.setWindowTitle("Confirmation") |
| 165 | + icon = QtGui.QIcon(ANSYS_FAVICON) |
| 166 | + self.user_confirmation_form.setWindowIcon(icon) |
| 167 | + self.user_confirmation_form.resize(400, 40) |
| 168 | + self.user_confirmation_form.setWindowFlag( |
| 169 | + QtCore.Qt.WindowCloseButtonHint, False |
| 170 | + ) |
| 171 | + self.user_confirmation_form.show() |
| 172 | + |
| 173 | + def is_vscode_installed(self): |
| 174 | + """Check if VSCode is installed or not. |
| 175 | +
|
| 176 | + Returns |
| 177 | + ------- |
| 178 | + bool |
| 179 | + Whether VSCode is installed or not. |
| 180 | + """ |
| 181 | + try: |
| 182 | + return_val = os.system("code --version") |
| 183 | + if return_val == 0: |
| 184 | + return True |
| 185 | + else: |
| 186 | + return False |
| 187 | + except: |
| 188 | + return False |
0 commit comments