1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Fritzing 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 Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 6904 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24 
25 ********************************************************************/
26 
27 #include "fileprogressdialog.h"
28 #include "../debugdialog.h"
29 #include "../processeventblocker.h"
30 
31 #include <QVBoxLayout>
32 #include <QTextStream>
33 #include <QLabel>
34 #include <QComboBox>
35 #include <QPushButton>
36 #include <QLocale>
37 #include <QDialogButtonBox>
38 #include <QGroupBox>
39 #include <QCloseEvent>
40 #include <QApplication>
41 #include <QDesktopWidget>
42 #include <QSplashScreen>
43 
44 /////////////////////////////////////
45 
FileProgressDialog(const QString & title,int initialMaximum,QWidget * parent)46 FileProgressDialog::FileProgressDialog(const QString & title, int initialMaximum, QWidget * parent) : QDialog(parent)
47 {
48     m_incValueMod = 2;
49 
50 	QSplashScreen *splash = NULL;
51 	foreach (QWidget *widget, QApplication::topLevelWidgets()) {
52         splash = qobject_cast<QSplashScreen *>(widget);
53 		if (splash) {
54 			break;
55 		}
56 	}
57 
58 	init(title, initialMaximum);
59     setModal(splash == NULL);			// OS X Lion doesn't seem to like modal dialogs during splash time
60 
61 	show();
62 	if (splash) {
63 		QRect sr = splash->geometry();
64 		QRect r = this->geometry();
65 		QRect fr = this->frameGeometry();
66 		int offset = sr.bottom() - r.top() + (fr.height() - r.height());
67 		r.adjust(0, offset, 0, offset);
68 		this->setGeometry(r);
69 	}
70 	ProcessEventBlocker::processEvents();
71 }
72 
FileProgressDialog(QWidget * parent)73 FileProgressDialog::FileProgressDialog(QWidget *parent) : QDialog(parent)
74 {
75 	init(QObject::tr("File Progress..."), 0);
76 }
77 
78 
~FileProgressDialog()79 FileProgressDialog::~FileProgressDialog() {
80     m_timer.stop();
81 }
82 
init(const QString & title,int initialMaximum)83 void FileProgressDialog::init(const QString & title, int initialMaximum)
84 {
85 	Qt::WindowFlags flags = windowFlags();
86 	flags |= Qt::CustomizeWindowHint;
87 	flags ^= Qt::WindowCloseButtonHint;
88 	flags ^= Qt::WindowSystemMenuHint;
89 	flags ^= Qt::WindowContextHelpButtonHint;
90 	//flags ^= Qt::WindowTitleHint;
91 	setWindowFlags(flags);
92 
93 	this->setWindowTitle(title);
94 
95 	QVBoxLayout * vLayout = new QVBoxLayout(this);
96 
97 	m_message = new QLabel(this);
98 	m_message->setMinimumWidth(300);
99 	vLayout->addWidget(m_message);
100 
101 	m_progressBar = new QProgressBar(this);
102 	m_progressBar->setMinimum(0);
103 	m_progressBar->setMaximum(initialMaximum);
104 	vLayout->addWidget(m_progressBar);
105 
106     //QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel);
107 	//buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
108 	//buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(false);
109     //connect(buttonBox, SIGNAL(rejected()), this, SLOT(sendCancel()));
110 	//vLayout->addWidget(buttonBox);
111 
112 	this->setLayout(vLayout);
113 }
114 
setMinimum(int minimum)115 void FileProgressDialog::setMinimum(int minimum) {
116 	m_progressBar->setMinimum(minimum);
117 }
118 
setMaximum(int maximum)119 void FileProgressDialog::setMaximum(int maximum) {
120 	m_progressBar->setMaximum(maximum);
121 	//DebugDialog::debug(QString("set maximum:%1").arg(maximum));
122 }
123 
addMaximum(int maximum)124 void FileProgressDialog::addMaximum(int maximum) {
125     if (maximum != 0) {
126 	    m_progressBar->setMaximum(m_progressBar->maximum() + maximum);
127 	    //DebugDialog::debug(QString("set maximum:%1").arg(maximum));
128     }
129 }
130 
setValue(int value)131 void FileProgressDialog::setValue(int value) {
132 	m_progressBar->setValue(value);
133 	ProcessEventBlocker::processEvents();
134 }
135 
incValue()136 void FileProgressDialog::incValue() {
137 	m_progressBar->setValue(m_progressBar->value() + 1);
138     if (m_progressBar->value() % m_incValueMod == 0) {
139 	    ProcessEventBlocker::processEvents();
140     }
141 }
142 
value()143 int FileProgressDialog::value() {
144 	return m_progressBar->value();
145 }
146 
sendCancel()147 void FileProgressDialog::sendCancel() {
148 	emit cancel();
149 }
150 
closeEvent(QCloseEvent * event)151 void FileProgressDialog::closeEvent(QCloseEvent *event)
152 {
153 	event->ignore();
154 }
155 
setMessage(const QString & message)156 void FileProgressDialog::setMessage(const QString & message) {
157 	m_message->setText(message);
158 	ProcessEventBlocker::processEvents();
159 }
160 
setBinLoadingCount(int count)161 void FileProgressDialog::setBinLoadingCount(int count)
162 {
163 	m_binLoadingCount = count;
164 	m_binLoadingIndex = -1;
165 	m_binLoadingStart = value();
166 }
167 
setBinLoadingChunk(int chunk)168 void FileProgressDialog::setBinLoadingChunk(int chunk)
169 {
170 	m_binLoadingChunk = chunk;
171 }
172 
loadingInstancesSlot(class ModelBase *,QDomElement & instances)173 void FileProgressDialog::loadingInstancesSlot(class ModelBase *, QDomElement & instances)
174 {
175 	m_binLoadingValue = m_binLoadingStart + (++m_binLoadingIndex * m_binLoadingChunk / (double) m_binLoadingCount);
176 	setValue(m_binLoadingValue);
177 
178 	int count = instances.childNodes().count();
179 
180 	// * 3 comes from: once for model part load, once for list view, once for icon view
181 	m_binLoadingInc = m_binLoadingChunk / (double) (m_binLoadingCount * 3 * count);
182 }
183 
loadingInstanceSlot(class ModelBase *,QDomElement & instance)184 void FileProgressDialog::loadingInstanceSlot(class ModelBase *, QDomElement & instance)
185 {
186 	Q_UNUSED(instance);
187 	//QString text;
188 	//QTextStream textStream(&text);
189 	//instance.save(textStream, 0);
190 	//DebugDialog::debug(QString("loading %1").arg(text));
191 	//DebugDialog::debug(QString("loading %1").arg(instance.attribute("path")));
192 	settingItemSlot();
193 }
194 
settingItemSlot()195 void FileProgressDialog::settingItemSlot()
196 {
197 	m_binLoadingValue += m_binLoadingInc;
198 	if ((int) m_binLoadingValue > value()) {
199 		setValue(m_binLoadingValue);
200 	}
201 }
202 
resizeEvent(QResizeEvent * event)203 void FileProgressDialog::resizeEvent(QResizeEvent * event)
204 {
205 	QDialog::resizeEvent(event);
206 	QRect scr = QApplication::desktop()->screenGeometry();
207 	move( scr.center() - rect().center() );
208 }
209 
setIncValueMod(int mod)210 void FileProgressDialog::setIncValueMod(int mod) {
211     m_incValueMod = mod;
212 }
213 
setIndeterminate()214 void FileProgressDialog::setIndeterminate() {
215     m_progressBar->setRange(0, 0);
216     m_timer.setSingleShot(false);
217     m_timer.setInterval(1000);
218     connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateIndeterminate()));
219     m_timer.start();
220 }
221 
updateIndeterminate()222 void FileProgressDialog::updateIndeterminate() {
223     if (m_progressBar) {
224         m_progressBar->setValue(0);
225     }
226 }
227