1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-11-14 11:39:03 +00:00
dupeguru/qt/recent.py

95 lines
2.8 KiB
Python
Raw Normal View History

2019-09-10 00:54:28 +00:00
# Created By: Virgil Dupras
# Created On: 2009-11-12
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
2019-09-10 00:54:28 +00:00
# http://www.gnu.org/licenses/gpl-3.0.html
from collections import namedtuple
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QAction
from hscommon.trans import trget
from hscommon.util import dedupe
2022-05-09 00:40:37 +00:00
tr = trget("ui")
MenuEntry = namedtuple("MenuEntry", "menu fixedItemCount")
2019-09-10 00:54:28 +00:00
class Recent(QObject):
2021-08-24 05:12:23 +00:00
def __init__(self, app, pref_name, max_item_count=10, **kwargs):
2019-09-10 00:54:28 +00:00
super().__init__(**kwargs)
self._app = app
self._menuEntries = []
2021-08-24 05:12:23 +00:00
self._prefName = pref_name
self._maxItemCount = max_item_count
2019-09-10 00:54:28 +00:00
self._items = []
self._loadFromPrefs()
2019-09-10 00:54:28 +00:00
self._app.willSavePrefs.connect(self._saveToPrefs)
# --- Private
2019-09-10 00:54:28 +00:00
def _loadFromPrefs(self):
items = getattr(self._app.prefs, self._prefName)
if not isinstance(items, list):
items = []
self._items = items
2019-09-10 00:54:28 +00:00
def _insertItem(self, item):
self._items = dedupe([item] + self._items)[: self._maxItemCount]
2021-08-24 05:12:23 +00:00
def _refreshMenu(self, menu_entry):
menu, fixed_item_count = menu_entry
for action in menu.actions()[fixed_item_count:]:
2019-09-10 00:54:28 +00:00
menu.removeAction(action)
for item in self._items:
action = QAction(item, menu)
action.setData(item)
action.triggered.connect(self.menuItemWasClicked)
menu.addAction(action)
menu.addSeparator()
action = QAction(tr("Clear List"), menu)
action.triggered.connect(self.clear)
menu.addAction(action)
2019-09-10 00:54:28 +00:00
def _refreshAllMenus(self):
2021-08-24 05:12:23 +00:00
for menu_entry in self._menuEntries:
self._refreshMenu(menu_entry)
2019-09-10 00:54:28 +00:00
def _saveToPrefs(self):
setattr(self._app.prefs, self._prefName, self._items)
# --- Public
2019-09-10 00:54:28 +00:00
def addMenu(self, menu):
2021-08-24 05:12:23 +00:00
menu_entry = MenuEntry(menu, len(menu.actions()))
self._menuEntries.append(menu_entry)
self._refreshMenu(menu_entry)
2019-09-10 00:54:28 +00:00
def clear(self):
self._items = []
self._refreshAllMenus()
self.itemsChanged.emit()
2019-09-10 00:54:28 +00:00
def insertItem(self, item):
self._insertItem(str(item))
self._refreshAllMenus()
self.itemsChanged.emit()
2019-09-10 00:54:28 +00:00
def isEmpty(self):
return not bool(self._items)
# --- Event Handlers
2019-09-10 00:54:28 +00:00
def menuItemWasClicked(self):
action = self.sender()
if action is not None:
item = action.data()
self.mustOpenItem.emit(item)
self._refreshAllMenus()
# --- Signals
2019-09-10 00:54:28 +00:00
mustOpenItem = pyqtSignal(str)
itemsChanged = pyqtSignal()