def __kdeGetSaveFileName(parent = None, caption = QString(), dir_ = QString(), filter = QString(), selectedFilter = None, options = QFileDialog.Options()): """ Module function to get the name of a file for saving it. @param parent parent widget of the dialog (QWidget) @param caption window title of the dialog (QString) @param dir_ working directory of the dialog (QString) @param filter filter string for the dialog (QString) @param selectedFilter selected filter for the dialog (QString) @param options various options for the dialog (QFileDialog.Options) @return name of file to be saved (QString) """ if not QString(filter).isEmpty(): filter = __convertFilter(filter, selectedFilter) wdir = __workingDirectory(dir_) dlg = KFileDialog(KUrl.fromPath(wdir), filter, parent) if wdir != dir_: dlg.setSelection(dir_) dlg.setOperationMode(KFileDialog.Saving) dlg.setMode(KFile.Modes(KFile.File) | KFile.Modes(KFile.LocalOnly)) dlg.setWindowTitle(caption.isEmpty() and \ QApplication.translate('KFileDialog', 'Save As') or caption) dlg.exec_() if selectedFilter is not None: flt = dlg.currentFilter() flt.prepend("(").append(")") selectedFilter.replace(0, selectedFilter.length(), flt) return dlg.selectedFile()
def get_linter(linter_name, callback): """tries to retrieve a linter and calls `callback` on it on success""" if linter_name in LINTERS: callback(LINTERS[linter_name]) return if linter_name not in NEEDS_LICENSE: showError(i18nc('@info:status', 'No acceptable linter named %1!', linter_name)) return license, objname, url = NEEDS_LICENSE[linter_name] cache_path = p.join(CACHE_DIR, linter_name + '.js') def success(): """store newly created linter and “return” it""" LINTERS[linter_name] = JSModule(JS_ENGINE, cache_path, objname) callback(LINTERS[linter_name]) if p.exists(cache_path): success() return # the user doesn’t have the file. ask to accept its license if not license_accepted(license): return download = KIO.file_copy(KUrl(url), KUrl.fromPath(cache_path)) @download.result.connect def _call(job): if job.error(): showError(i18nc('@info:status', 'Download failed')) else: success() download.start()
def item(icon, fileName): """ Add item to the fileList list widget. """ directory, name = os.path.split(fileName) if directory != basedir: name += " ({0})".format(os.path.normpath(directory)) i = QListWidgetItem(KIcon(icon), name, fileList) i.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsUserCheckable) i.ext = os.path.splitext(fileName)[1] i.url = KUrl.fromPath(fileName).url() i.setCheckState(Qt.Checked if i.ext in exts else Qt.Unchecked)
def slotFileDialog(self): """ Get file name from file dialog """ if ctx.Pds.session == ctx.pds.Kde4: from PyKDE4.kio import KFileDialog from PyKDE4.kdecore import KUrl url=KUrl.fromPath("/boot/") filename=unicode(KFileDialog.getOpenFileName(url, "", self, i18n("File System"))) else: filename=unicode(QtGui.QFileDialog.getOpenFileName(self, i18n("File System"), "/boot/")) if filename: self.setFile(filename)
def openFile(fileName, window, cmd = None): """ Opens a file with command cmd (string, read from config) or with the KDE default application (via KRun). """ if cmd: cmd, err = KShell.splitArgs(cmd) if err == KShell.NoError: cmd.append(fileName) try: Popen(cmd) return except OSError: pass # let C++ own the KRun object, it will delete itself. sip.transferto(KRun(KUrl.fromPath(fileName), window), None)
def add_key_clicked(self): """Provide a file chooser that allows to add the gnupg of a trusted software vendor""" home = QDir.homePath() if "SUDO_USER" in os.environ: home = os.path.expanduser("~%s" % os.environ["SUDO_USER"]) url = KUrl.fromPath(home) filename = KFileDialog.getOpenFileName(url, 'application/pgp-keys', self.userinterface, utf8(_("Import key"))) if filename: if not self.add_key(filename): title = _("Error importing selected file") text = _("The selected file may not be a GPG key file " \ "or it might be corrupt.") KMessageBox.sorry(self.userinterface, utf8(text), utf8(title)) self.show_keys()
def __kdeGetExistingDirectory(parent = None, caption = QString(), dir_ = QString(), options = QFileDialog.Options(QFileDialog.ShowDirsOnly)): """ Module function to get the name of a directory. @param parent parent widget of the dialog (QWidget) @param caption window title of the dialog (QString) @param dir_ working directory of the dialog (QString) @param options various options for the dialog (QFileDialog.Options) @return name of selected directory (QString) """ wdir = __workingDirectory(dir_) dlg = KFileDialog(KUrl.fromPath(wdir), QString(), parent) dlg.setOperationMode(KFileDialog.Opening) dlg.setMode(KFile.Modes(KFile.Directory) | KFile.Modes(KFile.LocalOnly) | \ KFile.Modes(KFile.ExistingOnly)) dlg.setWindowTitle(caption.isEmpty() and \ QApplication.translate('KFileDialog', 'Select Directory') or caption) dlg.exec_() return dlg.selectedFile()
def openPDF(self, fileName): if self.part: if self.part.openUrl(KUrl.fromPath(fileName)): self.setCurrentWidget(self.part.widget()) else: openPDF(fileName, self.window())