File indexing completed on 2024-04-28 15:39:50

0001 // SPDX-FileCopyrightText: 2006-2007 Tuomas Suutari <tuomas@nepnep.net>
0002 // SPDX-FileCopyrightText: 2006-2012 Jesper K. Pedersen <jesper.pedersen@kdab.com>
0003 // SPDX-FileCopyrightText: 2007 Dirk Mueller <mueller@kde.org>
0004 // SPDX-FileCopyrightText: 2009 Andrew Coles <andrew.i.coles@googlemail.com>
0005 // SPDX-FileCopyrightText: 2010 Jan Kundrát <jkt@flaska.net>
0006 // SPDX-FileCopyrightText: 2010 Miika Turkia <miika.turkia@gmail.com>
0007 // SPDX-FileCopyrightText: 2012-2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0008 // SPDX-FileCopyrightText: 2015 Yuri Chornoivan <yurchor@ukr.net>
0009 // SPDX-FileCopyrightText: 2015-2020 Tobias Leupold <tl@stonemx.de>
0010 // SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0011 //
0012 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0013 
0014 // Local includes
0015 
0016 #include "CheckDropItem.h"
0017 #include "DragItemInfo.h"
0018 #include "DragableTreeWidget.h"
0019 
0020 #include "DB/Category.h"
0021 #include "DB/CategoryItem.h"
0022 #include "DB/ImageDB.h"
0023 #include "DB/MemberMap.h"
0024 
0025 // KDE includes
0026 #include <KLocalizedString>
0027 #include <KMessageBox>
0028 
0029 // Qt includes
0030 #include <QDropEvent>
0031 
0032 CategoryListView::CheckDropItem::CheckDropItem(DragableTreeWidget *parent, const QString &column1,
0033                                                const QString &column2)
0034     : QTreeWidgetItem(parent)
0035     , m_listView(parent)
0036 {
0037     setCheckState(0, Qt::Unchecked);
0038     setText(0, column1);
0039     setText(1, column2);
0040 }
0041 
0042 CategoryListView::CheckDropItem::CheckDropItem(DragableTreeWidget *listView, QTreeWidgetItem *parent, const QString &column1,
0043                                                const QString &column2)
0044     : QTreeWidgetItem(parent)
0045     , m_listView(listView)
0046 {
0047     setCheckState(0, Qt::Unchecked);
0048     setText(0, column1);
0049     setText(1, column2);
0050 }
0051 
0052 CategoryListView::DragItemInfoSet CategoryListView::CheckDropItem::extractData(const QMimeData *data) const
0053 {
0054     DragItemInfoSet items;
0055     QByteArray array = data->data(QString::fromUtf8("x-kphotoalbum/x-categorydrag"));
0056     QDataStream stream(array);
0057     stream >> items;
0058 
0059     return items;
0060 }
0061 
0062 bool CategoryListView::CheckDropItem::dataDropped(const QMimeData *data)
0063 {
0064     const DragItemInfoSet items = extractData(data);
0065     const QString newParent = text(0);
0066     if (!verifyDropWasIntended(newParent, items))
0067         return false;
0068 
0069     DB::MemberMap &memberMap = DB::ImageDB::instance()->memberMap();
0070     memberMap.addGroup(m_listView->category()->name(), newParent);
0071 
0072     for (const auto &item : items) {
0073         const QString oldParent = item.parent();
0074         const QString child = item.child();
0075 
0076         memberMap.addMemberToGroup(m_listView->category()->name(), newParent, child);
0077         memberMap.removeMemberFromGroup(m_listView->category()->name(), oldParent, child);
0078     }
0079 
0080     // DB::ImageDB::instance()->setMemberMap( memberMap );
0081 
0082     m_listView->emitItemsChanged();
0083     return true;
0084 }
0085 
0086 bool CategoryListView::CheckDropItem::isSelfDrop(const QMimeData *data) const
0087 {
0088     const QString thisCategory = text(0);
0089     const DragItemInfoSet children = extractData(data);
0090     const DB::CategoryItemPtr categoryInfo = m_listView->category()->itemsCategories();
0091 
0092     for (DragItemInfoSet::const_iterator childIt = children.begin(); childIt != children.end(); ++childIt) {
0093         if (thisCategory == (*childIt).child() || categoryInfo->isDescendentOf(thisCategory, (*childIt).child()))
0094             return true;
0095     }
0096     return false;
0097 }
0098 
0099 void CategoryListView::CheckDropItem::setTristate(bool b)
0100 {
0101     if (b)
0102         setFlags(flags() | Qt::ItemIsAutoTristate);
0103     else
0104         setFlags(flags() & ~Qt::ItemIsAutoTristate);
0105 }
0106 
0107 bool CategoryListView::CheckDropItem::verifyDropWasIntended(const QString &parent, const DragItemInfoSet &items)
0108 {
0109     QStringList children;
0110     for (DragItemInfoSet::const_iterator itemIt = items.begin(); itemIt != items.end(); ++itemIt) {
0111         children.append((*itemIt).child());
0112     }
0113 
0114     QString allChildren;
0115     if (children.size() == 1) {
0116         allChildren = children[0];
0117     } else if (children.size() == 2) {
0118         allChildren = i18n("\"%1\" and \"%2\"", children[0], children[1]);
0119     } else {
0120         for (int i = 0; i < children.size() - 1; i++) {
0121             if (i == 0) {
0122                 allChildren += i18n("\"%1\"", children[i]);
0123             } else {
0124                 allChildren += i18n(", \"%1\"", children[i]);
0125             }
0126         }
0127         allChildren += i18n(" and \"%1\"", children[children.size() - 1]);
0128     }
0129 
0130     const QString msg = i18np(
0131         "<p>"
0132         "You have just dragged an item onto another. This will make the target item a tag group "
0133         "and define the dragged item as a member of this group. "
0134         "Tag groups may be used to denote facts such as 'Las Vegas is in the USA'. In that example "
0135         "you would drag Las Vegas onto USA. "
0136         "When you have set up tag groups, you can, for instance, see all images from the USA by "
0137         "simply selecting that item in the Browser."
0138         "</p>"
0139         "<p>"
0140         "Was it really your intention to make \"%3\" a tag group and add \"%2\" as a member?"
0141         "</p>",
0142 
0143         "<p>"
0144         "You have just dragged some items onto one other item. This will make the target item a "
0145         "tag group and define the dragged items as members of this group. "
0146         "Tag groups may be used to denote facts such as 'Las Vegas and New York are in the USA'. "
0147         "In that example you would drag Las Vegas and New York onto USA. "
0148         "When you have set up tag groups, you can, for instance, see all images from the USA by "
0149         "simply selecting that item in the Browser."
0150         "</p>"
0151         "<p>"
0152         "Was it really your intention to make \"%3\" a tag group and add %2 as members?"
0153         "</p>",
0154 
0155         children.size(), allChildren, parent);
0156 
0157     const int answer = KMessageBox::warningContinueCancel(nullptr, msg, i18n("Move Items"), KStandardGuiItem::cont(),
0158                                                           KStandardGuiItem::cancel(),
0159                                                           QString::fromLatin1("DoYouReallyWantToMessWithMemberGroups"));
0160     return answer == KMessageBox::Continue;
0161 }
0162 
0163 void CategoryListView::CheckDropItem::setDNDEnabled(const bool b)
0164 {
0165     if (b)
0166         setFlags(Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | flags());
0167     else
0168         setFlags(flags() & ~Qt::ItemIsDragEnabled & ~Qt::ItemIsDropEnabled);
0169 }
0170 // vi:expandtab:tabstop=4 shiftwidth=4: