예제 #1
0
    def context_menu(self, pos) -> None:
        item = self.tblDataSets.itemAt(pos)
        if item is None:
            log.debug('context menu requested but no item selected')
            return

        ds = self.workspace.datasets[self.tblDataSets.row(item)]

        menu = QMenu(self)
        icon_hidden = QIcon(
            platform_specific.get_embedded_file_path(
                'images/experimental.png',  # deployment
                'gui/images/experimental.png',  # development
            ))

        a_view = QAction("View...", menu)
        a_view.triggered.connect(self.catch_exc(ds.dlg_view))
        a_view.setStatusTip(
            'Display the dataset in a separate window. Also available via double click.'
        )
        menu.addAction(a_view)

        analyses = ds.get_analyses()
        if analyses:
            m_analyses = QMenu(menu)
            a_analyses = QAction("Analysis", menu)
            a_analyses.setMenu(m_analyses)
            a_analyses.setStatusTip('Run various analyses on the dataset.')
            menu.addAction(a_analyses)

            for analysis in analyses:
                if analysis.is_hidden:
                    if self.hidden_features_enabled:
                        icon = icon_hidden
                    else:
                        # skip this one
                        continue
                else:
                    icon = None

                def mkanalyse(analysis):
                    def analyse(_flag):
                        new_ds = ds.analyse(analysis)
                        if new_ds is not None:
                            self.add_dataset(new_ds)

                    return analyse

                if analysis.config:
                    analysis_name = analysis.name + '...'
                else:
                    analysis_name = analysis.name

                a_analysis = QAction(analysis_name, m_analyses)
                if icon:
                    a_analysis.setIcon(icon)
                a_analysis.analyse = mkanalyse(
                    analysis)  # create and save the closure
                a_analysis.triggered.connect(self.catch_exc(
                    a_analysis.analyse))
                m_analyses.addAction(a_analysis)

        export_variants = ds.get_export_variants()
        if export_variants:
            m_exports = QMenu(menu)
            a_exports = QAction("Export", menu)
            a_exports.setMenu(m_exports)
            a_exports.setStatusTip('Export the dataset into a file on disk.')
            menu.addAction(a_exports)

            for export_variant in export_variants:

                def mkexport(export_variant):
                    def export(_flag):
                        fname, fformat = QFileDialog.getSaveFileName(
                            self,
                            "Export dataset",
                            filter=
                            "Excel 2010+ (*.xlsx);;Comma Separated Values (*.csv)"
                        )
                        if not fname:
                            return

                        class MyWorker(Worker):
                            def work(self):
                                ds.export(fname, fformat, export_variant, self)

                        try:
                            MyWorker().run_with_progress(
                                None, "Exporting to %s..." % fname)
                        except Cancelled:
                            log.info('export cancelled')

                    return export

                a_export = QAction(export_variant.name + '...', m_exports)
                a_export.export = mkexport(
                    export_variant)  # create and save the closure
                a_export.triggered.connect(self.catch_exc(a_export.export))
                m_exports.addAction(a_export)

        menu.addSeparator()

        def delete(_flag):
            answer = QMessageBox.question(
                self,
                "Delete dataset",
                "Do you really want to delete the selected dataset?",
                defaultButton=QMessageBox.No)

            if answer == QMessageBox.Yes:
                self.delete_dataset(ds)

        a_delete = QAction("Delete", menu)
        a_delete.delete = delete
        a_delete.triggered.connect(self.catch_exc(a_delete.delete))
        a_delete.setStatusTip(
            'Remove the dataset from the workspace. Asks for confirmation.')
        menu.addAction(a_delete)

        menu.popup(self.tblDataSets.viewport().mapToGlobal(pos))