1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "DocumentSelection.h"
23
24 #include <U2Core/AppContext.h>
25 #include <U2Core/ProjectModel.h>
26
27 namespace U2 {
28
29 static QList<Document *> emptyDocs;
DocumentSelection(QObject * p)30 DocumentSelection::DocumentSelection(QObject *p)
31 : GSelection(GSelectionTypes::DOCUMENTS, p) {
32 connect(this, SIGNAL(si_selectionChanged(DocumentSelection *, QList<Document *>, QList<Document *>)), SLOT(sl_selectionChanged()));
33 }
getSelectedDocuments() const34 const QList<Document *> &DocumentSelection::getSelectedDocuments() const {
35 return selectedDocs;
36 }
37
isEmpty() const38 bool DocumentSelection::isEmpty() const {
39 return selectedDocs.isEmpty();
40 }
41
contains(Document * doc) const42 bool DocumentSelection::contains(Document *doc) const {
43 return selectedDocs.contains(doc);
44 }
45
clear()46 void DocumentSelection::clear() {
47 QList<Document *> tmpRemoved = selectedDocs;
48 selectedDocs.clear();
49 if (!tmpRemoved.isEmpty()) {
50 emit si_selectionChanged(this, emptyDocs, tmpRemoved);
51 }
52 }
53
setSelection(const QList<Document * > & docs)54 void DocumentSelection::setSelection(const QList<Document *> &docs) {
55 if (docs.isEmpty()) {
56 clear();
57 return;
58 }
59 if (isEmpty()) {
60 addToSelection(docs);
61 return;
62 }
63
64 QList<Document *> addedDocuments;
65 QList<Document *> removedDocuments;
66
67 for (Document *document : qAsConst(docs)) {
68 if (!selectedDocs.contains(document)) {
69 addedDocuments.append(document);
70 }
71 }
72 for (Document *document : qAsConst(selectedDocs)) {
73 if (!docs.contains(document)) {
74 removedDocuments.append(document);
75 }
76 }
77 for (Document *document : qAsConst(removedDocuments)) {
78 selectedDocs.removeAll(document);
79 }
80 for (Document *document : qAsConst(addedDocuments)) {
81 selectedDocs.append(document);
82 }
83 if (!addedDocuments.isEmpty() || !removedDocuments.isEmpty()) {
84 emit si_selectionChanged(this, addedDocuments, removedDocuments);
85 }
86 }
87
addToSelection(const QList<Document * > & documentsToAdd)88 void DocumentSelection::addToSelection(const QList<Document *> &documentsToAdd) {
89 QList<Document *> addedDocuments;
90 int documentCountBefore = selectedDocs.size();
91 for (Document *document : qAsConst(documentsToAdd)) {
92 if (!selectedDocs.contains(document)) {
93 addedDocuments.append(document);
94 selectedDocs.append(document);
95 }
96 }
97 if (selectedDocs.size() != documentCountBefore) {
98 emit si_selectionChanged(this, addedDocuments, emptyDocs);
99 }
100 }
101
removeFromSelection(const QList<Document * > & documentsToRemove)102 void DocumentSelection::removeFromSelection(const QList<Document *> &documentsToRemove) {
103 QList<Document *> removedDocuments;
104 int documentCountBefore = selectedDocs.size();
105 for (Document *document : qAsConst(documentsToRemove)) {
106 if (selectedDocs.removeAll(document) != 0) {
107 removedDocuments.append(document);
108 }
109 }
110 if (selectedDocs.size() != documentCountBefore) {
111 emit si_selectionChanged(this, emptyDocs, removedDocuments);
112 }
113 }
114
115 } // namespace U2
116