Add option to display passwords in color in preview panel

Closes #4099

* Fixed bug in Application that did not set the dark theme flag when the theme was changed from dark to light.
This commit is contained in:
Wolfram Rösler 2022-10-02 09:44:50 -04:00 committed by Jonathan White
parent 54f9b25b52
commit b1e7c34b82
8 changed files with 68 additions and 13 deletions

View File

@ -503,6 +503,10 @@
<source> recent files</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Show passwords in color</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ApplicationSettingsWidgetSecurity</name>

View File

@ -105,6 +105,7 @@ static const QHash<Config::ConfigKey, ConfigDirective> configStrings = {
{Config::GUI_HideUsernames, {QS("GUI/HideUsernames"), Roaming, false}},
{Config::GUI_HidePasswords, {QS("GUI/HidePasswords"), Roaming, true}},
{Config::GUI_AdvancedSettings, {QS("GUI/AdvancedSettings"), Roaming, false}},
{Config::GUI_ColorPasswords, {QS("GUI/ColorPasswords"), Roaming, false}},
{Config::GUI_MonospaceNotes, {QS("GUI/MonospaceNotes"), Roaming, false}},
{Config::GUI_ApplicationTheme, {QS("GUI/ApplicationTheme"), Roaming, QS("auto")}},
{Config::GUI_CompactMode, {QS("GUI/CompactMode"), Roaming, false}},

View File

@ -86,6 +86,7 @@ public:
GUI_HideUsernames,
GUI_HidePasswords,
GUI_AdvancedSettings,
GUI_ColorPasswords,
GUI_MonospaceNotes,
GUI_ApplicationTheme,
GUI_CompactMode,

View File

@ -183,6 +183,7 @@ void Application::applyTheme()
auto* s = new LightStyle;
setPalette(s->standardPalette());
setStyle(s);
m_darkTheme = false;
} else if (appTheme == "dark") {
auto* s = new DarkStyle;
setPalette(s->standardPalette());
@ -191,7 +192,9 @@ void Application::applyTheme()
} else {
// Classic mode, don't check for dark theme on Windows
// because Qt 5.x does not support it
#ifndef Q_OS_WIN
#if defined(Q_OS_WIN)
m_darkTheme = false;
#else
m_darkTheme = osUtils->isDarkMode();
#endif
QFile stylesheetFile(":/styles/base/classicstyle.qss");

View File

@ -239,6 +239,7 @@ void ApplicationSettingsWidget::loadSettings()
m_generalUi->toolbarMovableCheckBox->setChecked(config()->get(Config::GUI_MovableToolbar).toBool());
m_generalUi->monospaceNotesCheckBox->setChecked(config()->get(Config::GUI_MonospaceNotes).toBool());
m_generalUi->colorPasswordsCheckBox->setChecked(config()->get(Config::GUI_ColorPasswords).toBool());
m_generalUi->toolButtonStyleComboBox->clear();
m_generalUi->toolButtonStyleComboBox->addItem(tr("Icon only"), Qt::ToolButtonIconOnly);
@ -383,6 +384,7 @@ void ApplicationSettingsWidget::saveSettings()
config()->set(Config::GUI_MovableToolbar, m_generalUi->toolbarMovableCheckBox->isChecked());
config()->set(Config::GUI_MonospaceNotes, m_generalUi->monospaceNotesCheckBox->isChecked());
config()->set(Config::GUI_ColorPasswords, m_generalUi->colorPasswordsCheckBox->isChecked());
config()->set(Config::GUI_ToolButtonStyle, m_generalUi->toolButtonStyleComboBox->currentData().toString());

View File

@ -766,6 +766,13 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="colorPasswordsCheckBox">
<property name="text">
<string>Show passwords in color</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="monospaceNotesCheckBox">
<property name="text">

View File

@ -19,6 +19,8 @@
#include "EntryPreviewWidget.h"
#include "ui_EntryPreviewWidget.h"
#include "Application.h"
#include "core/Config.h"
#include "gui/Clipboard.h"
#include "gui/Font.h"
#include "gui/Icons.h"
@ -232,14 +234,31 @@ void EntryPreviewWidget::setPasswordVisible(bool state)
{
const QString password = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password());
if (state) {
m_ui->entryPasswordLabel->setText(password);
m_ui->entryPasswordLabel->setCursorPosition(0);
m_ui->entryPasswordLabel->setFont(Font::fixedFont());
if (config()->get(Config::GUI_ColorPasswords).toBool()) {
// Show the password in color
// clang-format off
QString html;
const auto dark = kpxcApp->isDarkTheme();
for (const auto c : password) {
const auto color = c.isDigit() ? (dark ? "lightblue" : "blue")
: c.isUpper() ? (dark ? "lightgreen" : "darkgreen")
: c.isLower() ? (dark ? "yellow" : "red")
: (dark ? "white" : "black");
html += "<span style=\"color: " + QString(color) + ";\">" + QString(c).toHtmlEscaped() + "</span>";
}
// clang-format on
m_ui->entryPasswordLabel->setHtml(html);
} else {
// No color
m_ui->entryPasswordLabel->setPlainText(password.toHtmlEscaped());
}
} else if (password.isEmpty() && !config()->get(Config::Security_PasswordEmptyPlaceholder).toBool()) {
m_ui->entryPasswordLabel->setText("");
m_ui->entryPasswordLabel->setPlainText("");
} else {
m_ui->entryPasswordLabel->setText(QString("\u25cf").repeated(6));
m_ui->entryPasswordLabel->setPlainText(QString("\u25cf").repeated(6));
}
m_ui->entryPasswordLabel->setFont(Font::fixedFont());
m_ui->togglePasswordButton->setIcon(icons()->onOffIcon("password-show", state));
}
@ -280,7 +299,7 @@ void EntryPreviewWidget::updateEntryGeneralTab()
// Hide password
setPasswordVisible(false);
// Show the password toggle button if there are dots in the label
m_ui->togglePasswordButton->setVisible(!m_ui->entryPasswordLabel->text().isEmpty());
m_ui->togglePasswordButton->setVisible(!m_currentEntry->password().isEmpty());
m_ui->togglePasswordButton->setChecked(false);
} else {
// Show password

View File

@ -315,24 +315,42 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="entryPasswordLabel">
<widget class="QTextEdit" name="entryPasswordLabel">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>30</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="text">
<string notr="true">password</string>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frame">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="tabChangesFocus">
<bool>true</bool>
</property>
<property name="undoRedoEnabled">
<bool>false</bool>
</property>
<property name="dragEnabled">
<bool>true</bool>
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>