2012-05-15 07:02:05 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EditWidgetIcons.h"
|
|
|
|
#include "ui_EditWidgetIcons.h"
|
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QFileDialog>
|
2016-07-31 08:43:33 -04:00
|
|
|
#include <QImageReader>
|
2012-05-15 07:02:05 -04:00
|
|
|
|
|
|
|
#include "core/Group.h"
|
|
|
|
#include "core/Metadata.h"
|
|
|
|
#include "core/Tools.h"
|
|
|
|
#include "gui/IconModels.h"
|
2013-10-08 16:09:20 -04:00
|
|
|
#include "gui/MessageBox.h"
|
2012-05-15 07:02:05 -04:00
|
|
|
|
|
|
|
IconStruct::IconStruct()
|
|
|
|
: uuid(Uuid())
|
|
|
|
, number(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-15 12:51:45 -04:00
|
|
|
EditWidgetIcons::EditWidgetIcons(QWidget* parent)
|
2012-05-15 07:02:05 -04:00
|
|
|
: QWidget(parent)
|
2012-07-19 07:57:55 -04:00
|
|
|
, m_ui(new Ui::EditWidgetIcons())
|
2015-07-24 12:28:12 -04:00
|
|
|
, m_database(nullptr)
|
2012-07-19 07:57:55 -04:00
|
|
|
, m_defaultIconModel(new DefaultIconModel(this))
|
|
|
|
, m_customIconModel(new CustomIconModel(this))
|
2012-05-15 07:02:05 -04:00
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
|
|
|
m_ui->defaultIconsView->setModel(m_defaultIconModel);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-31 17:53:26 -04:00
|
|
|
IconStruct EditWidgetIcons::state() const
|
2012-05-15 07:02:05 -04:00
|
|
|
{
|
|
|
|
IconStruct iconStruct;
|
|
|
|
if (m_ui->defaultIconsRadio->isChecked()) {
|
|
|
|
QModelIndex index = m_ui->defaultIconsView->currentIndex();
|
|
|
|
if (index.isValid()) {
|
|
|
|
iconStruct.number = index.row();
|
|
|
|
}
|
2012-05-15 10:50:42 -04:00
|
|
|
else {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
QModelIndex index = m_ui->customIconsView->currentIndex();
|
|
|
|
if (index.isValid()) {
|
|
|
|
iconStruct.uuid = m_customIconModel->uuidFromIndex(m_ui->customIconsView->currentIndex());
|
|
|
|
}
|
2012-05-15 10:50:42 -04:00
|
|
|
else {
|
|
|
|
iconStruct.number = -1;
|
|
|
|
}
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
|
2016-07-31 17:53:26 -04:00
|
|
|
return iconStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditWidgetIcons::reset()
|
|
|
|
{
|
2015-07-24 12:28:12 -04:00
|
|
|
m_database = nullptr;
|
2012-05-15 10:50:42 -04:00
|
|
|
m_currentUuid = Uuid();
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct iconStruct)
|
|
|
|
{
|
2012-05-15 10:50:42 -04:00
|
|
|
Q_ASSERT(database);
|
|
|
|
Q_ASSERT(!currentUuid.isNull());
|
|
|
|
|
2012-05-15 07:02:05 -04:00
|
|
|
m_database = database;
|
|
|
|
m_currentUuid = currentUuid;
|
|
|
|
|
2016-01-24 13:03:50 -05:00
|
|
|
m_customIconModel->setIcons(database->metadata()->customIconsScaledPixmaps(),
|
2012-05-15 10:50:42 -04:00
|
|
|
database->metadata()->customIconsOrder());
|
2012-05-15 07:02:05 -04:00
|
|
|
|
2012-05-15 10:50:42 -04:00
|
|
|
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);
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-05-15 10:50:42 -04:00
|
|
|
m_ui->defaultIconsView->setCurrentIndex(m_defaultIconModel->index(0, 0));
|
|
|
|
m_ui->defaultIconsRadio->setChecked(true);
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditWidgetIcons::addCustomIcon()
|
|
|
|
{
|
|
|
|
if (m_database) {
|
2012-06-06 04:21:17 -04:00
|
|
|
QString filter = QString("%1 (%2);;%3 (*)").arg(tr("Images"),
|
2012-05-15 07:02:05 -04:00
|
|
|
Tools::imageReaderFilter(), tr("All files"));
|
|
|
|
|
|
|
|
QString filename = QFileDialog::getOpenFileName(
|
|
|
|
this, tr("Select Image"), "", filter);
|
|
|
|
if (!filename.isEmpty()) {
|
2016-07-31 08:43:33 -04:00
|
|
|
QImageReader imageReader(filename);
|
2016-07-31 09:36:29 -04:00
|
|
|
// detect from content, otherwise reading fails if file extension is wrong
|
|
|
|
imageReader.setDecideFormatFromContent(true);
|
2016-07-31 08:43:33 -04:00
|
|
|
QImage image = imageReader.read();
|
2012-05-15 07:02:05 -04:00
|
|
|
if (!image.isNull()) {
|
|
|
|
Uuid uuid = Uuid::random();
|
2015-03-31 16:31:04 -04:00
|
|
|
m_database->metadata()->addCustomIconScaled(uuid, image);
|
2016-01-24 13:03:50 -05:00
|
|
|
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
|
2012-05-15 07:02:05 -04:00
|
|
|
m_database->metadata()->customIconsOrder());
|
|
|
|
QModelIndex index = m_customIconModel->indexFromUuid(uuid);
|
|
|
|
m_ui->customIconsView->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
else {
|
2016-07-31 08:43:33 -04:00
|
|
|
MessageBox::critical(this, tr("Error"),
|
|
|
|
tr("Can't read icon:").append("\n").append(imageReader.errorString()));
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditWidgetIcons::removeCustomIcon()
|
|
|
|
{
|
|
|
|
if (m_database) {
|
|
|
|
QModelIndex index = m_ui->customIconsView->currentIndex();
|
|
|
|
if (index.isValid()) {
|
|
|
|
Uuid iconUuid = m_customIconModel->uuidFromIndex(index);
|
|
|
|
int iconUsedCount = 0;
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
const QList<Entry*> allEntries = m_database->rootGroup()->entriesRecursive(true);
|
2012-05-15 10:00:30 -04:00
|
|
|
QList<Entry*> historyEntriesWithSameIcon;
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
for (Entry* entry : allEntries) {
|
2012-05-15 10:00:30 -04:00
|
|
|
bool isHistoryEntry = !entry->group();
|
|
|
|
if (iconUuid == entry->iconUuid()) {
|
|
|
|
if (isHistoryEntry) {
|
|
|
|
historyEntriesWithSameIcon << entry;
|
|
|
|
}
|
|
|
|
else if (m_currentUuid != entry->uuid()) {
|
|
|
|
iconUsedCount++;
|
|
|
|
}
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 13:51:51 -04:00
|
|
|
const QList<Group*> allGroups = m_database->rootGroup()->groupsRecursive(true);
|
|
|
|
for (const Group* group : allGroups) {
|
2012-05-15 07:02:05 -04:00
|
|
|
if (iconUuid == group->iconUuid() && m_currentUuid != group->uuid()) {
|
|
|
|
iconUsedCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iconUsedCount == 0) {
|
2016-09-02 13:51:51 -04:00
|
|
|
for (Entry* entry : asConst(historyEntriesWithSameIcon)) {
|
2012-05-15 10:00:30 -04:00
|
|
|
entry->setUpdateTimeinfo(false);
|
|
|
|
entry->setIcon(0);
|
|
|
|
entry->setUpdateTimeinfo(true);
|
|
|
|
}
|
|
|
|
|
2012-05-15 07:02:05 -04:00
|
|
|
m_database->metadata()->removeCustomIcon(iconUuid);
|
2016-01-24 13:03:50 -05:00
|
|
|
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
|
2012-05-15 07:02:05 -04:00
|
|
|
m_database->metadata()->customIconsOrder());
|
|
|
|
if (m_customIconModel->rowCount() > 0) {
|
|
|
|
m_ui->customIconsView->setCurrentIndex(m_customIconModel->index(0, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
updateRadioButtonDefaultIcons();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-10-08 16:09:20 -04:00
|
|
|
MessageBox::information(this, tr("Can't delete icon!"),
|
2014-05-17 12:15:06 -04:00
|
|
|
tr("Can't delete icon. Still used by %n item(s).", 0, iconUsedCount));
|
2012-05-15 07:02:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|