1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-10-31 22:05:58 +00:00
dupeguru/qt/preferences_dialog.py

373 lines
16 KiB
Python
Raw Normal View History

# Copyright 2016 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
# http://www.gnu.org/licenses/gpl-3.0.html
from PyQt5.QtCore import Qt, QSize, pyqtSlot
from PyQt5.QtWidgets import (
QDialog,
QDialogButtonBox,
QVBoxLayout,
QHBoxLayout,
QGridLayout,
QLabel,
QComboBox,
QSlider,
QSizePolicy,
QSpacerItem,
QCheckBox,
QLineEdit,
QMessageBox,
QSpinBox,
QLayout,
2020-07-20 01:10:06 +00:00
QTabWidget,
QWidget,
QColorDialog,
QPushButton,
QGroupBox,
QFormLayout,
)
from PyQt5.QtGui import QPixmap, QIcon
from hscommon.trans import trget
from hscommon.plat import ISLINUX
2011-09-23 14:29:25 +00:00
from qtlib.util import horizontalWrap
from qtlib.preferences import get_langnames
from enum import Flag, auto
2016-06-01 01:22:50 +00:00
from .preferences import Preferences
tr = trget("ui")
SUPPORTED_LANGUAGES = [
"en",
"fr",
"de",
"el",
"zh_CN",
"cs",
"it",
"hy",
"ru",
"uk",
"pt_BR",
"vi",
"pl_PL",
"ko",
"es",
"nl",
]
2011-11-03 14:38:31 +00:00
class Sections(Flag):
"""Filter blocks of preferences when reset or loaded"""
GENERAL = auto()
DISPLAY = auto()
ALL = GENERAL | DISPLAY
class PreferencesDialogBase(QDialog):
def __init__(self, parent, app, **kwargs):
flags = Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowSystemMenuHint
super().__init__(parent, flags, **kwargs)
self.app = app
all_languages = get_langnames()
self.supportedLanguages = sorted(
SUPPORTED_LANGUAGES, key=lambda lang: all_languages[lang]
)
self._setupUi()
self.filterHardnessSlider.valueChanged["int"].connect(
self.filterHardnessLabel.setNum
)
self.buttonBox.clicked.connect(self.buttonClicked)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
def _setupFilterHardnessBox(self):
self.filterHardnessHLayout = QHBoxLayout()
self.filterHardnessLabel = QLabel(self)
self.filterHardnessLabel.setText(tr("Filter Hardness:"))
self.filterHardnessLabel.setMinimumSize(QSize(0, 0))
self.filterHardnessHLayout.addWidget(self.filterHardnessLabel)
self.filterHardnessVLayout = QVBoxLayout()
self.filterHardnessVLayout.setSpacing(0)
self.filterHardnessHLayoutSub1 = QHBoxLayout()
self.filterHardnessHLayoutSub1.setSpacing(12)
self.filterHardnessSlider = QSlider(self)
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
self.filterHardnessSlider.sizePolicy().hasHeightForWidth()
)
self.filterHardnessSlider.setSizePolicy(sizePolicy)
self.filterHardnessSlider.setMinimum(1)
self.filterHardnessSlider.setMaximum(100)
self.filterHardnessSlider.setTracking(True)
self.filterHardnessSlider.setOrientation(Qt.Horizontal)
self.filterHardnessHLayoutSub1.addWidget(self.filterHardnessSlider)
self.filterHardnessLabel = QLabel(self)
self.filterHardnessLabel.setText("100")
self.filterHardnessLabel.setMinimumSize(QSize(21, 0))
self.filterHardnessHLayoutSub1.addWidget(self.filterHardnessLabel)
self.filterHardnessVLayout.addLayout(self.filterHardnessHLayoutSub1)
self.filterHardnessHLayoutSub2 = QHBoxLayout()
self.filterHardnessHLayoutSub2.setContentsMargins(-1, 0, -1, -1)
self.moreResultsLabel = QLabel(self)
self.moreResultsLabel.setText(tr("More Results"))
self.filterHardnessHLayoutSub2.addWidget(self.moreResultsLabel)
spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.filterHardnessHLayoutSub2.addItem(spacerItem)
self.fewerResultsLabel = QLabel(self)
self.fewerResultsLabel.setText(tr("Fewer Results"))
self.filterHardnessHLayoutSub2.addWidget(self.fewerResultsLabel)
self.filterHardnessVLayout.addLayout(self.filterHardnessHLayoutSub2)
self.filterHardnessHLayout.addLayout(self.filterHardnessVLayout)
def _setupBottomPart(self):
# The bottom part of the pref panel is always the same in all editions.
self.copyMoveLabel = QLabel(self)
self.copyMoveLabel.setText(tr("Copy and Move:"))
self.widgetsVLayout.addWidget(self.copyMoveLabel)
self.copyMoveDestinationComboBox = QComboBox(self)
self.copyMoveDestinationComboBox.addItem(tr("Right in destination"))
self.copyMoveDestinationComboBox.addItem(tr("Recreate relative path"))
self.copyMoveDestinationComboBox.addItem(tr("Recreate absolute path"))
self.widgetsVLayout.addWidget(self.copyMoveDestinationComboBox)
self.customCommandLabel = QLabel(self)
self.customCommandLabel.setText(
tr("Custom Command (arguments: %d for dupe, %r for ref):")
)
self.widgetsVLayout.addWidget(self.customCommandLabel)
self.customCommandEdit = QLineEdit(self)
self.widgetsVLayout.addWidget(self.customCommandEdit)
2020-07-20 01:10:06 +00:00
def _setupDisplayPage(self):
self.ui_groupbox = QGroupBox("&General Interface")
layout = QVBoxLayout()
self.languageLabel = QLabel(tr("Language:"), self)
self.languageComboBox = QComboBox(self)
for lang in self.supportedLanguages:
self.languageComboBox.addItem(get_langnames()[lang])
layout.addLayout(horizontalWrap([self.languageLabel, self.languageComboBox, None]))
self._setupAddCheckbox("tabs_default_pos",
tr("Use default position for tab bar (requires restart)"))
self.tabs_default_pos.setToolTip(
tr("Place the tab bar below the main menu instead of next to it\n\
On MacOS, the tab bar will fill up the window's width instead."))
layout.addWidget(self.tabs_default_pos)
self.ui_groupbox.setLayout(layout)
self.displayVLayout.addWidget(self.ui_groupbox)
gridlayout = QFormLayout()
result_groupbox = QGroupBox("&Result Table")
2011-09-23 14:29:25 +00:00
self.fontSizeSpinBox = QSpinBox()
self.fontSizeSpinBox.setMinimum(5)
gridlayout.addRow(tr("Font size:"), self.fontSizeSpinBox)
self._setupAddCheckbox("reference_bold_font",
tr("Use bold font for references"))
gridlayout.addRow(self.reference_bold_font)
self.result_table_ref_foreground_color = ColorPickerButton(self)
gridlayout.addRow(tr("Reference foreground color:"),
self.result_table_ref_foreground_color)
self.result_table_delta_foreground_color = ColorPickerButton(self)
gridlayout.addRow(tr("Delta foreground color:"),
self.result_table_delta_foreground_color)
gridlayout.setLabelAlignment(Qt.AlignLeft)
# Keep same vertical spacing as parent layout for consistency
gridlayout.setVerticalSpacing(self.displayVLayout.spacing())
result_groupbox.setLayout(gridlayout)
self.displayVLayout.addWidget(result_groupbox)
details_groupbox = QGroupBox("&Details Window")
self.details_groupbox_layout = QVBoxLayout()
self._setupAddCheckbox("details_dialog_titlebar_enabled",
tr("Show the title bar and can be docked"))
2020-07-20 01:10:06 +00:00
self.details_dialog_titlebar_enabled.setToolTip(
tr("While the title bar is hidden, \
use the modifier key to drag the floating window around") if ISLINUX else
tr("The title bar can only be disabled while the window is docked"))
self.details_groupbox_layout.addWidget(self.details_dialog_titlebar_enabled)
self._setupAddCheckbox("details_dialog_vertical_titlebar",
2020-07-20 01:10:06 +00:00
tr("Vertical title bar"))
self.details_dialog_vertical_titlebar.setToolTip(
tr("Change the title bar from horizontal on top, to vertical on the left side"))
self.details_groupbox_layout.addWidget(self.details_dialog_vertical_titlebar)
self.details_dialog_vertical_titlebar.setEnabled(
self.details_dialog_titlebar_enabled.isChecked())
self.details_dialog_titlebar_enabled.stateChanged.connect(
self.details_dialog_vertical_titlebar.setEnabled)
gridlayout = QGridLayout()
self.details_table_delta_foreground_color_label = QLabel(tr("Delta foreground color:"))
gridlayout.addWidget(self.details_table_delta_foreground_color_label, 4, 0)
self.details_table_delta_foreground_color = ColorPickerButton(self)
gridlayout.addWidget(self.details_table_delta_foreground_color, 4, 2, 1, 1, Qt.AlignLeft)
gridlayout.setColumnStretch(1, 1)
gridlayout.setColumnStretch(3, 4)
self.details_groupbox_layout.addLayout(gridlayout)
details_groupbox.setLayout(self.details_groupbox_layout)
self.displayVLayout.addWidget(details_groupbox)
def _setupAddCheckbox(self, name, label, parent=None):
if parent is None:
parent = self
cb = QCheckBox(parent)
cb.setText(label)
setattr(self, name, cb)
def _setupPreferenceWidgets(self):
# Edition-specific
pass
def _setupUi(self):
self.setWindowTitle(tr("Options"))
self.setSizeGripEnabled(False)
self.setModal(True)
self.mainVLayout = QVBoxLayout(self)
2020-07-20 01:10:06 +00:00
self.tabwidget = QTabWidget()
self.page_general = QWidget()
self.page_display = QWidget()
self.widgetsVLayout = QVBoxLayout()
2020-07-20 01:10:06 +00:00
self.page_general.setLayout(self.widgetsVLayout)
self.displayVLayout = QVBoxLayout()
self.displayVLayout.setSpacing(5) # arbitrary value, might conflict with style
2020-07-20 01:10:06 +00:00
self.page_display.setLayout(self.displayVLayout)
self._setupPreferenceWidgets()
self._setupDisplayPage()
2020-07-20 01:10:06 +00:00
# self.mainVLayout.addLayout(self.widgetsVLayout)
self.buttonBox = QDialogButtonBox(self)
self.buttonBox.setStandardButtons(
QDialogButtonBox.Cancel
| QDialogButtonBox.Ok
| QDialogButtonBox.RestoreDefaults
)
2020-07-20 01:10:06 +00:00
self.mainVLayout.addWidget(self.tabwidget)
self.mainVLayout.addWidget(self.buttonBox)
self.layout().setSizeConstraint(QLayout.SetFixedSize)
2020-07-20 01:10:06 +00:00
self.tabwidget.addTab(self.page_general, "General")
self.tabwidget.addTab(self.page_display, "Display")
self.displayVLayout.addStretch(0)
self.widgetsVLayout.addStretch(0)
def _load(self, prefs, setchecked, section):
# Edition-specific
pass
def _save(self, prefs, ischecked):
# Edition-specific
pass
def load(self, prefs=None, section=Sections.ALL):
if prefs is None:
prefs = self.app.prefs
setchecked = lambda cb, b: cb.setCheckState(Qt.Checked if b else Qt.Unchecked)
if section & Sections.GENERAL:
self.filterHardnessSlider.setValue(prefs.filter_hardness)
self.filterHardnessLabel.setNum(prefs.filter_hardness)
setchecked(self.mixFileKindBox, prefs.mix_file_kind)
setchecked(self.useRegexpBox, prefs.use_regexp)
setchecked(self.removeEmptyFoldersBox, prefs.remove_empty_folders)
setchecked(self.ignoreHardlinkMatches, prefs.ignore_hardlink_matches)
setchecked(self.debugModeBox, prefs.debug_mode)
self.copyMoveDestinationComboBox.setCurrentIndex(prefs.destination_type)
self.customCommandEdit.setText(prefs.custom_command)
if section & Sections.DISPLAY:
setchecked(self.reference_bold_font, prefs.reference_bold_font)
setchecked(self.tabs_default_pos, prefs.tabs_default_pos)
setchecked(self.details_dialog_titlebar_enabled,
prefs.details_dialog_titlebar_enabled)
setchecked(self.details_dialog_vertical_titlebar,
prefs.details_dialog_vertical_titlebar)
self.fontSizeSpinBox.setValue(prefs.tableFontSize)
self.details_table_delta_foreground_color.setColor(
prefs.details_table_delta_foreground_color)
self.result_table_ref_foreground_color.setColor(
prefs.result_table_ref_foreground_color)
self.result_table_delta_foreground_color.setColor(
prefs.result_table_delta_foreground_color)
try:
langindex = self.supportedLanguages.index(self.app.prefs.language)
except ValueError:
langindex = 0
self.languageComboBox.setCurrentIndex(langindex)
self._load(prefs, setchecked, section)
def save(self):
prefs = self.app.prefs
prefs.filter_hardness = self.filterHardnessSlider.value()
ischecked = lambda cb: cb.checkState() == Qt.Checked
prefs.mix_file_kind = ischecked(self.mixFileKindBox)
prefs.use_regexp = ischecked(self.useRegexpBox)
prefs.remove_empty_folders = ischecked(self.removeEmptyFoldersBox)
prefs.ignore_hardlink_matches = ischecked(self.ignoreHardlinkMatches)
prefs.debug_mode = ischecked(self.debugModeBox)
prefs.reference_bold_font = ischecked(self.reference_bold_font)
Squashed commit of the following: commit ac941037ff51158b64daa65c244df26346af10cf Author: glubsy <glubsy@users.noreply.github.com> Date: Thu Jul 16 22:21:24 2020 +0200 Fix resize of top frame not updating scaled pixmap * Also limit viewing features such as zoom levels when files have different dimensions * GraphicsViewImageViewer is still a bit buggy: the scrollbars are toggled on when the pixmap is null in the reference viewer (we do not use that class right anyway) commit 733b3b0ed4fbd6de908c968402af03879df3336f Author: glubsy <glubsy@users.noreply.github.com> Date: Thu Jul 16 01:31:24 2020 +0200 Prevent zoom for images of differing dimensions * If images are not the same size, prevent zooming features from being used by disabling the normal size button, only enable swap commit 9168d72f38faaf0a12230cd544f14190cd29fca4 Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 22:47:32 2020 +0200 Update preferences on show(), not in constructor * If the dialog window shouldn't have a titlebar during construction, update accordingly only when showing to fix Windows displaying a window without titlebar on first show * Only save geometry if the window is floating. Otherwise geometry while docked is saved whih gives weird results on subsequent starts, since it may be floating by default anyway (at least on Linux where titlebar being disabled is allowed while floating) * Vertical title bar doesn't seem to work on Windows, add note in preferences dialog commit 75621cc816120597f493e0debc6d88e2e0bbd30a Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 22:04:19 2020 +0200 Prevent Windows from floating if no decoration * Windows users cannot move a window which has no native decorations. Toggling a dock widget's titlebar off also removes native decorations on a floating window. Until we implement a replacement titlebar by overriding paintEvents, simply force the floating window to go back to docked state after we toggled the titlebar off. commit 3c816b2f11ddc66a78cdc6327ee102df46d1a552 Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 21:43:01 2020 +0200 Fix computing and setting offset to 0 for tableview commit 85d6e05cd406b999e8f6ae421a9746a0c9f146bb Merge: 66127d02 3eddeb6a Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 21:25:44 2020 +0200 Merge branch 'dockable_windows' into details_dialog_improvements_dev commit 66127d025e9a497ee13126f955166946acdb35a8 Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 20:22:13 2020 +0200 Add credit for icons used, upscale exchange icon * Jason Cho gave his express permission to use the icon (it was made 10 years ago and he doesn't have the source files anymore) * Used waifu2x to upscale the icon * Used GIMP to draw dark outline around the icon * Source files are included commit 58c675d1fa90a7247233d9887a460cf5a8e4cbf5 Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 05:25:47 2020 +0200 Add custom icons * Use custom icons on platforms which do not provide theme * Old zoom icons credits to "schollidesign" from icon pack Office and Entertainment (GPL licence). * Exchange icon credit to Jason Cho (Unknown license). * Use hack to resize viewers on first show() as well commit 95b8406c7b97aab170d127b466ff506b724def3c Author: glubsy <glubsy@users.noreply.github.com> Date: Wed Jul 15 04:14:24 2020 +0200 Fix scrollbar displayed while splitter maxed out * For some reason the table's height is a few pixel longer on Windows so we work around the issue by adding a small offset to the maximum height hint. * No idea about MacOS yet but this might need the same treatment. commit 3eddeb6aebc99126e62eb05af60333ba3bd22e82 Author: glubsy <glubsy@users.noreply.github.com> Date: Tue Jul 14 17:37:48 2020 +0200 Fix ME/SE details dialogs, add preferences * Fix ME and SE versions of details dialog not displaying their content properly after change to QDockWidget * Add option to toggle titlebar and orientation of titlebar in preferences dialog * Fix setting layout on PE details dialog window while layout already set, by removing the self (parent) reference in constructing the QSplitter commit 56912a71084415eac2f447650279d833d9857686 Author: glubsy <glubsy@users.noreply.github.com> Date: Mon Jul 13 05:06:04 2020 +0200 Make details dialog dockable
2020-07-16 20:31:54 +00:00
prefs.details_dialog_titlebar_enabled = ischecked(self.details_dialog_titlebar_enabled)
prefs.details_dialog_vertical_titlebar = ischecked(self.details_dialog_vertical_titlebar)
prefs.details_table_delta_foreground_color = self.details_table_delta_foreground_color.color
prefs.result_table_ref_foreground_color = self.result_table_ref_foreground_color.color
prefs.result_table_delta_foreground_color = self.result_table_delta_foreground_color.color
prefs.destination_type = self.copyMoveDestinationComboBox.currentIndex()
prefs.custom_command = str(self.customCommandEdit.text())
2011-09-23 14:29:25 +00:00
prefs.tableFontSize = self.fontSizeSpinBox.value()
prefs.tabs_default_pos = ischecked(self.tabs_default_pos)
lang = self.supportedLanguages[self.languageComboBox.currentIndex()]
oldlang = self.app.prefs.language
if oldlang not in self.supportedLanguages:
oldlang = "en"
if lang != oldlang:
QMessageBox.information(
self,
"",
tr("dupeGuru has to restart for language changes to take effect."),
)
self.app.prefs.language = lang
self._save(prefs, ischecked)
def resetToDefaults(self, section_to_update):
self.load(Preferences(), section_to_update)
2016-06-01 01:22:50 +00:00
# --- Events
def buttonClicked(self, button):
role = self.buttonBox.buttonRole(button)
if role == QDialogButtonBox.ResetRole:
current_tab = self.tabwidget.currentWidget()
section_to_update = Sections.ALL
if current_tab is self.page_general:
section_to_update = Sections.GENERAL
if current_tab is self.page_display:
section_to_update = Sections.DISPLAY
self.resetToDefaults(section_to_update)
class ColorPickerButton(QPushButton):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.color = None
self.clicked.connect(self.onClicked)
@pyqtSlot()
def onClicked(self):
color = QColorDialog.getColor(
self.color if self.color is not None else Qt.white,
self.parent)
self.setColor(color)
def setColor(self, color):
size = QSize(16, 16)
px = QPixmap(size)
if color is None:
size.width = 0
size.height = 0
elif not color.isValid():
return
else:
self.color = color
px.fill(color)
self.setIcon(QIcon(px))