Merge pull request #2011 from keepassxreboot/fix/favicon-multiselect

Allow multiple image selections when adding favicons
This commit is contained in:
Jonathan White 2018-06-20 20:32:40 -04:00 committed by GitHub
commit f9eef6d986
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 23 deletions

View file

@ -125,8 +125,14 @@ void EditWidget::enableApplyButton(bool enabled)
void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type) void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type)
{ {
m_ui->messageWidget->setCloseButtonVisible(false); // Show error messages for a longer time to make sure the user can read them
m_ui->messageWidget->showMessage(text, type, 2000); if (type == MessageWidget::Error) {
m_ui->messageWidget->setCloseButtonVisible(true);
m_ui->messageWidget->showMessage(text, type, 15000);
} else {
m_ui->messageWidget->setCloseButtonVisible(false);
m_ui->messageWidget->showMessage(text, type, 2000);
}
} }
void EditWidget::hideMessage() void EditWidget::hideMessage()

View file

@ -92,6 +92,7 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent)
SIGNAL(widgetUpdated())); SIGNAL(widgetUpdated()));
m_ui->faviconButton->setVisible(false); m_ui->faviconButton->setVisible(false);
m_ui->addButton->setEnabled(true);
} }
EditWidgetIcons::~EditWidgetIcons() EditWidgetIcons::~EditWidgetIcons()
@ -268,7 +269,9 @@ void EditWidgetIcons::fetchFinished()
} }
if (!image.isNull()) { if (!image.isNull()) {
addCustomIcon(image); if (!addCustomIcon(image)) {
emit messageEditEntry(tr("Custom icon already exists"), MessageWidget::Information);
}
} else if (!m_urlsToTry.empty()) { } else if (!m_urlsToTry.empty()) {
m_redirects = 0; m_redirects = 0;
startFetchFavicon(m_urlsToTry.takeFirst()); startFetchFavicon(m_urlsToTry.takeFirst());
@ -324,35 +327,66 @@ void EditWidgetIcons::addCustomIconFromFile()
if (m_database) { if (m_database) {
QString filter = QString("%1 (%2);;%3 (*)").arg(tr("Images"), Tools::imageReaderFilter(), tr("All files")); QString filter = QString("%1 (%2);;%3 (*)").arg(tr("Images"), Tools::imageReaderFilter(), tr("All files"));
QString filename = QFileDialog::getOpenFileName(this, tr("Select Image"), "", filter); auto filenames = QFileDialog::getOpenFileNames(this, tr("Select Image(s)"), "", filter);
if (!filename.isEmpty()) { if (!filenames.empty()) {
auto icon = QImage(filename); QStringList errornames;
if (!icon.isNull()) { int numexisting = 0;
addCustomIcon(QImage(filename)); for (const auto& filename : filenames) {
if (!filename.isEmpty()) {
auto icon = QImage(filename);
if (icon.isNull()) {
errornames << filename;
} else if (!addCustomIcon(icon)) {
// Icon already exists in database
++numexisting;
}
}
}
int numloaded = filenames.size() - errornames.size() - numexisting;
QString msg;
if (numloaded > 0) {
msg = tr("Successfully loaded %1 of %n icon(s)", "", filenames.size()).arg(numloaded);
} else { } else {
emit messageEditEntry(tr("Can't read icon"), MessageWidget::Error); msg = tr("No icons were loaded");
}
if (numexisting > 0) {
msg += "\n" + tr("%n icon(s) already exist in the database", "", numexisting);
}
if (!errornames.empty()) {
// Show the first 8 icons that failed to load
errornames = errornames.mid(0, 8);
emit messageEditEntry(msg + "\n" + tr("The following icon(s) failed:", "", errornames.size()) +
"\n" + errornames.join("\n"), MessageWidget::Error);
} else if (numloaded > 0) {
emit messageEditEntry(msg, MessageWidget::Positive);
} else {
emit messageEditEntry(msg, MessageWidget::Information);
} }
} }
} }
} }
void EditWidgetIcons::addCustomIcon(const QImage& icon) bool EditWidgetIcons::addCustomIcon(const QImage& icon)
{ {
bool added = false;
if (m_database) { if (m_database) {
Uuid uuid = m_database->metadata()->findCustomIcon(icon); // Don't add an icon larger than 128x128, but retain original size if smaller
auto scaledicon = icon;
if (icon.width() > 128 || icon.height() > 128) {
scaledicon = icon.scaled(128, 128);
}
Uuid uuid = m_database->metadata()->findCustomIcon(scaledicon);
if (uuid.isNull()) { if (uuid.isNull()) {
uuid = Uuid::random(); uuid = Uuid::random();
// Don't add an icon larger than 128x128, but retain original size if smaller m_database->metadata()->addCustomIcon(uuid, scaledicon);
if (icon.width() > 128 || icon.height() > 128) {
m_database->metadata()->addCustomIcon(uuid, icon.scaled(128, 128));
} else {
m_database->metadata()->addCustomIcon(uuid, icon);
}
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(), m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder()); m_database->metadata()->customIconsOrder());
} else { added = true;
emit messageEditEntry(tr("Custom icon already exists"), MessageWidget::Information);
} }
// Select the new or existing icon // Select the new or existing icon
@ -362,6 +396,8 @@ void EditWidgetIcons::addCustomIcon(const QImage& icon)
emit widgetUpdated(); emit widgetUpdated();
} }
return added;
} }
void EditWidgetIcons::removeCustomIcon() void EditWidgetIcons::removeCustomIcon()
@ -458,7 +494,6 @@ void EditWidgetIcons::updateWidgetsDefaultIcons(bool check)
m_ui->defaultIconsView->setCurrentIndex(index); m_ui->defaultIconsView->setCurrentIndex(index);
} }
m_ui->customIconsView->selectionModel()->clearSelection(); m_ui->customIconsView->selectionModel()->clearSelection();
m_ui->addButton->setEnabled(false);
m_ui->deleteButton->setEnabled(false); m_ui->deleteButton->setEnabled(false);
} }
} }
@ -473,7 +508,6 @@ void EditWidgetIcons::updateWidgetsCustomIcons(bool check)
m_ui->customIconsView->setCurrentIndex(index); m_ui->customIconsView->setCurrentIndex(index);
} }
m_ui->defaultIconsView->selectionModel()->clearSelection(); m_ui->defaultIconsView->selectionModel()->clearSelection();
m_ui->addButton->setEnabled(true);
m_ui->deleteButton->setEnabled(true); m_ui->deleteButton->setEnabled(true);
} }
} }

View file

@ -88,7 +88,7 @@ private slots:
void fetchReadyRead(); void fetchReadyRead();
void fetchCanceled(); void fetchCanceled();
void addCustomIconFromFile(); void addCustomIconFromFile();
void addCustomIcon(const QImage& icon); bool addCustomIcon(const QImage& icon);
void removeCustomIcon(); void removeCustomIcon();
void updateWidgetsDefaultIcons(bool checked); void updateWidgetsDefaultIcons(bool checked);
void updateWidgetsCustomIcons(bool checked); void updateWidgetsCustomIcons(bool checked);