OSDN Git Service

Added Chinese translation by 456Vv <123@456vv.com>.
[lamexp/LameXP.git] / src / Dialog_WorkingBanner.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 void WorkingBanner::close(void)
82 {
83         m_canClose = true;
84         QDialog::close();
85 }
86
87 void WorkingBanner::show(const QString &text, QThread *thread)
88 {
89         //Show splash
90         this->show(text);
91
92         //Start the thread
93         thread->start();
94
95         //Set taskbar state
96         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
97         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
98
99         //Loop while thread is running
100         while(thread->isRunning())
101         {
102                 QApplication::processEvents(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents);
103         }
104
105         //Set taskbar state
106         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
107         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
108
109         //Hide splash
110         this->close();
111 }
112
113 void WorkingBanner::show(const QString &text, QEventLoop *loop)
114 {
115         //Show splash
116         this->show(text);
117
118         //Set taskbar state
119         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
120         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
121
122         //Loop while thread is running
123         loop->exec(QEventLoop::ExcludeUserInputEvents);
124
125         //Set taskbar state
126         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
127         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
128
129         //Hide splash
130         this->close();
131 }
132
133 ////////////////////////////////////////////////////////////
134 // EVENTS
135 ////////////////////////////////////////////////////////////
136
137 void WorkingBanner::keyPressEvent(QKeyEvent *event)
138 {
139         if(event->key() == Qt::Key_Escape)
140         {
141                 qDebug("QT::KEY_ESCAPE pressed!");
142                 emit userAbort();
143         }
144         
145         event->ignore();
146 }
147
148 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
149 {
150         event->ignore();
151 }
152
153 void WorkingBanner::closeEvent(QCloseEvent *event)
154 {
155         if(!m_canClose) event->ignore();
156 }
157
158 bool WorkingBanner::winEvent(MSG *message, long *result)
159 {
160         return WinSevenTaskbar::handleWinEvent(message, result);
161 }
162
163 ////////////////////////////////////////////////////////////
164 // SLOTS
165 ////////////////////////////////////////////////////////////
166
167 void WorkingBanner::setText(const QString &text)
168 {
169         QFontMetrics metrics(labelStatus->font());
170         if(metrics.width(text) <= labelStatus->width())
171         {
172                 labelStatus->setText(text);
173         }
174         else
175         {
176                 QString choppedText = text.simplified().append("...");
177                 while(metrics.width(choppedText) > labelStatus->width() && choppedText.length() > 8)
178                 {
179                         choppedText.chop(4);
180                         choppedText = choppedText.trimmed();
181                         choppedText.append("...");
182                 }
183                 labelStatus->setText(choppedText);
184         }
185         if(this->isVisible())
186         {
187                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
188         }
189 }