def __init__(self, gui=True, **argv): """ gui: whether to show webkit window or run headless """ super(AjaxBrowser, self).__init__() self.argv = argv self.stats = stats.RenderStats() # use grid layout to hold widgets self.grid = QVBoxLayout() self.url_input = UrlInput(self.load_url) self.grid.addWidget(self.url_input) self._view = None # create status box self.status_table = StatusTable() self.grid.addWidget(self.status_table) # create results table self.records_table = ResultsTable() self.grid.addWidget(self.records_table) self.setLayout(self.grid) # log all responses response_filename = os.path.join(webkit.OUTPUT_DIR, 'responses.csv') self.response_writer = csv.writer(open(response_filename, 'w')) self.response_writer.writerow( ['URL', 'Content-Type', 'Referer', 'Size']) # Define keyboard shortcuts for convenient interaction QShortcut(QKeySequence.Close, self, self.close) QShortcut(QKeySequence.Quit, self, self.close) QShortcut(QKeySequence(Qt.CTRL + Qt.Key_F), self, self.fullscreen) if gui: self.showMaximized() self.raise_() # give focus to this browser window
def initGui(self): for keyseq, slot in ( (Qt.CTRL + Qt.ALT + Qt.Key_K, self.__create_group), (Qt.CTRL + Qt.ALT + Qt.Key_S, self.__select_next_group), (Qt.CTRL + Qt.ALT + Qt.Key_N, self.__next_section), (Qt.CTRL + Qt.ALT + Qt.Key_B, self.__previous_section), ): short = QShortcut(QKeySequence(keyseq), self.__iface.mainWindow()) short.setContext(Qt.ApplicationShortcut) short.activated.connect(slot) self.__shortcuts.append(short) self.__menu = QMenu("Albion") self.__menu.aboutToShow.connect(self.__create_menu_entries) self.__iface.mainWindow().menuBar().addMenu(self.__menu) self.__toolbar = QToolBar("Albion") self.__iface.addToolBar(self.__toolbar) self.__toolbar.addAction(icon("log_strati.svg"), "stratigraphic log").triggered.connect( self.__log_strati_clicked) self.__toolbar.addWidget(self.__current_graph) self.__current_graph.currentIndexChanged[unicode].connect( self.__current_graph_changed) self.__toolbar.addWidget(self.__current_section) self.__current_section.currentIndexChanged[unicode].connect( self.__current_section_changed) self.__toolbar.addAction( icon("previous_line.svg"), "previous section (Ctrl+Alt+b)").triggered.connect( self.__previous_section) self.__toolbar.addAction( icon("next_line.svg"), "next section (Ctrl+Alt+n)").triggered.connect(self.__next_section) self.__toolbar.addAction(icon("line_from_selected.svg"), "create temporary section").triggered.connect( self.__section_from_selection) self.__viewer3d = QDockWidget("3D") self.__viewer3d.setWidget(Viewer3d()) self.__iface.addDockWidget(Qt.LeftDockWidgetArea, self.__viewer3d) self.__iface.mainWindow().tabifyDockWidget( self.__iface.mainWindow().findChild(QDockWidget, "Layers"), self.__viewer3d) self.__viewer3d_ctrl = QToolBar("3D controls") self.__viewer3d_ctrl.addWidget(ViewerControls( self.__viewer3d.widget())) self.__iface.addToolBar(self.__viewer3d_ctrl) QgsProject.instance().readProject.connect(self.__qgis__project__loaded) self.__qgis__project__loaded() # case of reload
def __init__(self, parent, cls, zid): """ Arguments: parent: parent widget cls: the Class this quiz belongs to zid: the Quiz to display results for """ QDialog.__init__(self) self.form = Ui_Dialog() self.form.setupUi(self) self.parent = parent self.cls = cls self.zid = zid self.form.closeButton.clicked.connect(self.reject) self.form.viewQuizButton.clicked.connect(self.onViewQuiz) self.form.deleteResultsButton.clicked.connect(self.onDeleteResults) self.tableModel = AnswersTableModel(self) self.form.stuAnswersTable.setModel(self.tableModel) self.form.studentsTable.itemSelectionChanged.connect( self.displayStudent) qlShortcut = QShortcut(QKeySequence("Alt+L"), self.form.studentsTable) qlShortcut.connect(qlShortcut, QtCore.SIGNAL("activated()"), self.form.studentsTable.setFocus) # initialize lists self.setupStudentList() self.setupSummaryData() self.form.studentsTable.setCurrentRow(0) self.displayStudent()
def __init__(self, parentObject, windowTitle, windowIcon=QIcon(), shortcut=None): QDockWidget.__init__(self, parentObject) self._showAction = None self.setObjectName(str(self.__class__)) self.setWindowTitle(windowTitle) self.setFeatures(self.features() & (~QDockWidget.DockWidgetFloatable)) if not windowIcon.isNull(): self.setWindowIcon(windowIcon) if shortcut is not None: self.showAction().setShortcut(shortcut) self._titleBar = _TitleBar(self) self.setTitleBarWidget(self._titleBar) if shortcut is not None: toolTip = "Move focus with <b>%s</b>,<br/>close with <b>Esc</b>" % shortcut else: toolTip = "Close with <b>Esc</b>" self._titleBar.setToolTip(toolTip) self._closeShortcut = QShortcut(QKeySequence("Esc"), self) self._closeShortcut.setContext(Qt.WidgetWithChildrenShortcut) self._closeShortcut.activated.connect(self._close)
def define_global_shortcuts(self): sequence = { 'Ctrl+Shift+Left': self.view_container.prev_chapter, 'Ctrl+Left': self.view_container.first_page, 'Left': self.view_container.page_up, 'Right': self.view_container.page_down, 'Space': self.view_container.page_down, 'Ctrl+Right': self.view_container.last_page, 'Ctrl+Shift+Right': self.view_container.next_chapter, 'Ctrl+R': self.view_container.rotate_right, 'Ctrl+Shift+R': self.view_container.rotate_left, 'Ctrl+B': self.view_container.first_page, 'Ctrl+E': self.view_container.last_page, 'S': self.switch_double_page_direction, 'D': self.switch_double_page, 'M': self.switch_viewing_modes, '1': self.view_container.original_fit, '2': self.view_container.vertical_fit, '3': self.view_container.horizontal_fit, '4': self.view_container.best_fit, 'Q': self.close, } for key, value in sequence.items(): s = QShortcut(QKeySequence(key), self.view_container, value) s.setEnabled(True) self.global_shortcuts.append(s)
def __init__(self, parent, dbConf, qConf): QDialog.__init__(self) self.form = Ui_Dialog() self.form.setupUi(self) self.dbConf = dbConf self.qConf = qConf self.form.tableView.horizontalHeader().setMovable(True) self.form.closeButton.clicked.connect(self.reject) self.form.viewQuizButton.clicked.connect(self.onViewQuiz) self.form.emailResultsButton.clicked.connect(self.onEmailResults) self.form.importResultsButton.clicked.connect(self.onImportResults) self.form.viewResultsButton.clicked.connect(self.onViewResults) self.setupClassCombo() self.tableModel = HistoryTableModel(self) self.form.tableView.setModel(self.tableModel) self.sm = self.form.tableView.selectionModel() self.sm.selectionChanged.connect(self.checkButtonEnablement) self.reFillHistory() self.form.classCombo.activated.connect(self.reFillHistory) qlShortcut = QShortcut(QKeySequence("Alt+L"), self.form.tableView) qlShortcut.connect(qlShortcut, QtCore.SIGNAL("activated()"), lambda: self.form.tableView.setFocus())
def __init__(self, parent): QDialog.__init__(self) self.form = Ui_Dialog() self.form.setupUi(self) self.form.tableView.horizontalHeader().setMovable(True) self.form.addButton.clicked.connect(self.onAdd) self.form.deleteButton.clicked.connect(self.onDelete) self.form.importButton.clicked.connect(self.onImport) self.form.exportButton.clicked.connect(self.onExport) self.form.closeButton.clicked.connect(self.reject) self.form.fastEditBox.toggled.connect(self.onChangeFastEdit) self.onChangeFastEdit() # set up initial state self.setupClassCombo() self.tableModel = StudentTableModel(self) self.form.tableView.setModel(self.tableModel) self.sm = self.form.tableView.selectionModel() self.sm.selectionChanged.connect(self.checkButtonEnablement) self.reFillStudents() self.form.classCombo.activated.connect(self.reFillStudents) self.tableModel.valueRejected.connect(self.onValueRejected) self.tableModel.rowsRemoved.connect(self._updateStudentTotal) self.tableModel.rowsInserted.connect(self._updateStudentTotal) self.tableModel.modelReset.connect(self._updateStudentTotal) self._updateStudentTotal() qlShortcut = QShortcut(QKeySequence("Alt+L"), self.form.tableView) qlShortcut.connect(qlShortcut, QtCore.SIGNAL("activated()"), lambda: self.form.tableView.setFocus())
def _initShortcuts(self): mgr = ShortcutManager() shortcutGroupName = "Predictions" togglePredictions = QShortcut( QKeySequence("p"), self, member=self._viewerControlUi.checkShowPredictions.click) mgr.register(shortcutGroupName, "Toggle Prediction Layer Visibility", togglePredictions, self._viewerControlUi.checkShowPredictions) toggleSegmentation = QShortcut( QKeySequence("s"), self, member=self._viewerControlUi.checkShowSegmentation.click) mgr.register(shortcutGroupName, "Toggle Segmentaton Layer Visibility", toggleSegmentation, self._viewerControlUi.checkShowSegmentation) toggleLivePredict = QShortcut( QKeySequence("l"), self, member=self.labelingDrawerUi.liveUpdateButton.toggle) mgr.register(shortcutGroupName, "Toggle Live Prediction Mode", toggleLivePredict, self.labelingDrawerUi.liveUpdateButton)
def __init__(self, com, nLanes=4): Kronoz.__init__(self) QWidget.__init__(self) gui_MainWindow.__init__(self) self.setupUi(self) self.lanes = [GUILane(i) for i in range(nLanes)] for l in self.lanes: self.laneList.addWidget(l) self.com = com self.btStart.clicked.connect(self.on_start) self.btStop.clicked.connect(self.on_stop) self.btSave.clicked.connect(self.save_results) self.btOpen.clicked.connect(self.open_res_file) self.btBrowse.clicked.connect(self.browse_res_folder) self.shortcuts = (QShortcut(QKeySequence('1'), self.btStart, self.btStart.click), QShortcut(QKeySequence('2'), self.btStop, self.btStop.click), QShortcut(QKeySequence('3'), self.btSave, self.btSave.click), QShortcut(QKeySequence('4'), self.btOpen, self.btOpen.click), QShortcut(QKeySequence('5'), self.btBrowse, self.btBrowse.click)) if com is not None: self.timer = QTimer() self.timer.timeout.connect(self.poll_timer) self.timer.start(self.poll_interval) self.set_state(Kronoz.Connecting) else: self.timer = None self.set_state(Kronoz.Disconnected)
def __init__(self, parent=None): super(ShowPresets, self).__init__(parent) self.original_presets_file = '/usr/share/ffmulticonverter/presets.xml' self.config_folder = os.getenv('HOME') + '/.config/ffmulticonverter/' self.current_presets_file = self.config_folder + 'presets.xml' self.presListWidget = QListWidget() labelLabel = QLabel(self.tr('Preset label')) self.labelLineEdit = QLineEdit() self.labelLineEdit.setReadOnly(True) commandLabel = QLabel(self.tr('Preset command line parameters')) self.commandLineEdit = QLineEdit() self.commandLineEdit.setReadOnly(True) extLabel = QLabel(self.tr('Output file extension')) self.extLineEdit = QLineEdit() self.extLineEdit.setReadOnly(True) addButton = QPushButton(self.tr('Add')) self.deleteButton = QPushButton(self.tr('Delete')) self.delete_allButton = QPushButton(self.tr('Delete all')) self.editButton = QPushButton(self.tr('Edit')) searchLabel = QLabel(self.tr('Search')) self.searchLineEdit = QLineEdit() okButton = QPushButton(self.tr('OK')) okButton.setDefault(True) spc1 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) spc2 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) spc3 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) grid = pyqttools.add_to_grid( QGridLayout(), [self.delete_allButton, addButton, spc1], [self.deleteButton, self.editButton, spc2]) hlayout = pyqttools.add_to_layout(QHBoxLayout(), searchLabel, self.searchLineEdit, None, okButton) final_layout = pyqttools.add_to_layout( QVBoxLayout(), self.presListWidget, labelLabel, self.labelLineEdit, commandLabel, self.commandLineEdit, extLabel, self.extLineEdit, grid, spc3, hlayout) self.setLayout(final_layout) okButton.clicked.connect(self.accept) self.presListWidget.currentRowChanged.connect(self.show_preset) addButton.clicked.connect(self.add_preset) self.deleteButton.clicked.connect(self.delete_preset) self.delete_allButton.clicked.connect(self.delete_all_presets) self.editButton.clicked.connect(self.edit_preset) self.searchLineEdit.textEdited.connect(self.search) del_shortcut = QShortcut(self) del_shortcut.setKey(Qt.Key_Delete) del_shortcut.activated.connect(self.delete_preset) self.resize(430, 480) self.setWindowTitle(self.tr('Edit Presets')) QTimer.singleShot(0, self.load_xml) QTimer.singleShot(0, self.fill_presListWidget)
def _shortcutHelper(self, keySequence, group, description, parent, function, context = None, enabled = None): shortcut = QShortcut(QKeySequence(keySequence), parent, member=function, ambiguousMember=function) if context != None: shortcut.setContext(context) if enabled != None: shortcut.setEnabled(True) return shortcut, group, description
def _vincular_atajos_de_teclado(self): QShortcut(QKeySequence("F5"), self, self.cuando_pulsa_el_boton_ejecutar) QShortcut(QKeySequence("Ctrl+r"), self, self.cuando_pulsa_el_boton_ejecutar) # Solo en MacOS informa que la tecla Command sustituye a CTRL. if sys.platform == 'darwin': self.boton_ejecutar.setToolTip(u"Ejecutar el código actual (F5 o ⌘R)")
def __init__(self, parent, history_filename, profile=False): ShellBaseWidget.__init__(self, parent, history_filename, profile) TracebackLinksMixin.__init__(self) InspectObjectMixin.__init__(self) # Local shortcuts self.inspectsc = QShortcut(QKeySequence("Ctrl+I"), self, self.inspect_current_object) self.inspectsc.setContext(Qt.WidgetWithChildrenShortcut)
def __init__(self, parent=None): QFrame.__init__(self, parent=parent) DegenPrimerGUI.load_ui(self._ui_file, self) self.editor = self.findChild(QTextEdit, 'ReportTextEdit') self.search = self.findChild(QLineEdit, 'SearchLineEdit') self.search.textChanged.connect(self.find_text) self.search.returnPressed.connect(self.find_next) QShortcut(QKeySequence(Qt.Key_F3), self, self.find_next) QShortcut(QKeySequence(Qt.Key_F2), self, self.find_prev) self.term = ''
def install_shortcuts(obj, actions, ide): short = resources.get_shortcut for action in actions: short_key = action.get("shortcut", None) action_data = action.get("action", None) connect = action.get("connect", None) shortcut = None item_ui = None func = None if connect: func = getattr(obj, connect, None) if short_key and not action_data: if isinstance(short_key, QKeySequence): shortcut = QShortcut(short_key, ide) else: shortcut = QShortcut(short(short_key), ide) shortcut.setContext(Qt.ApplicationShortcut) if isinstance(func, collections.Callable): ide.connect(shortcut, SIGNAL("activated()"), func) if action_data: is_menu = action_data.get('is_menu', False) if is_menu: item_ui = QMenu(action_data['text'], ide) else: item_ui = QAction(action_data['text'], ide) object_name = "%s.%s" % (obj.__class__.__name__, connect) item_ui.setObjectName(object_name) image_name = action_data.get('image', None) section = action_data.get('section', None) weight = action_data.get('weight', None) keysequence = action_data.get('keysequence', None) if image_name: if isinstance(image_name, int): icon = ide.style().standardIcon(image_name) item_ui.setIcon(icon) elif isinstance(image_name, str): icon = QIcon(":img/" + image_name) item_ui.setIcon(icon) if short_key and not is_menu: item_ui.setShortcut(short(short_key)) item_ui.setShortcutContext(Qt.ApplicationShortcut) elif keysequence and not is_menu: item_ui.setShortcut(short(keysequence)) item_ui.setShortcutContext(Qt.ApplicationShortcut) if isinstance(func, collections.Callable) and not is_menu: ide.connect(item_ui, SIGNAL("triggered()"), func) if section and section[0] is not None and weight: ide.register_menuitem(item_ui, section, weight) if image_name and not is_menu: ide.register_toolbar(item_ui, section, weight) if short_key and shortcut: ide.register_shortcut(short_key, shortcut, item_ui)
def __init__(self, config, weboob, app, parent=None): QtMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.config = config self.weboob = weboob self.app = app # search history is a list of patterns which have been searched self.search_history = self.loadSearchHistory() self.updateCompletion() self.connect(self.ui.searchEdit, SIGNAL("returnPressed()"), self.search) self.connect(self.ui.idEdit, SIGNAL("returnPressed()"), self.searchId) self.connect(self.ui.typeCombo, SIGNAL("currentIndexChanged(QString)"), self.typeComboChanged) count = self.config.get('settings', 'maxresultsnumber') self.ui.countSpin.setValue(int(count)) showT = self.config.get('settings', 'showthumbnails') self.ui.showTCheck.setChecked(showT == '1') self.connect(self.ui.stopButton, SIGNAL("clicked()"), self.stopProcess) self.ui.stopButton.hide() self.connect(self.ui.actionBackends, SIGNAL("triggered()"), self.backendsConfig) q = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_Q), self) self.connect(q, SIGNAL("activated()"), self.close) n = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_PageDown), self) self.connect(n, SIGNAL("activated()"), self.nextTab) p = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_PageUp), self) self.connect(p, SIGNAL("activated()"), self.prevTab) w = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_W), self) self.connect(w, SIGNAL("activated()"), self.closeCurrentTab) l = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_L), self) self.connect(l, SIGNAL("activated()"), self.ui.searchEdit.setFocus) self.connect(l, SIGNAL("activated()"), self.ui.searchEdit.selectAll) self.connect(self.ui.resultsTab, SIGNAL("tabCloseRequested(int)"), self.closeTab) self.loadBackendsList() if self.ui.backendEdit.count() == 0: self.backendsConfig() langs = sorted(LANGUAGE_CONV.keys()) for lang in langs: self.ui.langCombo.addItem(lang) self.ui.langCombo.hide() self.ui.langLabel.hide()
def __init__(self, fileBrowser): QTreeView.__init__(self, fileBrowser) self._fileBrowser = fileBrowser self.setAttribute( Qt.WA_MacShowFocusRect, False ) self.setAttribute( Qt.WA_MacSmallSize ) self.setContextMenuPolicy( Qt.ActionsContextMenu ) self.setHeaderHidden( True ) self.setUniformRowHeights( True ) self.setTextElideMode(Qt.ElideMiddle) # dir model self._dirsModel = _FileSystemModel( self ) self._dirsModel.setNameFilterDisables( False ) self._dirsModel.setFilter( QDir.AllDirs | QDir.AllEntries | QDir.CaseSensitive | QDir.NoDotAndDotDot ) # self._dirsModel.directoryLoaded.connect(self.setFocus) TODO don't have this signal in my Qt version # create proxy model self._filteredModel = FileBrowserFilteredModel( self ) self._filteredModel.setSourceModel( self._dirsModel ) self.setModel( self._filteredModel) if not sys.platform.startswith('win'): self._dirsModel.setRootPath( "/" ) else: self._dirsModel.setRootPath('') # shortcut accessible only when self._tree has focus self._upShortcut = QShortcut( QKeySequence( "BackSpace" ), self ) self._upShortcut.setContext( Qt.WidgetShortcut ) self._upShortcut.activated.connect(self.moveUp) # shortcut accessible only when self._tree has focus self._homeShortcut = QShortcut( QKeySequence( "`" ), self ) self._homeShortcut.setContext( Qt.WidgetShortcut ) self._homeShortcut.activated.connect(self._goUserHomeDir) # shortcut accessible only when self._tree has focus self._homeShortcut = QShortcut( QKeySequence( "." ), self ) self._homeShortcut.setContext( Qt.WidgetShortcut ) self._homeShortcut.activated.connect(self._goCurrentDir) self.activated.connect(self._onActivated) self._fileActivated.connect(fileBrowser.fileActivated) # QDirModel loads item asynchronously, therefore we need timer for setting focus to the first item self._setFocusTimer = QTimer() self._setFocusTimer.timeout.connect(self._setFirstItemAsCurrent) self._setFocusTimer.setInterval(50) self.destroyed.connect(self._setFocusTimer.stop) self._timerAttempts = 0
class Shortcut(object): def __init__(self, widget, *shortcut): self.qshortcut = QShortcut(QKeySequence(*shortcut), widget) if isinstance(widget, QMainWindow): self.qshortcut.setContext(Qt.ApplicationShortcut) else: self.qshortcut.setContext(Qt.WidgetShortcut) def __rshift__(self, functor): s = self.qshortcut s.connect(s, SIGNAL("activated()"), functor)
def install_shortcuts(obj, actions, ide): short = resources.get_shortcut for action in actions: short_key = action.get("shortcut", None) action_data = action.get("action", None) connect = action.get("connect", None) shortcut = None item_ui = None func = None if connect: func = getattr(obj, connect, None) if short_key and not action_data: if isinstance(short_key, QKeySequence): shortcut = QShortcut(short_key, ide) else: shortcut = QShortcut(short(short_key), ide) if isinstance(func, collections.Callable): ide.connect(shortcut, SIGNAL("activated()"), func) if action_data: is_menu = action_data.get('is_menu', False) if is_menu: item_ui = QMenu(action_data['text'], ide) else: item_ui = QAction(action_data['text'], ide) object_name = "%s.%s" % (obj.__class__.__name__, connect) item_ui.setObjectName(object_name) image_name = action_data.get('image', None) section = action_data.get('section', None) weight = action_data.get('weight', None) keysequence = action_data.get('keysequence', None) if image_name: if isinstance(image_name, int): icon = ide.style().standardIcon(image_name) item_ui.setIcon(icon) elif isinstance(image_name, str): icon = QIcon(":img/" + image_name) item_ui.setIcon(icon) if short_key and not is_menu: item_ui.setShortcut(short(short_key)) elif keysequence: item_ui.setShortcut(short(keysequence)) if isinstance(func, collections.Callable) and not is_menu: ide.connect(item_ui, SIGNAL("triggered()"), func) if section and weight: ide.register_menuitem(item_ui, section, weight) if image_name and not is_menu: ide.register_toolbar(item_ui, section, weight) if short_key and shortcut: ide.register_shortcut(short_key, shortcut, item_ui)
def __new__(cls, *args, captionTitle=None, **kwargs): self = super().__new__(cls, None, cls.get_flags()) QDialog.__init__(self, None, self.get_flags()) WidgetMessagesMixin.__init__(self) stored_settings = kwargs.get('stored_settings', None) if self.settingsHandler: self.settingsHandler.initialize(self, stored_settings) self.signalManager = kwargs.get('signal_manager', None) self.__env = _asmappingproxy(kwargs.get("env", {})) setattr(self, gui.CONTROLLED_ATTRIBUTES, ControlledAttributesDict(self)) self.graphButton = None self.report_button = None OWWidget.widget_id += 1 self.widget_id = OWWidget.widget_id captionTitle = self.name if captionTitle is None else captionTitle # must be set without invoking setCaption self.captionTitle = captionTitle self.setWindowTitle(captionTitle) self.setFocusPolicy(Qt.StrongFocus) self.__blocking = False # flag indicating if the widget's position was already restored self.__was_restored = False self.__statusMessage = "" self.__msgwidget = None self.__msgchoice = 0 self.left_side = None self.controlArea = self.mainArea = self.buttonsArea = None self.splitter = None if self.want_basic_layout: self.set_basic_layout() sc = QShortcut(QKeySequence(Qt.ShiftModifier | Qt.Key_F1), self) sc.activated.connect(self.__quicktip) sc = QShortcut(QKeySequence.Copy, self) sc.activated.connect(self.copy_to_clipboard) if self.controlArea is not None: # Otherwise, the first control has focus self.controlArea.setFocus(Qt.ActiveWindowFocusReason) return self
def __init__(self): super(WrapperTable, self).__init__() QShortcut(QKeySequence.Close, self, self.close) QShortcut(QKeySequence.Quit, self, self.close) self.wrapper_name = None header = 'Website', 'HTTP method', 'Response format', 'Category', 'Notes', 'Run' font = QFont('Times New Roman', 16) self.setFont(font) self.setColumnCount(len(header)) self.setHorizontalHeaderLabels(header) font.setBold(True) self.horizontalHeader().setFont(font) self.setVerticalHeaderLabels([]) for wrapper_name in get_wrappers(): wrapper = load_wrapper(wrapper_name) try: if not wrapper.enabled: continue # ignore this disabled wrapper except AttributeError: pass num_rows = self.rowCount() self.insertRow(num_rows) self.setItem( num_rows, 0, QTableWidgetItem(wrapper_name.replace('_', ' ').title())) for i, attr in enumerate( ['http_method', 'response_format', 'category', 'notes']): try: value = getattr(wrapper, attr) except AttributeError: pass else: if attr == 'notes': # add line breaks value = value.replace('.', '.\n') self.setItem(num_rows, i + 1, QTableWidgetItem(value)) # add button to activate wrapper button = QPushButton('Go') button.clicked.connect(self.select_wrapper(wrapper_name)) self.setCellWidget(num_rows, len(header) - 1, button) self.setVerticalHeaderItem(num_rows, QTableWidgetItem('')) self.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) self.showMaximized() self.setSortingEnabled(True) self.sortItems(0) self.raise_()
def __init__(self, mw): QDialog.__init__(self) self.mw = mw self.form = Ui_dialog() self.form.setupUi(self) self.fillList() self.form.closeButton.clicked.connect(self.accept) self.form.addButton.clicked.connect(self.add) self.form.deleteButton.clicked.connect(self.delete) self.form.renameButton.clicked.connect(self.rename) self.form.studentsButton.clicked.connect(self.students) qlShortcut = QShortcut(QKeySequence("Alt+L"), self.form.listWidget) qlShortcut.connect(qlShortcut, QtCore.SIGNAL("activated()"), lambda: self.form.listWidget.setFocus())
def __init__(self, parent, actions=None): QTabWidget.__init__(self, parent) tab_bar = TabsBase(self, parent) self.connect(tab_bar, SIGNAL('move_tab(int,int)'), self.move_tab) self.connect(tab_bar, SIGNAL('move_tab(long,int,int)'), self.move_tab_from_another_tabwidget) self.setTabBar(tab_bar) self.menu = QMenu(self) if actions: add_actions(self.menu, actions) self.index_history = [] self.connect(self, SIGNAL('currentChanged(int)'), self.__current_changed) tabsc = QShortcut(QKeySequence("Ctrl+Tab"), parent, self.tab_navigate) tabsc.setContext(Qt.WidgetWithChildrenShortcut)
def __init__(self, name): QWebView.__init__(self) self.setPage(PlotWebPage(name)) self.setRenderHint(QPainter.Antialiasing, True) self.setContextMenuPolicy(Qt.NoContextMenu) self.settings().setAttribute(QWebSettings.JavascriptEnabled, True) self.settings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, True) self.settings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True) self.settings().clearMemoryCaches() self.__inspector_window = None shortcut = QShortcut(self) shortcut.setKey(Qt.Key_F12) shortcut.activated.connect(self.toggleInspector)
def registerShortcuts(mgr): scA = QShortcut(QKeySequence("1"), mainWindow, member=partial(trigger, "A")) mgr.register("Group 1", "Shortcut 1A", scA, None) scB = QShortcut(QKeySequence("2"), mainWindow, member=partial(trigger, "B")) mgr.register("Group 1", "Shortcut 1B", scB, None) scC = QShortcut(QKeySequence("3"), mainWindow, member=partial(trigger, "C")) mgr.register("Group 2", "Shortcut 2C", scC, None)
class DockWidget(QDockWidget): """Extended QDockWidget for Enki main window """ def __init__(self, *args): QDockWidget.__init__(self, *args) self._showAction = None self._titleBar = _TitleBar( self ) self.setTitleBarWidget( self._titleBar ) self._closeShortcut = QShortcut( QKeySequence( "Esc" ), self ) self._closeShortcut.setContext( Qt.WidgetWithChildrenShortcut ) self._closeShortcut.activated.connect(self._hide) def showAction(self): """Action shows the widget and set focus on it. Add this action to the main menu """ if not self._showAction : self._showAction = QAction(self.windowIcon(), self.windowTitle(), self) self._showAction.triggered.connect(self.show) self._showAction.triggered.connect(self._handleFocusProxy) return self._showAction def titleBarWidget(self): """QToolBar on the title. You may add own actions to this tool bar """ # method was added only for documenting return QDockWidget.titleBarWidget(self) def _handleFocusProxy(self): """Set focus to focus proxy. Called after widget has been shown """ if self.focusProxy() is not None: self.setFocus() def _hide(self): """Hide and return focus to MainWindow focus proxy """ self.hide() if self.parent() is not None and \ self.parent().focusProxy() is not None: self.parent().focusProxy().setFocus()
def __init__(self, parent=None): QWidget.__init__(self, parent) self.secs = None self.recording = False self.flashMillisecs = 1000 self.flashTimes = [0, 250, 500, 750] self.resize(400, 400) self.mainLayout = QVBoxLayout() self.setLayout(self.mainLayout) self.talkInfoFont = QFont('Serif', 50, QFont.Light) self.countdownFont = QFont('Serif', 300, QFont.Light) self.talkInfoString = QLabel() self.mainLayout.addWidget(self.talkInfoString) self.talkInfoString.setFont(self.talkInfoFont) self.talkInfoString.setAlignment(Qt.AlignCenter) self.talkInfoString.setStyleSheet( "QLabel { background-color : white; color : black; }") self.countdownString = QLabel() self.mainLayout.addWidget(self.countdownString) self.countdownString.setFont(self.countdownFont) self.countdownString.setAlignment(Qt.AlignCenter) self.countdownString.setStyleSheet( "QLabel { background-color : white; color : black; }") self.countdownTimer = QTimer() self.countdownTimer.timeout.connect(self.timertick) self.flashTimer = QTimer() self.flashTimer.timeout.connect(self.flash_display_text) QShortcut(QKeySequence("Esc"), self, self.showNormal)
def setupUi(self): super(MainWindow, self).setupUi(self) self.search_box.shortcut = QShortcut(self.search_box) self.search_box.shortcut.setKey('Ctrl+F') self.output_devices_group = QActionGroup(self) self.input_devices_group = QActionGroup(self) self.alert_devices_group = QActionGroup(self) self.video_devices_group = QActionGroup(self) self.request_screen_action = QAction('Request screen', self, triggered=self._AH_RequestScreenActionTriggered) self.share_my_screen_action = QAction('Share my screen', self, triggered=self._AH_ShareMyScreenActionTriggered) self.screen_sharing_button.addAction(self.request_screen_action) self.screen_sharing_button.addAction(self.share_my_screen_action) # adjust search box height depending on theme as the value set in designer isn't suited for all themes search_box = self.search_box option = QStyleOptionFrameV2() search_box.initStyleOption(option) frame_width = search_box.style().pixelMetric(QStyle.PM_DefaultFrameWidth, option, search_box) if frame_width < 4: search_box.setMinimumHeight(20 + 2*frame_width) # adjust the combo boxes for themes with too much padding (like the default theme on Ubuntu 10.04) option = QStyleOptionComboBox() self.identity.initStyleOption(option) wide_padding = self.identity.style().subControlRect(QStyle.CC_ComboBox, option, QStyle.SC_ComboBoxEditField, self.identity).height() < 10 self.identity.setStyleSheet("""QComboBox { padding: 0px 4px 0px 4px; }""" if wide_padding else "")
def __init__(self, parent=None): super(PyboritaWidget, self).__init__(parent) self.parent = parent main_container = QVBoxLayout(self) ## Información puntaje box_score = QHBoxLayout() self._score = "<h2>Puntaje: %s</h2>" self.lbl_score = QLabel(self._score % 0) self.lbl_score.setStyleSheet("background: #232729") self.lbl_score.setAlignment(Qt.AlignCenter) box_score.addWidget(self.lbl_score) self._max_score = "<h2>Máximo Puntaje: %s</h2>" self.lbl_max_score = QLabel(self._max_score % 0) self.lbl_max_score.setStyleSheet("background: #232729") self.lbl_max_score.setAlignment(Qt.AlignCenter) box_score.addWidget(self.lbl_max_score) main_container.addLayout(box_score) # Snake self.frame_snake = pyborita.Pyborita() main_container.addWidget(self.frame_snake) main_container.setAlignment(Qt.AlignCenter) tecla_escape = QShortcut(QKeySequence(Qt.Key_Escape), self) # Conexiones tecla_escape.activated.connect(self._mostrar_dialogo) self.frame_snake.scoreChanged[int].connect(self.update_score) self.frame_snake.highscoreChanged[int].connect(self.update_max_score)
def __init__(self, plugin): QFrame.__init__(self, core.workspace()) self._mode = None self.plugin = plugin from PyQt4 import uic # lazy import for better startup performance uic.loadUi(os.path.join(os.path.dirname(__file__), "SearchWidget.ui"), self) self.cbSearch.setCompleter(None) self.cbReplace.setCompleter(None) self.cbMask.setCompleter(None) self.fsModel = QDirModel(self.cbPath.lineEdit()) self.fsModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot) self.cbPath.lineEdit().setCompleter(QCompleter(self.fsModel, self.cbPath.lineEdit())) # TODO QDirModel is deprecated but QCompleter does not yet handle # QFileSystemodel - please update when possible.""" self.cbSearch.setCompleter(None) self.pbSearchStop.setVisible(False) self.pbReplaceCheckedStop.setVisible(False) self._progress = QProgressBar(self) self._progress.setAlignment(Qt.AlignCenter) self._progress.setToolTip(self.tr("Search in progress...")) self._progress.setMaximumSize(QSize(80, 16)) core.mainWindow().statusBar().insertPermanentWidget(1, self._progress) self._progress.setVisible(False) # cd up action self.tbCdUp = QToolButton(self.cbPath.lineEdit()) self.tbCdUp.setIcon(QIcon(":/enkiicons/go-up.png")) self.tbCdUp.setCursor(Qt.ArrowCursor) self.tbCdUp.installEventFilter(self) # for drawing button self.cbSearch.installEventFilter(self) # for catching Tab and Shift+Tab self.cbReplace.installEventFilter(self) # for catching Tab and Shift+Tab self.cbPath.installEventFilter(self) # for catching Tab and Shift+Tab self.cbMask.installEventFilter(self) # for catching Tab and Shift+Tab self._closeShortcut = QShortcut(QKeySequence("Esc"), self) self._closeShortcut.setContext(Qt.WidgetWithChildrenShortcut) self._closeShortcut.activated.connect(self.hide) # connections self.cbSearch.lineEdit().textChanged.connect(self._onSearchRegExpChanged) self.cbSearch.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbReplace.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbPath.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbMask.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbRegularExpression.stateChanged.connect(self._onSearchRegExpChanged) self.cbCaseSensitive.stateChanged.connect(self._onSearchRegExpChanged) self.tbCdUp.clicked.connect(self._onCdUpPressed) core.mainWindow().hideAllWindows.connect(self.hide) core.workspace().currentDocumentChanged.connect( lambda old, new: self.setVisible(self.isVisible() and new is not None) )
def addLabelClass(self, label_config): # Check label configuration if 'attributes' not in label_config: raise ImproperlyConfigured("Label with no 'attributes' dict found") attrs = label_config['attributes'] if 'class' not in attrs: raise ImproperlyConfigured("Labels must have an attribute 'class'") label_class = attrs['class'] if label_class in self._class_config: raise ImproperlyConfigured("Label with class '%s' defined more than once" % label_class) # Store config self._class_config[label_class] = label_config # Parse configuration and create handlers and item self.parseConfiguration(label_class, label_config) # Add label class button button = QPushButton(label_class, self) button.setCheckable(True) button.setFlat(True) button.clicked.connect(bind(self.onClassButtonPressed, label_class)) self._class_buttons[label_class] = button self._classbox_layout.addWidget(button) # Add hotkey if 'hotkey' in label_config: hotkey = QShortcut(QKeySequence(label_config['hotkey']), self) hotkey.activated.connect(button.click) self._class_shortcuts[label_class] = hotkey
def create_dockwidget(self): """Add to parent QMainWindow as a dock widget""" # This is not clear yet why the following do not work... # (see Issue #880) ## # Using Qt.Window window flags solves Issue #880 (detached dockwidgets ## # are not painted after restarting Spyder and restoring their hexstate) ## # but it does not work with PyQt <=v4.7 (dockwidgets can't be docked) ## # or non-Windows platforms (lot of warnings are printed out) ## # (so in those cases, we use the default window flags: Qt.Widget): ## flags = Qt.Widget if is_old_pyqt or os.name != 'nt' else Qt.Window dock = QDockWidget(self.get_plugin_title(), self.main) #, flags) dock.setObjectName(self.__class__.__name__ + "_dw") dock.setAllowedAreas(self.ALLOWED_AREAS) dock.setFeatures(self.FEATURES) dock.setWidget(self) self.update_margins() self.connect(dock, SIGNAL('visibilityChanged(bool)'), self.visibility_changed) self.dockwidget = dock short = self.get_option("shortcut", None) if short is not None: shortcut = QShortcut(QKeySequence(short), self.main, self.switch_to_plugin) self.register_shortcut(shortcut, "_", "Switch to %s" % self.CONF_SECTION, default=short) return (dock, self.LOCATION)
def __init__(self): super(Demo, self).__init__() self.code_editor = CodeEditor(self) self.code_editor.setup_editor( language = "python", font = QFont("Courier New") ) run_sc = QShortcut(QKeySequence("F5"), self, self.run) self.shell = InternalShell(self, {"demo":self}, multithreaded = False, max_line_count = 3000, font = QFont("Courier new", 10), message='caonima' ) self.dict_editor = DictEditorWidget(self, {}) self.dict_editor.editor.set_filter(self.filter_namespace) self.dict_editor.set_data(self.shell.interpreter.namespace) vbox = QVBoxLayout() vbox.addWidget(self.code_editor) vbox.addWidget(self.shell) hbox = QHBoxLayout() hbox.addWidget(self.dict_editor) hbox.addLayout(vbox) self.setLayout(hbox) self.resize(800, 600)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setRenderHint(QPainter.Antialiasing) self.setRenderHint(QPainter.TextAntialiasing) self.__layoutMode = ThumbnailView.AutoReflow self.__columnCount = -1 self.__grid = GraphicsThumbnailGrid() self.__grid.currentThumbnailChanged.connect( self.__onCurrentThumbnailChanged ) self.__previewWidget = None scene = GraphicsScene(self) scene.addItem(self.__grid) scene.selectionRectPointChanged.connect( self.__ensureVisible, Qt.QueuedConnection ) self.setScene(scene) sh = QShortcut(Qt.Key_Space, self, context=Qt.WidgetWithChildrenShortcut) sh.activated.connect(self.__previewToogle) self.__grid.geometryChanged.connect(self.__updateSceneRect)
def __init__(self): super(_StatusBar, self).__init__() self.current_status = _STATUSBAR_STATE_SEARCH self._widgetStatus = QWidget() vbox = QVBoxLayout(self._widgetStatus) vbox.setContentsMargins(0, 0, 0, 0) vbox.setSpacing(0) #Search Layout self._searchWidget = SearchWidget() vbox.addWidget(self._searchWidget) #Replace Layout self._replaceWidget = ReplaceWidget() vbox.addWidget(self._replaceWidget) self._replaceWidget.setVisible(False) #Code Locator self._codeLocator = locator.CodeLocatorWidget() vbox.addWidget(self._codeLocator) self._codeLocator.setVisible(False) #File system completer self._fileSystemOpener = FileSystemOpener() vbox.addWidget(self._fileSystemOpener) self._fileSystemOpener.setVisible(False) self.addWidget(self._widgetStatus) # Not Configurable Shortcuts shortEscStatus = QShortcut(QKeySequence(Qt.Key_Escape), self) self.connect(shortEscStatus, SIGNAL("activated()"), self.hide_status) self.connect(self._searchWidget._btnClose, SIGNAL("clicked()"), self.hide_status) self.connect(self._replaceWidget._btnCloseReplace, SIGNAL("clicked()"), lambda: self._replaceWidget.setVisible(False)) self.connect(self._fileSystemOpener.btnClose, SIGNAL("clicked()"), self.hide_status) self.connect(self._fileSystemOpener, SIGNAL("requestHide()"), self.hide_status) self.connect(self._codeLocator, SIGNAL("hidden()"), self.hide_status) #Register signals connections connections = ( { 'target': 'main_container', 'signal_name': 'currentEditorChanged(QString)', 'slot': self._handle_tab_changed }, { 'target': 'main_container', 'signal_name': 'updateLocator(QString)', 'slot': self._explore_file_code }, { 'target': 'projects_explorer', 'signal_name': 'updateLocator()', 'slot': self._explore_code }, ) IDE.register_signals('status_bar', connections) IDE.register_service('status_bar', self)
def _firstLetterShortcut(self, button, text): """ Provide first-letter only shortcuts (not Alt+first letter which is already present because of the & before letters) for a button. button - One of self.buttonBox.[Discard/Cancel/Save] text - Text for this button's label. The letter following the & will have a shortcut created for it. Return value: the shortcut for the given button. """ # Set the text of the button, which automatically provides an Alt+letter # shortcut. self.buttonBox.button(button).setText(self.tr(text)) # Get the letter after the ampersand (there should be exactly one # ampersand in the string). assert text.count('&') == 1 letter = text[text.index('&') + 1] # Create a first-letter only shortcut, which clicks the button when # the letter is typed. shortcut = QShortcut(QKeySequence(letter), self) shortcut.activated.connect(self.buttonBox.button(button).click) return shortcut
def __init__(self, parent, mainwin): QWebView.__init__(self, parent) self.mainwin = mainwin self.setUrl(QUrl("about:blank")) self.setObjectName("webview") self.setTextSizeMultiplier(self.ZOOM_DEFAULT) shortcut = QShortcut(QKeySequence.ZoomIn, self) shortcut.activated.connect(self._zoom_in) shortcut.setContext(Qt.WidgetShortcut) shortcut = QShortcut(QKeySequence(Qt.CTRL | Qt.Key_Equal), self) shortcut.activated.connect(self._zoom_neutral) shortcut.setContext(Qt.WidgetShortcut) shortcut = QShortcut(QKeySequence.ZoomOut, self) shortcut.activated.connect(self._zoom_out) shortcut.setContext(Qt.WidgetShortcut)
def __init__(self, *args): QDockWidget.__init__(self, *args) self._showAction = None self._titleBar = _TitleBar( self ) self.setTitleBarWidget( self._titleBar ) self._closeShortcut = QShortcut( QKeySequence( "Esc" ), self ) self._closeShortcut.setContext( Qt.WidgetWithChildrenShortcut ) self._closeShortcut.activated.connect(self._hide)
def __init__(self, parent, actions=None): BaseTabs.__init__(self, parent, actions) tab_bar = TabBar(self, parent) self.connect(tab_bar, SIGNAL('move_tab(int,int)'), self.move_tab) self.connect(tab_bar, SIGNAL('move_tab(long,int,int)'), self.move_tab_from_another_tabwidget) self.setTabBar(tab_bar) self.index_history = [] self.connect(self, SIGNAL('currentChanged(int)'), self.__current_changed) tabsc = QShortcut(QKeySequence("Ctrl+Tab"), parent, self.tab_navigate) tabsc.setContext(Qt.WidgetWithChildrenShortcut) # Browsing tabs button browse_button = create_toolbutton(self, icon=get_icon("browse_tab.png"), tip=translate("Tabs", "Browse tabs")) self.browse_tabs_menu = QMenu(self) browse_button.setMenu(self.browse_tabs_menu) browse_button.setPopupMode(browse_button.InstantPopup) self.connect(self.browse_tabs_menu, SIGNAL("aboutToShow()"), self.update_browse_tabs_menu) self.setCornerWidget(browse_button)
def setup_buttons(chooser, buttons, text, do_function): bhbl = QHBoxLayout() if not isMac: bhbl.setSpacing(0) for button_item in buttons: b = QPushButton(button_item["label"]) b.setToolTip( _("Change {what} to {name}.").format( what=text, name=button_item["name"])) l = lambda s=chooser, nn=button_item["name"]: do_function(s, nn) try: s = QShortcut( QKeySequence(_(button_item["shortcut"])), chooser.widget) except KeyError: pass else: s.connect(s, SIGNAL("activated()"), l) if isMac: b.setStyleSheet("padding: 5px; padding-right: 7px;") bhbl.addWidget(b) chooser.connect(b, SIGNAL("clicked()"), l) chooser.addLayout(bhbl)
def setup_model_buttons(self): bhbl = QHBoxLayout() bhbl.setSpacing(default_button_spacing) for button_item in extra_buttons: b = QPushButton(button_item["label"]) b.setToolTip(_("Change Note Type to {note_name}.").format( note_name=button_item["note_name"])) l = lambda s=self, nn=button_item["note_name"]: change_model_to(s, nn) try: s = QShortcut( QKeySequence(_(button_item["shortcut"])), self.widget) except KeyError: pass else: s.connect(s, SIGNAL("activated()"), l) try: b.setFixedWidth(button_item["button_width"]) except KeyError: b.setFixedWidth(default_button_width) bhbl.addWidget(b) self.connect(b, SIGNAL("clicked()"), l) self.addLayout(bhbl)
def initShortcuts(self): (ctrl, shift) = (self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16) # Disable some shortcuts self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('D') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl + shift) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('T') + ctrl) #self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Z") + ctrl) #self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Y") + ctrl) # Use Ctrl+Space for autocompletion self.shortcutAutocomplete = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_Space), self) self.shortcutAutocomplete.setContext(Qt.WidgetShortcut) self.shortcutAutocomplete.activated.connect(self.autoComplete)
def __init__(self, parent, mainwin): QPlainTextEdit.__init__(self, parent) self.mainwin = mainwin self.setObjectName("restedit") self._formats = {} self.textChanged.connect(self._restedit_update) self.blockCountChanged.connect(self._blockcount_update) shortcut = QShortcut(QKeySequence("Ctrl+T"), self) shortcut.activated.connect(self._choose_font) shortcut = QShortcut(QKeySequence.ZoomIn, self) shortcut.activated.connect(self._zoom_in) shortcut.setContext(Qt.WidgetShortcut) shortcut = QShortcut(QKeySequence.ZoomOut, self) shortcut.activated.connect(self._zoom_out) shortcut.setContext(Qt.WidgetShortcut) self._doc = QApplication.instance().rest self._last_warnings = {} # should be moved to the document
def __init__(self, parentObject, windowTitle, windowIcon = QIcon(), shortcut = None): QDockWidget.__init__(self, parentObject) self._showAction = None self.setObjectName(str(self.__class__)) self.setWindowTitle(windowTitle) if not windowIcon.isNull(): self.setWindowIcon(windowIcon) if shortcut is not None: self.showAction().setShortcut(shortcut) self._titleBar = _TitleBar( self ) self.setTitleBarWidget( self._titleBar ) if shortcut is not None: toolTip = "Move focus with <b>%s</b>,<br/>close with <b>Esc</b>" % shortcut else: toolTip = "Close with <b>Esc</b>" self._titleBar.setToolTip(toolTip) self._closeShortcut = QShortcut( QKeySequence( "Esc" ), self ) self._closeShortcut.setContext( Qt.WidgetWithChildrenShortcut ) self._closeShortcut.activated.connect(self._close)
def __init__(self, *args, **kw): # To override the Qt widget used by RichIPythonWidget self.custom_control = IPythonControlWidget self.custom_page_control = IPythonPageControlWidget super(SMIPythonWidget, self).__init__(*args, **kw) self.set_background_color() # --- Spyder variables --- self.ipyclient = None # --- Keyboard shortcuts --- inspectsc = QShortcut(QKeySequence("Ctrl+I"), self, self._control.inspect_current_object) inspectsc.setContext(Qt.WidgetWithChildrenShortcut) clear_consolesc = QShortcut(QKeySequence("Ctrl+L"), self, self.clear_console) clear_consolesc.setContext(Qt.WidgetWithChildrenShortcut) # --- IPython variables --- # To send an interrupt signal to the Spyder kernel self.custom_interrupt = True # To restart the Spyder kernel in case it dies self.custom_restart = True
class DockWidget(QDockWidget): """Extended QDockWidget for Enki main window """ closed = pyqtSignal() """ closed() **Signal** emitted, when dock is closed """ shown = pyqtSignal() """ shown() **Signal** emitted, when dock is shown """ def __init__(self, parentObject, windowTitle, windowIcon = QIcon(), shortcut = None): QDockWidget.__init__(self, parentObject) self._showAction = None self.setObjectName(str(self.__class__)) self.setWindowTitle(windowTitle) if not windowIcon.isNull(): self.setWindowIcon(windowIcon) if shortcut is not None: self.showAction().setShortcut(shortcut) self._titleBar = _TitleBar( self ) self.setTitleBarWidget( self._titleBar ) if shortcut is not None: toolTip = "Move focus with <b>%s</b>,<br/>close with <b>Esc</b>" % shortcut else: toolTip = "Close with <b>Esc</b>" self._titleBar.setToolTip(toolTip) self._closeShortcut = QShortcut( QKeySequence( "Esc" ), self ) self._closeShortcut.setContext( Qt.WidgetWithChildrenShortcut ) self._closeShortcut.activated.connect(self._close) def keyPressEvent(self, event): """Catch Esc. Not using QShortcut, because dock shall be closed, only if child widgets haven't catched Esc event """ if event.key() == Qt.Key_Escape and \ event.modifiers() == Qt.NoModifier: self._hide() else: QDockWidget.keyPressEvent(self, event) def showAction(self): """Action shows the widget and set focus on it. Add this action to the main menu """ if not self._showAction : self._showAction = QAction(self.windowIcon(), self.windowTitle(), self) self._showAction.triggered.connect(self.show) self._showAction.triggered.connect(self._handleFocusProxy) return self._showAction def titleBarWidget(self): """QToolBar on the title. You may add own actions to this tool bar """ # method was added only for documenting return QDockWidget.titleBarWidget(self) def _handleFocusProxy(self): """Set focus to focus proxy. Called after widget has been shown """ if self.focusProxy() is not None: self.setFocus() def _close(self): """Hide and return focus to MainWindow focus proxy """ self.close() if self.parent() is not None and \ self.parent().focusProxy() is not None: self.parent().focusProxy().setFocus() def closeEvent(self, event): """Widget was closed""" self.closed.emit() def showEvent(self, event): """Widget was shown""" self.shown.emit()
class SearchWidget(QFrame): """Widget, appeared, when Ctrl+F pressed. Has different forms for different search modes """ Normal = "normal" Good = "good" Bad = "bad" Incorrect = "incorrect" visibilityChanged = pyqtSignal(bool) """ visibilityChanged(visible) **Signal** emitted, when widget has been shown or hidden """ # pylint: disable=W0105 searchInDirectoryStartPressed = pyqtSignal(type(re.compile("")), list, unicode) """ searchInDirectoryStartPressed(regEx, mask, path) **Signal** emitted, when 'search in directory' button had been pressed """ # pylint: disable=W0105 searchInDirectoryStopPressed = pyqtSignal() """ searchInDirectoryStopPressed() **Signal** emitted, when 'stop search in directory' button had been pressed """ # pylint: disable=W0105 replaceCheckedStartPressed = pyqtSignal(unicode) """ replaceCheckedStartPressed(replText) **Signal** emitted, when 'replace checked' button had been pressed """ # pylint: disable=W0105 replaceCheckedStopPressed = pyqtSignal() """ replaceCheckedStartPressed() **Signal** emitted, when 'stop replacing checked' button had been pressed """ # pylint: disable=W0105 searchRegExpChanged = pyqtSignal(type(re.compile(""))) """ searchRegExpValidStateChanged(regEx) **Signal** emitted, when search regexp has been changed. If reg exp is invalid - regEx object contains empty pattern """ # pylint: disable=W0105 searchNext = pyqtSignal() """ searchNext() **Signal** emitted, when 'Search Next' had been pressed """ # pylint: disable=W0105 searchPrevious = pyqtSignal() """ searchPrevious() **Signal** emitted, when 'Search Previous' had been pressed """ # pylint: disable=W0105 replaceFileOne = pyqtSignal(unicode) """ replaceFileOne(replText) **Signal** emitted, when 'Replace' had been pressed """ # pylint: disable=W0105 replaceFileAll = pyqtSignal(unicode) """ replaceFileAll(replText) **Signal** emitted, when 'Replace All' had been pressed """ # pylint: disable=W0105 def __init__(self, plugin): QFrame.__init__(self, core.workspace()) self._mode = None self.plugin = plugin from PyQt4 import uic # lazy import for better startup performance uic.loadUi(os.path.join(os.path.dirname(__file__), "SearchWidget.ui"), self) self.cbSearch.setCompleter(None) self.cbReplace.setCompleter(None) self.cbMask.setCompleter(None) self.fsModel = QDirModel(self.cbPath.lineEdit()) self.fsModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot) self.cbPath.lineEdit().setCompleter(QCompleter(self.fsModel, self.cbPath.lineEdit())) # TODO QDirModel is deprecated but QCompleter does not yet handle # QFileSystemodel - please update when possible.""" self.cbSearch.setCompleter(None) self.pbSearchStop.setVisible(False) self.pbReplaceCheckedStop.setVisible(False) self._progress = QProgressBar(self) self._progress.setAlignment(Qt.AlignCenter) self._progress.setToolTip(self.tr("Search in progress...")) self._progress.setMaximumSize(QSize(80, 16)) core.mainWindow().statusBar().insertPermanentWidget(1, self._progress) self._progress.setVisible(False) # cd up action self.tbCdUp = QToolButton(self.cbPath.lineEdit()) self.tbCdUp.setIcon(QIcon(":/enkiicons/go-up.png")) self.tbCdUp.setCursor(Qt.ArrowCursor) self.tbCdUp.installEventFilter(self) # for drawing button self.cbSearch.installEventFilter(self) # for catching Tab and Shift+Tab self.cbReplace.installEventFilter(self) # for catching Tab and Shift+Tab self.cbPath.installEventFilter(self) # for catching Tab and Shift+Tab self.cbMask.installEventFilter(self) # for catching Tab and Shift+Tab self._closeShortcut = QShortcut(QKeySequence("Esc"), self) self._closeShortcut.setContext(Qt.WidgetWithChildrenShortcut) self._closeShortcut.activated.connect(self.hide) # connections self.cbSearch.lineEdit().textChanged.connect(self._onSearchRegExpChanged) self.cbSearch.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbReplace.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbPath.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbMask.lineEdit().returnPressed.connect(self._onReturnPressed) self.cbRegularExpression.stateChanged.connect(self._onSearchRegExpChanged) self.cbCaseSensitive.stateChanged.connect(self._onSearchRegExpChanged) self.tbCdUp.clicked.connect(self._onCdUpPressed) core.mainWindow().hideAllWindows.connect(self.hide) core.workspace().currentDocumentChanged.connect( lambda old, new: self.setVisible(self.isVisible() and new is not None) ) def show(self): """Reimplemented function. Sends signal """ super(SearchWidget, self).show() self.visibilityChanged.emit(self.isVisible()) def hide(self): """Reimplemented function. Sends signal, returns focus to workspace """ super(SearchWidget, self).hide() core.workspace().focusCurrentDocument() self.visibilityChanged.emit(self.isVisible()) def setVisible(self, visible): """Reimplemented function. Sends signal """ super(SearchWidget, self).setVisible(visible) self.visibilityChanged.emit(self.isVisible()) def _regExEscape(self, text): """Improved version of re.escape() Doesn't escape space, comma, underscore. Escapes \n and \t """ text = re.escape(text) # re.escape escapes space, comma, underscore, but, it is not necessary and makes text not readable for symbol in " ,_='\"/:@#%&": text = text.replace("\\" + symbol, symbol) text = text.replace("\\\n", "\\n") text = text.replace("\\\t", "\\t") return text def _makeEscapeSeqsVisible(self, text): """Replace invisible \n and \t with escape sequences """ text = text.replace("\t", "\\t") text = text.replace("\n", "\\n") return text def setMode(self, mode): """Change search mode. i.e. from "Search file" to "Replace directory" """ if self._mode == mode and self.isVisible(): if core.workspace().currentDocument() is not None and not core.workspace().currentDocument().hasFocus(): self.cbSearch.lineEdit().selectAll() self.cbSearch.setFocus() self._mode = mode # Set Search and Replace text if ( core.workspace().currentDocument() is not None and core.workspace().currentDocument().hasFocus() and core.workspace().currentDocument().selectedText() ): searchText = core.workspace().currentDocument().selectedText() self.cbReplace.setEditText(self._makeEscapeSeqsVisible(searchText)) if self.cbRegularExpression.checkState() == Qt.Checked: searchText = self._regExEscape(searchText) self.cbSearch.setEditText(searchText) if ( not self.cbReplace.lineEdit().text() and self.cbSearch.lineEdit().text() and not self.cbRegularExpression.checkState() == Qt.Checked ): self.cbReplace.setEditText(self.cbSearch.lineEdit().text()) # Move focus to Search edit self.cbSearch.setFocus() self.cbSearch.lineEdit().selectAll() # Set search path if mode & ModeFlagDirectory and not (self.isVisible() and self.cbPath.isVisible()): try: searchPath = os.path.abspath(unicode(os.path.curdir)) self.cbPath.setEditText(searchPath) except OSError: # current directory might have been deleted pass # Set widgets visibility flag according to state widgets = ( self.wSearch, self.pbPrevious, self.pbNext, self.pbSearch, self.wReplace, self.wPath, self.pbReplace, self.pbReplaceAll, self.pbReplaceChecked, self.wOptions, self.wMask, ) # wSear pbPrev pbNext pbSear wRepl wPath pbRep pbRAll pbRCHK wOpti wMask visible = { ModeSearch: (1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0), ModeReplace: (1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0), ModeSearchDirectory: (1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1), ModeReplaceDirectory: (1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1), ModeSearchOpenedFiles: (1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1), ModeReplaceOpenedFiles: (1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1), } for i, widget in enumerate(widgets): widget.setVisible(visible[mode][i]) # Search next button text if mode == ModeReplace: self.pbNext.setText("Next") else: self.pbNext.setText(u"Next↵") # Finaly show all with valid size self.show() # show before updating widgets and labels self._updateLabels() self._updateWidgets() def eventFilter(self, object_, event): """ Event filter for mode switch tool button Draws icons in the search and path lineEdits """ if event.type() == QEvent.Paint and object_ is self.tbCdUp: # draw CdUp button in search path QLineEdit toolButton = object_ lineEdit = self.cbPath.lineEdit() lineEdit.setContentsMargins(lineEdit.height(), 0, 0, 0) height = lineEdit.height() availableRect = QRect(0, 0, height, height) if toolButton.rect() != availableRect: toolButton.setGeometry(availableRect) painter = QPainter(toolButton) toolButton.icon().paint(painter, availableRect) return True elif event.type() == QEvent.KeyPress: # Tab and Shift+Tab in QLineEdits if event.key() == Qt.Key_Tab: self._moveFocus(1) return True elif event.key() == Qt.Key_Backtab: self._moveFocus(-1) return True return QFrame.eventFilter(self, object_, event) def _onReturnPressed(self): """Return or Enter pressed on widget. Search next or Replace next """ if self.pbReplace.isVisible(): self.pbReplace.click() elif self.pbNext.isVisible(): self.pbNext.click() elif self.pbSearch.isVisible(): self.pbSearch.click() elif self.pbSearchStop.isVisible(): self.pbSearchStop.click() def _moveFocus(self, step): """Move focus forward or backward according to step. Standard Qt Keyboard focus algorithm doesn't allow circular navigation """ allFocusableWidgets = (self.cbSearch, self.cbReplace, self.cbPath, self.cbMask) visibleWidgets = [widget for widget in allFocusableWidgets if widget.isVisible()] try: focusedIndex = visibleWidgets.index(QApplication.focusWidget()) except ValueError: print >>sys.stderr, "Invalid focused widget in Search Widget" return nextFocusedIndex = (focusedIndex + step) % len(visibleWidgets) visibleWidgets[nextFocusedIndex].setFocus() visibleWidgets[nextFocusedIndex].lineEdit().selectAll() def _updateLabels(self): """Update 'Search' 'Replace' 'Path' labels geometry """ width = 0 if self.lSearch.isVisible(): width = max(width, self.lSearch.minimumSizeHint().width()) if self.lReplace.isVisible(): width = max(width, self.lReplace.minimumSizeHint().width()) if self.lPath.isVisible(): width = max(width, self.lPath.minimumSizeHint().width()) self.lSearch.setMinimumWidth(width) self.lReplace.setMinimumWidth(width) self.lPath.setMinimumWidth(width) def _updateWidgets(self): """Update geometry of widgets with buttons """ width = 0 if self.wSearchRight.isVisible(): width = max(width, self.wSearchRight.minimumSizeHint().width()) if self.wReplaceRight.isVisible(): width = max(width, self.wReplaceRight.minimumSizeHint().width()) if self.wPathRight.isVisible(): width = max(width, self.wPathRight.minimumSizeHint().width()) self.wSearchRight.setMinimumWidth(width) self.wReplaceRight.setMinimumWidth(width) self.wPathRight.setMinimumWidth(width) def _updateComboBoxes(self): """Update comboboxes with last used texts """ searchText = self.cbSearch.currentText() replaceText = self.cbReplace.currentText() maskText = self.cbMask.currentText() # search if searchText: index = self.cbSearch.findText(searchText) if index == -1: self.cbSearch.addItem(searchText) # replace if replaceText: index = self.cbReplace.findText(replaceText) if index == -1: self.cbReplace.addItem(replaceText) # mask if maskText: index = self.cbMask.findText(maskText) if index == -1: self.cbMask.addItem(maskText) def _searchPatternTextAndFlags(self): """Get search pattern and flags """ pattern = self.cbSearch.currentText() if not self.cbRegularExpression.checkState() == Qt.Checked: pattern = re.escape(pattern) flags = 0 if not self.cbCaseSensitive.checkState() == Qt.Checked: flags = re.IGNORECASE return pattern, flags def getRegExp(self): """Read search parameters from controls and present it as a regular expression """ pattern, flags = self._searchPatternTextAndFlags() return re.compile(pattern, flags) def isSearchRegExpValid(self): """Try to compile search pattern to check if it is valid Returns bool result and text error """ pattern, flags = self._searchPatternTextAndFlags() try: re.compile(pattern, flags) except re.error, ex: return False, unicode(ex) return True, None
def install_shortcuts(self, ide): """Install the shortcuts to the IDE.""" self.ide = ide short = resources.get_shortcut self.shortChangeTab = QShortcut(short("Change-Tab"), self.ide) self.shortChangeTabReverse = QShortcut( short("Change-Tab-Reverse"), self.ide) self.shortDuplicate = QShortcut(short("Duplicate"), self.ide) self.shortRemove = QShortcut(short("Remove-line"), self.ide) self.shortMoveUp = QShortcut(short("Move-up"), self.ide) self.shortMoveDown = QShortcut(short("Move-down"), self.ide) self.shortCloseTab = QShortcut(short("Close-tab"), self.ide) self.shortNew = QShortcut(short("New-file"), self.ide) self.shortNewProject = QShortcut(short("New-project"), self.ide) self.shortOpen = QShortcut(short("Open-file"), self.ide) self.shortOpenProject = QShortcut(short("Open-project"), self.ide) self.shortSave = QShortcut(short("Save-file"), self.ide) self.shortSaveProject = QShortcut(short("Save-project"), self.ide) self.shortPrint = QShortcut(short("Print-file"), self.ide) self.shortRedo = QShortcut(short("Redo"), self.ide) self.shortAddBookmark = QShortcut(short("Add-Bookmark-or-Breakpoint"), self.ide) self.shortComment = QShortcut(short("Comment"), self.ide) self.shortUncomment = QShortcut(short("Uncomment"), self.ide) self.shortHorizontalLine = QShortcut(short("Horizontal-line"), self.ide) self.shortTitleComment = QShortcut(short("Title-comment"), self.ide) self.shortIndentLess = QShortcut(short("Indent-less"), self.ide) self.shortHideMisc = QShortcut(short("Hide-misc"), self.ide) self.shortHideEditor = QShortcut(short("Hide-editor"), self.ide) self.shortHideExplorer = QShortcut(short("Hide-explorer"), self.ide) self.shortRunFile = QShortcut(short("Run-file"), self.ide) self.shortRunProject = QShortcut(short("Run-project"), self.ide) self.shortSwitchFocus = QShortcut(short("Switch-Focus"), self.ide) self.shortStopExecution = QShortcut(short("Stop-execution"), self.ide) self.shortHideAll = QShortcut(short("Hide-all"), self.ide) self.shortFullscreen = QShortcut(short("Full-screen"), self.ide) self.shortFind = QShortcut(short("Find"), self.ide) self.shortFindNext = QShortcut(short("Find-next"), self.ide) self.shortFindPrevious = QShortcut(short("Find-previous"), self.ide) self.shortFindReplace = QShortcut(short("Find-replace"), self.ide) self.shortFindWithWord = QShortcut(short("Find-with-word"), self.ide) self.shortHelp = QShortcut(short("Help"), self.ide) self.shortSplitHorizontal = QShortcut(short("Split-horizontal"), self.ide) self.shortSplitVertical = QShortcut(short("Split-vertical"), self.ide) self.shortFollowMode = QShortcut(short("Follow-mode"), self.ide) self.shortReloadFile = QShortcut(short("Reload-file"), self.ide) self.shortFindInFiles = QShortcut(short("Find-in-files"), self.ide) self.shortImport = QShortcut(short("Import"), self.ide) self.shortGoToDefinition = QShortcut(short("Go-to-definition"), self.ide) self.shortCompleteDeclarations = QShortcut( short("Complete-Declarations"), self.ide) self.shortCodeLocator = QShortcut(short("Code-locator"), self.ide) self.shortFileOpener = QShortcut(short("File-Opener"), self.ide) self.shortNavigateBack = QShortcut(short("Navigate-back"), self.ide) self.shortNavigateForward = QShortcut(short("Navigate-forward"), self.ide) self.shortOpenLastTabOpened = QShortcut(short("Open-recent-closed"), self.ide) self.shortShowCodeNav = QShortcut(short("Show-Code-Nav"), self.ide) self.shortShowPasteHistory = QShortcut(short("Show-Paste-History"), self.ide) self.shortPasteHistory = QShortcut(short("History-Paste"), self.ide) self.shortCopyHistory = QShortcut(short("History-Copy"), self.ide) self.shortHighlightWord = QShortcut(short("Highlight-Word"), self.ide) self.shortChangeSplitFocus = QShortcut(short("change-split-focus"), self.ide) self.shortMoveTabSplit = QShortcut(short("move-tab-to-next-split"), self.ide) self.shortChangeTabVisibility = QShortcut( short("change-tab-visibility"), self.ide) #Connect Shortcuts Signals self.connect(self.shortNavigateBack, SIGNAL("activated()"), lambda: self.__navigate_with_keyboard(False)) self.connect(self.shortNavigateForward, SIGNAL("activated()"), lambda: self.__navigate_with_keyboard(True)) self.connect(self.shortCodeLocator, SIGNAL("activated()"), self.ide.status.show_locator) self.connect(self.shortFileOpener, SIGNAL("activated()"), self.ide.status.show_file_opener) self.connect(self.shortGoToDefinition, SIGNAL("activated()"), self.editor_go_to_definition) self.connect(self.shortCompleteDeclarations, SIGNAL("activated()"), self.editor_complete_declaration) self.connect(self.shortRedo, SIGNAL("activated()"), self.editor_redo) self.connect(self.shortHorizontalLine, SIGNAL("activated()"), self.editor_insert_horizontal_line) self.connect(self.shortTitleComment, SIGNAL("activated()"), self.editor_insert_title_comment) self.connect(self.shortFollowMode, SIGNAL("activated()"), self.ide.mainContainer.show_follow_mode) self.connect(self.shortReloadFile, SIGNAL("activated()"), self.ide.mainContainer.reload_file) self.connect(self.shortSplitHorizontal, SIGNAL("activated()"), lambda: self.ide.mainContainer.split_tab(True)) self.connect(self.shortSplitVertical, SIGNAL("activated()"), lambda: self.ide.mainContainer.split_tab(False)) self.connect(self.shortNew, SIGNAL("activated()"), self.ide.mainContainer.add_editor) self.connect(self.shortNewProject, SIGNAL("activated()"), self.ide.explorer.create_new_project) self.connect(self.shortHideMisc, SIGNAL("activated()"), self.view_misc_visibility) self.connect(self.shortHideEditor, SIGNAL("activated()"), self.view_main_visibility) self.connect(self.shortHideExplorer, SIGNAL("activated()"), self.view_explorer_visibility) self.connect(self.shortHideAll, SIGNAL("activated()"), self.hide_all) self.connect(self.shortFullscreen, SIGNAL("activated()"), self.fullscreen_mode) self.connect(self.shortOpen, SIGNAL("activated()"), self.ide.mainContainer.open_file) self.connect(self.shortOpenProject, SIGNAL("activated()"), self.open_project) self.connect(self.shortCloseTab, SIGNAL("activated()"), self.ide.mainContainer.close_tab) self.connect(self.shortSave, SIGNAL("activated()"), self.ide.mainContainer.save_file) self.connect(self.shortSaveProject, SIGNAL("activated()"), self.save_project) self.connect(self.shortPrint, SIGNAL("activated()"), self.print_file) self.connect(self.shortFind, SIGNAL("activated()"), self.ide.status.show) self.connect(self.shortFindPrevious, SIGNAL("activated()"), self.ide.status._searchWidget.find_previous) self.connect(self.shortFindNext, SIGNAL("activated()"), self.ide.status._searchWidget.find_next) self.connect(self.shortFindWithWord, SIGNAL("activated()"), self.ide.status.show_with_word) self.connect(self.shortFindReplace, SIGNAL("activated()"), self.ide.status.show_replace) self.connect(self.shortRunFile, SIGNAL("activated()"), self.execute_file) self.connect(self.shortRunProject, SIGNAL("activated()"), self.execute_project) self.connect(self.shortSwitchFocus, SIGNAL("activated()"), self.switch_focus) self.connect(self.shortStopExecution, SIGNAL("activated()"), self.kill_execution) self.connect(self.shortIndentLess, SIGNAL("activated()"), self.editor_indent_less) self.connect(self.shortComment, SIGNAL("activated()"), self.editor_comment) self.connect(self.shortUncomment, SIGNAL("activated()"), self.editor_uncomment) self.connect(self.shortHelp, SIGNAL("activated()"), self.ide.mainContainer.show_python_doc) self.connect(self.shortImport, SIGNAL("activated()"), self.import_from_everywhere) self.connect(self.shortFindInFiles, SIGNAL("activated()"), self.ide.misc.show_find_in_files_widget) self.connect(self.shortMoveUp, SIGNAL("activated()"), self.editor_move_up) self.connect(self.shortMoveDown, SIGNAL("activated()"), self.editor_move_down) self.connect(self.shortRemove, SIGNAL("activated()"), self.editor_remove_line) self.connect(self.shortDuplicate, SIGNAL("activated()"), self.editor_duplicate) self.connect(self.shortOpenLastTabOpened, SIGNAL("activated()"), self.reopen_last_tab) self.connect(self.shortChangeTab, SIGNAL("activated()"), self.ide.mainContainer.change_tab) self.connect(self.shortChangeTabReverse, SIGNAL("activated()"), self.ide.mainContainer.change_tab_reverse) self.connect(self.shortShowCodeNav, SIGNAL("activated()"), self.ide.mainContainer.show_navigation_buttons) self.connect(self.shortAddBookmark, SIGNAL("activated()"), self._add_bookmark_breakpoint) self.connect(self.shortShowPasteHistory, SIGNAL("activated()"), self.ide.central.lateralPanel.combo.showPopup) self.connect(self.shortCopyHistory, SIGNAL("activated()"), self._copy_history) self.connect(self.shortPasteHistory, SIGNAL("activated()"), self._paste_history) self.connect(self.shortHighlightWord, SIGNAL("activated()"), self.editor_highlight_word) self.connect(self.shortChangeSplitFocus, SIGNAL("activated()"), self.ide.mainContainer.change_split_focus) self.connect(self.shortMoveTabSplit, SIGNAL("activated()"), self.move_tab_to_next_split) self.connect(self.shortChangeTabVisibility, SIGNAL("activated()"), self.ide.mainContainer.change_tabs_visibility) key = Qt.Key_1 for i in range(10): if sys.platform == "darwin": short = TabShortcuts( QKeySequence(Qt.CTRL + Qt.ALT + key), self.ide, i) else: short = TabShortcuts(QKeySequence(Qt.ALT + key), self.ide, i) key += 1 self.connect(short, SIGNAL("activated()"), self._change_tab_index) short = TabShortcuts(QKeySequence(Qt.ALT + Qt.Key_0), self.ide, 10) self.connect(short, SIGNAL("activated()"), self._change_tab_index) #Connect SIGNALs from other objects self.connect(self.ide.mainContainer._tabMain, SIGNAL("runFile()"), self.execute_file) self.connect(self.ide.mainContainer._tabSecondary, SIGNAL("runFile()"), self.execute_file) self.connect(self.ide.mainContainer._tabMain, SIGNAL("addToProject(QString)"), self._add_file_to_project) self.connect(self.ide.mainContainer._tabSecondary, SIGNAL("addToProject(QString)"), self._add_file_to_project) self.connect(self.ide.mainContainer, SIGNAL("openProject(QString)"), self.open_project) # Not Configurable Shortcuts self._shortEscStatus = QShortcut(QKeySequence(Qt.Key_Escape), self.ide.status) self._shortEscMisc = QShortcut(QKeySequence(Qt.Key_Escape), self.ide.misc) self.connect(self._shortEscStatus, SIGNAL("activated()"), self.ide.status.hide_status) self.connect(self._shortEscMisc, SIGNAL("activated()"), self.ide.misc.hide)
class __Actions(QObject): """This class is like the Sauron's Ring: One ring to rule them all, One ring to find them, One ring to bring them all and in the darkness bind them. This Class knows all the containers, and its know by all the containers, but the containers don't need to know between each other, in this way we can keep a better api without the need to tie the behaviour between the widgets, and let them just consume the 'actions' they need.""" def __init__(self): QObject.__init__(self) #Definition Locator self._locator = locator.Locator() self.__codeBack = [] self.__codeForward = [] self.__bookmarksFile = '' self.__bookmarksPos = -1 self.__breakpointsFile = '' self.__breakpointsPos = -1 self.__operations = { 0: self._navigate_code_jumps, 1: self._navigate_bookmarks, 2: self._navigate_breakpoints} def install_shortcuts(self, ide): """Install the shortcuts to the IDE.""" self.ide = ide short = resources.get_shortcut self.shortChangeTab = QShortcut(short("Change-Tab"), self.ide) self.shortChangeTabReverse = QShortcut( short("Change-Tab-Reverse"), self.ide) self.shortDuplicate = QShortcut(short("Duplicate"), self.ide) self.shortRemove = QShortcut(short("Remove-line"), self.ide) self.shortMoveUp = QShortcut(short("Move-up"), self.ide) self.shortMoveDown = QShortcut(short("Move-down"), self.ide) self.shortCloseTab = QShortcut(short("Close-tab"), self.ide) self.shortNew = QShortcut(short("New-file"), self.ide) self.shortNewProject = QShortcut(short("New-project"), self.ide) self.shortOpen = QShortcut(short("Open-file"), self.ide) self.shortOpenProject = QShortcut(short("Open-project"), self.ide) self.shortSave = QShortcut(short("Save-file"), self.ide) self.shortSaveProject = QShortcut(short("Save-project"), self.ide) self.shortPrint = QShortcut(short("Print-file"), self.ide) self.shortRedo = QShortcut(short("Redo"), self.ide) self.shortAddBookmark = QShortcut(short("Add-Bookmark-or-Breakpoint"), self.ide) self.shortComment = QShortcut(short("Comment"), self.ide) self.shortUncomment = QShortcut(short("Uncomment"), self.ide) self.shortHorizontalLine = QShortcut(short("Horizontal-line"), self.ide) self.shortTitleComment = QShortcut(short("Title-comment"), self.ide) self.shortIndentLess = QShortcut(short("Indent-less"), self.ide) self.shortHideMisc = QShortcut(short("Hide-misc"), self.ide) self.shortHideEditor = QShortcut(short("Hide-editor"), self.ide) self.shortHideExplorer = QShortcut(short("Hide-explorer"), self.ide) self.shortRunFile = QShortcut(short("Run-file"), self.ide) self.shortRunProject = QShortcut(short("Run-project"), self.ide) self.shortSwitchFocus = QShortcut(short("Switch-Focus"), self.ide) self.shortStopExecution = QShortcut(short("Stop-execution"), self.ide) self.shortHideAll = QShortcut(short("Hide-all"), self.ide) self.shortFullscreen = QShortcut(short("Full-screen"), self.ide) self.shortFind = QShortcut(short("Find"), self.ide) self.shortFindNext = QShortcut(short("Find-next"), self.ide) self.shortFindPrevious = QShortcut(short("Find-previous"), self.ide) self.shortFindReplace = QShortcut(short("Find-replace"), self.ide) self.shortFindWithWord = QShortcut(short("Find-with-word"), self.ide) self.shortHelp = QShortcut(short("Help"), self.ide) self.shortSplitHorizontal = QShortcut(short("Split-horizontal"), self.ide) self.shortSplitVertical = QShortcut(short("Split-vertical"), self.ide) self.shortFollowMode = QShortcut(short("Follow-mode"), self.ide) self.shortReloadFile = QShortcut(short("Reload-file"), self.ide) self.shortFindInFiles = QShortcut(short("Find-in-files"), self.ide) self.shortImport = QShortcut(short("Import"), self.ide) self.shortGoToDefinition = QShortcut(short("Go-to-definition"), self.ide) self.shortCompleteDeclarations = QShortcut( short("Complete-Declarations"), self.ide) self.shortCodeLocator = QShortcut(short("Code-locator"), self.ide) self.shortFileOpener = QShortcut(short("File-Opener"), self.ide) self.shortNavigateBack = QShortcut(short("Navigate-back"), self.ide) self.shortNavigateForward = QShortcut(short("Navigate-forward"), self.ide) self.shortOpenLastTabOpened = QShortcut(short("Open-recent-closed"), self.ide) self.shortShowCodeNav = QShortcut(short("Show-Code-Nav"), self.ide) self.shortShowPasteHistory = QShortcut(short("Show-Paste-History"), self.ide) self.shortPasteHistory = QShortcut(short("History-Paste"), self.ide) self.shortCopyHistory = QShortcut(short("History-Copy"), self.ide) self.shortHighlightWord = QShortcut(short("Highlight-Word"), self.ide) self.shortChangeSplitFocus = QShortcut(short("change-split-focus"), self.ide) self.shortMoveTabSplit = QShortcut(short("move-tab-to-next-split"), self.ide) self.shortChangeTabVisibility = QShortcut( short("change-tab-visibility"), self.ide) #Connect Shortcuts Signals self.connect(self.shortNavigateBack, SIGNAL("activated()"), lambda: self.__navigate_with_keyboard(False)) self.connect(self.shortNavigateForward, SIGNAL("activated()"), lambda: self.__navigate_with_keyboard(True)) self.connect(self.shortCodeLocator, SIGNAL("activated()"), self.ide.status.show_locator) self.connect(self.shortFileOpener, SIGNAL("activated()"), self.ide.status.show_file_opener) self.connect(self.shortGoToDefinition, SIGNAL("activated()"), self.editor_go_to_definition) self.connect(self.shortCompleteDeclarations, SIGNAL("activated()"), self.editor_complete_declaration) self.connect(self.shortRedo, SIGNAL("activated()"), self.editor_redo) self.connect(self.shortHorizontalLine, SIGNAL("activated()"), self.editor_insert_horizontal_line) self.connect(self.shortTitleComment, SIGNAL("activated()"), self.editor_insert_title_comment) self.connect(self.shortFollowMode, SIGNAL("activated()"), self.ide.mainContainer.show_follow_mode) self.connect(self.shortReloadFile, SIGNAL("activated()"), self.ide.mainContainer.reload_file) self.connect(self.shortSplitHorizontal, SIGNAL("activated()"), lambda: self.ide.mainContainer.split_tab(True)) self.connect(self.shortSplitVertical, SIGNAL("activated()"), lambda: self.ide.mainContainer.split_tab(False)) self.connect(self.shortNew, SIGNAL("activated()"), self.ide.mainContainer.add_editor) self.connect(self.shortNewProject, SIGNAL("activated()"), self.ide.explorer.create_new_project) self.connect(self.shortHideMisc, SIGNAL("activated()"), self.view_misc_visibility) self.connect(self.shortHideEditor, SIGNAL("activated()"), self.view_main_visibility) self.connect(self.shortHideExplorer, SIGNAL("activated()"), self.view_explorer_visibility) self.connect(self.shortHideAll, SIGNAL("activated()"), self.hide_all) self.connect(self.shortFullscreen, SIGNAL("activated()"), self.fullscreen_mode) self.connect(self.shortOpen, SIGNAL("activated()"), self.ide.mainContainer.open_file) self.connect(self.shortOpenProject, SIGNAL("activated()"), self.open_project) self.connect(self.shortCloseTab, SIGNAL("activated()"), self.ide.mainContainer.close_tab) self.connect(self.shortSave, SIGNAL("activated()"), self.ide.mainContainer.save_file) self.connect(self.shortSaveProject, SIGNAL("activated()"), self.save_project) self.connect(self.shortPrint, SIGNAL("activated()"), self.print_file) self.connect(self.shortFind, SIGNAL("activated()"), self.ide.status.show) self.connect(self.shortFindPrevious, SIGNAL("activated()"), self.ide.status._searchWidget.find_previous) self.connect(self.shortFindNext, SIGNAL("activated()"), self.ide.status._searchWidget.find_next) self.connect(self.shortFindWithWord, SIGNAL("activated()"), self.ide.status.show_with_word) self.connect(self.shortFindReplace, SIGNAL("activated()"), self.ide.status.show_replace) self.connect(self.shortRunFile, SIGNAL("activated()"), self.execute_file) self.connect(self.shortRunProject, SIGNAL("activated()"), self.execute_project) self.connect(self.shortSwitchFocus, SIGNAL("activated()"), self.switch_focus) self.connect(self.shortStopExecution, SIGNAL("activated()"), self.kill_execution) self.connect(self.shortIndentLess, SIGNAL("activated()"), self.editor_indent_less) self.connect(self.shortComment, SIGNAL("activated()"), self.editor_comment) self.connect(self.shortUncomment, SIGNAL("activated()"), self.editor_uncomment) self.connect(self.shortHelp, SIGNAL("activated()"), self.ide.mainContainer.show_python_doc) self.connect(self.shortImport, SIGNAL("activated()"), self.import_from_everywhere) self.connect(self.shortFindInFiles, SIGNAL("activated()"), self.ide.misc.show_find_in_files_widget) self.connect(self.shortMoveUp, SIGNAL("activated()"), self.editor_move_up) self.connect(self.shortMoveDown, SIGNAL("activated()"), self.editor_move_down) self.connect(self.shortRemove, SIGNAL("activated()"), self.editor_remove_line) self.connect(self.shortDuplicate, SIGNAL("activated()"), self.editor_duplicate) self.connect(self.shortOpenLastTabOpened, SIGNAL("activated()"), self.reopen_last_tab) self.connect(self.shortChangeTab, SIGNAL("activated()"), self.ide.mainContainer.change_tab) self.connect(self.shortChangeTabReverse, SIGNAL("activated()"), self.ide.mainContainer.change_tab_reverse) self.connect(self.shortShowCodeNav, SIGNAL("activated()"), self.ide.mainContainer.show_navigation_buttons) self.connect(self.shortAddBookmark, SIGNAL("activated()"), self._add_bookmark_breakpoint) self.connect(self.shortShowPasteHistory, SIGNAL("activated()"), self.ide.central.lateralPanel.combo.showPopup) self.connect(self.shortCopyHistory, SIGNAL("activated()"), self._copy_history) self.connect(self.shortPasteHistory, SIGNAL("activated()"), self._paste_history) self.connect(self.shortHighlightWord, SIGNAL("activated()"), self.editor_highlight_word) self.connect(self.shortChangeSplitFocus, SIGNAL("activated()"), self.ide.mainContainer.change_split_focus) self.connect(self.shortMoveTabSplit, SIGNAL("activated()"), self.move_tab_to_next_split) self.connect(self.shortChangeTabVisibility, SIGNAL("activated()"), self.ide.mainContainer.change_tabs_visibility) key = Qt.Key_1 for i in range(10): if sys.platform == "darwin": short = TabShortcuts( QKeySequence(Qt.CTRL + Qt.ALT + key), self.ide, i) else: short = TabShortcuts(QKeySequence(Qt.ALT + key), self.ide, i) key += 1 self.connect(short, SIGNAL("activated()"), self._change_tab_index) short = TabShortcuts(QKeySequence(Qt.ALT + Qt.Key_0), self.ide, 10) self.connect(short, SIGNAL("activated()"), self._change_tab_index) #Connect SIGNALs from other objects self.connect(self.ide.mainContainer._tabMain, SIGNAL("runFile()"), self.execute_file) self.connect(self.ide.mainContainer._tabSecondary, SIGNAL("runFile()"), self.execute_file) self.connect(self.ide.mainContainer._tabMain, SIGNAL("addToProject(QString)"), self._add_file_to_project) self.connect(self.ide.mainContainer._tabSecondary, SIGNAL("addToProject(QString)"), self._add_file_to_project) self.connect(self.ide.mainContainer, SIGNAL("openProject(QString)"), self.open_project) # Not Configurable Shortcuts self._shortEscStatus = QShortcut(QKeySequence(Qt.Key_Escape), self.ide.status) self._shortEscMisc = QShortcut(QKeySequence(Qt.Key_Escape), self.ide.misc) self.connect(self._shortEscStatus, SIGNAL("activated()"), self.ide.status.hide_status) self.connect(self._shortEscMisc, SIGNAL("activated()"), self.ide.misc.hide) def update_shortcuts(self): """If the user update the key binded to any shortcut, update them.""" resources.load_shortcuts() short = resources.get_shortcut self.shortDuplicate.setKey(short("Duplicate")) self.shortRemove.setKey(short("Remove-line")) self.shortMoveUp.setKey(short("Move-up")) self.shortMoveDown.setKey(short("Move-down")) self.shortCloseTab.setKey(short("Close-tab")) self.shortNew.setKey(short("New-file")) self.shortNewProject.setKey(short("New-project")) self.shortOpen.setKey(short("Open-file")) self.shortOpenProject.setKey(short("Open-project")) self.shortSave.setKey(short("Save-file")) self.shortSaveProject.setKey(short("Save-project")) self.shortPrint.setKey(short("Print-file")) self.shortRedo.setKey(short("Redo")) self.shortComment.setKey(short("Comment")) self.shortHorizontalLine.setKey(short("Horizontal-line")) self.shortTitleComment.setKey(short("Title-comment")) self.shortIndentLess.setKey(short("Indent-less")) self.shortHideMisc.setKey(short("Hide-misc")) self.shortHideEditor.setKey(short("Hide-editor")) self.shortHideExplorer.setKey(short("Hide-explorer")) self.shortRunFile.setKey(short("Run-file")) self.shortRunProject.setKey(short("Run-project")) self.shortSwitchFocus.setKey(short("Switch-Focus")) self.shortStopExecution.setKey(short("Stop-execution")) self.shortHideAll.setKey(short("Hide-all")) self.shortFullscreen.setKey(short("Full-screen")) self.shortFind.setKey(short("Find")) self.shortFindNext.setKey(short("Find-next")) self.shortFindPrevious.setKey(short("Find-previous")) self.shortFindReplace.setKey(short("Find-replace")) self.shortFindWithWord.setKey(short("Find-with-word")) self.shortHelp.setKey(short("Help")) self.shortSplitHorizontal.setKey(short("Split-horizontal")) self.shortSplitVertical.setKey(short("Split-vertical")) self.shortFollowMode.setKey(short("Follow-mode")) self.shortReloadFile.setKey(short("Reload-file")) self.shortFindInFiles.setKey(short("Find-in-files")) self.shortImport.setKey(short("Import")) self.shortGoToDefinition.setKey(short("Go-to-definition")) self.shortCodeLocator.setKey(short("Code-locator")) self.shortNavigateBack.setKey(short("Navigate-back")) self.shortNavigateForward.setKey(short("Navigate-forward")) self.shortOpenLastTabOpened.setKey(short("Open-recent-closed")) self.shortChangeTab.setKey(short("Change-Tab")) self.shortChangeTabReverse.setKey(short("Change-Tab-Reverse")) self.shortAddBookmark.setKey(short("Add-Bookmark-or-Breakpoint")) self.shortShowCodeNav.setKey(short("Show-Code-Nav")) self.shortShowPasteHistory.setKey(short("Show-Paste-History")) self.shortMoveTabSplit.setKey(short("move-tab-to-next-split")) self.shortChangeTabVisibility.setKey(short("change-tab-visibility")) def move_tab_to_next_split(self): self.ide.mainContainer.move_tab_to_next_split( self.ide.mainContainer.actualTab) def switch_focus(self): widget = QApplication.focusWidget() if widget: if widget in (self.ide.mainContainer.actualTab, self.ide.mainContainer.actualTab.currentWidget()): self.ide.explorer.currentWidget().setFocus() elif widget in (self.ide.explorer, self.ide.explorer.currentWidget()): if self.ide.misc.isVisible(): self.ide.misc.stack.currentWidget().setFocus() else: self.ide.mainContainer.actualTab.currentWidget().setFocus() elif widget.parent() is self.ide.misc.stack: self.ide.mainContainer.actualTab.currentWidget().setFocus() def _change_tab_index(self): editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): container = self.ide.mainContainer.actualTab else: container = self.ide.explorer obj = self.sender() if obj.index < container.count(): container.setCurrentIndex(obj.index) def _copy_history(self): """Copy the selected text into the copy/paste history.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): cursor = editorWidget.textCursor() copy = cursor.selectedText() self.ide.central.lateralPanel.add_new_copy(copy) def _paste_history(self): """Paste the text from the copy/paste history.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): cursor = editorWidget.textCursor() paste = self.ide.central.lateralPanel.get_paste() cursor.insertText(paste) def _add_bookmark_breakpoint(self): """Add a bookmark or breakpoint to the current file in the editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): if self.ide.mainContainer.actualTab.navigator.operation == 1: editorWidget._sidebarWidget.set_bookmark( editorWidget.textCursor().blockNumber()) elif self.ide.mainContainer.actualTab.navigator.operation == 2: editorWidget._sidebarWidget.set_breakpoint( editorWidget.textCursor().blockNumber()) def __navigate_with_keyboard(self, val): """Navigate between the positions in the jump history stack.""" op = self.ide.mainContainer._tabMain.navigator.operation self.navigate_code_history(val, op) def _add_file_to_project(self, path): """Add the file for 'path' in the project the user choose here.""" pathProject = [self.ide.explorer.get_actual_project()] addToProject = ui_tools.AddToProject(pathProject, self.ide) addToProject.exec_() if not addToProject.pathSelected: return editorWidget = self.ide.mainContainer.get_actual_editor() if not editorWidget.ID: name = QInputDialog.getText(None, self.tr("Add File To Project"), self.tr("File Name:"))[0] if not name: QMessageBox.information(self, self.tr("Invalid Name"), self.tr("The file name is empty, please enter a name")) return else: name = file_manager.get_basename(editorWidget.ID) path = file_manager.create_path(addToProject.pathSelected, name) try: path = file_manager.store_file_content( path, editorWidget.get_text(), newFile=True) self.ide.mainContainer._file_watcher.allow_kill = False if path != editorWidget.ID: self.ide.mainContainer.remove_standalone_watcher( editorWidget.ID) editorWidget.ID = path self.ide.mainContainer.add_standalone_watcher(path) self.ide.mainContainer._file_watcher.allow_kill = True self.ide.explorer.add_existing_file(path) self.ide.change_window_title(path) name = file_manager.get_basename(path) self.ide.mainContainer.actualTab.setTabText( self.ide.mainContainer.actualTab.currentIndex(), name) editorWidget._file_saved() except file_manager.NinjaFileExistsException as ex: QMessageBox.information(self, self.tr("File Already Exists"), self.tr("Invalid Path: the file '%s' already exists." % ex.filename)) def add_project_to_console(self, projectFolder): """Add the namespace of the project received into the ninja-console.""" self.ide.misc._console.load_project_into_console(projectFolder) def remove_project_from_console(self, projectFolder): """Remove the namespace of the project received from the console.""" self.ide.misc._console.unload_project_from_console(projectFolder) def import_from_everywhere(self): """Show the dialog to insert an import from any place in the editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: text = editorWidget.get_text() froms = re.findall('^from (.*)', text, re.MULTILINE) fromSection = list(set([f.split(' import')[0] for f in froms])) dialog = from_import_dialog.FromImportDialog(fromSection, editorWidget, self.ide) dialog.show() def open_project(self, path=''): """Open a Project and load the symbols in the Code Locator.""" self.ide.explorer.open_project_folder(path) def open_project_properties(self): """Open a Project and load the symbols in the Code Locator.""" self.ide.explorer.open_project_properties() def create_profile(self): """Create a profile binding files and projects to a key.""" profileInfo = QInputDialog.getText(None, self.tr("Create Profile"), self.tr( "The Current Files and Projects will " "be associated to this profile.\n" "Profile Name:")) if profileInfo[1]: profileName = profileInfo[0] if not profileName or profileName in settings.PROFILES: QMessageBox.information(self, self.tr("Profile Name Invalid"), self.tr("The Profile name is invalid or already exists.")) return self.save_profile(profileName) return profileName def save_profile(self, profileName): """Save the updates from a profile.""" projects_obj = self.ide.explorer.get_opened_projects() projects = [p.path for p in projects_obj] files = self.ide.mainContainer.get_opened_documents() files = files[0] + files[1] settings.PROFILES[profileName] = [files, projects] qsettings = QSettings() qsettings.setValue('ide/profiles', settings.PROFILES) def activate_profile(self): """Show the Profile Manager dialog.""" profilesLoader = ui_tools.ProfilesLoader(self._load_profile_data, self.create_profile, self.save_profile, settings.PROFILES, self.ide) profilesLoader.show() def deactivate_profile(self): """Close the Profile Session.""" self.ide.Profile = None def _load_profile_data(self, key): """Activate the selected profile, closing the current files/projects""" self.ide.explorer.close_opened_projects() self.ide.mainContainer.open_files(settings.PROFILES[key][0]) self.ide.explorer.open_session_projects(settings.PROFILES[key][1]) def close_files_from_project(self, project): """Close the files related to this project.""" if project: tabMain = self.ide.mainContainer._tabMain for tabIndex in reversed(list(range(tabMain.count()))): if file_manager.belongs_to_folder( project, tabMain.widget(tabIndex).ID): tabMain.removeTab(tabIndex) tabSecondary = self.ide.mainContainer._tabSecondary for tabIndex in reversed(list(range(tabSecondary.count()))): if file_manager.belongs_to_folder( project, tabSecondary.widget(tabIndex).ID): tabSecondary.removeTab(tabIndex) self.ide.profile = None def count_file_code_lines(self): """Count the lines of code in the current file.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: block_count = editorWidget.blockCount() blanks = re.findall('(^\n)|(^(\s+)?#)|(^( +)?($|\n))', editorWidget.get_text(), re.M) blanks_count = len(blanks) resume = self.tr("Lines code: %s\n" % (block_count - blanks_count)) resume += self.tr("Blanks and commented lines: %s\n\n" % blanks_count) resume += self.tr("Total lines: %s" % block_count) msgBox = QMessageBox(QMessageBox.Information, self.tr("Summary of lines"), resume, QMessageBox.Ok, editorWidget) msgBox.exec_() def execute_file(self): """Execute the current file.""" editorWidget = self.ide.mainContainer.get_actual_editor() #emit a signal for plugin! self.emit(SIGNAL("fileExecuted(QString)"), editorWidget.ID) if editorWidget: self.ide.mainContainer.save_file(editorWidget) ext = file_manager.get_file_extension(editorWidget.ID) #TODO: Remove the IF statment with polymorphism using Handler if ext == 'py': self.ide.misc.run_application(editorWidget.ID) elif ext == 'html': self.ide.misc.render_web_page(editorWidget.ID) def execute_project(self): """Execute the project marked as Main Project.""" mainFile = self.ide.explorer.get_project_main_file() if not mainFile and self.ide.explorer._treeProjects and \ self.ide.explorer._treeProjects._actualProject: self.ide.explorer._treeProjects.open_project_properties() elif mainFile: self.save_project() path = self.ide.explorer.get_actual_project() #emit a signal for plugin! self.emit(SIGNAL("projectExecuted(QString)"), path) # load our jutsus! project = json_manager.read_ninja_project(path) python_exec = project.get('venv', False) if not python_exec: python_exec = project.get('pythonPath', 'python') PYTHONPATH = project.get('PYTHONPATH', None) params = project.get('programParams', '') preExec = project.get('preExecScript', '') postExec = project.get('postExecScript', '') mainFile = file_manager.create_path(path, mainFile) self.ide.misc.run_application(mainFile, pythonPath=python_exec, PYTHONPATH=PYTHONPATH, programParams=params, preExec=preExec, postExec=postExec) def kill_execution(self): """Kill the execution of the current file or project.""" self.ide.misc.kill_application() def fullscreen_mode(self): """Change to fullscreen mode.""" if self.ide.isFullScreen(): self.ide.showMaximized() else: self.ide.showFullScreen() def editor_redo(self): """Execute the redo action in the current editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): editorWidget.redo() def editor_indent_less(self): """Indent 1 position to the left for the current line or selection.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): editorWidget.indent_less() def editor_indent_more(self): """Indent 1 position to the right for the current line or selection.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): editorWidget.indent_more() def editor_insert_debugging_prints(self): """Insert a print statement in each selected line.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: helpers.insert_debugging_prints(editorWidget) def editor_insert_pdb(self): """Insert a pdb.set_trace() statement in tjhe current line.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: helpers.insert_pdb(editorWidget) def editor_comment(self): """Mark the current line or selection as a comment.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.comment(editorWidget) def editor_uncomment(self): """Uncomment the current line or selection.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.uncomment(editorWidget) def editor_insert_horizontal_line(self): """Insert an horizontal lines of comment symbols.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.insert_horizontal_line(editorWidget) def editor_insert_title_comment(self): """Insert a Title surrounded by comment symbols.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.insert_title_comment(editorWidget) def editor_remove_trailing_spaces(self): """Remove the trailing spaces in the current editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: helpers.remove_trailing_spaces(editorWidget) def editor_replace_tabs_with_spaces(self): """Replace the Tabs with Spaces in the current editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: helpers.replace_tabs_with_spaces(editorWidget) def editor_move_up(self): """Move the current line or selection one position up.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.move_up(editorWidget) def editor_move_down(self): """Move the current line or selection one position down.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.move_down(editorWidget) def editor_remove_line(self): """Remove the current line or selection.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.remove_line(editorWidget) def editor_duplicate(self): """Duplicate the current line or selection.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): helpers.duplicate(editorWidget) def editor_go_to_definition(self): """Search the definition of the method or variable under the cursor. If more than one method or variable is found with the same name, shows a table with the results and let the user decide where to go.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): editorWidget.go_to_definition() def editor_highlight_word(self): """Highlight the occurrences of the current word in the editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): editorWidget.highlight_selected_word() def editor_complete_declaration(self): """Do the opposite action that Complete Declaration expect.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget and editorWidget.hasFocus(): editorWidget.complete_declaration() def editor_go_to_line(self, line): """Jump to the specified line in the current editor.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: editorWidget.jump_to_line(line) def reset_editor_flags(self): """Reset the Flags for all the opened editors.""" self.ide.mainContainer.reset_editor_flags() def call_editors_function(self, call_function, *args, **kwargs): self.ide.mainContainer.call_editors_function( call_function, args, kwargs) def preview_in_browser(self): """Load the current html file in the default browser.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: if not editorWidget.ID: self.ide.mainContainer.save_file() ext = file_manager.get_file_extension(editorWidget.ID) if ext == 'html': webbrowser.open(editorWidget.ID) def hide_all(self): """Hide/Show all the containers except the editor.""" if self.ide.menuBar().isVisible(): self.ide.central.lateralPanel.hide() self.ide.misc.hide() self.ide.toolbar.hide() self.ide.menuBar().hide() else: self.ide.central.lateralPanel.show() self.ide.toolbar.show() self.ide.menuBar().show() self.ide._menuView.hideAllAction.setChecked( self.ide.menuBar().isVisible()) self.ide._menuView.hideConsoleAction.setChecked( self.ide.central.misc.isVisible()) self.ide._menuView.hideEditorAction.setChecked( self.ide.central.mainContainer.isVisible()) self.ide._menuView.hideExplorerAction.setChecked( self.ide.central.lateralPanel.isVisible()) self.ide._menuView.hideToolbarAction.setChecked( self.ide.toolbar.isVisible()) def view_misc_visibility(self): self.ide.central.change_misc_visibility() self.ide._menuView.hideConsoleAction.setChecked( self.ide.central.misc.isVisible()) def view_main_visibility(self): self.ide.central.change_main_visibility() self.ide._menuView.hideEditorAction.setChecked( self.ide.central.mainContainer.isVisible()) def view_explorer_visibility(self): self.ide.central.change_explorer_visibility() self.ide._menuView.hideExplorerAction.setChecked( self.ide.central.lateralPanel.isVisible()) def save_project(self): """Save all the opened files that belongs to the actual project.""" path = self.ide.explorer.get_actual_project() if path: self.ide.mainContainer.save_project(path) def save_all(self): """Save all the opened files.""" self.ide.mainContainer.save_all() def print_file(self): """Call the print of ui_tool Call print of ui_tool depending on the focus of the application""" #TODO: Add funtionality for proyect tab and methods tab editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget is not None: fileName = "newDocument.pdf" if editorWidget.ID: fileName = file_manager.get_basename( editorWidget.ID) fileName = fileName[:fileName.rfind('.')] + '.pdf' ui_tools.print_file(fileName, editorWidget.print_) def locate_function(self, function, filePath, isVariable): """Move the cursor to the proper position in the navigate stack.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: self.__codeBack.append((editorWidget.ID, editorWidget.textCursor().position())) self.__codeForward = [] self._locator.navigate_to(function, filePath, isVariable) def update_explorer(self): """Update the symbols in the Symbol Explorer when a file is saved.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: ext = file_manager.get_file_extension(editorWidget.ID) #obtain a symbols handler for this file extension symbols_handler = settings.get_symbols_handler(ext) if symbols_handler: source = editorWidget.toPlainText() if editorWidget.encoding is not None: source = source.encode(editorWidget.encoding) if ext == 'py': args = (source, True) else: args = (source,) symbols = symbols_handler.obtain_symbols(*args) self.ide.explorer.update_symbols(symbols, editorWidget.ID) #TODO: Should we change the code below similar to the code above? exts = settings.SYNTAX.get('python')['extension'] if ext in exts or editorWidget.newDocument: self.ide.explorer.update_errors( editorWidget.errors, editorWidget.pep8) def update_migration_tips(self): """Update the migration tips in the Explorer.""" # This should be refactored with the new definition of singals in # the MainContainer editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: self.ide.explorer.update_migration(editorWidget.migration) def navigate_code_history(self, val, op): """Navigate the code history.""" self.__operations[op](val) def _navigate_code_jumps(self, val): """Navigate between the jump points.""" node = None if not val and self.__codeBack: node = self.__codeBack.pop() editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: self.__codeForward.append((editorWidget.ID, editorWidget.textCursor().position())) elif val and self.__codeForward: node = self.__codeForward.pop() editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: self.__codeBack.append((editorWidget.ID, editorWidget.textCursor().position())) if node: self.ide.mainContainer.open_file(node[0], node[1]) def _navigate_breakpoints(self, val): """Navigate between the breakpoints.""" breakList = list(settings.BREAKPOINTS.keys()) breakList.sort() if not breakList: return if self.__breakpointsFile not in breakList: self.__breakpointsFile = breakList[0] index = breakList.index(self.__breakpointsFile) breaks = settings.BREAKPOINTS.get(self.__breakpointsFile, []) lineNumber = 0 #val == True: forward if val: if (len(breaks) - 1) > self.__breakpointsPos: self.__breakpointsPos += 1 lineNumber = breaks[self.__breakpointsPos] elif len(breaks) > 0: if index < (len(breakList) - 1): self.__breakpointsFile = breakList[index + 1] else: self.__breakpointsFile = breakList[0] self.__breakpointsPos = 0 breaks = settings.BREAKPOINTS[self.__breakpointsFile] lineNumber = breaks[0] else: if self.__breakpointsPos > 0: self.__breakpointsPos -= 1 lineNumber = breaks[self.__breakpointsPos] elif len(breaks) > 0: self.__breakpointsFile = breakList[index - 1] breaks = settings.BREAKPOINTS[self.__breakpointsFile] self.__breakpointsPos = len(breaks) - 1 lineNumber = breaks[self.__breakpointsPos] if file_manager.file_exists(self.__breakpointsFile): self.ide.mainContainer.open_file(self.__breakpointsFile, lineNumber, None, True) else: settings.BREAKPOINTS.pop(self.__breakpointsFile) def _navigate_bookmarks(self, val): """Navigate between the bookmarks.""" bookList = list(settings.BOOKMARKS.keys()) bookList.sort() if not bookList: return if self.__bookmarksFile not in bookList: self.__bookmarksFile = bookList[0] index = bookList.index(self.__bookmarksFile) bookms = settings.BOOKMARKS.get(self.__bookmarksFile, []) lineNumber = 0 #val == True: forward if val: if (len(bookms) - 1) > self.__bookmarksPos: self.__bookmarksPos += 1 lineNumber = bookms[self.__bookmarksPos] elif len(bookms) > 0: if index < (len(bookList) - 1): self.__bookmarksFile = bookList[index + 1] else: self.__bookmarksFile = bookList[0] self.__bookmarksPos = 0 bookms = settings.BOOKMARKS[self.__bookmarksFile] lineNumber = bookms[0] else: if self.__bookmarksPos > 0: self.__bookmarksPos -= 1 lineNumber = bookms[self.__bookmarksPos] elif len(bookms) > 0: self.__bookmarksFile = bookList[index - 1] bookms = settings.BOOKMARKS[self.__bookmarksFile] self.__bookmarksPos = len(bookms) - 1 lineNumber = bookms[self.__bookmarksPos] if file_manager.file_exists(self.__bookmarksFile): self.ide.mainContainer.open_file(self.__bookmarksFile, lineNumber, None, True) else: settings.BOOKMARKS.pop(self.__bookmarksFile) def add_back_item_navigation(self): """Add an item to the back stack and reset the forward stack.""" editorWidget = self.ide.mainContainer.get_actual_editor() if editorWidget: self.__codeBack.append((editorWidget.ID, editorWidget.textCursor().position())) self.__codeForward = [] def group_tabs_together(self): """Group files that belongs to the same project together.""" if self.ide.explorer._treeProjects is None: return projects_obj = self.ide.explorer.get_opened_projects() projects = [p.path for p in projects_obj] for project in projects: projectName = self.ide.explorer.get_project_name(project) if not projectName: projectName = file_manager.get_basename(project) tabGroup = tab_group.TabGroup(project, projectName, self) for index in reversed(list(range( self.ide.mainContainer._tabMain.count()))): widget = self.ide.mainContainer._tabMain.widget(index) if type(widget) is editor.Editor and \ file_manager.belongs_to_folder(project, widget.ID): tabGroup.add_widget(widget) self.ide.mainContainer._tabMain.removeTab(index) if tabGroup.tabs: self.ide.mainContainer._tabMain.add_tab(tabGroup, projectName) def deactivate_tabs_groups(self): """Deactivate tab grouping based in the project they belong.""" for index in reversed(list(range( self.ide.mainContainer._tabMain.count()))): widget = self.ide.mainContainer._tabMain.widget(index) if type(widget) is tab_group.TabGroup: widget.only_expand() def reopen_last_tab(self): """Reopen the last closed tab.""" self.ide.mainContainer.actualTab._reopen_last_tab() def open_class_diagram(self): """Open the Class Diagram Generator.""" diagram = class_diagram.ClassDiagram(self) self.ide.mainContainer.add_tab(diagram, self.tr("Class Diagram v.0.1")) def reload_toolbar(self): """Reload the Toolbar.""" self.ide.load_toolbar()
def __init__(self, parent=None): super(ShellScintilla, self).__init__(parent) code.InteractiveInterpreter.__init__(self, locals=None) self.parent = parent self.opening = ['(', '{', '[', "'", '"'] self.closing = [')', '}', ']', "'", '"'] self.settings = QSettings() # Enable non-ascii chars for editor self.setUtf8(True) self.new_input_line = True self.setMarginWidth(0, 0) self.setMarginWidth(1, 0) self.setMarginWidth(2, 0) self.buffer = [] self.displayPrompt(False) for line in _init_commands: self.runsource(line) self.history = [] self.historyIndex = 0 # Read history command file self.readHistoryFile() self.historyDlg = HistoryDialog(self) # Brace matching: enable for a brace immediately before or after # the current position self.setBraceMatching(QsciScintilla.SloppyBraceMatch) self.setMatchedBraceBackgroundColor(QColor("#b7f907")) # Current line visible with special background color self.setCaretWidth(2) self.refreshSettingsShell() # Don't want to see the horizontal scrollbar at all # Use raw message to Scintilla here (all messages are documented # here: http://www.scintilla.org/ScintillaDoc.html) self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0) # not too small #self.setMinimumSize(500, 300) self.setWrapMode(QsciScintilla.WrapCharacter) self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER) ## Disable command key ctrl, shift = self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16 self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('T') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('D') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('Z') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('Y') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl + shift) ## New QShortcut = ctrl+space/ctrl+alt+space for Autocomplete self.newShortcutCSS = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Space), self) self.newShortcutCAS = QShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_Space), self) self.newShortcutCSS.setContext(Qt.WidgetShortcut) self.newShortcutCAS.setContext(Qt.WidgetShortcut) self.newShortcutCAS.activated.connect(self.autoCompleteKeyBinding) self.newShortcutCSS.activated.connect(self.showHistory)
class ShellOutputScintilla(QsciScintilla): def __init__(self, parent=None): super(ShellOutputScintilla, self).__init__(parent) self.parent = parent self.shell = self.parent.shell self.settings = QSettings() # Creates layout for message bar self.layout = QGridLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) spacerItem = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.layout.addItem(spacerItem, 1, 0, 1, 1) # messageBar instance self.infoBar = QgsMessageBar() sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) self.infoBar.setSizePolicy(sizePolicy) self.layout.addWidget(self.infoBar, 0, 0, 1, 1) # Enable non-ascii chars for editor self.setUtf8(True) sys.stdout = writeOut(self, sys.stdout) sys.stderr = writeOut(self, sys.stderr, "_traceback") self.insertInitText() self.refreshSettingsOutput() self.setReadOnly(True) # Set the default font font = QFont() font.setFamily('Courier') font.setFixedPitch(True) font.setPointSize(10) self.setFont(font) self.setMarginsFont(font) # Margin 0 is used for line numbers self.setMarginWidth(0, 0) self.setMarginWidth(1, 0) self.setMarginWidth(2, 0) #fm = QFontMetrics(font) self.setMarginsFont(font) self.setMarginWidth(1, "00000") self.setMarginLineNumbers(1, True) self.setMarginsForegroundColor(QColor("#3E3EE3")) self.setMarginsBackgroundColor(QColor("#f9f9f9")) self.setCaretLineVisible(True) self.setCaretWidth(0) self.setMinimumHeight(120) self.setWrapMode(QsciScintilla.WrapCharacter) self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0) self.runScut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_E), self) self.runScut.setContext(Qt.WidgetShortcut) self.runScut.activated.connect(self.enteredSelected) # Reimplemeted copy action to prevent paste prompt (>>>,...) in command view self.copyShortcut = QShortcut(QKeySequence.Copy, self) self.copyShortcut.activated.connect(self.copy) self.selectAllShortcut = QShortcut(QKeySequence.SelectAll, self) self.selectAllShortcut.activated.connect(self.selectAll) def insertInitText(self): txtInit = QCoreApplication.translate("PythonConsole", "Python Console \n" "Use iface to access QGIS API interface or Type help(iface) for more info") ## some translation string for the console header ends without '\n' ## and the first command in console will be appended at the header text. ## The following code add a '\n' at the end of the string if not present. if txtInit.endswith('\n'): self.setText(txtInit) else: self.setText(txtInit + '\n') def refreshSettingsOutput(self): # Set Python lexer self.setLexers() caretLineColor = self.settings.value("pythonConsole/caretLineColor", QColor("#fcf3ed")) cursorColor = self.settings.value("pythonConsole/cursorColor", QColor(Qt.black)) self.setCaretLineBackgroundColor(caretLineColor) self.setCaretForegroundColor(cursorColor) def setLexers(self): self.lexer = QsciLexerPython() loadFont = self.settings.value("pythonConsole/fontfamilytext", "Monospace") fontSize = self.settings.value("pythonConsole/fontsize", 10, type=int) font = QFont(loadFont) font.setFixedPitch(True) font.setPointSize(fontSize) font.setStyleHint(QFont.TypeWriter) font.setStretch(QFont.SemiCondensed) font.setLetterSpacing(QFont.PercentageSpacing, 87.0) font.setBold(False) self.lexer.setDefaultFont(font) self.lexer.setDefaultColor(QColor(self.settings.value("pythonConsole/defaultFontColor", QColor(Qt.black)))) self.lexer.setColor(QColor(self.settings.value("pythonConsole/commentFontColor", QColor(Qt.gray))), 1) self.lexer.setColor(QColor(self.settings.value("pythonConsole/keywordFontColor", QColor(Qt.darkGreen))), 5) self.lexer.setColor(QColor(self.settings.value("pythonConsole/classFontColor", QColor(Qt.blue))), 8) self.lexer.setColor(QColor(self.settings.value("pythonConsole/methodFontColor", QColor(Qt.darkGray))), 9) self.lexer.setColor(QColor(self.settings.value("pythonConsole/decorFontColor", QColor(Qt.darkBlue))), 15) self.lexer.setColor(QColor(self.settings.value("pythonConsole/commentBlockFontColor", QColor(Qt.gray))), 12) self.lexer.setColor(QColor(self.settings.value("pythonConsole/singleQuoteFontColor", QColor(Qt.blue))), 4) self.lexer.setColor(QColor(self.settings.value("pythonConsole/doubleQuoteFontColor", QColor(Qt.blue))), 3) self.lexer.setColor(QColor(self.settings.value("pythonConsole/tripleSingleQuoteFontColor", QColor(Qt.blue))), 6) self.lexer.setColor(QColor(self.settings.value("pythonConsole/tripleDoubleQuoteFontColor", QColor(Qt.blue))), 7) self.lexer.setColor(QColor(Qt.red), 14) self.lexer.setFont(font, 1) self.lexer.setFont(font, 2) self.lexer.setFont(font, 3) self.lexer.setFont(font, 4) for style in range(0, 33): paperColor = QColor(self.settings.value("pythonConsole/paperBackgroundColor", QColor(Qt.white))) self.lexer.setPaper(paperColor, style) self.setLexer(self.lexer) def clearConsole(self): self.setText('') self.insertInitText() self.shell.setFocus() def contextMenuEvent(self, e): menu = QMenu(self) iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png") iconClear = QgsApplication.getThemeIcon("console/iconClearConsole.png") iconHideTool = QgsApplication.getThemeIcon("console/iconHideToolConsole.png") iconSettings = QgsApplication.getThemeIcon("console/iconSettingsConsole.png") menu.addAction(iconHideTool, QCoreApplication.translate("PythonConsole", "Hide/Show Toolbar"), self.hideToolBar) menu.addSeparator() showEditorAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Show Editor"), self.showEditor) menu.addSeparator() runAction = menu.addAction(iconRun, QCoreApplication.translate("PythonConsole", "Enter Selected"), self.enteredSelected, QKeySequence(Qt.CTRL + Qt.Key_E)) clearAction = menu.addAction(iconClear, QCoreApplication.translate("PythonConsole", "Clear console"), self.clearConsole) menu.addSeparator() copyAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Copy"), self.copy, QKeySequence.Copy) menu.addSeparator() selectAllAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Select All"), self.selectAll, QKeySequence.SelectAll) menu.addSeparator() menu.addAction(iconSettings, QCoreApplication.translate("PythonConsole", "Settings"), self.parent.openSettings) runAction.setEnabled(False) clearAction.setEnabled(False) copyAction.setEnabled(False) selectAllAction.setEnabled(False) showEditorAction.setEnabled(True) if self.hasSelectedText(): runAction.setEnabled(True) copyAction.setEnabled(True) if not self.text(3) == '': selectAllAction.setEnabled(True) clearAction.setEnabled(True) if self.parent.tabEditorWidget.isVisible(): showEditorAction.setEnabled(False) menu.exec_(self.mapToGlobal(e.pos())) def hideToolBar(self): tB = self.parent.toolBar tB.hide() if tB.isVisible() else tB.show() self.shell.setFocus() def showEditor(self): Ed = self.parent.splitterObj if not Ed.isVisible(): Ed.show() self.parent.showEditorButton.setChecked(True) self.shell.setFocus() def copy(self): """Copy text to clipboard... or keyboard interrupt""" if self.hasSelectedText(): text = unicode(self.selectedText()) text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts QApplication.clipboard().setText(text) else: self.emit(SIGNAL("keyboard_interrupt()")) def enteredSelected(self): cmd = self.selectedText() self.shell.insertFromDropPaste(cmd) self.shell.entered() def keyPressEvent(self, e): # empty text indicates possible shortcut key sequence so stay in output txt = e.text() if len(txt) and txt >= " ": self.shell.append(txt) self.shell.move_cursor_to_end() self.shell.setFocus() e.ignore() else: # possible shortcut key sequence, accept it e.accept() def widgetMessageBar(self, iface, text): timeout = iface.messageTimeout() self.infoBar.pushMessage(text, QgsMessageBar.INFO, timeout)
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.fnames = [] # list of file names to be converted self.office_listener_started = False self.parse_cla() addQPB = QPushButton(self.tr('Add')) delQPB = QPushButton(self.tr('Delete')) clearQPB = QPushButton(self.tr('Clear')) vlayout1 = utils.add_to_layout('v', addQPB, delQPB, clearQPB, None) self.filesList = utils.FilesList() self.filesList.setSelectionMode(QAbstractItemView.ExtendedSelection) hlayout1 = utils.add_to_layout('h', self.filesList, vlayout1) outputQL = QLabel(self.tr('Output folder:')) self.toQLE = QLineEdit() self.toQLE.setReadOnly(True) self.toQTB = QToolButton() self.toQTB.setText('...') hlayout2 = utils.add_to_layout('h', outputQL, self.toQLE, self.toQTB) self.audiovideo_tab = AudioVideoTab(self) self.image_tab = ImageTab(self) self.document_tab = DocumentTab(self) self.tabs = [self.audiovideo_tab, self.image_tab, self.document_tab] tab_names = [self.tr('Audio/Video'), self.tr('Images'), self.tr('Documents')] self.tabWidget = QTabWidget() for num, tab in enumerate(tab_names): self.tabWidget.addTab(self.tabs[num], tab) self.tabWidget.setCurrentIndex(0) self.origQCB = QCheckBox( self.tr('Save each file in the same\nfolder as input file')) self.deleteQCB = QCheckBox(self.tr('Delete original')) convertQPB = QPushButton(self.tr('&Convert')) hlayout3 = utils.add_to_layout('h', self.origQCB, self.deleteQCB, None) hlayout4 = utils.add_to_layout('h', None, convertQPB) final_layout = utils.add_to_layout( 'v', hlayout1, self.tabWidget, hlayout2, hlayout3, hlayout4) self.dependenciesQL = QLabel() self.statusBar().addPermanentWidget(self.dependenciesQL, stretch=1) widget = QWidget() widget.setLayout(final_layout) self.setCentralWidget(widget) openAction = utils.create_action( self, self.tr('Open'), QKeySequence.Open, None, self.tr('Open a file'), self.add_files ) convertAction = utils.create_action( self, self.tr('Convert'), 'Ctrl+C', None, self.tr('Convert files'), self.start_conversion ) quitAction = utils.create_action( self, self.tr('Quit'), 'Ctrl+Q', None, self.tr('Quit'), self.close ) edit_presetsAction = utils.create_action( self, self.tr('Edit Presets'), 'Ctrl+P', None, self.tr('Edit Presets'), self.open_dialog_presets ) importAction = utils.create_action( self, self.tr('Import'), None, None, self.tr('Import presets'), self.import_presets ) exportAction = utils.create_action( self, self.tr('Export'), None, None, self.tr('Export presets'), self.export_presets ) resetAction = utils.create_action( self, self.tr('Reset'), None, None, self.tr('Reset presets'), self.reset_presets ) syncAction = utils.create_action( self, self.tr('Synchronize'), None, None, self.tr('Synchronize presets'), self.sync_presets ) removeoldAction = utils.create_action( self, self.tr('Remove old'), None, None, self.tr('Remove old presets'), self.removeold_presets ) clearallAction = utils.create_action( self, self.tr('Clear All'), None, None, self.tr('Clear form'), self.clear_all ) preferencesAction = utils.create_action( self, self.tr('Preferences'), 'Alt+Ctrl+P', None, self.tr('Preferences'), self.open_dialog_preferences ) trackerAction = utils.create_action( self, 'Issue tracker', None, None, None, lambda: webbrowser.open( "https://github.com/Ilias95/FF-Multi-Converter/issues") ) wikiAction = utils.create_action( self, 'Wiki', None, None, None, lambda: webbrowser.open( "https://github.com/Ilias95/FF-Multi-Converter/wiki") ) ffmpegdocAction = utils.create_action( self, 'FFmpeg ' + self.tr('documentation'), None, None, None, lambda: webbrowser.open( "https://www.ffmpeg.org/documentation.html") ) imagemagickdocAction = utils.create_action( self, 'ImageMagick ' + self.tr('documentation'), None, None, None, lambda: webbrowser.open( "http://www.imagemagick.org/script/convert.php") ) aboutAction = utils.create_action( self, self.tr('About'), 'Ctrl+?', None, self.tr('About'), self.open_dialog_about ) fileMenu = self.menuBar().addMenu(self.tr('File')) editMenu = self.menuBar().addMenu(self.tr('Edit')) presetsMenu = self.menuBar().addMenu(self.tr('Presets')) helpMenu = self.menuBar().addMenu(self.tr('Help')) utils.add_actions( fileMenu, [openAction, convertAction, None, quitAction]) utils.add_actions( presetsMenu, [edit_presetsAction, importAction, exportAction, resetAction, None, syncAction, removeoldAction] ) utils.add_actions(editMenu, [clearallAction, None, preferencesAction]) utils.add_actions( helpMenu, [trackerAction, wikiAction, None, ffmpegdocAction, imagemagickdocAction, None, aboutAction] ) self.filesList.dropped.connect(self.add_files_dropped) addQPB.clicked.connect(self.add_files) delQPB.clicked.connect(self.delete_files) clearQPB.clicked.connect(self.clear_fileslist) self.tabWidget.currentChanged.connect( lambda: self.tabs[0].moreQPB.setChecked(False)) self.origQCB.toggled.connect( lambda: self.toQLE.setEnabled(not self.origQCB.isChecked())) self.toQTB.clicked.connect(self.open_dir) convertQPB.clicked.connect(convertAction.triggered) del_shortcut = QShortcut(self) del_shortcut.setKey(Qt.Key_Delete) del_shortcut.activated.connect(self.delete_files) self.setWindowTitle('FF Multi Converter') QTimer.singleShot(0, self.check_for_dependencies) QTimer.singleShot(0, self.load_settings) QTimer.singleShot(0, self.audiovideo_tab.set_default_command) QTimer.singleShot(0, self.update_filesList)
class PythonShellWidget(TracebackLinksMixin, ShellBaseWidget, InspectObjectMixin): """Python shell widget""" QT_CLASS = ShellBaseWidget INITHISTORY = ['# -*- coding: utf-8 -*-', '# *** Spyder Python Console History Log ***',] SEPARATOR = '%s##---(%s)---' % (os.linesep*2, time.ctime()) def __init__(self, parent, history_filename, profile=False): ShellBaseWidget.__init__(self, parent, history_filename, profile) TracebackLinksMixin.__init__(self) InspectObjectMixin.__init__(self) # Local shortcuts self.inspectsc = QShortcut(QKeySequence("Ctrl+I"), self, self.inspect_current_object) self.inspectsc.setContext(Qt.WidgetWithChildrenShortcut) def get_shortcut_data(self): """ Returns shortcut data, a list of tuples (shortcut, text, default) shortcut (QShortcut or QAction instance) text (string): action/shortcut description default (string): default key sequence """ return [ (self.inspectsc, "Inspect current object", "Ctrl+I"), ] #------ Context menu def setup_context_menu(self): """Reimplements ShellBaseWidget method""" ShellBaseWidget.setup_context_menu(self) self.copy_without_prompts_action = create_action(self, _("Copy without prompts"), icon=get_icon('copywop.png'), triggered=self.copy_without_prompts) clear_line_action = create_action(self, _("Clear line"), QKeySequence("Shift+Escape"), icon=get_icon('eraser.png'), tip=_("Clear line"), triggered=self.clear_line) clear_action = create_action(self, _("Clear shell"), QKeySequence("Ctrl+L"), icon=get_icon('clear.png'), tip=_("Clear shell contents " "('cls' command)"), triggered=self.clear_terminal) add_actions(self.menu, (self.copy_without_prompts_action, clear_line_action, clear_action)) def contextMenuEvent(self, event): """Reimplements ShellBaseWidget method""" state = self.has_selected_text() self.copy_without_prompts_action.setEnabled(state) ShellBaseWidget.contextMenuEvent(self, event) def copy_without_prompts(self): """Copy text to clipboard without prompts""" text = self.get_selected_text() lines = text.split(os.linesep) for index, line in enumerate(lines): if line.startswith('>>> ') or line.startswith('... '): lines[index] = line[4:] text = os.linesep.join(lines) QApplication.clipboard().setText(text) #------ Key handlers def postprocess_keyevent(self, event): """Process keypress event""" ShellBaseWidget.postprocess_keyevent(self, event) if QToolTip.isVisible(): _event, _text, key, _ctrl, _shift = restore_keyevent(event) self.hide_tooltip_if_necessary(key) def _key_other(self, text): """1 character key""" if self.is_completion_widget_visible(): self.completion_text += text def _key_backspace(self, cursor_position): """Action for Backspace key""" if self.has_selected_text(): self.check_selection() self.remove_selected_text() elif self.current_prompt_pos == cursor_position: # Avoid deleting prompt return elif self.is_cursor_on_last_line(): self.stdkey_backspace() if self.is_completion_widget_visible(): # Removing only last character because if there was a selection # the completion widget would have been canceled self.completion_text = self.completion_text[:-1] def _key_tab(self): """Action for TAB key""" if self.is_cursor_on_last_line(): empty_line = not self.get_current_line_to_cursor().strip() if empty_line: self.stdkey_tab() else: self.show_code_completion(automatic=False) def _key_ctrl_space(self): """Action for Ctrl+Space""" if not self.is_completion_widget_visible(): self.show_code_completion(automatic=False) def _key_pageup(self): """Action for PageUp key""" pass def _key_pagedown(self): """Action for PageDown key""" pass def _key_escape(self): """Action for ESCAPE key""" if self.is_completion_widget_visible(): self.hide_completion_widget() def _key_question(self, text): """Action for '?'""" if self.get_current_line_to_cursor(): last_obj = self.get_last_obj() if last_obj and not last_obj.isdigit(): self.show_docstring(last_obj) self.insert_text(text) # In case calltip and completion are shown at the same time: if self.is_completion_widget_visible(): self.completion_text += '?' def _key_parenleft(self, text): """Action for '('""" self.hide_completion_widget() if self.get_current_line_to_cursor(): last_obj = self.get_last_obj() if last_obj and not last_obj.isdigit(): self.show_docstring(last_obj, call=True) self.insert_text(text) def _key_period(self, text): """Action for '.'""" self.insert_text(text) if self.codecompletion_auto: # Enable auto-completion only if last token isn't a float last_obj = self.get_last_obj() if last_obj and not last_obj.isdigit(): self.show_code_completion(automatic=True) #------ Paste def paste(self): """Reimplemented slot to handle multiline paste action""" text = unicode(QApplication.clipboard().text()) if len(text.splitlines()) > 1: # Multiline paste if self.new_input_line: self.on_new_line() self.remove_selected_text() # Remove selection, eventually end = self.get_current_line_from_cursor() lines = self.get_current_line_to_cursor() + text + end self.clear_line() self.execute_lines(lines) self.move_cursor(-len(end)) else: # Standard paste ShellBaseWidget.paste(self) #------ Code Completion / Calltips # Methods implemented in child class: # (e.g. InternalShell) def get_dir(self, objtxt): """Return dir(object)""" raise NotImplementedError def get_module_completion(self, objtxt): """Return module completion list associated to object name""" pass def get_globals_keys(self): """Return shell globals() keys""" raise NotImplementedError def get_cdlistdir(self): """Return shell current directory list dir""" raise NotImplementedError def iscallable(self, objtxt): """Is object callable?""" raise NotImplementedError def get_arglist(self, objtxt): """Get func/method argument list""" raise NotImplementedError def get__doc__(self, objtxt): """Get object __doc__""" raise NotImplementedError def get_doc(self, objtxt): """Get object documentation""" raise NotImplementedError def get_source(self, objtxt): """Get object source""" raise NotImplementedError def is_defined(self, objtxt, force_import=False): """Return True if object is defined""" raise NotImplementedError def show_code_completion(self, automatic): """Display a completion list based on the current line""" # Note: unicode conversion is needed only for ExternalShellBase text = unicode(self.get_current_line_to_cursor()) last_obj = self.get_last_obj() if text.startswith('import '): obj_list = self.get_module_completion(text) words = text.split(' ') if ',' in words[-1]: words = words[-1].split(',') self.show_completion_list(obj_list, completion_text=words[-1], automatic=automatic) return elif text.startswith('from '): obj_list = self.get_module_completion(text) if obj_list is None: return words = text.split(' ') if '(' in words[-1]: words = words[:-2] + words[-1].split('(') if ',' in words[-1]: words = words[:-2] + words[-1].split(',') self.show_completion_list(obj_list, completion_text=words[-1], automatic=automatic) return obj_dir = self.get_dir(last_obj) if last_obj and obj_dir and text.endswith('.'): self.show_completion_list(obj_dir, automatic=automatic) return # Builtins and globals import __builtin__, keyword if not text.endswith('.') and last_obj \ and re.match(r'[a-zA-Z_0-9]*$', last_obj): b_k_g = dir(__builtin__)+self.get_globals_keys()+keyword.kwlist for objname in b_k_g: if objname.startswith(last_obj) and objname != last_obj: self.show_completion_list(b_k_g, completion_text=last_obj, automatic=automatic) return else: return # Looking for an incomplete completion if last_obj is None: last_obj = text dot_pos = last_obj.rfind('.') if dot_pos != -1: if dot_pos == len(last_obj)-1: completion_text = "" else: completion_text = last_obj[dot_pos+1:] last_obj = last_obj[:dot_pos] completions = self.get_dir(last_obj) if completions is not None: self.show_completion_list(completions, completion_text=completion_text, automatic=automatic) return # Looking for ' or ": filename completion q_pos = max([text.rfind("'"), text.rfind('"')]) if q_pos != -1: completions = self.get_cdlistdir() if completions: self.show_completion_list(completions, completion_text=text[q_pos+1:], automatic=automatic) return #------ Drag'n Drop def drop_pathlist(self, pathlist): """Drop path list""" if pathlist: files = ["r'%s'" % path for path in pathlist] if len(files) == 1: text = files[0] else: text = "[" + ", ".join(files) + "]" if self.new_input_line: self.on_new_line() self.insert_text(text) self.setFocus()
def __init__(self, key, parent, index): QShortcut.__init__(self, key, parent) self.index = index
def __init__(self, parent=None): super(ShellOutputScintilla, self).__init__(parent) self.parent = parent self.shell = self.parent.shell self.settings = QSettings() # Creates layout for message bar self.layout = QGridLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) spacerItem = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.layout.addItem(spacerItem, 1, 0, 1, 1) # messageBar instance self.infoBar = QgsMessageBar() sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) self.infoBar.setSizePolicy(sizePolicy) self.layout.addWidget(self.infoBar, 0, 0, 1, 1) # Enable non-ascii chars for editor self.setUtf8(True) sys.stdout = writeOut(self, sys.stdout) sys.stderr = writeOut(self, sys.stderr, "_traceback") self.insertInitText() self.refreshSettingsOutput() self.setReadOnly(True) # Set the default font font = QFont() font.setFamily('Courier') font.setFixedPitch(True) font.setPointSize(10) self.setFont(font) self.setMarginsFont(font) # Margin 0 is used for line numbers self.setMarginWidth(0, 0) self.setMarginWidth(1, 0) self.setMarginWidth(2, 0) #fm = QFontMetrics(font) self.setMarginsFont(font) self.setMarginWidth(1, "00000") self.setMarginLineNumbers(1, True) self.setMarginsForegroundColor(QColor("#3E3EE3")) self.setMarginsBackgroundColor(QColor("#f9f9f9")) self.setCaretLineVisible(True) self.setCaretWidth(0) self.setMinimumHeight(120) self.setWrapMode(QsciScintilla.WrapCharacter) self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0) self.runScut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_E), self) self.runScut.setContext(Qt.WidgetShortcut) self.runScut.activated.connect(self.enteredSelected) # Reimplemeted copy action to prevent paste prompt (>>>,...) in command view self.copyShortcut = QShortcut(QKeySequence.Copy, self) self.copyShortcut.activated.connect(self.copy) self.selectAllShortcut = QShortcut(QKeySequence.SelectAll, self) self.selectAllShortcut.activated.connect(self.selectAll)
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) # main window dimensions self.main_width = 780 self.main_height = 510 self.main_fixed_height = 684 self.default_command = config.default_ffmpeg_cmd self.fnames = list() # list of file names to be converted self.docconv = False # True when a documents conversion is running self.parse_cla() addQPB = QPushButton(self.tr('Add')) delQPB = QPushButton(self.tr('Delete')) clearQPB = QPushButton(self.tr('Clear')) vlayout1 = utils.add_to_layout('v', addQPB, delQPB, clearQPB, None) self.filesList = utils.FilesList() self.filesList.setSelectionMode(QAbstractItemView.ExtendedSelection) hlayout1 = utils.add_to_layout('h', self.filesList, vlayout1) outputQL = QLabel(self.tr('Output folder:')) self.toQLE = QLineEdit() self.toQLE.setReadOnly(True) self.toQTB = QToolButton() self.toQTB.setText('...') hlayout2 = utils.add_to_layout('h', outputQL, self.toQLE, self.toQTB) self.audiovideo_tab = AudioVideoTab(self) self.image_tab = ImageTab(self) self.document_tab = DocumentTab(self) self.tabs = [self.audiovideo_tab, self.image_tab, self.document_tab] tab_names = [self.tr('Audio/Video'), self.tr('Images'), self.tr('Documents')] self.tabWidget = QTabWidget() for num, tab in enumerate(tab_names): self.tabWidget.addTab(self.tabs[num], tab) self.tabWidget.setCurrentIndex(0) self.origQCB = QCheckBox( self.tr('Save each file in the same\nfolder as input file')) self.deleteQCB = QCheckBox(self.tr('Delete original')) convertQPB = QPushButton(self.tr('&Convert')) hlayout3 = utils.add_to_layout('h', self.origQCB, self.deleteQCB, None) hlayout4 = utils.add_to_layout('h', None, convertQPB) final_layout = utils.add_to_layout( 'v', hlayout1, self.tabWidget, hlayout2, hlayout3, hlayout4) self.dependenciesQL = QLabel() self.statusBar().addPermanentWidget(self.dependenciesQL, stretch=1) widget = QWidget() widget.setLayout(final_layout) self.setCentralWidget(widget) openAction = utils.create_action( self, self.tr('Open'), QKeySequence.Open, None, self.tr('Open a file'), self.add_files ) convertAction = utils.create_action( self, self.tr('Convert'), 'Ctrl+C', None, self.tr('Convert files'), self.start_conversion ) quitAction = utils.create_action( self, self.tr('Quit'), 'Ctrl+Q', None, self.tr('Quit'), self.close ) edit_presetsAction = utils.create_action( self, self.tr('Edit Presets'), 'Ctrl+P', None, self.tr('Edit Presets'), self.presets ) importAction = utils.create_action( self, self.tr('Import'), None, None, self.tr('Import presets'), self.import_presets ) exportAction = utils.create_action( self, self.tr('Export'), None, None, self.tr('Export presets'), self.export_presets ) resetAction = utils.create_action( self, self.tr('Reset'), None, None, self.tr('Reset presets'), self.reset_presets ) syncAction = utils.create_action( self, self.tr('Synchronize'), None, None, self.tr('Synchronize presets'), self.sync_presets ) removeoldAction = utils.create_action( self, self.tr('Remove old'), None, None, self.tr('Remove old presets'), self.removeold_presets ) clearallAction = utils.create_action( self, self.tr('Clear All'), None, None, self.tr('Clear form'), self.clear_all ) preferencesAction = utils.create_action( self, self.tr('Preferences'), 'Alt+Ctrl+P', None, self.tr('Preferences'), self.preferences ) aboutAction = utils.create_action( self, self.tr('About'), 'Ctrl+?', None, self.tr('About'), self.about ) fileMenu = self.menuBar().addMenu(self.tr('File')) editMenu = self.menuBar().addMenu(self.tr('Edit')) presetsMenu = self.menuBar().addMenu(self.tr('Presets')) helpMenu = self.menuBar().addMenu(self.tr('Help')) utils.add_actions( fileMenu, [openAction, convertAction, None, quitAction]) utils.add_actions( presetsMenu, [edit_presetsAction, importAction, exportAction, resetAction, None, syncAction, removeoldAction] ) utils.add_actions(editMenu, [clearallAction, None, preferencesAction]) utils.add_actions(helpMenu, [aboutAction]) self.filesList.dropped.connect(self.url_dropped) addQPB.clicked.connect(self.add_files) delQPB.clicked.connect(self.delete_files) clearQPB.clicked.connect(self.clear_fileslist) self.tabWidget.currentChanged.connect( lambda: self.tabs[0].moreQPB.setChecked(False)) self.origQCB.clicked.connect( lambda: self.toQLE.setEnabled(not self.origQCB.isChecked())) self.toQTB.clicked.connect(self.open_dir) convertQPB.clicked.connect(convertAction.triggered) del_shortcut = QShortcut(self) del_shortcut.setKey(Qt.Key_Delete) del_shortcut.activated.connect(self.delete_files) self.resize(self.main_width, self.main_height) self.setWindowTitle('FF Multi Converter') QTimer.singleShot(0, self.check_for_dependencies) QTimer.singleShot(0, self.load_settings) QTimer.singleShot(0, self.audiovideo_tab.set_default_command) QTimer.singleShot(0, self.update_filesList)
class ShellScintilla(QsciScintilla, code.InteractiveInterpreter): def __init__(self, parent=None): super(ShellScintilla, self).__init__(parent) code.InteractiveInterpreter.__init__(self, locals=None) self.parent = parent self.opening = ['(', '{', '[', "'", '"'] self.closing = [')', '}', ']', "'", '"'] self.settings = QSettings() # Enable non-ascii chars for editor self.setUtf8(True) self.new_input_line = True self.setMarginWidth(0, 0) self.setMarginWidth(1, 0) self.setMarginWidth(2, 0) self.buffer = [] self.displayPrompt(False) for line in _init_commands: self.runsource(line) self.history = [] self.historyIndex = 0 # Read history command file self.readHistoryFile() self.historyDlg = HistoryDialog(self) # Brace matching: enable for a brace immediately before or after # the current position self.setBraceMatching(QsciScintilla.SloppyBraceMatch) self.setMatchedBraceBackgroundColor(QColor("#b7f907")) # Current line visible with special background color self.setCaretWidth(2) self.refreshSettingsShell() # Don't want to see the horizontal scrollbar at all # Use raw message to Scintilla here (all messages are documented # here: http://www.scintilla.org/ScintillaDoc.html) self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0) # not too small #self.setMinimumSize(500, 300) self.setWrapMode(QsciScintilla.WrapCharacter) self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER) ## Disable command key ctrl, shift = self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16 self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('T') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('D') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('Z') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('Y') + ctrl) self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl + shift) ## New QShortcut = ctrl+space/ctrl+alt+space for Autocomplete self.newShortcutCSS = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Space), self) self.newShortcutCAS = QShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_Space), self) self.newShortcutCSS.setContext(Qt.WidgetShortcut) self.newShortcutCAS.setContext(Qt.WidgetShortcut) self.newShortcutCAS.activated.connect(self.autoCompleteKeyBinding) self.newShortcutCSS.activated.connect(self.showHistory) def _setMinimumHeight(self): fnt = self.settings.value("pythonConsole/fontfamilytext", "Monospace") fntSize = self.settings.value("pythonConsole/fontsize", 10, type=int) fm = QFontMetrics(QFont(fnt, fntSize)) self.setMinimumHeight(fm.height() + 10) def refreshSettingsShell(self): # Set Python lexer self.setLexers() threshold = self.settings.value("pythonConsole/autoCompThreshold", 2, type=int) self.setAutoCompletionThreshold(threshold) radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI') autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool) if autoCompEnabled: if radioButtonSource == 'fromDoc': self.setAutoCompletionSource(self.AcsDocument) elif radioButtonSource == 'fromAPI': self.setAutoCompletionSource(self.AcsAPIs) elif radioButtonSource == 'fromDocAPI': self.setAutoCompletionSource(self.AcsAll) else: self.setAutoCompletionSource(self.AcsNone) cursorColor = self.settings.value("pythonConsole/cursorColor", QColor(Qt.black)) self.setCaretForegroundColor(cursorColor) # Sets minimum height for input area based of font metric self._setMinimumHeight() def showHistory(self): if not self.historyDlg.isVisible(): self.historyDlg.show() self.historyDlg._reloadHistory() self.historyDlg.activateWindow() def autoCompleteKeyBinding(self): radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI') autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool) if autoCompEnabled: if radioButtonSource == 'fromDoc': self.autoCompleteFromDocument() elif radioButtonSource == 'fromAPI': self.autoCompleteFromAPIs() elif radioButtonSource == 'fromDocAPI': self.autoCompleteFromAll() def commandConsole(self, command): if not self.is_cursor_on_last_line(): self.move_cursor_to_end() line, pos = self.getCursorPosition() selCmdLenght = len(self.text(line)) self.setSelection(line, 4, line, selCmdLenght) self.removeSelectedText() if command == "processing": # import Processing class self.append('import processing') elif command == "qtCore": # import QtCore class self.append('from PyQt4.QtCore import *') elif command == "qtGui": # import QtGui class self.append('from PyQt4.QtGui import *') self.entered() self.move_cursor_to_end() self.setFocus() def setLexers(self): self.lexer = QsciLexerPython() loadFont = self.settings.value("pythonConsole/fontfamilytext", "Monospace") fontSize = self.settings.value("pythonConsole/fontsize", 10, type=int) font = QFont(loadFont) font.setFixedPitch(True) font.setPointSize(fontSize) font.setStyleHint(QFont.TypeWriter) font.setStretch(QFont.SemiCondensed) font.setLetterSpacing(QFont.PercentageSpacing, 87.0) font.setBold(False) self.lexer.setDefaultFont(font) self.lexer.setDefaultColor(QColor(self.settings.value("pythonConsole/defaultFontColor", QColor(Qt.black)))) self.lexer.setColor(QColor(self.settings.value("pythonConsole/commentFontColor", QColor(Qt.gray))), 1) self.lexer.setColor(QColor(self.settings.value("pythonConsole/keywordFontColor", QColor(Qt.darkGreen))), 5) self.lexer.setColor(QColor(self.settings.value("pythonConsole/classFontColor", QColor(Qt.blue))), 8) self.lexer.setColor(QColor(self.settings.value("pythonConsole/methodFontColor", QColor(Qt.darkGray))), 9) self.lexer.setColor(QColor(self.settings.value("pythonConsole/decorFontColor", QColor(Qt.darkBlue))), 15) self.lexer.setColor(QColor(self.settings.value("pythonConsole/commentBlockFontColor", QColor(Qt.gray))), 12) self.lexer.setColor(QColor(self.settings.value("pythonConsole/singleQuoteFontColor", QColor(Qt.blue))), 4) self.lexer.setColor(QColor(self.settings.value("pythonConsole/doubleQuoteFontColor", QColor(Qt.blue))), 3) self.lexer.setColor(QColor(self.settings.value("pythonConsole/tripleSingleQuoteFontColor", QColor(Qt.blue))), 6) self.lexer.setColor(QColor(self.settings.value("pythonConsole/tripleDoubleQuoteFontColor", QColor(Qt.blue))), 7) self.lexer.setFont(font, 1) self.lexer.setFont(font, 3) self.lexer.setFont(font, 4) for style in range(0, 33): paperColor = QColor(self.settings.value("pythonConsole/paperBackgroundColor", QColor(Qt.white))) self.lexer.setPaper(paperColor, style) self.api = QsciAPIs(self.lexer) chekBoxAPI = self.settings.value("pythonConsole/preloadAPI", True, type=bool) chekBoxPreparedAPI = self.settings.value("pythonConsole/usePreparedAPIFile", False, type=bool) if chekBoxAPI: pap = os.path.join(QgsApplication.pkgDataPath(), "python", "qsci_apis", "pyqgis.pap") self.api.loadPrepared(pap) elif chekBoxPreparedAPI: self.api.loadPrepared(self.settings.value("pythonConsole/preparedAPIFile")) else: apiPath = self.settings.value("pythonConsole/userAPI", []) for i in range(0, len(apiPath)): self.api.load(unicode(apiPath[i])) self.api.prepare() self.lexer.setAPIs(self.api) self.setLexer(self.lexer) ## TODO: show completion list for file and directory def getText(self): """ Get the text as a unicode string. """ value = self.getBytes().decode('utf-8') # print (value) printing can give an error because the console font # may not have all unicode characters return value def getBytes(self): """ Get the text as bytes (utf-8 encoded). This is how the data is stored internally. """ len = self.SendScintilla(self.SCI_GETLENGTH) + 1 bb = QByteArray(len, '0') self.SendScintilla(self.SCI_GETTEXT, len, bb) return bytes(bb)[:-1] def getTextLength(self): return self.SendScintilla(QsciScintilla.SCI_GETLENGTH) def get_end_pos(self): """Return (line, index) position of the last character""" line = self.lines() - 1 return (line, len(self.text(line))) def is_cursor_at_end(self): """Return True if cursor is at the end of text""" cline, cindex = self.getCursorPosition() return (cline, cindex) == self.get_end_pos() def move_cursor_to_end(self): """Move cursor to end of text""" line, index = self.get_end_pos() self.setCursorPosition(line, index) self.ensureCursorVisible() self.ensureLineVisible(line) def is_cursor_on_last_line(self): """Return True if cursor is on the last line""" cline, _ = self.getCursorPosition() return cline == self.lines() - 1 def is_cursor_on_edition_zone(self): """ Return True if the cursor is in the edition zone """ cline, cindex = self.getCursorPosition() return cline == self.lines() - 1 and cindex >= 4 def new_prompt(self, prompt): """ Print a new prompt and save its (line, index) position """ self.write(prompt, prompt=True) # now we update our cursor giving end of prompt line, index = self.getCursorPosition() self.ensureCursorVisible() self.ensureLineVisible(line) def displayPrompt(self, more=False): self.append("... ") if more else self.append(">>> ") self.move_cursor_to_end() def updateHistory(self, command): if isinstance(command, list): for line in command: self.history.append(line) elif not command == "": if len(self.history) <= 0 or \ command != self.history[-1]: self.history.append(command) self.historyIndex = len(self.history) def writeHistoryFile(self, fromCloseConsole=False): ok = False try: wH = codecs.open(_historyFile, 'w', encoding='utf-8') for s in self.history: wH.write(s + '\n') ok = True except: raise wH.close() if ok and not fromCloseConsole: msgText = QCoreApplication.translate('PythonConsole', 'History saved successfully.') self.parent.callWidgetMessageBar(msgText) def readHistoryFile(self): fileExist = QFile.exists(_historyFile) if fileExist: rH = codecs.open(_historyFile, 'r', encoding='utf-8') for line in rH: if line != "\n": l = line.rstrip('\n') self.updateHistory(l) else: return def clearHistory(self, clearSession=False): if clearSession: self.history = [] msgText = QCoreApplication.translate('PythonConsole', 'Session and file history cleared successfully.') self.parent.callWidgetMessageBar(msgText) return ok = False try: cH = codecs.open(_historyFile, 'w', encoding='utf-8') ok = True except: raise cH.close() if ok: msgText = QCoreApplication.translate('PythonConsole', 'History cleared successfully.') self.parent.callWidgetMessageBar(msgText) def clearHistorySession(self): self.clearHistory(True) def showPrevious(self): if self.historyIndex < len(self.history) and self.history: line, pos = self.getCursorPosition() selCmdLenght = len(self.text(line)) self.setSelection(line, 4, line, selCmdLenght) self.removeSelectedText() self.historyIndex += 1 if self.historyIndex == len(self.history): self.insert("") pass else: self.insert(self.history[self.historyIndex]) self.move_cursor_to_end() #self.SendScintilla(QsciScintilla.SCI_DELETEBACK) def showNext(self): if self.historyIndex > 0 and self.history: line, pos = self.getCursorPosition() selCmdLenght = len(self.text(line)) self.setSelection(line, 4, line, selCmdLenght) self.removeSelectedText() self.historyIndex -= 1 if self.historyIndex == len(self.history): self.insert("") else: self.insert(self.history[self.historyIndex]) self.move_cursor_to_end() #self.SendScintilla(QsciScintilla.SCI_DELETEBACK) def keyPressEvent(self, e): startLine, startPos, endLine, endPos = self.getSelection() # handle invalid cursor position and multiline selections if not self.is_cursor_on_edition_zone() or startLine < endLine: # allow to copy and select if e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier): if e.key() in (Qt.Key_C, Qt.Key_A): QsciScintilla.keyPressEvent(self, e) return # allow selection if e.modifiers() & Qt.ShiftModifier: if e.key() in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Home, Qt.Key_End): QsciScintilla.keyPressEvent(self, e) return # all other keystrokes get sent to the input line self.move_cursor_to_end() line, index = self.getCursorPosition() cmd = self.text(line) if e.key() in (Qt.Key_Return, Qt.Key_Enter) and not self.isListActive(): self.entered() elif e.key() in (Qt.Key_Left, Qt.Key_Home): QsciScintilla.keyPressEvent(self, e) # check whether the cursor is moved out of the edition zone newline, newindex = self.getCursorPosition() if newline < line or newindex < 4: # fix selection and the cursor position if self.hasSelectedText(): self.setSelection(line, self.getSelection()[3], line, 4) else: self.setCursorPosition(line, 4) elif e.key() in (Qt.Key_Backspace, Qt.Key_Delete): QsciScintilla.keyPressEvent(self, e) # check whether the cursor is moved out of the edition zone _, newindex = self.getCursorPosition() if newindex < 4: # restore the prompt chars (if removed) and # fix the cursor position self.insert(cmd[:3 - newindex] + " ") self.setCursorPosition(line, 4) self.recolor() elif (e.modifiers() & (Qt.ControlModifier | Qt.MetaModifier) and e.key() == Qt.Key_V) or \ (e.modifiers() & Qt.ShiftModifier and e.key() == Qt.Key_Insert): self.paste() e.accept() elif e.key() == Qt.Key_Down and not self.isListActive(): self.showPrevious() elif e.key() == Qt.Key_Up and not self.isListActive(): self.showNext() ## TODO: press event for auto-completion file directory else: t = unicode(e.text()) self.autoCloseBracket = self.settings.value("pythonConsole/autoCloseBracket", False, type=bool) self.autoImport = self.settings.value("pythonConsole/autoInsertionImport", True, type=bool) txt = cmd[:index].replace('>>> ', '').replace('... ', '') ## Close bracket automatically if t in self.opening and self.autoCloseBracket: i = self.opening.index(t) if self.hasSelectedText() and startPos != 0: selText = self.selectedText() self.removeSelectedText() self.insert(self.opening[i] + selText + self.closing[i]) self.setCursorPosition(endLine, endPos + 2) return elif t == '(' and (re.match(r'^[ \t]*def \w+$', txt) or re.match(r'^[ \t]*class \w+$', txt)): self.insert('):') else: self.insert(self.closing[i]) ## FIXES #8392 (automatically removes the redundant char ## when autoclosing brackets option is enabled) elif t in [')', ']', '}'] and self.autoCloseBracket: txt = self.text(line) try: if txt[index - 1] in self.opening and t == txt[index]: self.setCursorPosition(line, index + 1) self.SendScintilla(QsciScintilla.SCI_DELETEBACK) except IndexError: pass elif t == ' ' and self.autoImport: ptrn = r'^[ \t]*from [\w.]+$' if re.match(ptrn, txt): self.insert(' import') self.setCursorPosition(line, index + 7) QsciScintilla.keyPressEvent(self, e) def contextMenuEvent(self, e): menu = QMenu(self) subMenu = QMenu(menu) titleHistoryMenu = QCoreApplication.translate("PythonConsole", "Command History") subMenu.setTitle(titleHistoryMenu) subMenu.addAction( QCoreApplication.translate("PythonConsole", "Show"), self.showHistory, 'Ctrl+Shift+SPACE') subMenu.addSeparator() subMenu.addAction( QCoreApplication.translate("PythonConsole", "Save"), self.writeHistoryFile) subMenu.addSeparator() subMenu.addAction( QCoreApplication.translate("PythonConsole", "Clear File"), self.clearHistory) subMenu.addAction( QCoreApplication.translate("PythonConsole", "Clear Session"), self.clearHistorySession) menu.addMenu(subMenu) menu.addSeparator() copyAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Copy"), self.copy, QKeySequence.Copy) pasteAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Paste"), self.paste, QKeySequence.Paste) copyAction.setEnabled(False) pasteAction.setEnabled(False) if self.hasSelectedText(): copyAction.setEnabled(True) if QApplication.clipboard().text(): pasteAction.setEnabled(True) menu.exec_(self.mapToGlobal(e.pos())) def mousePressEvent(self, e): """ Re-implemented to handle the mouse press event. e: the mouse press event (QMouseEvent) """ self.setFocus() if e.button() == Qt.MidButton: stringSel = unicode(QApplication.clipboard().text(QClipboard.Selection)) if not self.is_cursor_on_last_line(): self.move_cursor_to_end() self.insertFromDropPaste(stringSel) e.accept() else: QsciScintilla.mousePressEvent(self, e) def paste(self): """ Method to display data from the clipboard. XXX: It should reimplement the virtual QScintilla.paste method, but it seems not used by QScintilla code. """ stringPaste = unicode(QApplication.clipboard().text()) if self.is_cursor_on_last_line(): if self.hasSelectedText(): self.removeSelectedText() else: self.move_cursor_to_end() self.insertFromDropPaste(stringPaste) ## Drag and drop def dropEvent(self, e): if e.mimeData().hasText(): stringDrag = e.mimeData().text() self.insertFromDropPaste(stringDrag) self.setFocus() e.setDropAction(Qt.CopyAction) e.accept() else: QsciScintillaCompat.dropEvent(self, e) def insertFromDropPaste(self, textDP): pasteList = unicode(textDP).splitlines() if pasteList: for line in pasteList[:-1]: cleanLine = line.replace(">>> ", "").replace("... ", "") self.insert(unicode(cleanLine)) self.move_cursor_to_end() self.runCommand(unicode(self.currentCommand())) if pasteList[-1] != "": line = pasteList[-1] cleanLine = line.replace(">>> ", "").replace("... ", "") self.insert(unicode(cleanLine)) self.move_cursor_to_end() def insertTextFromFile(self, listOpenFile): for line in listOpenFile[:-1]: self.append(line) self.move_cursor_to_end() self.SendScintilla(QsciScintilla.SCI_DELETEBACK) self.runCommand(unicode(self.currentCommand())) self.append(unicode(listOpenFile[-1])) self.move_cursor_to_end() self.SendScintilla(QsciScintilla.SCI_DELETEBACK) def entered(self): self.move_cursor_to_end() self.runCommand(unicode(self.currentCommand())) self.setFocus() self.move_cursor_to_end() def currentCommand(self): linenr, index = self.getCursorPosition() string = self.text() cmdLine = string[4:] cmd = unicode(cmdLine) return cmd def runCommand(self, cmd): self.writeCMD(cmd) import webbrowser self.updateHistory(cmd) if cmd in ('_pyqgis', '_api'): if cmd == '_pyqgis': webbrowser.open("http://qgis.org/pyqgis-cookbook/") elif cmd == '_api': webbrowser.open("http://qgis.org/api/") more = False else: self.buffer.append(cmd) src = u"\n".join(self.buffer) more = self.runsource(src, "<input>") if not more: self.buffer = [] ## prevents to commands with more lines to break the console ## in the case they have a eol different from '\n' self.setText('') self.move_cursor_to_end() self.displayPrompt(more) def write(self, txt): sys.stderr.write(txt) def writeCMD(self, txt): if len(txt) > 0: getCmdString = self.text() prompt = getCmdString[0:4] sys.stdout.write(prompt + txt + '\n')