From 1cb64f0c9f92f5e1bcf1864d065aa9d4273570b5 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Tue, 15 May 2012 13:02:05 +0200 Subject: [PATCH] Make icons edit ui a widget. --- src/CMakeLists.txt | 2 + src/gui/EditWidgetIcons.cpp | 231 ++++++++++++++++++++++++++++++++++++ src/gui/EditWidgetIcons.h | 70 +++++++++++ 3 files changed, 303 insertions(+) create mode 100644 src/gui/EditWidgetIcons.cpp create mode 100644 src/gui/EditWidgetIcons.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5cb6d7b83..7bed6499f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -61,6 +61,7 @@ set(keepassx_SOURCES gui/EditEntryWidget.cpp gui/EditGroupWidget.cpp gui/EditWidget.cpp + gui/EditWidgetIcons.cpp gui/EntryAttachmentsModel.cpp gui/EntryAttributesModel.cpp gui/EntryModel.cpp @@ -98,6 +99,7 @@ set(keepassx_MOC gui/EditEntryWidget.h gui/EditGroupWidget.h gui/EditWidget.h + gui/EditWidgetIcons.h gui/EntryAttachmentsModel.h gui/EntryAttributesModel.h gui/EntryModel.h diff --git a/src/gui/EditWidgetIcons.cpp b/src/gui/EditWidgetIcons.cpp new file mode 100644 index 000000000..b9836e1a2 --- /dev/null +++ b/src/gui/EditWidgetIcons.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "EditWidgetIcons.h" +#include "ui_EditWidgetIcons.h" + +#include +#include + +#include "core/Group.h" +#include "core/Metadata.h" +#include "core/Tools.h" +#include "gui/IconModels.h" + +IconStruct::IconStruct() + : uuid(Uuid()) + , number(0) +{ +} + +EditWidgetIcons::EditWidgetIcons(QWidget *parent) + : QWidget(parent) + , m_ui(new Ui::EditWidgetIcons()) +{ + m_ui->setupUi(this); + + m_defaultIconModel = new DefaultIconModel(this); + m_ui->defaultIconsView->setModel(m_defaultIconModel); + + m_customIconModel = new CustomIconModel(this); + m_ui->customIconsView->setModel(m_customIconModel); + + connect(m_ui->defaultIconsView, SIGNAL(clicked(QModelIndex)), + this, SLOT(updateRadioButtonDefaultIcons())); + connect(m_ui->customIconsView, SIGNAL(clicked(QModelIndex)), + this, SLOT(updateRadioButtonCustomIcons())); + connect(m_ui->defaultIconsRadio, SIGNAL(toggled(bool)), + this, SLOT(updateWidgetsDefaultIcons(bool))); + connect(m_ui->customIconsRadio, SIGNAL(toggled(bool)), + this, SLOT(updateWidgetsCustomIcons(bool))); + connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIcon())); + connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon())); +} + +EditWidgetIcons::~EditWidgetIcons() +{ +} + +IconStruct EditWidgetIcons::save() +{ + IconStruct iconStruct; + if (m_ui->defaultIconsRadio->isChecked()) { + QModelIndex index = m_ui->defaultIconsView->currentIndex(); + if (index.isValid()) { + iconStruct.number = index.row(); + } + else { + iconStruct.number = 0; + } + } + else { + QModelIndex index = m_ui->customIconsView->currentIndex(); + if (index.isValid()) { + iconStruct.uuid = m_customIconModel->uuidFromIndex(m_ui->customIconsView->currentIndex()); + } + else { + iconStruct.number = 0; + } + } + + return iconStruct; +} + +void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct iconStruct) +{ + m_database = database; + m_currentUuid = currentUuid; + + if (database) { + this->setEnabled(true); + + m_customIconModel->setIcons(database->metadata()->customIcons(), + database->metadata()->customIconsOrder()); + + Uuid iconUuid = iconStruct.uuid; + if (iconUuid.isNull()) { + int iconNumber = iconStruct.number; + m_ui->defaultIconsView->setCurrentIndex(m_defaultIconModel->index(iconNumber, 0)); + m_ui->defaultIconsRadio->setChecked(true); + } + else { + QModelIndex index = m_customIconModel->indexFromUuid(iconUuid); + if (index.isValid()) { + m_ui->customIconsView->setCurrentIndex(index); + m_ui->customIconsRadio->setChecked(true); + } + else { + m_ui->defaultIconsView->setCurrentIndex(m_defaultIconModel->index(0, 0)); + m_ui->defaultIconsRadio->setChecked(true); + } + } + } + else { + this->setEnabled(false); + } +} + +void EditWidgetIcons::addCustomIcon() +{ + if (m_database) { + QString filter = QString("%1 (%2);;%3 (*.*)").arg(tr("Images"), + Tools::imageReaderFilter(), tr("All files")); + + QString filename = QFileDialog::getOpenFileName( + this, tr("Select Image"), "", filter); + if (!filename.isEmpty()) { + QImage image(filename); + if (!image.isNull()) { + Uuid uuid = Uuid::random(); + m_database->metadata()->addCustomIcon(uuid, image.scaled(16, 16)); + m_customIconModel->setIcons(m_database->metadata()->customIcons(), + m_database->metadata()->customIconsOrder()); + QModelIndex index = m_customIconModel->indexFromUuid(uuid); + m_ui->customIconsView->setCurrentIndex(index); + } + else { + // TODO: show error + } + } + } +} + +void EditWidgetIcons::removeCustomIcon() +{ + if (m_database) { + QModelIndex index = m_ui->customIconsView->currentIndex(); + if (index.isValid()) { + Uuid iconUuid = m_customIconModel->uuidFromIndex(index); + int iconUsedCount = 0; + + QList allEntries = m_database->rootGroup()->entriesRecursive(false); + Q_FOREACH (const Entry* entry, allEntries) { + if (iconUuid == entry->iconUuid() && m_currentUuid != entry->uuid()) { + iconUsedCount++; + } + } + + QList allGroups = m_database->rootGroup()->groupsRecursive(true); + Q_FOREACH (const Group* group, allGroups) { + if (iconUuid == group->iconUuid() && m_currentUuid != group->uuid()) { + iconUsedCount++; + } + } + + if (iconUsedCount == 0) { + m_database->metadata()->removeCustomIcon(iconUuid); + // TODO update icons of history items + // with updateTimeinfo = false + m_customIconModel->setIcons(m_database->metadata()->customIcons(), + m_database->metadata()->customIconsOrder()); + if (m_customIconModel->rowCount() > 0) { + m_ui->customIconsView->setCurrentIndex(m_customIconModel->index(0, 0)); + } + else { + updateRadioButtonDefaultIcons(); + } + } + else { + QMessageBox::information(this, tr("Can't delete icon!"), + tr("Can't delete icon. Still used by %1 items.") + .arg(iconUsedCount)); + } + } + } +} + +void EditWidgetIcons::updateWidgetsDefaultIcons(bool check) +{ + if (check) { + QModelIndex index = m_ui->defaultIconsView->currentIndex(); + if (!index.isValid()) { + m_ui->defaultIconsView->setCurrentIndex(m_defaultIconModel->index(0, 0)); + } + else { + m_ui->defaultIconsView->setCurrentIndex(index); + } + m_ui->customIconsView->selectionModel()->clearSelection(); + m_ui->addButton->setEnabled(false); + m_ui->deleteButton->setEnabled(false); + } +} + +void EditWidgetIcons::updateWidgetsCustomIcons(bool check) +{ + if (check) { + QModelIndex index = m_ui->customIconsView->currentIndex(); + if (!index.isValid()) { + m_ui->customIconsView->setCurrentIndex(m_customIconModel->index(0, 0)); + } + else { + m_ui->customIconsView->setCurrentIndex(index); + } + m_ui->defaultIconsView->selectionModel()->clearSelection(); + m_ui->addButton->setEnabled(true); + m_ui->deleteButton->setEnabled(true); + } +} + +void EditWidgetIcons::updateRadioButtonDefaultIcons() +{ + m_ui->defaultIconsRadio->setChecked(true); +} + +void EditWidgetIcons::updateRadioButtonCustomIcons() +{ + m_ui->customIconsRadio->setChecked(true); +} diff --git a/src/gui/EditWidgetIcons.h b/src/gui/EditWidgetIcons.h new file mode 100644 index 000000000..4921c0cbc --- /dev/null +++ b/src/gui/EditWidgetIcons.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_EDITWIDGETICONS_H +#define KEEPASSX_EDITWIDGETICONS_H + +#include + +#include "core/Database.h" +#include "core/Entry.h" + +class DefaultIconModel; +class CustomIconModel; + +namespace Ui { + class EditWidgetIcons; +} + +struct IconStruct +{ + IconStruct(); + + Uuid uuid; + int number; +}; + +class EditWidgetIcons : public QWidget +{ + Q_OBJECT + +public: + explicit EditWidgetIcons(QWidget *parent = 0); + ~EditWidgetIcons(); + + IconStruct save(); + void load(Database* database, IconStruct iconStruct); + +private Q_SLOTS: + void addCustomIcon(); + void removeCustomIcon(); + void updateWidgetsDefaultIcons(bool checked); + void updateWidgetsCustomIcons(bool checked); + void updateRadioButtonDefaultIcons(); + void updateRadioButtonCustomIcons(); + +private: + const QScopedPointer m_ui; + Database* m_database; + Uuid m_currentUuid; + DefaultIconModel* m_defaultIconModel; + CustomIconModel* m_customIconModel; + + Q_DISABLE_COPY(EditWidgetIcons) +}; + +#endif // KEEPASSX_EDITWIDGETICONS_H