1 /*
2 * This file is part of Converseen, an open-source batch image converter
3 * and resizer.
4 *
5 * (C) Francesco Mondello 2009 - 2021
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Contact e-mail: Francesco Mondello <faster3ck@gmail.com>
21 *
22 */
23 
24 #include <QKeyEvent>
25 #include <QDropEvent>
26 #include <QDragEnterEvent>
27 #include <QDragMoveEvent>
28 #include <QDir>
29 #include <QFileInfo>
30 #include <QMimeData>
31 #include <QUrl>
32 #include "pixtreewidget.h"
33 #include "formats.h"
34 #include "sizeutil.h"
35 #include "cachingsystem.h"
36 
PixTreeWidget(QWidget * parent)37 PixTreeWidget::PixTreeWidget(QWidget *parent):QTreeWidget(parent)
38 {
39     QStringList headers;
40     headers.append(tr("To convert"));
41     headers.append(tr("Status"));
42     headers.append(tr("File name"));
43     headers.append(tr("Image type"));
44     headers.append(tr("File size"));
45     headers.append(tr("File path"));
46 
47     setRootIsDecorated(false);
48     setAlternatingRowColors(true);
49 
50     setSelectionMode(QAbstractItemView::ExtendedSelection);
51     setHeaderLabels(headers);
52     setUniformRowHeights(true);
53     /*setSortingEnabled(true);
54     sortItems(0, Qt::AscendingOrder);*/
55     setAcceptDrops(true);
56 }
57 
addItems(QList<ImageAttributes> * iAList)58 void PixTreeWidget::addItems(QList<ImageAttributes> *iAList)
59 {
60     int cnt = iAList->count();
61 
62     for (int i = 0; i < cnt; i++) {
63         QTreeWidgetItem *item = new QTreeWidgetItem(this);
64         item->setCheckState(0, Qt::Unchecked);
65         item->setText(2, iAList->at(i).fileName);
66         item->setText(3, iAList->at(i).suffix);
67         item->setText(4, SizeUtil::simplifyFileSize(iAList->at(i).size));
68 		item->setText(5, QDir::toNativeSeparators(iAList->at(i).path));
69     }
70 }
71 
dragMoveEvent(QDragMoveEvent * event)72 void PixTreeWidget::dragMoveEvent(QDragMoveEvent *event)
73 {
74     event->acceptProposedAction();
75 }
76 
dragEnterEvent(QDragEnterEvent * event)77 void PixTreeWidget::dragEnterEvent(QDragEnterEvent *event)
78 {
79     if (event->mimeData()->hasFormat("text/uri-list"))
80         event->acceptProposedAction();
81 }
82 
dropEvent(QDropEvent * event)83 void PixTreeWidget::dropEvent(QDropEvent *event)
84 {
85     QList<QUrl> urls = event->mimeData()->urls();
86     if (urls.isEmpty())
87         return;
88 
89     QStringList fileNames;
90     QFileInfo fi;
91 
92     for (int i = 0; i < urls.count(); i++) {
93         fi = urls.at(i).toLocalFile();
94 
95         QStringList readableFomats = Formats::readableFilters();
96 
97         if (readableFomats.contains(fi.suffix(), Qt::CaseInsensitive))
98             fileNames << urls.at(i).toLocalFile().toLocal8Bit();
99     }
100 
101     if (!fileNames.isEmpty())
102         emit dropped(fileNames);
103 }
104 
checkItems()105 void PixTreeWidget::checkItems()
106 {
107     QTreeWidgetItemIterator it(this);
108     while (*it) {
109         if ((*it)->isSelected())
110             (*it)->setCheckState (0, Qt::Checked);
111         ++it;
112     }
113 }
114 
checkAllItems()115 void PixTreeWidget::checkAllItems()
116 {
117     QTreeWidgetItemIterator it(this);
118     while (*it) {
119         (*it)->setCheckState (0, Qt::Checked);
120         ++it;
121     }
122 }
123 
uncheckItems()124 void PixTreeWidget::uncheckItems()
125 {
126     QTreeWidgetItemIterator it(this);
127     while (*it) {
128         if ((*it)->isSelected())
129             (*it)->setCheckState (0, Qt::Unchecked);
130         ++it;
131     }
132 }
133 
uncheckAllItems()134 void PixTreeWidget::uncheckAllItems()
135 {
136     QTreeWidgetItemIterator it(this);
137     while (*it) {
138         (*it)->setCheckState (0, Qt::Unchecked);
139         ++it;
140     }
141 }
142 
removeItems(QList<ImageAttributes> * iAList)143 void PixTreeWidget::removeItems(QList<ImageAttributes> *iAList)
144 {
145     for (int i = this->topLevelItemCount() - 1; i >= 0; i--)
146         if ((this->topLevelItem(i))->isSelected()) {
147             QString s = iAList->at(i).completeFileName;
148 
149             iAList->removeAt(i);
150             this->takeTopLevelItem(i);
151             CachingSystem::remove(s);
152         }
153 }
154 
thereAreItemsChecked()155 bool PixTreeWidget::thereAreItemsChecked()
156 {
157     QTreeWidgetItemIterator it(this);
158     while (*it) {
159         if ((*it)->checkState(0) == Qt::Checked)
160             return true;
161         ++it;
162     }
163 
164     return false;
165 }
166 
countChecked()167 int PixTreeWidget::countChecked()
168 {
169     int cnt = 0;
170 
171     QTreeWidgetItemIterator it(this);
172     while (*it) {
173         if ((*it)->checkState(0) == Qt::Checked)
174             cnt++;
175         ++it;
176     }
177 
178     return cnt;
179 }
180