|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show all saved logins. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot, QSortFilterProxyModel |
|
11 from PyQt6.QtGui import QFont, QFontMetrics |
|
12 from PyQt6.QtWidgets import QDialog |
|
13 |
|
14 from EricWidgets import EricMessageBox |
|
15 |
|
16 from .Ui_PasswordsDialog import Ui_PasswordsDialog |
|
17 |
|
18 |
|
19 class PasswordsDialog(QDialog, Ui_PasswordsDialog): |
|
20 """ |
|
21 Class implementing a dialog to show all saved logins. |
|
22 """ |
|
23 def __init__(self, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param parent reference to the parent widget (QWidget) |
|
28 """ |
|
29 super().__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 self.__showPasswordsText = self.tr("Show Passwords") |
|
33 self.__hidePasswordsText = self.tr("Hide Passwords") |
|
34 self.passwordsButton.setText(self.__showPasswordsText) |
|
35 |
|
36 self.removeButton.clicked.connect( |
|
37 self.passwordsTable.removeSelected) |
|
38 self.removeAllButton.clicked.connect(self.passwordsTable.removeAll) |
|
39 |
|
40 import WebBrowser.WebBrowserWindow |
|
41 from .PasswordModel import PasswordModel |
|
42 |
|
43 self.passwordsTable.verticalHeader().hide() |
|
44 self.__passwordModel = PasswordModel( |
|
45 WebBrowser.WebBrowserWindow.WebBrowserWindow.passwordManager(), |
|
46 self) |
|
47 self.__proxyModel = QSortFilterProxyModel(self) |
|
48 self.__proxyModel.setSourceModel(self.__passwordModel) |
|
49 self.searchEdit.textChanged.connect( |
|
50 self.__proxyModel.setFilterFixedString) |
|
51 self.passwordsTable.setModel(self.__proxyModel) |
|
52 |
|
53 fm = QFontMetrics(QFont()) |
|
54 height = fm.height() + fm.height() // 3 |
|
55 self.passwordsTable.verticalHeader().setDefaultSectionSize(height) |
|
56 self.passwordsTable.verticalHeader().setMinimumSectionSize(-1) |
|
57 |
|
58 self.__calculateHeaderSizes() |
|
59 |
|
60 def __calculateHeaderSizes(self): |
|
61 """ |
|
62 Private method to calculate the section sizes of the horizontal header. |
|
63 """ |
|
64 fm = QFontMetrics(QFont()) |
|
65 for section in range(self.__passwordModel.columnCount()): |
|
66 header = self.passwordsTable.horizontalHeader().sectionSizeHint( |
|
67 section) |
|
68 if section == 0: |
|
69 try: |
|
70 header = fm.horizontalAdvance("averagebiglongsitename") |
|
71 except AttributeError: |
|
72 header = fm.width("averagebiglongsitename") |
|
73 elif section == 1: |
|
74 try: |
|
75 header = fm.horizontalAdvance("averagelongusername") |
|
76 except AttributeError: |
|
77 header = fm.width("averagelongusername") |
|
78 elif section == 2: |
|
79 try: |
|
80 header = fm.horizontalAdvance("averagelongpassword") |
|
81 except AttributeError: |
|
82 header = fm.width("averagelongpassword") |
|
83 try: |
|
84 buffer = fm.horizontalAdvance("mm") |
|
85 except AttributeError: |
|
86 buffer = fm.width("mm") |
|
87 header += buffer |
|
88 self.passwordsTable.horizontalHeader().resizeSection( |
|
89 section, header) |
|
90 self.passwordsTable.horizontalHeader().setStretchLastSection(True) |
|
91 |
|
92 @pyqtSlot() |
|
93 def on_passwordsButton_clicked(self): |
|
94 """ |
|
95 Private slot to switch the password display mode. |
|
96 """ |
|
97 if self.__passwordModel.showPasswords(): |
|
98 self.__passwordModel.setShowPasswords(False) |
|
99 self.passwordsButton.setText(self.__showPasswordsText) |
|
100 else: |
|
101 res = EricMessageBox.yesNo( |
|
102 self, |
|
103 self.tr("Saved Passwords"), |
|
104 self.tr("""Do you really want to show passwords?""")) |
|
105 if res: |
|
106 self.__passwordModel.setShowPasswords(True) |
|
107 self.passwordsButton.setText(self.__hidePasswordsText) |
|
108 self.__calculateHeaderSizes() |