OSDN Git Service

Update MediaInfo binaries to v0.7.53 (2012-01-24), compiled with ICL 12.1.6 and MSVC...
[lamexp/LameXP.git] / src / Dialog_WorkingBanner.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Dialog_WorkingBanner.h"
23
24 #include "Global.h"
25 #include "WinSevenTaskbar.h"
26
27 #include <QThread>
28 #include <QMovie>
29 #include <QKeyEvent>
30 #include <QFontMetrics>
31
32 #define EPS (1.0E-5)
33
34 ////////////////////////////////////////////////////////////
35 // Constructor
36 ////////////////////////////////////////////////////////////
37
38 WorkingBanner::WorkingBanner(QWidget *parent)
39 :
40         QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
41 {
42         //Init the dialog, from the .ui file
43         setupUi(this);
44         setModal(true);
45
46         //Start animation
47         m_working = new QMovie(":/images/Busy.gif");
48         labelWorking->setMovie(m_working);
49         m_working->start();
50
51         //Set wait cursor
52         setCursor(Qt::WaitCursor);
53 }
54
55 ////////////////////////////////////////////////////////////
56 // Destructor
57 ////////////////////////////////////////////////////////////
58
59 WorkingBanner::~WorkingBanner(void)
60 {
61         if(m_working)
62         {
63                 m_working->stop();
64                 delete m_working;
65                 m_working = NULL;
66         }
67 }
68
69 ////////////////////////////////////////////////////////////
70 // PUBLIC FUNCTIONS
71 ////////////////////////////////////////////////////////////
72
73 void WorkingBanner::show(const QString &text)
74 {
75         m_canClose = false;
76         QDialog::show();
77         setText(text);
78         QApplication::processEvents();
79 }
80
81 bool WorkingBanner::close(void)
82 {
83         m_canClose = true;
84         emit userAbort();
85         return QDialog::close();
86 }
87
88 void WorkingBanner::show(const QString &text, QThread *thread)
89 {
90         //Show splash
91         this->show(text);
92
93         //Start the thread
94         thread->start();
95
96         //Set taskbar state
97         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
98         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
99
100         //Loop while thread is running
101         while(thread->isRunning())
102         {
103                 QApplication::processEvents(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents);
104         }
105
106         //Set taskbar state
107         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
108         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
109
110         //Hide splash
111         this->close();
112 }
113
114 void WorkingBanner::show(const QString &text, QEventLoop *loop)
115 {
116         //Show splash
117         this->show(text);
118
119         //Set taskbar state
120         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
121         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
122
123         //Loop while thread is running
124         loop->exec(QEventLoop::ExcludeUserInputEvents);
125
126         //Set taskbar state
127         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
128         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
129
130         //Hide splash
131         this->close();
132 }
133
134 ////////////////////////////////////////////////////////////
135 // EVENTS
136 ////////////////////////////////////////////////////////////
137
138 void WorkingBanner::keyPressEvent(QKeyEvent *event)
139 {
140         if(event->key() == Qt::Key_Escape)
141         {
142                 qDebug("QT::KEY_ESCAPE pressed!");
143                 emit userAbort();
144         }
145         
146         event->ignore();
147 }
148
149 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
150 {
151         event->ignore();
152 }
153
154 void WorkingBanner::closeEvent(QCloseEvent *event)
155 {
156         if(!m_canClose) event->ignore();
157 }
158
159 bool WorkingBanner::winEvent(MSG *message, long *result)
160 {
161         return WinSevenTaskbar::handleWinEvent(message, result);
162 }
163
164 ////////////////////////////////////////////////////////////
165 // SLOTS
166 ////////////////////////////////////////////////////////////
167
168 void WorkingBanner::setText(const QString &text)
169 {
170         QFontMetrics metrics(labelStatus->font());
171         if(metrics.width(text) <= labelStatus->width())
172         {
173                 labelStatus->setText(text);
174         }
175         else
176         {
177                 QString choppedText = text.simplified().append("...");
178                 while(metrics.width(choppedText) > labelStatus->width() && choppedText.length() > 8)
179                 {
180                         choppedText.chop(4);
181                         choppedText = choppedText.trimmed();
182                         choppedText.append("...");
183                 }
184                 labelStatus->setText(choppedText);
185         }
186         if(this->isVisible())
187         {
188                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
189         }
190 }