예제 #1
0
class DataManagerController(ToolController):
    content_ctrl: ContentController = RequiredFeature(ContentController.name)

    def __init__(self):
        self._view = QtWidgets.QWidget()
        self._ui = Ui_DataManager()
        self._ui.setupUi(self._view)

        self._ui.refresh_button.setIcon(self._view.style().standardIcon(
            QStyle.SP_BrowserReload))
        self._ui.remove_button.setIcon(self._view.style().standardIcon(
            QStyle.SP_DialogNoButton))
        self._ui.add_button.setIcon(self._view.style().standardIcon(
            QStyle.SP_DialogYesButton))

        self.dlg = DataManagerFilterDialog()

        db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName(
            sunpy.config.get("database", "url").replace("sqlite:///", ""))

        model = QtSql.QSqlTableModel()
        model.setTable("data")
        model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
        model.select()
        self._ui.data_table.setModel(model)

        self.initTableHeader(model)

        self._ui.add_button.clicked.connect(lambda x: self.onAdd())
        self._ui.remove_button.clicked.connect(lambda x: self.onRemove())
        self._ui.open_button.clicked.connect(lambda x: self.onOpen())
        self._ui.refresh_button.clicked.connect(lambda x: self.model.select())
        self._ui.filter_button.clicked.connect(lambda x: self.onFilter())

        self.sunpy_db = Database()
        self.model = model

    def initTableHeader(self, model):
        header = self._ui.data_table.horizontalHeader()
        for i in range(model.columnCount()):
            header.setSectionResizeMode(i,
                                        QtWidgets.QHeaderView.ResizeToContents)
        header.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        header.customContextMenuRequested.connect(self.onHeaderMenu)
        self._ui.data_table.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self._ui.data_table.customContextMenuRequested.connect(
            self.onHeaderMenu)

        self._ui.data_table.hideColumn(0)
        self._ui.data_table.hideColumn(2)
        self._ui.data_table.hideColumn(3)
        self._ui.data_table.hideColumn(4)
        self._ui.data_table.hideColumn(8)
        self._ui.data_table.hideColumn(11)
        self._ui.data_table.hideColumn(12)
        self._ui.data_table.hideColumn(13)
        self._ui.data_table.hideColumn(14)

    def onAdd(self):
        paths, _ = QtWidgets.QFileDialog.getOpenFileNames(
            None, filter="FITS files (*.fits; *.fit; *.fts)")
        for p in paths:
            self.sunpy_db.add_from_file(p)
            self.sunpy_db.commit()
        self.model.select()

    def onRemove(self):
        rows = set([i.row() for i in self._ui.data_table.selectedIndexes()])
        for r in rows:
            self.model.removeRow(r)
        self.model.submitAll()
        self.model.select()

    def onOpen(self):
        rows = set([i.row() for i in self._ui.data_table.selectedIndexes()])
        paths = [
            self.model.index(row, self.model.fieldIndex("path")).data()
            for row in rows
        ]
        for path in paths:
            viewer_ctrl = MapViewerController.fromFile(path)
            self.content_ctrl.addViewerController(viewer_ctrl)

    def onFilter(self):
        if self.dlg.exec_():
            self.model.setFilter(self.dlg.getFilter())
            self.model.select()

    def onHeaderMenu(self, point):
        menu = QMenu(self._view)

        actions = []
        for column in range(self.model.columnCount()):
            label = self.model.headerData(column, QtCore.Qt.Horizontal)
            action = QtWidgets.QAction(label)
            action.setCheckable(True)
            action.setChecked(not self._ui.data_table.isColumnHidden(column))
            event = lambda checked, c=column: self._ui.data_table.showColumn(
                c) if checked else self._ui.data_table.hideColumn(c)
            action.triggered.connect(event)
            actions.append(action)
        menu.addActions(actions)

        menu.exec_(QCursor.pos())

    @property
    def item_config(self) -> ItemConfig:
        return ItemConfig().setMenuPath("File/Data Manager").setTitle(
            "Data Manager")

    @property
    def view(self) -> QWidget:
        self.model.select()
        return self._view