class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('Direct tree') self.model = QFileSystemModel() self.model.setRootPath(QDir.rootPath()) self.tree = QTreeView() self.tree.setModel(self.model) self.tree.setRootIndex(self.model.index(os.getcwd())) self.tree.doubleClicked.connect(self._on_double_clicked) self.tree.setAnimated(False) self.tree.setIndentation(20) self.tree.setSortingEnabled(True) self.textEdit = QTextEdit() self.textEdit.setReadOnly(True) splitter = QSplitter() splitter.addWidget(self.tree) splitter.addWidget(self.textEdit) splitter.setSizes([50, 200]) self.setCentralWidget(splitter) def _on_double_clicked(self, index): file_name = self.model.filePath(index) with open(file_name, encoding='utf-8') as f: text = f.read() self.textEdit.setPlainText(text)
def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('Direct tree') self.model = QFileSystemModel() self.model.setRootPath(QDir.rootPath()) self.tree = QTreeView() self.tree.setModel(self.model) self.tree.setRootIndex(self.model.index(os.getcwd())) self.tree.doubleClicked.connect(self._on_double_clicked) self.tree.setAnimated(False) self.tree.setIndentation(20) self.tree.setSortingEnabled(True) self.textEdit = QTextEdit() self.textEdit.setReadOnly(True) splitter = QSplitter() splitter.addWidget(self.tree) splitter.addWidget(self.textEdit) splitter.setSizes([50, 200]) self.setCentralWidget(splitter)
def __init__(self): super().__init__() path = QDir.rootPath() self.model = QFileSystemModel() self.model.setRootPath(path) self.tree_view = QTreeView() self.tree_view.setModel(self.model) self.tree_view.setSelectionMode(QTreeView.ExtendedSelection) self.tree_view.selectionModel().selectionChanged.connect( self._on_selection_changed) self.list_files = QListWidget() self.button_add = QPushButton('Добавить!') self.button_add.setEnabled(False) self.button_add.clicked.connect(self._on_add) splitter = QSplitter(Qt.Horizontal) splitter.addWidget(self.tree_view) splitter.addWidget(self.list_files) main_layout = QVBoxLayout(self) main_layout.addWidget(splitter) main_layout.addWidget(self.button_add)
def setData(self, index, value, role=Qt.EditRole): if role == Qt.CheckStateRole: plugin = self.plugins[index.row()][0] if value == Qt.Checked: self.checkedPlugins.add(plugin.name) else: self.checkedPlugins.discard(plugin.name) return True return QFileSystemModel.setData(self, index, value, role)
def __init__(self, parent=None): super(MyMainWindow, self).__init__(parent) # Delegate Main Window self.ui = Ui_MainWindow() self.setupUi(self) self.setWindowIcon(QApplication.style().standardIcon( QStyle.SP_TitleBarMenuButton)) self.setWindowTitle('KW De-noise App') self.statusBar().showMessage('Status Here') self.frame = QtWidgets.QFrame() self.vl = QtWidgets.QVBoxLayout() self.hl = QtWidgets.QHBoxLayout() # #KW Custom UI Marco self.make_app_in_screen_center() self.init_toolbar() self.DBName = 'kwDCMDB' self.myDB = DcmDataBase(db_name=self.DBName) self.dirModel = QFileSystemModel() self.treeView.setModel(self.dirModel) # self.treeView.doubleClicked.connect(self.get_selected_item_path) # 只能連接一次不然會執行多次 self.treeView.clicked.connect(self.get_selected_item_path)
class Widget(QWidget): def __init__(self): super().__init__() path = QDir.rootPath() self.model = QFileSystemModel() self.model.setRootPath(path) self.tree_view = QTreeView() self.tree_view.setModel(self.model) self.tree_view.setSelectionMode(QTreeView.ExtendedSelection) self.tree_view.selectionModel().selectionChanged.connect( self._on_selection_changed) self.list_files = QListWidget() self.button_add = QPushButton('Добавить!') self.button_add.setEnabled(False) self.button_add.clicked.connect(self._on_add) splitter = QSplitter(Qt.Horizontal) splitter.addWidget(self.tree_view) splitter.addWidget(self.list_files) main_layout = QVBoxLayout(self) main_layout.addWidget(splitter) main_layout.addWidget(self.button_add) def _on_selection_changed(self, selected, deselected): has = self.tree_view.selectionModel().hasSelection() self.button_add.setEnabled(has) def _on_add(self): for row in self.tree_view.selectionModel().selectedRows(): path = self.model.filePath(row) self.list_files.addItem(path)
class MyMainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MyMainWindow, self).__init__(parent) # Delegate Main Window self.ui = Ui_MainWindow() self.setupUi(self) self.setWindowIcon(QApplication.style().standardIcon( QStyle.SP_TitleBarMenuButton)) self.setWindowTitle('KW De-noise App') self.statusBar().showMessage('Status Here') self.frame = QtWidgets.QFrame() self.vl = QtWidgets.QVBoxLayout() self.hl = QtWidgets.QHBoxLayout() # #KW Custom UI Marco self.make_app_in_screen_center() self.init_toolbar() self.DBName = 'kwDCMDB' self.myDB = DcmDataBase(db_name=self.DBName) self.dirModel = QFileSystemModel() self.treeView.setModel(self.dirModel) # self.treeView.doubleClicked.connect(self.get_selected_item_path) # 只能連接一次不然會執行多次 self.treeView.clicked.connect(self.get_selected_item_path) # self.treeView.selectionModel().selectionChanged.connect(self.get_change) # self.getSeriesFromDB() def get_change(self, selected, deselected): indexes = [] for index in selected.indexes(): if index.column() == 0: indexes.append(index) pprint(indexes) return indexes def addItems(self, parent, elements): for text, children in elements: item = QStandardItem(text) parent.appendRow(item) if children: self.addItems(item, children) def make_app_in_screen_center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) # print('Position to screen Center!!') def init_toolbar(self): # Init ToolBar toolbar = QToolBar("My main toolbar") self.addToolBar(toolbar) toolbar.setContextMenuPolicy( Qt.PreventContextMenu) # prevent users from hiding toolbar toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) """Actions""" # terminate App action_exit_app = self.actionExit action_exit_app.triggered.connect(qApp.quit) # Open Single Dcm action_openOneImg = self.actionOpen_Single_Image action_openOneImg.triggered.connect(self.open_one_dcm) # Scan folder for dcm action_openSeries = self.actionOpen_Series_Folder action_openSeries.triggered.connect(self.openDirUpdateView) # Parse DICOMDIR action_ParseDcmDir = self.actionParse_DICOMDIR action_ParseDcmDir.triggered.connect(self.parseDICOMDIR) """" Set Keyboard Short Cut for actions""" action_openOneImg.setShortcut(QKeySequence.Open) """Link actions to ToolBar as Buttons""" toolbar.addAction(action_openOneImg) toolbar.addAction(action_openSeries) toolbar.addAction(action_ParseDcmDir) """"Set ToolBar Buttons Icon""" action_openOneImg.setIcon(QApplication.style().standardIcon( QStyle.SP_ComputerIcon)) action_openSeries.setIcon(QApplication.style().standardIcon( QStyle.SP_DialogOpenButton)) action_ParseDcmDir.setIcon(QApplication.style().standardIcon( QStyle.SP_DriveCDIcon)) def parseDICOMDIR(self): source, _ = QFileDialog.getOpenFileName(self, 'Open \"DICOMDIR\" File', 'DICOMDIR', 'DICOMDIR File (*)') destination_path = QFileDialog.getExistingDirectory( self, 'select Destination to store parsed Dicom images') if source != '' and destination_path != '': s = str((Path(source)).parent) DicomDirParser(source=s, destination=destination_path).parseDIR() def addToDB(self, p): p = QFileDialog.getExistingDirectory( self, "Choose Folder to Scan for DICOM") if p != '': self.myDB.createDBbyScan(path_to_scan=p) self.getSeriesFromDB() def getSeriesFromDB(self): print('getSeriesFromDB') sql_cmd = "SELECT zPatient_name,zPatient_id,zSeriesDescription, zPath FROM TBSeries ORDER BY zPatient_name ASC" self.myDB.sqlite.cur.execute(sql_cmd) series_list = [value for value in self.myDB.sqlite.cur] print(series_list) # print(self.myDB.sqlite.getSQLtableColumn('TBStudy', 'zPatient_id')) # print(self.myDB.sqlite.getSQLtableColumn('TBSeries', 'zSeriesDescription')) def openDirUpdateView(self): p = QFileDialog.getExistingDirectory(self, "Choose Folder to View DICOM") if p != '': self.dirModel.setRootPath(p) self.treeView.setRootIndex(self.dirModel.index(p)) self.treeView.expandAll() tkview = DcmViewFrame(dcm_dir=p, view_plane='Transverse') self.hl.addWidget(tkview) self.frame.setLayout(self.hl) self.setCentralWidget(self.frame) def open_dcm_onlick(self, p): if Path(p).is_file(): file_path = Path(p) print('path=', file_path) self.statusBar().showMessage('Reading DICOM File.') # self.vtk_display(str(file_path)) dcm_ds, default_wl, default_ww, rescale_intercept, rescale_slope = DcmViewCalibration.read_dcm_file( file_path) dcm_image_scaled = DcmViewCalibration.ct_windowed( dcm_ds, default_wl, default_ww, np.uint8, (0, 255)) image = cv2.cvtColor(dcm_image_scaled, cv2.COLOR_GRAY2RGB) cv2.imshow('DICOM', image) cv2.waitKey(0) def open_one_dcm(self): p, _ = QFileDialog.getOpenFileName(self, 'choose DICOM File to Open', '', 'DICOM Image File (*.dcm)') if Path(p).is_file(): file_path = Path(p) print('path=', file_path) self.statusBar().showMessage('Reading DICOM File.') # self.vtk_display(str(file_path)) dcm_ds, default_wl, default_ww, rescale_intercept, rescale_slope = DcmViewCalibration.read_dcm_file( file_path) dcm_image_scaled = DcmViewCalibration.ct_windowed( dcm_ds, default_wl, default_ww, np.uint8, (0, 255)) image = cv2.cvtColor(dcm_image_scaled, cv2.COLOR_GRAY2RGB) cv2.imshow('DICOM', image) cv2.waitKey(0) cv2.destroyAllWindows() else: self.statusBar().showMessage('Please choose Folder.') def get_selected_item_path(self, signal): file_path = self.dirModel.filePath(signal) print('get_selected', file_path) self.open_dcm_onlick(file_path) # return file_path def treeMedia_doubleClicked(self, index): item = self.treeView.selectedIndexes()[0] print(item.model().itemFromIndex(index).text())