def create_file_new_actions(self, fnames): """Return actions for submenu 'New...'""" if not fnames: return [] new_file_act = create_action( self, _("File..."), icon=ima.icon('filenew'), triggered=lambda: self.new_file(fnames[-1])) new_module_act = create_action( self, _("Module..."), icon=ima.icon('spyder'), triggered=lambda: self.new_module(fnames[-1])) new_folder_act = create_action( self, _("Folder..."), icon=ima.icon('folder_new'), triggered=lambda: self.new_folder(fnames[-1])) new_package_act = create_action( self, _("Package..."), icon=ima.icon('package_new'), triggered=lambda: self.new_package(fnames[-1])) return [ new_file_act, new_folder_act, None, new_module_act, new_package_act ]
def setup_toolbars(self): menubar = self.menuBar() file_menu = menubar.addMenu('&File') file_menu.addAction( create_action(self, "&Preferences", triggered=self.show_preferences, shortcut="Ctrl+P")) file_menu.addAction( create_action(self, "&Quit", triggered=self.close, shortcut="Ctrl+Q")) helpmenu = menubar.addMenu('&Help') helpmenu.addAction( create_action(self, "&Online Documentation", triggered=lambda: self.open_url(self.DOCS_URL))) helpmenu.addAction( create_action(self, "&Logs viewer", triggered=self.show_log_viewer, shortcut="F6")) helpmenu.addSeparator() helpmenu.addAction( create_action(self, "&About", triggered=self.show_about))
def actions_menu_requested(self): """Create and display menu for the currently selected application.""" self.menu_options.clear() self.menu_versions.clear() # Add versions menu versions = self.versions if self.versions else [] version_actions = [] for version in reversed(versions): action = create_action(self.widget, version, triggered=lambda value, version=version: self.install_application(version=version)) action.setCheckable(True) if self.version == version and self.installed: action.setChecked(True) action.setDisabled(True) version_actions.append(action) install_action = create_action( self.widget, 'Install application', triggered=lambda: self.install_application()) install_action.setEnabled(not self.installed) update_action = create_action( self.widget, 'Update application', triggered=lambda: self.update_application()) if versions and versions[-1] == self.version: update_action.setDisabled(True) else: update_action.setDisabled(False) if self.non_conda and self.name == GLOBAL_VSCODE_APP: update_action.setDisabled(True) remove_action = create_action( self.widget, 'Remove application', triggered=lambda: self.remove_application()) remove_action.setEnabled(self.installed) actions = [ install_action, update_action, remove_action, None, self.menu_versions ] add_actions(self.menu_options, actions) add_actions(self.menu_versions, version_actions) offset = QPoint(self.button_options.width(), 0) position = self.button_options.mapToGlobal(QPoint(0, 0)) self.menu_versions.setEnabled(len(versions) > 1) self.menu_options.move(position + offset) self.menu_options.exec_()
def create_file_new_actions(self, fnames): """Return actions for submenu 'New...'.""" if not fnames: return [] new_file_act = create_action( self, "File...", icon=QIcon(), # ima.icon('filenew'), triggered=lambda: self.new_file(fnames[-1])) new_folder_act = create_action( self, "Folder...", icon=QIcon(), # ima.icon('folder_new'), triggered=lambda: self.new_folder(fnames[-1])) return [new_file_act, new_folder_act]
def setup_common_actions(self): """Setup context menu common actions""" # Filters filters_action = create_action(self, _("Edit filename filters..."), None, ima.icon('filter'), triggered=self.edit_filter) # Show all files all_action = create_action(self, _("Show all files"), toggled=self.toggle_all) all_action.setChecked(self.show_all) self.toggle_all(self.show_all) return [filters_action, all_action]
def showPopup(self): """Override Qt method.""" index = self.currentIndex() menu = QMenu(self) # See: https://github.com/ContinuumIO/navigator/issues/1565 try: menu.setToolTipsVisible(True) except AttributeError: pass actions = [] for i in range(self.count()): tip = self.itemData(i, Qt.ToolTipRole) text = self.itemText(i) action = create_action( self, text, toggled=lambda v=None, i=i: self.setCurrentIndex(i), tip=tip ) actions.append(action) if i == index: action.setChecked(True) add_actions(menu, actions) menu.setFixedWidth(self.width()) bottom_left = self.contentsRect().bottomLeft() menu.popup(self.mapToGlobal(bottom_left))
def showPopup(self): """Override Qt method.""" index = self.currentIndex() menu = QMenu(self) menu.setToolTipsVisible(True) actions = [] for i in range(self.count()): tip = self.itemData(i, Qt.ToolTipRole) text = self.itemText(i) action = create_action( self, text, toggled=lambda v=None, i=i: self.setCurrentIndex(i), tip=tip) actions.append(action) if i == index: action.setChecked(True) add_actions(menu, actions) menu.setFixedWidth(self.width()) bottom_left = self.contentsRect().bottomLeft() menu.popup(self.mapToGlobal(bottom_left))
def __init__( self, parent=None, name_filters=['*.py', '*.pyw'], show_all=True, show_cd_only=None, show_icontext=False, home=None, ): """Explorer widget.""" QWidget.__init__(self, parent) self.home = home # Widgets self.treewidget = ExplorerTreeWidget(self, show_cd_only=show_cd_only) self.button_home = QToolButton(self) self.button_home.setFocusPolicy(Qt.StrongFocus) self.label_breadcrumb = QLabel() home_action = create_action( self, text="Home", icon=QIcon(), # ima.icon('ArrowBack'), triggered=self.go_home) # Setup widgets self.treewidget.setup(name_filters=name_filters, show_all=show_all) self.treewidget.chdir(getcwd()) self.button_home.setDefaultAction(home_action) # Layouts blayout = QHBoxLayout() blayout.addWidget(self.button_home) blayout.addWidget(self.label_breadcrumb) blayout.addStretch() layout = QVBoxLayout() layout.addLayout(blayout) layout.addWidget(self.treewidget) self.setLayout(layout) # Signals and slots self.treewidget.sig_add_to_project.connect(self.sig_add_to_project) self.treewidget.sig_changed_dir.connect(self.update_breadcrumb) self.label_breadcrumb.linkActivated.connect(self.go_to) self.update_breadcrumb()
def setup_common_actions(self): """Setup context menu common actions""" actions = super(ExplorerTreeWidget, self).setup_common_actions() if self.show_cd_only is None: # Enabling the 'show current directory only' option but do not # allow the user to disable it self.show_cd_only = True else: # Show current directory only cd_only_action = create_action(self, _("Show current directory only"), toggled=self.toggle_show_cd_only) cd_only_action.setChecked(self.show_cd_only) self.toggle_show_cd_only(self.show_cd_only) actions.append(cd_only_action) return actions
def setup_common_actions(self): """Setup context menu common actions.""" # Filters # filters_action = create_action( # self, # "Edit filename filters...", # None, # QIcon(), # ima.icon('filter'), # triggered=self.edit_filter # ) # Show all files all_action = create_action(self, "Show all files", toggled=self.toggle_all) all_action.setChecked(self.show_all) self.toggle_all(self.show_all) # return [filters_action, all_action] return []
def context_menu_requested(self, event, right_click=False): """Custom context menu.""" if self.proxy_model is None: return self._menu = QMenu(self) left_click = not right_click index = self.currentIndex() model_index = self.proxy_model.mapToSource(index) row_data = self.source_model.row(model_index.row()) column = model_index.column() name = row_data[C.COL_NAME] # package_type = row_data[C.COL_PACKAGE_TYPE] versions = self.source_model.get_package_versions(name) current_version = self.source_model.get_package_version(name) action_version = row_data[C.COL_ACTION_VERSION] package_status = row_data[C.COL_STATUS] package_type = row_data[C.COL_PACKAGE_TYPE] remove_actions = bool(self.source_model.count_remove_actions()) install_actions = bool(self.source_model.count_install_actions()) update_actions = bool(self.source_model.count_update_actions()) if column in [C.COL_ACTION] and left_click: is_installable = self.source_model.is_installable(model_index) is_removable = self.source_model.is_removable(model_index) is_upgradable = self.source_model.is_upgradable(model_index) action_status = self.source_model.action_status(model_index) actions = [] action_unmark = create_action( self, _('Unmark'), triggered=lambda: self.set_action_status( model_index, C.ACTION_NONE, current_version)) action_install = create_action( self, _('Mark for installation'), toggled=lambda: self.set_action_status(model_index, C. ACTION_INSTALL)) action_update = create_action( self, _('Mark for update'), toggled=lambda: self.set_action_status(model_index, C. ACTION_UPDATE, None)) action_remove = create_action( self, _('Mark for removal'), toggled=lambda: self.set_action_status( model_index, C.ACTION_REMOVE, current_version)) version_actions = [] for version in reversed(versions): def trigger(model_index=model_index, action=C.ACTION_INSTALL, version=version): return lambda: self.set_action_status( model_index, status=action, version=version) if version == current_version: version_action = create_action( self, version, icon=QIcon(), triggered=trigger(model_index, C.ACTION_INSTALL, version)) if not is_installable: version_action.setCheckable(True) version_action.setChecked(True) version_action.setDisabled(True) elif version != current_version: if ((version in versions and versions.index(version)) > (current_version in versions and versions.index(current_version))): upgrade_or_downgrade_action = C.ACTION_UPGRADE else: upgrade_or_downgrade_action = C.ACTION_DOWNGRADE if is_installable: upgrade_or_downgrade_action = C.ACTION_INSTALL version_action = create_action( self, version, icon=QIcon(), triggered=trigger(model_index, upgrade_or_downgrade_action, version)) if action_version == version: version_action.setCheckable(True) version_action.setChecked(True) version_actions.append(version_action) install_versions_menu = QMenu( 'Mark for specific version ' 'installation', self) add_actions(install_versions_menu, version_actions) actions = [ action_unmark, action_install, action_update, action_remove ] actions += [None, install_versions_menu] # Disable firing of signals, while setting the checked status for ac in actions + version_actions: if ac: ac.blockSignals(True) if action_status == C.ACTION_NONE: action_unmark.setEnabled(False) action_install.setEnabled(is_installable) action_update.setEnabled(is_upgradable) action_remove.setEnabled(is_removable) if install_actions: # Invalidate remove and update if install actions selected action_update.setDisabled(True) action_remove.setDisabled(True) elif remove_actions: # Invalidate install/update if remove actions already action_install.setDisabled(True) action_update.setDisabled(True) elif update_actions: # Invalidate install/update if remove actions already action_install.setDisabled(True) action_remove.setDisabled(True) install_versions_menu.setDisabled(False) elif action_status == C.ACTION_INSTALL: action_unmark.setEnabled(True) action_install.setEnabled(False) action_install.setChecked(True) action_update.setEnabled(False) action_remove.setEnabled(False) elif action_status == C.ACTION_REMOVE: action_unmark.setEnabled(True) action_install.setEnabled(False) action_update.setEnabled(False) action_remove.setEnabled(False) action_remove.setChecked(True) elif action_status == C.ACTION_UPDATE: action_unmark.setEnabled(True) action_install.setEnabled(False) action_update.setEnabled(False) action_update.setChecked(True) action_remove.setEnabled(False) elif action_status in [C.ACTION_UPGRADE, C.ACTION_DOWNGRADE]: action_unmark.setEnabled(True) action_install.setEnabled(False) action_update.setEnabled(False) action_update.setChecked(False) action_remove.setEnabled(False) install_versions_menu.setEnabled(False) if package_status == C.NOT_INSTALLED: action_remove.setEnabled(False) action_update.setEnabled(False) if package_type == C.PIP_PACKAGE: action_unmark.setEnabled(False) action_install.setEnabled(False) action_update.setEnabled(False) action_remove.setEnabled(False) # Enable firing of signals, while setting the checked status for ac in actions + version_actions: if ac: ac.blockSignals(False) install_versions_menu.setDisabled(True) install_versions_menu.setEnabled( len(version_actions) > 1 and not remove_actions and not update_actions) elif right_click: license_ = row_data[C.COL_LICENSE] metadata = self.metadata_links.get(name, {}) pypi = metadata.get('pypi', '') home = metadata.get('home', '') dev = metadata.get('dev', '') docs = metadata.get('docs', '') q_pypi = QIcon(get_image_path('python.png')) q_home = QIcon(get_image_path('home.png')) q_docs = QIcon(get_image_path('conda_docs.png')) if 'git' in dev: q_dev = QIcon(get_image_path('conda_github.png')) elif 'bitbucket' in dev: q_dev = QIcon(get_image_path('conda_bitbucket.png')) else: q_dev = QIcon() if 'mit' in license_.lower(): lic = 'http://opensource.org/licenses/MIT' elif 'bsd' == license_.lower(): lic = 'http://opensource.org/licenses/BSD-3-Clause' else: lic = None actions = [] if license_ != '': actions.append( create_action(self, _('License: ' + license_), icon=QIcon(), triggered=lambda: self.open_url(lic))) actions.append(None) if pypi != '': actions.append( create_action(self, _('Python Package Index'), icon=q_pypi, triggered=lambda: self.open_url(pypi))) if home != '': actions.append( create_action(self, _('Homepage'), icon=q_home, triggered=lambda: self.open_url(home))) if docs != '': actions.append( create_action(self, _('Documentation'), icon=q_docs, triggered=lambda: self.open_url(docs))) if dev != '': actions.append( create_action(self, _('Development'), icon=q_dev, triggered=lambda: self.open_url(dev))) if actions and len(actions) > 1: # self._menu = QMenu(self) add_actions(self._menu, actions) if event.type() == QEvent.KeyRelease: rect = self.visualRect(index) global_pos = self.viewport().mapToGlobal(rect.bottomRight()) else: pos = QPoint(event.x(), event.y()) global_pos = self.viewport().mapToGlobal(pos) self._menu.popup(global_pos)
def create_file_manage_actions(self, fnames): """Return file management actions""" only_files = all([osp.isfile(_fn) for _fn in fnames]) only_modules = all([ osp.splitext(_fn)[1] in ('.py', '.pyw', '.ipy') for _fn in fnames ]) only_notebooks = all( [osp.splitext(_fn)[1] == '.ipynb' for _fn in fnames]) only_exe = all([ os.path.isfile(_fn) and os.access(_fn, os.X_OK) for _fn in fnames ]) only_valid = all([encoding.is_text_file(_fn) for _fn in fnames]) run_action = create_action(self, _("Run"), icon=ima.icon('run'), triggered=self.run) exe_action = create_action(self, _("Execute"), icon=ima.icon('run'), triggered=self.exe) run_con_action = create_action(self, _("Run (console)"), icon=ima.icon('run'), triggered=self.run_con) edit_action = create_action(self, _("Edit"), icon=ima.icon('edit'), triggered=self.edit_file) move_action = create_action(self, _("Move..."), icon=QIcon(), triggered=self.move) delete_action = create_action(self, _("Delete..."), icon=ima.icon('editdelete'), triggered=self.delete) rename_action = create_action(self, _("Rename..."), icon=ima.icon('rename'), triggered=self.rename) open_action = create_action(self, _("Open"), triggered=self.open) ipynb_convert_action = create_action(self, _("Convert to Python script"), icon=ima.icon('python'), triggered=self.convert_notebooks) ipynb_open_action = create_action(self, _("Open notebook"), icon=ima.icon('python'), triggered=self.open_notebook) endpoint_action = create_action(self, _("Make endpoint"), triggered=self.make_endpoint) actions = [] if only_modules: actions.append(run_action) if only_valid and only_files: actions.append(edit_action) if only_modules: actions.extend([run_action, run_con_action]) if only_notebooks and nbexporter is not None: actions.extend([ipynb_open_action, ipynb_convert_action]) if only_notebooks and nbexporter is None: actions.extend([ipynb_open_action]) if only_modules or only_notebooks or only_exe: actions.extend([exe_action, endpoint_action]) sep = QAction(self) sep.setSeparator(True) actions.append(sep) actions += [delete_action, rename_action] basedir = fixpath(osp.dirname(fnames[0])) if all([fixpath(osp.dirname(_fn)) == basedir for _fn in fnames]): actions.append(move_action) sep = QAction(self) sep.setSeparator(True) actions.append(sep) return actions
def __init__(self, parent=None, name_filters=['*.py', '*.pyw'], show_all=False, show_cd_only=None, show_icontext=True): QWidget.__init__(self, parent) # Widgets self.treewidget = ExplorerTreeWidget(self, show_cd_only=show_cd_only) button_home = QToolButton(self) button_previous = QToolButton(self) button_next = QToolButton(self) button_parent = QToolButton(self) self.button_menu = QToolButton(self) menu = QMenu(self) self.action_widgets = [ button_home, button_previous, button_next, button_parent, self.button_menu ] # Actions icontext_action = create_action(self, _("Show icons and text"), toggled=self.toggle_icontext) home_action = create_action(self, text=_("Home"), icon=qta.icon('fa.home'), triggered=self.treewidget.go_home) previous_action = create_action( self, text=_("Previous"), icon=qta.icon('fa.arrow-left'), triggered=self.treewidget.go_to_previous_directory) next_action = create_action( self, text=_("Next"), icon=qta.icon('fa.arrow-right'), triggered=self.treewidget.go_to_next_directory) parent_action = create_action( self, text=_("Parent"), icon=qta.icon('fa.arrow-up'), triggered=self.treewidget.go_to_parent_directory) options_action = create_action(self, text='', tip=_('Options')) # Setup widgets self.treewidget.setup(name_filters=name_filters, show_all=show_all) self.treewidget.chdir(getcwd()) self.treewidget.common_actions += [None, icontext_action] button_home.setDefaultAction(home_action) button_previous.setDefaultAction(previous_action) previous_action.setEnabled(False) button_next.setDefaultAction(next_action) next_action.setEnabled(False) button_parent.setDefaultAction(parent_action) self.button_menu.setIcon(ima.icon('tooloptions')) self.button_menu.setPopupMode(QToolButton.InstantPopup) self.button_menu.setMenu(menu) add_actions(menu, self.treewidget.common_actions) options_action.setMenu(menu) self.toggle_icontext(show_icontext) icontext_action.setChecked(show_icontext) for widget in self.action_widgets: widget.setAutoRaise(True) widget.setIconSize(QSize(16, 16)) # Layouts blayout = QHBoxLayout() blayout.addWidget(button_home) blayout.addWidget(button_previous) blayout.addWidget(button_next) blayout.addWidget(button_parent) blayout.addStretch() blayout.addWidget(self.button_menu) layout = QVBoxLayout() layout.addLayout(blayout) layout.addWidget(self.treewidget) self.setLayout(layout) # Signals and slots self.treewidget.set_previous_enabled.connect( previous_action.setEnabled) self.treewidget.set_next_enabled.connect(next_action.setEnabled) button_home.clicked.connect(self.sig_home_clicked)
def create_file_manage_actions(self, fnames): """Return file management actions.""" only_files = all(osp.isfile(_fn) for _fn in fnames) fname = fnames[0] # only_modules = all( # [ # osp.splitext(_fn)[1] in ('.py', '.pyw', '.ipy') # for _fn in fnames # ] # ) # only_notebooks = all( # [osp.splitext(_fn)[1] == '.ipynb' for _fn in fnames] # ) # only_valid = all([encoding.is_text_file(_fn) for _fn in # fnames]) # run_action = create_action( # self, # "Run", # icon=QIcon(), # ima.icon('run'), # triggered=self.run, # ) # edit_action = create_action( # self, # "Edit", # icon=QIcon(), # ima.icon('edit'), # triggered=self.clicked, # ) move_action = create_action( self, "Move...", icon=QIcon(), # "move.png", triggered=self.move, ) delete_action = create_action( self, "Delete...", icon=QIcon(), # ima.icon('editdelete'), triggered=self.delete) add_to_project_action = create_action( self, "Add to project...", icon=QIcon(), # ima.icon('rename'), triggered=lambda x, fname=fname: self.add_to_project(fname), ) rename_action = create_action( self, "Rename...", icon=QIcon(), # ima.icon('rename'), triggered=self.rename) # open_action = create_action(self, "Open", triggered=self.open) # ipynb_convert_action = create_action( # self, # "Convert to Python script", # icon=QIcon(), # ima.icon('python'), # triggered=self.convert_notebooks # ) actions = [] # if only_modules: # actions.append(run_action) # if only_valid and only_files: # actions.append(edit_action) # else: # actions.append(open_action) actions += [delete_action, rename_action] basedir = fixpath(osp.dirname(fnames[0])) if all(fixpath(osp.dirname(_fn)) == basedir for _fn in fnames): actions.append(move_action) if only_files and os.path.dirname(fname) != self.project_path: actions += [None, add_to_project_action] # if only_notebooks and nbexporter is not None: # actions.append(ipynb_convert_action) return actions