Cleanup welcome screen recent databases view (#4822)

* Press "Delete" to remove recent database
* Refresh recent databases on becoming visible
This commit is contained in:
JMcKiern 2020-06-06 13:26:46 +01:00 committed by GitHub
parent 712b8224c6
commit 6c9125402a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View File

@ -40,11 +40,6 @@ WelcomeWidget::WelcomeWidget(QWidget* parent)
refreshLastDatabases();
bool recent_visibility = (m_ui->recentListWidget->count() > 0);
m_ui->startLabel->setVisible(!recent_visibility);
m_ui->recentListWidget->setVisible(recent_visibility);
m_ui->recentLabel->setVisible(recent_visibility);
connect(m_ui->buttonNewDatabase, SIGNAL(clicked()), SIGNAL(newDatabase()));
connect(m_ui->buttonOpenDatabase, SIGNAL(clicked()), SIGNAL(openDatabase()));
connect(m_ui->buttonImportKeePass1, SIGNAL(clicked()), SIGNAL(importKeePass1Database()));
@ -62,12 +57,26 @@ WelcomeWidget::~WelcomeWidget()
void WelcomeWidget::openDatabaseFromFile(QListWidgetItem* item)
{
if (item->text().isEmpty()) {
if (!item || item->text().isEmpty()) {
return;
}
emit openDatabaseFile(item->text());
}
void WelcomeWidget::removeFromLastDatabases(QListWidgetItem* item)
{
if (!item || item->text().isEmpty()) {
return;
}
if (config()->get(Config::RememberLastDatabases).toBool()) {
QStringList lastDatabases = config()->get(Config::LastDatabases).toStringList();
lastDatabases.removeOne(item->text());
config()->set(Config::LastDatabases, lastDatabases);
}
refreshLastDatabases();
}
void WelcomeWidget::refreshLastDatabases()
{
m_ui->recentListWidget->clear();
@ -77,13 +86,28 @@ void WelcomeWidget::refreshLastDatabases()
itm->setText(database);
m_ui->recentListWidget->addItem(itm);
}
bool recent_visibility = (m_ui->recentListWidget->count() > 0);
m_ui->startLabel->setVisible(!recent_visibility);
m_ui->recentListWidget->setVisible(recent_visibility);
m_ui->recentLabel->setVisible(recent_visibility);
}
void WelcomeWidget::keyPressEvent(QKeyEvent* event)
{
if (m_ui->recentListWidget->hasFocus() && (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)) {
if (m_ui->recentListWidget->hasFocus()) {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
openDatabaseFromFile(m_ui->recentListWidget->currentItem());
} else if (event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace) {
removeFromLastDatabases(m_ui->recentListWidget->currentItem());
}
}
QWidget::keyPressEvent(event);
}
void WelcomeWidget::showEvent(QShowEvent* event)
{
refreshLastDatabases();
QWidget::showEvent(event);
}

View File

@ -46,12 +46,14 @@ signals:
protected:
void keyPressEvent(QKeyEvent* event) override;
void showEvent(QShowEvent* event) override;
private slots:
void openDatabaseFromFile(QListWidgetItem* item);
private:
const QScopedPointer<Ui::WelcomeWidget> m_ui;
void removeFromLastDatabases(QListWidgetItem* item);
};
#endif // KEEPASSX_WELCOMEWIDGET_H