def __init__(self, model): self.settings = SettingsModel() self.favorites = FavoritesModel() self.model = model self.canvas = CanvasController(self.model.canvas) self.start() # Initial calls
def update_ui_from_model(self): """Update UI from model.""" self.settings = SettingsModel() # On changing directory self.file_model.setRootPath(self.model.directory) self.ui.treeView.setRootIndex( self.file_model.index(self.model.directory)) #if self.model.image_path is not None: # self.ui.statusbar.showMessage(str(self.model.image_path) + " " + str(self.model.image_index + 1) + " of " + str(len(self.model.image_paths))) self.ui.graphicsView.setScene(self.canvas.scene) # Fullscreen mode switching if self.model.is_fullscreen: self.showFullScreen() if self.settings.get('Misc', 'hide_menubar') == 'True': self.ui.menubar.hide() if self.settings.get('Misc', 'hide_statusbar') == 'True': self.ui.statusbar.hide() else: self.showNormal() if self.settings.get('Misc', 'hide_menubar') == 'True': self.ui.menubar.show() if self.settings.get('Misc', 'hide_statusbar') == 'True': self.ui.statusbar.show()
def __init__(self, parent, parent_ui): self.model = SettingsModel() self.controller = SettingsController(self.model) self.parent_ui = parent_ui super(OptionDialog, self).__init__(parent) self.build_ui() self.model.subscribe_update_func(self.update_ui_from_model)
def __init__(self, model, controller): self.settings = SettingsModel() self.model = model self.canvas = self.model.canvas self.main_controller = controller self.canvas_controller = CanvasController(self.canvas) super(MainView, self).__init__() self.build_ui() self.center_ui() self.model.subscribe_update_func(self.update_ui_from_model)
class OptionDialog(QDialog): def __init__(self, parent, parent_ui): self.model = SettingsModel() self.controller = SettingsController(self.model) self.parent_ui = parent_ui super(OptionDialog, self).__init__(parent) self.build_ui() self.model.subscribe_update_func(self.update_ui_from_model) def build_ui(self): self.ui = Ui_Dialog() self.ui.setupUi(self) # Layout self.ui.button_browse_directory.clicked.connect(self.on_update_default_directory) self.ui.input_default_directory.setText(self.model.get('Directory', 'default')) self.ui.input_background_color.setText(self.model.get('Look', 'background')) self.ui.button_colorpicker.clicked.connect(self.on_update_background) self.generate_checkbox_list() self.generate_hotkey_list() # Global self.ui.button_save.clicked.connect(self.on_save) self.ui.button_cancel.clicked.connect(self.close) # Hotkey assigning self.new_shortcut = QKeySequenceEdit(self.ui.input_new_shortcut) self.ui.button_assign.clicked.connect(self.on_new_shortcut) self.ui.button_reset.clicked.connect(self.controller.load_defaults) def generate_checkbox_list(self): self.ui.checkbox_check_updates.setChecked(self.model.get('Misc', 'check_updates') == 'True') self.ui.checkbox_hide_menubar.setChecked(self.model.get('Misc', 'hide_menubar') == 'True') self.ui.checkbox_hide_statusbar.setChecked(self.model.get('Misc', 'hide_statusbar') == 'True') def generate_hotkey_list(self): for option in self.model.options('Hotkeys'): item = QListWidgetItem() item.setText(option) item.setData(Qt.UserRole, self.model.get('Hotkeys', option)) self.ui.listWidget.addItem(item) def on_update_default_directory(self): self.controller.update_default_directory() self.ui.input_default_directory.setText(self.model.get('Directory', 'default')) def on_update_background(self): self.controller.update_background() self.ui.input_background_color.setText(self.model.get('Look', 'background')) def on_listWidget_currentItemChanged(self, new, prev): if new is not None: self.selected_option = new.text() self.ui.input_cur_shortcut.setText(new.data(Qt.UserRole)) def on_new_shortcut(self): _new_shortcut = str(QKeySequence.toString(self.new_shortcut.keySequence())) self.controller.update_hotkey(self.selected_option, _new_shortcut) # Regenerate list self.ui.listWidget.clear() self.generate_hotkey_list() # Update inputs self.ui.input_cur_shortcut.setText(_new_shortcut) # possible self.model.get('Hotkeys', self.selected_option) self.new_shortcut.clear() def on_checkbox_check_updates_stateChanged(self, state): self.controller.update_boolean('check_updates', state) def on_checkbox_hide_menubar_stateChanged(self, state): self.controller.update_boolean('hide_menubar', state) def on_checkbox_hide_statusbar_stateChanged(self, state): self.controller.update_boolean('hide_statusbar', state) def on_save(self): self.controller.apply_settings() self.parent_ui.graphicsView.setBackgroundBrush(QBrush(QColor(self.model.get('Look', 'background')), Qt.SolidPattern)) self.close() def update_ui_from_model(self): self.ui.button_save.setEnabled(True)
def __init__(self): self.settings = SettingsModel() self.favorites = [] for option in self.settings.options('Favorites'): self.add(self.settings.get('Favorites', option))
class FavoritesModel(object): def __init__(self): self.settings = SettingsModel() self.favorites = [] for option in self.settings.options('Favorites'): self.add(self.settings.get('Favorites', option)) def add(self, path): if path not in self.favorites: self.favorites.append(path) def remove(self, path): if path in self.favorites: self.favorites.remove(path) def items(self): return self.favorites def save(self): self.settings.remove_section('Favorites') if not self.settings.has_section('Favorites'): self.settings.add_section('Favorites') for index, item in enumerate(self.items()): if not self.settings.has_option('Favorites', str(index)): self.settings.set('Favorites', str(index), str(item)) self.settings.apply_settings()
class MainController(object): def __init__(self, model): self.settings = SettingsModel() self.favorites = FavoritesModel() self.model = model self.canvas = CanvasController(self.model.canvas) self.start() # Initial calls def start(self): """Initial calls.""" self.change_directory(self.settings.get('Directory', 'default')) def change_directory(self, directory=None): """Change current directory.""" if not directory: new_directory = QFileDialog.getExistingDirectory( None, "Change current directory", '/') else: new_directory = directory # if dialog canceled if not new_directory: return # old concept: open directory and load paths of images #if self.model.include_subfolders == True: # _image_paths = [i for i in Path(new_directory).rglob("*") if i.suffix.lower() in ['.jpg', '.png']] #else: # _image_paths = [i for i in Path(new_directory).glob("*") if i.suffix.lower() in ['.jpg', '.png']] #if len(_image_paths) > 0: #self.model.image_paths = _image_paths #self.model.image_index = 0 self.model.directory = new_directory #print(Path(str(new_directory))) #image = self.model.get_image() #self.canvas.update_canvas(container_width, container_height, image) self.model.announce_update() def add_to_favorites(self): """Favorite current directory.""" self.favorites.add(str(self.model.directory)) self.favorites.save() def remove_from_favorites(self): """Remove current directory from favorite list.""" self.favorites.remove(str(self.model.directory)) self.favorites.save() def update_canvas(self, container_width, container_height, image=None): """Update canvas with image.""" self.canvas.update_canvas(container_width, container_height, image) self.model.announce_update() def toggle_fullscreen(self): """Toggle between fullscreen mode.""" if self.model.is_fullscreen: self.model.is_fullscreen = False else: self.model.is_fullscreen = True # Update UI self.model.announce_update() def open_image(self, container_width, container_height, path): """Open specific image via path.""" image = QImage(str(path)) self.model.image_path = path # Update image index #if _path in self.model.image_paths: #self.model.image_index = self.model.image_paths.index(_path) self.canvas.update_canvas(container_width, container_height, image) self.model.announce_update() #def prev_image(self, container_width, container_height): # """Go to previous image.""" # if self.model.image_index > 0: # self.model.image_index -= 1 # image = self.model.get_image() # # # Use canvas controller # self.canvas.update_canvas(container_width, container_height, image) # self.model.announce_update() # #def next_image(self, container_width, container_height): # """Go to next image.""" # if self.model.image_index < (len(self.model.image_paths) - 1): # self.model.image_index += 1 # image = self.model.get_image() # # self.canvas.update_canvas(container_width, container_height, image) # self.model.announce_update() def copy_to_clipboard(self): if self.model.image_path is not None: self.model.clipboard.setImage(self.model.get_image(), QClipboard.Clipboard)
class MainView(QMainWindow): def __init__(self, model, controller): self.settings = SettingsModel() self.model = model self.canvas = self.model.canvas self.main_controller = controller self.canvas_controller = CanvasController(self.canvas) super(MainView, self).__init__() self.build_ui() self.center_ui() self.model.subscribe_update_func(self.update_ui_from_model) def build_ui(self): self.ui = Ui_Hitagi() self.ui.setupUi(self) # File menu self.ui.actionSet_as_wallpaper.triggered.connect( self.on_set_as_wallpaper) self.ui.actionCopy_to_clipboard.triggered.connect(self.on_clipboard) self.ui.actionOpen_current_directory.triggered.connect( self.on_current_dir) self.ui.actionOptions.triggered.connect(self.on_options) self.ui.actionExit.triggered.connect(self.on_close) # Folder menu self.ui.actionOpen_next.triggered.connect(self.on_next_item) self.ui.actionOpen_previous.triggered.connect(self.on_previous_item) self.ui.actionChange_directory.triggered.connect( self.on_change_directory) # View menu self.ui.actionZoom_in.triggered.connect(self.on_zoom_in) self.ui.actionZoom_out.triggered.connect(self.on_zoom_out) self.ui.actionOriginal_size.triggered.connect(self.on_zoom_original) self.ui.actionFit_image_width.triggered.connect( self.on_scale_image_to_width) self.ui.actionFit_image_height.triggered.connect( self.on_scale_image_to_height) self.ui.actionFile_list.triggered.connect(self.on_toggle_filelist) self.ui.actionFullscreen.triggered.connect(self.on_fullscreen) # Favorite menu self.ui.actionAdd_to_favorites.triggered.connect( self.on_add_to_favorites) self.ui.actionRemove_from_favorites.triggered.connect( self.on_remove_from_favorites) # Help menu self.ui.actionChangelog.triggered.connect(self.on_changelog) self.ui.actionAbout.triggered.connect(self.on_about) # Load stylesheet stylesheet_dir = "resources/hitagi.stylesheet" with open(stylesheet_dir, "r") as sh: self.setStyleSheet(sh.read()) # File listing self.file_model = QFileSystemModel() self.file_model.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs | QDir.Files) self.file_model.setNameFilters(['*.jpg', '*.png', '*.jpeg']) self.file_model.setNameFilterDisables(False) self.file_model.setRootPath(self.settings.get('Directory', 'default')) self.ui.treeView.setModel(self.file_model) self.ui.treeView.setColumnWidth(0, 200) self.ui.treeView.setColumnWidth(1, 200) self.ui.treeView.hideColumn(1) self.ui.treeView.hideColumn(2) # Double click self.ui.treeView.activated.connect(self.on_dir_list_activated) # Update file list self.ui.treeView.clicked.connect(self.on_dir_list_clicked) # Open parent self.ui.button_open_parent.clicked.connect(self.on_open_parent) # Shortcuts _translate = QtCore.QCoreApplication.translate self.ui.actionExit.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Exit'))) self.ui.actionOpen_next.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Next'))) self.ui.actionOpen_previous.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Previous'))) self.ui.actionChange_directory.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Directory'))) self.ui.actionZoom_in.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Zoom in'))) self.ui.actionZoom_out.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Zoom out'))) self.ui.actionOriginal_size.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Zoom original'))) self.ui.actionFullscreen.setShortcut( _translate("Hitagi", self.settings.get('Hotkeys', 'Fullscreen'))) # Load favorites in UI self.load_favorites() # Background self.ui.graphicsView.setBackgroundBrush( QBrush(QColor(self.settings.get('Look', 'background')), Qt.SolidPattern)) def load_favorites(self): self.favorites = FavoritesModel() self.ui.menuFavorites.clear() for item in self.favorites.items(): self.ui.menuFavorites.addAction(item).triggered.connect( (lambda item: lambda: self.on_open_favorite(item))(item)) def on_open_favorite(self, path): self.main_controller.change_directory(path) def center_ui(self): ui_geometry = self.frameGeometry() center_point = QDesktopWidget().availableGeometry().center() ui_geometry.moveCenter(center_point) self.move(ui_geometry.topLeft()) # On resize def resizeEvent(self, resizeEvent): self.main_controller.update_canvas(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.model.get_image()) # Additional static shortcuts def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_Escape and self.model.is_fullscreen: self.main_controller.toggle_fullscreen() # Redefine shortcuts when hiding menubar # somehow not working 18/2/2015 if self.model.is_fullscreen and self.ui.menubar.isHidden(): if e.key() == QKeySequence(self.settings.get('Hotkeys', 'Exit')): self.on_close() elif e.key() == QKeySequence(self.settings.get('Hotkeys', 'Next')): self.on_next_item() elif e.key() == QKeySequence( self.settings.get('Hotkeys', 'Previous')): self.on_previous_item() elif e.key() == QKeySequence( self.settings.get('Hotkeys', 'Directory')): self.on_change_directory() elif e.key() == QKeySequence( self.settings.get('Hotkeys', 'Zoom in')): self.on_zoom_in() elif e.key() == QKeySequence( self.settings.get('Hotkeys', 'Zoom out')): self.on_zoom_out() elif e.key() == QKeySequence( self.settings.get('Hotkeys', 'Zoom original')): self.on_zoom_original() elif e.key() == QKeySequence( self.settings.get('Hotkeys', 'Fullscreen')): self.main_controller.toggle_fullscreen() def on_open_parent(self): parent_index = self.file_model.parent( self.file_model.index(self.file_model.rootPath())) self.file_model.setRootPath(self.file_model.filePath(parent_index)) self.ui.treeView.setRootIndex(parent_index) # Update directory path self.model.directory = self.file_model.filePath(parent_index) def on_dir_list_activated(self, index): if self.file_model.hasChildren(index) is not False: self.file_model.setRootPath(self.file_model.filePath(index)) self.ui.treeView.setRootIndex(index) # Save current path self.model.directory = self.file_model.filePath(index) def on_dir_list_clicked(self, index): self.main_controller.open_image(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.file_model.filePath(index)) # File menu def on_set_as_wallpaper(self): from view.WallpaperView import WallpaperDialog from controller.wallpaper import WallpaperController _image = self.model.get_image() if _image is not None: dialog = WallpaperDialog(self, None, WallpaperController(self.model), _image) dialog.show() def on_clipboard(self): self.main_controller.copy_to_clipboard() def on_current_dir(self): import subprocess # Windows subprocess.Popen(r'explorer /select,' + self.model.get_image_path()) def on_options(self): from view.OptionsView import OptionDialog self.dialog = OptionDialog(self, self.ui) self.dialog.show() def on_close(self): self.close() # Folder menu def on_next_item(self): index = self.ui.treeView.moveCursor(QAbstractItemView.MoveDown, Qt.NoModifier) self.ui.treeView.setCurrentIndex(index) self.main_controller.open_image(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.file_model.filePath(index)) def on_previous_item(self): index = self.ui.treeView.moveCursor(QAbstractItemView.MoveUp, Qt.NoModifier) self.ui.treeView.setCurrentIndex(index) self.main_controller.open_image(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.file_model.filePath(index)) def on_change_directory(self): self.main_controller.change_directory() # View menu def on_zoom_in(self): self.canvas_controller.update_canvas(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.model.get_image(), 1, 1.1) def on_zoom_out(self): self.canvas_controller.update_canvas(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.model.get_image(), 1, 0.9) def on_zoom_original(self): self.canvas_controller.update_canvas(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.model.get_image(), 4) def on_scale_image_to_width(self): self.canvas_controller.update_canvas(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.model.get_image(), 2) def on_scale_image_to_height(self): self.canvas_controller.update_canvas(self.ui.graphicsView.width(), self.ui.graphicsView.height(), self.model.get_image(), 3) def on_toggle_filelist(self): if self.ui.actionFile_list.isChecked(): self.ui.fileWidget.show() else: self.ui.fileWidget.hide() def on_fullscreen(self): self.main_controller.toggle_fullscreen() # Favorite menu def on_add_to_favorites(self): self.main_controller.add_to_favorites() self.load_favorites() def on_remove_from_favorites(self): self.main_controller.remove_from_favorites() self.load_favorites() # Help menu def on_changelog(self): webbrowser.open('https://gimu.org/hitagi-reader/docs') def on_about(self): from view.AboutView import AboutDialog dialog = AboutDialog(self, None, None) dialog.show() def update_ui_from_model(self): """Update UI from model.""" self.settings = SettingsModel() # On changing directory self.file_model.setRootPath(self.model.directory) self.ui.treeView.setRootIndex( self.file_model.index(self.model.directory)) #if self.model.image_path is not None: # self.ui.statusbar.showMessage(str(self.model.image_path) + " " + str(self.model.image_index + 1) + " of " + str(len(self.model.image_paths))) self.ui.graphicsView.setScene(self.canvas.scene) # Fullscreen mode switching if self.model.is_fullscreen: self.showFullScreen() if self.settings.get('Misc', 'hide_menubar') == 'True': self.ui.menubar.hide() if self.settings.get('Misc', 'hide_statusbar') == 'True': self.ui.statusbar.hide() else: self.showNormal() if self.settings.get('Misc', 'hide_menubar') == 'True': self.ui.menubar.show() if self.settings.get('Misc', 'hide_statusbar') == 'True': self.ui.statusbar.show()