def __init__(self, graph_field, side_widget, main_controller: MainController, io_controller: IOController, command_handler, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setupUi(self) self.__command_handler = command_handler self.__graph_field = graph_field self.__main_controller = main_controller self.__io_controller = io_controller self.__file_path = "" self.setCentralWidget(graph_field) self.__conditional_actions = [] dock = QDockWidget(QCoreApplication.translate("main_window", "Operations"), self) dock.setWidget(side_widget) dock.setFeatures(dock.DockWidgetMovable | dock.DockWidgetFloatable) self.addDockWidget(Qt.RightDockWidgetArea, dock) # Observe can_undo and can_redo on command handler # to enable/disable und and redo buttons self.__command_handler.can_undo.observe( lambda old, new: self.action_undo.setDisabled(not new)) self.__command_handler.can_redo.observe( lambda old, new: self.action_redo.setDisabled(not new)) # Observe selection changes self.__graph_field.selection.observe( self.__handle_selection_change) # Connect triggers to methods self.action_redo.triggered.connect(self.__command_handler.redo) self.action_undo.triggered.connect(self.__command_handler.undo) self.action_save.triggered.connect(self.__save) self.action_save_as.triggered.connect(self.__save_as) self.action_select_all.triggered.connect(self.__select_all) self.action_open.triggered.connect(self.__open) self.action_new.triggered.connect(self.__new) self.action_exit.triggered.connect(lambda: self.close()) self.action_about.triggered.connect(self.__about) self.__conditional_actions.append(ConditionalAction( [MinNumNodesOrBranchesSelected(1)], self.action_remove_branch_or_node, self.action_remove_branch_or_node.triggered, lambda sel, *args: self.__main_controller.remove_nodes_and_branches(sel) )) # Creat inital graph self.__new()
class MainWindowMPDJ(QMainWindow): """Display the main window of the application where the connections and other stuff is edited by the user.""" def show(self): """Shows this window (maximized). I will work on a better solution.""" QMainWindow.showMaximized(self) def update(self): """Updates the view.""" global_properties = GlobalProperties.get_instance() mpdj_data = global_properties.mpdj_data self.tf_min_per_selection.setText( str(mpdj_data.min_units_per_node_touch)) self.tf_max_per_selection.setText( str(mpdj_data.max_units_per_node_touch)) self.tf_min_global_song_duration.setText( str(mpdj_data.global_min_song_duration)) self.tf_max_global_song_duration.setText( str(mpdj_data.global_max_song_duration)) self.cb_global_limit_overflow.setChecked( mpdj_data.limit_overspill_global) self.tf_global_node_max_overflow_minutes.setText( str(mpdj_data.global_node_max_overspill)) self.tf_global_node_max_overflow_minutes.setDisabled( not mpdj_data.limit_overspill_global) text_to_find = mpdj_data.unit_per_node_touch.gui_representation() index = self.combo_box_minutes_or_titles.findText( text_to_find, Qt.MatchFixedString) self.combo_box_minutes_or_titles.setCurrentIndex(index) # self.limit_artist_play_chk_box.setChecked( # global_properties.mpdj_data.limit_artist_in_node_touch) self.chk_box_graph_is_directed.setChecked(mpdj_data.graph_is_directed) self.setWindowTitle('MPDJ: {}'.format( global_properties.path_of_current_file)) def write_min_global_song_duration_to_mpdj(self): """Writes min global song duration to mpdj.""" global_properties = GlobalProperties.get_instance() global_properties.mpdj_data.global_min_song_duration = int( self.tf_min_global_song_duration.text()) def write_max_global_song_duration_to_mpdj(self): """Writes min global song duration to mpdj.""" global_properties = GlobalProperties.get_instance() global_properties.mpdj_data.global_max_song_duration = int( self.tf_max_global_song_duration.text()) def write_min_per_note_to_mpdj(self): """Write the selected min count per node touch to the mpdj which is currently worked on.""" global_properties = GlobalProperties.get_instance() global_properties.mpdj_data.min_units_per_node_touch = int( self.tf_min_per_selection.text()) def write_max_per_note_to_mpdj(self): """Writes the selected max count per node touch to the mpdj which is currently worked on.""" global_properties = GlobalProperties.get_instance() global_properties.mpdj_data.max_units_per_node_touch = int( self.tf_max_per_selection.text()) def write_unit_per_node_touch_to_mpdj(self): """Writes the selected unit for min and max per touch to the mpdj. This does not do anything at the moment. Has to be implemented properly""" global_properties = GlobalProperties.get_instance() selection_text = self.combo_box_minutes_or_titles.currentText() global_properties.mpdj_data.unit_per_node_touch = UnitPerNodeTouch[ selection_text.upper()] def write_limit_artists_played_to_mpdj(self): # """Write to the current mpdj if the artist are limited per node touch.""" global_properties = GlobalProperties.get_instance() state = self.limit_artist_play_chk_box.isChecked() global_properties.mpdj_data.limit_artists_in_node_touch = state def write_limit_overspill_to_mpdj(self): global_properties = GlobalProperties.get_instance() state = self.cb_global_limit_overflow.isChecked() global_properties.mpdj_data.limit_overspill_global = state def write_global_node_max_overflow_to_mpdj(self): global_properties = GlobalProperties.get_instance() global_node_max_overflow_text = self.tf_global_node_max_overflow_minutes.text( ) if global_node_max_overflow_text: new_max_overflow = int(global_node_max_overflow_text) global_properties.mpdj_data.global_node_max_overspill = new_max_overflow def write_graph_is_directed_to_mpdj(self): """Write if the graph is directed to mpd, this should be changed somehow, since this does only concern the editing of the connections.""" global_properties = GlobalProperties.get_instance() state = self.chk_box_graph_is_directed.isChecked() global_properties.mpdj_data.graph_is_directed = state def file_dialog(self, load_save_type=QFileDialog.AcceptSave): """Opens an file save dialog and returns the selected filename.""" file_save_dialog = QFileDialog(self) file_save_dialog.setFileMode(QFileDialog.AnyFile) file_save_dialog.setAcceptMode(load_save_type) file_save_dialog.setNameFilters( ["MPDJ files (*.{})".format(FILE_SUFFIX)]) file_save_dialog.selectNameFilter( "MPDJ files (*.{})".format(FILE_SUFFIX)) file_save_dialog.setDefaultSuffix((FILE_SUFFIX)) exec_value = file_save_dialog.exec() if exec_value == 0: return None file_names = file_save_dialog.selectedFiles() if len(file_names) != 1: message_box = QMessageBox() message_box.setText('Please select only one file!') message_box.setWindowTitle('Save error.') message_box.setStandardButtons(QMessageBox.Ok) message_box.setIcon(QMessageBox.Information) message_box.exec_() return None return file_names[0] def file_save_as(self): """Saves the file. opens a file_dialog which asks for the filename.""" file_name = self.file_dialog(load_save_type=QFileDialog.AcceptSave) if file_name: self.save_mpdj_data_to_file(file_name) def save_mpdj_data_to_file(self, p_file_name: str): """Saves the current mpdj data to the file by the path given in p_file_name.""" try: global_properties = GlobalProperties.get_instance() global_properties.save_mpdj_data_to_file(p_file_name) self.statusBar().showMessage('Saved to {}'.format(p_file_name), 5000) except (OSError, IOError) as exception: message_box = QMessageBox() message_box.setText('Error saving the file: {}'.format( str(exception))) message_box.setWindowTitle('Error saving the file.') message_box.setStandardButtons(QMessageBox.Ok) message_box.setIcon(QMessageBox.Warning) message_box.exec_() def file_save(self): """Saves the current mpdj data to the current file.""" global_properties = GlobalProperties.get_instance() if len(global_properties.path_of_current_file) > 0: self.save_mpdj_data_to_file(global_properties.path_of_current_file) else: self.file_save_as() def file_load(self): """Loads mpdj data from a file. Opens a file dialog which asks for the file to load.""" global_properties = GlobalProperties.get_instance() file_name = self.file_dialog(load_save_type=QFileDialog.AcceptOpen) if file_name: if global_properties.changes_happened_since_last_save: retval = show_discard_data_ok_cancel_message() if not global_properties.changes_happened_since_last_save or retval == QMessageBox.Ok: try: global_properties.load_mpdjdata_from_file(file_name) except AttributeError as err: message_box = QMessageBox() message_box.setText( 'Error reading your MPDJ-File: {}'.format(err)) message_box.setWindowTitle('Load error.') message_box.setStandardButtons(QMessageBox.Ok) message_box.setIcon(QMessageBox.Warning) message_box.exec_() def __init__(self): """Constructor""" QMainWindow.__init__(self) global_properties = GlobalProperties.get_instance() self.connection_table = ConnectionTableWidget() global_properties.add_listener(self.connection_table) self.setCentralWidget(self.connection_table) self.connection_table.update() self.menu_bar = self.menuBar() self.menu_file = self.menu_bar.addMenu('File') self.menu_file.addAction('New', file_new_clicked) self.menu_file.addAction('Open', self.file_load) self.menu_file.addSeparator() self.menu_file.addAction('Save', self.file_save) self.menu_file.addAction('Save as', self.file_save_as) self.menu_file.addSeparator() self.menu_file.addAction('Exit', sys.exit) self.menu_file = self.menu_bar.addMenu('Connections') self.make_birectional_menu = self.menu_file.addMenu( 'Make bidirectional') self.make_birectional_menu.addAction("with and", make_bidirectional_and) self.make_birectional_menu.addAction("with or", make_bidirectional_or) self.menu_selection = self.menu_bar.addMenu('Selections') self.action_add_selection = self.menu_selection.addAction( 'Add Selection') self.action_add_selection.triggered.connect( create_add_selection_window) self.action_merge_selections = self.menu_selection.addAction( 'Merge Selections') self.action_merge_selections.triggered.connect( create_merge_selectoon_window) self.setMenuBar(self.menu_bar) self.statusBar().showMessage('Welcome to mpdj!', 5000) self.mpdj_options_dock = QDockWidget("MPDJ Options Panel", self) self.mpdj_options_dock_layout = QFormLayout() self.mpdj_docked_widget = QWidget() self.tf_min_global_song_duration = QLineEdit() self.tf_min_global_song_duration.setValidator( QIntValidator(0, 2147483647)) self.mpdj_options_dock_layout.addRow('Global min song duration:', self.tf_min_global_song_duration) self.tf_min_global_song_duration.editingFinished.connect( self.write_min_global_song_duration_to_mpdj) self.tf_max_global_song_duration = QLineEdit() self.tf_max_global_song_duration.setValidator( QIntValidator(0, 2147483647)) self.mpdj_options_dock_layout.addRow('Global max song duration:', self.tf_max_global_song_duration) self.tf_max_global_song_duration.editingFinished.connect( self.write_max_global_song_duration_to_mpdj) self.tf_min_per_selection = QLineEdit() self.tf_min_per_selection.setValidator(QIntValidator(0, 2147483647)) self.mpdj_options_dock_layout.addRow('Min per Node touch:', self.tf_min_per_selection) self.tf_min_per_selection.editingFinished.connect( self.write_min_per_note_to_mpdj) self.tf_max_per_selection = QLineEdit() self.tf_max_per_selection.setValidator(QIntValidator(0, 2147483647)) self.mpdj_options_dock_layout.addRow('Max per Node touch:', self.tf_max_per_selection) self.tf_max_per_selection.editingFinished.connect( self.write_max_per_note_to_mpdj) self.combo_box_minutes_or_titles = QComboBox() self.combo_box_minutes_or_titles.addItems( [unit.gui_representation() for unit in UnitPerNodeTouch]) self.mpdj_options_dock_layout.addRow('Unit:', self.combo_box_minutes_or_titles) self.combo_box_minutes_or_titles.currentTextChanged.connect( self.write_unit_per_node_touch_to_mpdj) self.tf_global_node_max_overflow_minutes = QLineEdit() self.tf_global_node_max_overflow_minutes.setValidator( QIntValidator(0, 2147483647)) self.tf_global_node_max_overflow_minutes.editingFinished.connect( self.write_global_node_max_overflow_to_mpdj) self.cb_global_limit_overflow = QCheckBox() self.cb_global_limit_overflow.stateChanged.connect(lambda: list( map(lambda m: m(), [ lambda: self.tf_global_node_max_overflow_minutes.setDisabled( not self.cb_global_limit_overflow.isChecked()), self. write_limit_overspill_to_mpdj ]))) self.overflow_layoout = QHBoxLayout() self.overflow_layoout.addWidget(self.cb_global_limit_overflow) self.overflow_layoout.addWidget( self.tf_global_node_max_overflow_minutes) self.mpdj_options_dock_layout.addRow("Limit overflow:", self.overflow_layoout) self.mpdj_docked_widget.setLayout(self.mpdj_options_dock_layout) self.mpdj_options_dock.setWidget(self.mpdj_docked_widget) self.addDockWidget(Qt.LeftDockWidgetArea, self.mpdj_options_dock) self.chk_box_graph_is_directed = QCheckBox() self.chk_box_graph_is_directed.stateChanged.connect( self.write_graph_is_directed_to_mpdj) self.mpdj_options_dock_layout.addRow(QLabel('Graph is directed'), self.chk_box_graph_is_directed) self.opened_selection_window = None global_properties.add_listener(self) self.update()