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 <QMenu>
25 #include <QAction>
26 #include <QList>
27 #include <QDesktopServices>
28 #include <QUrl>
29 #include "dialogconversionstatus.h"
30 
DialogConversionStatus(QWidget * parent)31 DialogConversionStatus::DialogConversionStatus(QWidget *parent) :
32     QDialog(parent){
33     setupUi(this);
34 
35     setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint);
36 
37     connect(pushClose, SIGNAL(clicked()), this, SLOT(close()));
38     connect(pushAbort, SIGNAL(clicked()), this, SLOT(abort()));
39 
40     menu = new QMenu(this);
41     actionOpenAllDirs = new QAction(tr("Open all the destination folders"), this);
42 
43     connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(openDir(QAction*)));
44     connect(actionOpenAllDirs, SIGNAL(triggered()), this, SLOT(openAllDirs()));
45 }
46 
setup(int n_images)47 void DialogConversionStatus::setup(int n_images)
48 {
49     treeWidget->clear();
50 
51     c_tot = 0;
52     c_ok = 0;
53     c_no = 0;
54 
55     progressBar->reset();
56     progressBar->setMinimum(0);
57     progressBar->setMaximum(n_images);
58 
59     pushClose->setEnabled(false);
60     pushAbort->setEnabled(true);
61 
62     m_totimages = n_images;
63 
64     m_msg.clear();
65     outDirs.clear();
66 
67     pushOpen->setEnabled(false);
68 }
69 
conversionStatus(int conv_status,QString fileName)70 void DialogConversionStatus::conversionStatus(int conv_status, QString fileName)
71 {
72     QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
73 
74     if (conv_status == 1)
75         m_msg = tr("Converted");
76 
77     item->setText(0, fileName);
78     item->setText(1, m_msg);
79 }
80 
counter(int conv_status)81 void DialogConversionStatus::counter(int conv_status)
82 {
83     c_tot++;    // Count total images and compare with total global to reset the window
84 
85     if (conv_status == 1)
86         c_ok++;
87     if (conv_status == -1)
88         c_no++;
89 
90     labelOk->setText(QString::number(c_ok));
91     labelKo->setText(QString::number(c_no));
92     labelTotal->setText(QString::number(c_tot));
93 }
94 
step(int conv_status,QString fileName)95 void DialogConversionStatus::step(int conv_status, QString fileName) // avviene quando un'immagine è stata processata (o saltata)
96 {
97     counter(conv_status);
98     conversionStatus(conv_status, fileName);
99     progressBar->setValue(c_tot);
100 
101     if (c_tot == m_totimages)
102         resetButtons();
103 }
104 
abort()105 void DialogConversionStatus::abort()
106 {
107     resetButtons();
108     emit stopProcess();
109 }
110 
resetButtons()111 void DialogConversionStatus::resetButtons()
112 {
113     pushClose->setEnabled(true);
114     pushAbort->setEnabled(false);
115 }
116 
setErrorMsg(QString err_status)117 void DialogConversionStatus::setErrorMsg(QString err_status)
118 {
119     m_msg = err_status;
120 }
121 
addOutputDirectory(QString path)122 void DialogConversionStatus::addOutputDirectory(QString path)
123 {
124     if (!outDirs.contains(path))
125         outDirs.append(path);
126 
127     if (c_tot == m_totimages) {
128         if (outDirs.count() > 0) {
129             setupOpenButton();
130             pushOpen->setEnabled(true);
131         }
132     }
133 }
134 
setupOpenButton()135 void DialogConversionStatus::setupOpenButton()
136 {
137     QList<QAction *> aL;
138 
139 	menu->clear();
140 
141     for (int i = 0; i < outDirs.count(); i++) {
142         QString s = outDirs.at(i);
143         QAction *a = new QAction(s, this);
144         a->setData(s);
145         aL << a;
146     }
147 
148     menu->addActions(aL);
149     menu->addSeparator();
150     menu->addAction(actionOpenAllDirs);
151 
152     pushOpen->setMenu(menu);
153 }
154 
openDir(QAction * action)155 void DialogConversionStatus::openDir(QAction *action)
156 {
157     QString value = action->data().toString();
158     QDesktopServices::openUrl(QUrl("file:///" + value, QUrl::TolerantMode));
159 }
160 
openAllDirs()161 void DialogConversionStatus::openAllDirs()
162 {
163     for (int i = 0; i < outDirs.count(); i++) {
164         QDesktopServices::openUrl(QUrl("file:///" + outDirs.at(i), QUrl::TolerantMode));
165     }
166 }
167