Skip to content

Commit

Permalink
Add database icon support
Browse files Browse the repository at this point in the history
  • Loading branch information
droidmonkey committed Oct 7, 2024
1 parent e46b810 commit 4382e18
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 15 deletions.
8 changes: 8 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,14 @@ removed from the database.</source>
<source>Clear</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Display icon:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Select Database Icon</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DatabaseSettingsWidgetKeeShare</name>
Expand Down
18 changes: 18 additions & 0 deletions src/core/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,24 @@ void Database::setPublicColor(const QString& color)
markAsModified();
}

int Database::publicIcon()
{
if (publicCustomData().contains("KPXC_PUBLIC_ICON")) {
return publicCustomData().value("KPXC_PUBLIC_ICON").toInt();
}
return -1;
}

void Database::setPublicIcon(int iconIndex)
{
if (iconIndex < 0) {
publicCustomData().remove("KPXC_PUBLIC_ICON");
} else {
publicCustomData().insert("KPXC_PUBLIC_ICON", iconIndex);
}
markAsModified();
}

void Database::markAsTemporaryDatabase()
{
m_isTemporaryDatabase = true;
Expand Down
2 changes: 2 additions & 0 deletions src/core/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class Database : public ModifiableObject
void setPublicName(const QString& name);
QString publicColor();
void setPublicColor(const QString& color);
int publicIcon();
void setPublicIcon(int iconIndex);

Metadata* metadata();
const Metadata* metadata() const;
Expand Down
12 changes: 12 additions & 0 deletions src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,31 @@ void DatabaseOpenWidget::load(const QString& filename)

m_ui->fileNameLabel->setRawText(m_filename);

// Set the public name if defined
auto label = tr("Unlock KeePassXC Database");
if (!m_db->publicName().isEmpty()) {
label.append(QString(": %1").arg(m_db->publicName()));
}
m_ui->labelHeadline->setText(label);

// Apply the public color to the central unlock stack if defined
auto color = m_db->publicColor();
if (!color.isEmpty()) {
m_ui->centralStack->setStyleSheet(QString("QStackedWidget {border: 4px solid %1}").arg(color));
} else {
m_ui->centralStack->setStyleSheet("");
}

// Show the database icon if defined
auto iconIndex = m_db->publicIcon();
if (iconIndex >= 0 && iconIndex < databaseIcons()->count()) {
m_ui->dbIconLabel->setPixmap(databaseIcons()->icon(iconIndex, IconSize::Large));
m_ui->dbIconLabel->setVisible(true);
} else {
m_ui->dbIconLabel->setPixmap({});
m_ui->dbIconLabel->setVisible(false);
}

if (config()->get(Config::RememberLastKeyFiles).toBool()) {
auto lastKeyFiles = config()->get(Config::LastKeyFiles).toHash();
if (lastKeyFiles.contains(m_filename)) {
Expand Down
16 changes: 16 additions & 0 deletions src/gui/DatabaseOpenWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@
<property name="spacing">
<number>9</number>
</property>
<item>
<widget class="QLabel" name="dbIconLabel">
<property name="minimumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelHeadline">
<property name="font">
Expand Down
88 changes: 77 additions & 11 deletions src/gui/dbsettings/DatabaseSettingsWidgetGeneral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
#include "ui_DatabaseSettingsWidgetGeneral.h"

#include <QColorDialog>
#include <QDialogButtonBox>
#include <QInputDialog>
#include <QListView>

#include "core/Clock.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "gui/DatabaseIcons.h"
#include "gui/IconModels.h"
#include "gui/MessageBox.h"

DatabaseSettingsWidgetGeneral::DatabaseSettingsWidgetGeneral(QWidget* parent)
Expand All @@ -31,8 +36,10 @@ DatabaseSettingsWidgetGeneral::DatabaseSettingsWidgetGeneral(QWidget* parent)
{
m_ui->setupUi(this);

connect(m_ui->dbPublicColorButton, &QPushButton::clicked, this, &DatabaseSettingsWidgetGeneral::pickColor);
connect(m_ui->dbPublicColorClearButton, &QPushButton::clicked, this, [this] { setupColorButton({}); });
connect(m_ui->dbPublicColorButton, &QPushButton::clicked, this, &DatabaseSettingsWidgetGeneral::pickPublicColor);
connect(m_ui->dbPublicColorClearButton, &QPushButton::clicked, this, [this] { setupPublicColorButton({}); });
connect(m_ui->dbPublicIconButton, &QPushButton::clicked, this, &DatabaseSettingsWidgetGeneral::pickPublicIcon);
connect(m_ui->dbPublicIconClearButton, &QPushButton::clicked, this, [this] { setupPublicIconButton(-1); });

connect(m_ui->historyMaxItemsCheckBox, SIGNAL(toggled(bool)), m_ui->historyMaxItemsSpinBox, SLOT(setEnabled(bool)));
connect(m_ui->historyMaxSizeCheckBox, SIGNAL(toggled(bool)), m_ui->historyMaxSizeSpinBox, SLOT(setEnabled(bool)));
Expand All @@ -52,7 +59,8 @@ void DatabaseSettingsWidgetGeneral::initialize()
m_ui->compressionCheckbox->setChecked(m_db->compressionAlgorithm() != Database::CompressionNone);

m_ui->dbPublicName->setText(m_db->publicName());
setupColorButton(m_db->publicColor());
setupPublicColorButton(m_db->publicColor());
setupPublicIconButton(m_db->publicIcon());

if (meta->historyMaxItems() > -1) {
m_ui->historyMaxItemsSpinBox->setValue(meta->historyMaxItems());
Expand Down Expand Up @@ -126,6 +134,7 @@ bool DatabaseSettingsWidgetGeneral::saveSettings()

m_db->setPublicName(m_ui->dbPublicName->text());
m_db->setPublicColor(m_ui->dbPublicColorButton->property("color").toString());
m_db->setPublicIcon(m_ui->dbPublicIconButton->property("iconIndex").toInt());

bool truncate = false;

Expand Down Expand Up @@ -167,21 +176,78 @@ bool DatabaseSettingsWidgetGeneral::saveSettings()
return true;
}

void DatabaseSettingsWidgetGeneral::pickColor()
void DatabaseSettingsWidgetGeneral::pickPublicColor()
{
auto oldColor = QColor(m_ui->dbPublicColorButton->property("color").toString());
setupColorButton(QColorDialog::getColor(oldColor));
auto newColor = QColorDialog::getColor(oldColor);
if (newColor.isValid()) {
setupPublicColorButton(newColor);
}
}

void DatabaseSettingsWidgetGeneral::setupColorButton(const QColor& color)
void DatabaseSettingsWidgetGeneral::setupPublicColorButton(const QColor& color)
{
m_ui->dbPublicColorClearButton->setVisible(color.isValid());
auto button = m_ui->dbPublicColorButton;
if (color.isValid()) {
button->setStyleSheet(QString("background-color:%1").arg(color.name()));
button->setProperty("color", color.name());
m_ui->dbPublicColorButton->setStyleSheet(QString("background-color:%1").arg(color.name()));
m_ui->dbPublicColorButton->setProperty("color", color.name());
} else {
m_ui->dbPublicColorButton->setStyleSheet("");
m_ui->dbPublicColorButton->setProperty("color", {});
}
}

void DatabaseSettingsWidgetGeneral::pickPublicIcon()
{
QDialog dialog(this);
dialog.setSizeGripEnabled(false);
dialog.setWindowTitle(tr("Select Database Icon"));

auto iconList = new QListView;
iconList->setFlow(QListView::LeftToRight);
iconList->setMovement(QListView::Static);
iconList->setResizeMode(QListView::Adjust);
iconList->setWrapping(true);
iconList->setSpacing(4);

auto iconModel = new DefaultIconModel;
iconList->setModel(iconModel);
if (m_ui->dbPublicIconButton->property("iconIndex").toInt() >= 0) {
iconList->setCurrentIndex(iconModel->index(m_ui->dbPublicIconButton->property("iconIndex").toInt(), 0));
} else {
iconList->setCurrentIndex(iconModel->index(0, 0));
}

auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
auto layout = new QVBoxLayout(&dialog);
layout->addWidget(iconList);
layout->addWidget(buttonBox);

// Resize the dialog to fit the default icon list
auto cellSize = iconList->sizeHintForIndex(iconModel->index(0, 0));
auto spacing = iconList->spacing() * 2;
dialog.resize((cellSize.width() + spacing) * 15, (cellSize.height() + spacing) * 6 + 16);

connect(iconList, &QListView::doubleClicked, &dialog, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
connect(
&dialog, &QDialog::accepted, this, [this, iconList] { setupPublicIconButton(iconList->currentIndex().row()); });

dialog.exec();
}

void DatabaseSettingsWidgetGeneral::setupPublicIconButton(int iconIndex)
{
auto valid = iconIndex >= 0 && iconIndex < databaseIcons()->count();
m_ui->dbPublicIconClearButton->setVisible(valid);
if (valid) {
m_ui->dbPublicIconButton->setIcon(databaseIcons()->icon(iconIndex));
m_ui->dbPublicIconButton->setProperty("iconIndex", iconIndex);
m_ui->dbPublicIconClearButton->setVisible(true);
} else {
button->setStyleSheet("");
button->setProperty("color", {});
m_ui->dbPublicIconButton->setIcon(QIcon());
m_ui->dbPublicIconButton->setProperty("iconIndex", -1);
m_ui->dbPublicIconClearButton->setVisible(false);
}
}
6 changes: 4 additions & 2 deletions src/gui/dbsettings/DatabaseSettingsWidgetGeneral.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public slots:
void showEvent(QShowEvent* event) override;

private slots:
void pickColor();
void setupColorButton(const QColor& color);
void pickPublicColor();
void setupPublicColorButton(const QColor& color);
void pickPublicIcon();
void setupPublicIconButton(int iconIndex);

private:
const QScopedPointer<Ui::DatabaseSettingsWidgetGeneral> m_ui;
Expand Down
54 changes: 52 additions & 2 deletions src/gui/dbsettings/DatabaseSettingsWidgetGeneral.ui
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
<widget class="QPushButton" name="dbPublicColorButton">
<property name="maximumSize">
<size>
<width>25</width>
<height>25</height>
<width>30</width>
<height>30</height>
</size>
</property>
<property name="toolTip">
Expand Down Expand Up @@ -182,6 +182,56 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="dbPublicIconLabel">
<property name="text">
<string>Display icon:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="dbPublicIconButton">
<property name="maximumSize">
<size>
<width>30</width>
<height>30</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>30</width>
<height>30</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="dbPublicIconClearButton">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
</layout>
Expand Down

0 comments on commit 4382e18

Please sign in to comment.