OSDN Git Service

Improved Russian & Spanish translations for the installer
[lamexp/LameXP.git] / src / Dialog_DropBox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program 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 along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Dialog_DropBox.h"
24
25 #include "UIC_DropBox.h"
26
27 //Internal
28 #include "Global.h"
29 #include "Model_Settings.h"
30
31 //MUtils
32 #include <MUtils/Global.h>
33
34 //Qt
35 #include <QThread>
36 #include <QMovie>
37 #include <QKeyEvent>
38 #include <QDesktopWidget>
39 #include <QToolTip>
40 #include <QTimer>
41
42 #define EPS (1.0E-5)
43 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = (WIDGET)->font(); _font.setBold(BOLD); (WIDGET)->setFont(_font); }
44
45 static QRect SCREEN_GEOMETRY(void)
46 {
47         QDesktopWidget *desktop = QApplication::desktop();
48         return (desktop->isVirtualDesktop() ? desktop->screen()->geometry() : desktop->availableGeometry());
49 }
50
51 static const double LOW_OPACITY = 0.85;
52
53 ////////////////////////////////////////////////////////////
54 // Constructor
55 ////////////////////////////////////////////////////////////
56
57 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
58 :
59 QDialog(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
60         ui(new Ui::DropBox),
61         m_model(model),
62         m_settings(settings),
63         m_moving(false),
64         m_screenGeometry(SCREEN_GEOMETRY()),
65         m_firstShow(true)
66 {
67         //Init the dialog, from the .ui file
68         ui->setupUi(this);
69
70         //Init counter
71         m_counterLabel = new QLabel(this);
72         m_counterLabel->setParent(ui->dropBoxLabel);
73         m_counterLabel->setText("0");
74         m_counterLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
75         SET_FONT_BOLD(m_counterLabel, true);
76
77         m_windowReferencePoint = new QPoint;
78         m_mouseReferencePoint = new QPoint;
79
80         //Prevent close
81         m_canClose = false;
82
83         //Make transparent
84         setAttribute(Qt::WA_TranslucentBackground);
85         setWindowOpacity(LOW_OPACITY);
86
87         //Translate UI
88         QEvent languageChangeEvent(QEvent::LanguageChange);
89         changeEvent(&languageChangeEvent);
90 }
91
92 ////////////////////////////////////////////////////////////
93 // Destructor
94 ////////////////////////////////////////////////////////////
95
96 DropBox::~DropBox(void)
97 {
98         if(!m_firstShow)
99         {
100                 m_settings->dropBoxWidgetPositionX(this->x());
101                 m_settings->dropBoxWidgetPositionY(this->y());
102         }
103
104         MUTILS_DELETE(m_counterLabel);
105         MUTILS_DELETE(m_windowReferencePoint);
106         MUTILS_DELETE(m_mouseReferencePoint);
107         MUTILS_DELETE(ui);
108 }
109
110 ////////////////////////////////////////////////////////////
111 // PUBLIC SLOTS
112 ////////////////////////////////////////////////////////////
113
114 void DropBox::modelChanged(void)
115 {
116         if(m_model)
117         {
118                 m_counterLabel->setText(QString::number(m_model->rowCount()));
119         }
120 }
121
122 ////////////////////////////////////////////////////////////
123 // EVENTS
124 ////////////////////////////////////////////////////////////
125
126 /*
127  * Re-translate the UI
128  */
129 void DropBox::changeEvent(QEvent *e)
130 {
131         if(e->type() == QEvent::LanguageChange)
132         {
133                 ui->retranslateUi(this);
134                 ui->dropBoxLabel->setToolTip(QString("<b>%1</b><br><nobr>%2</nobr><br><nobr>%3</nobr>").arg(tr("LameXP DropBox"), tr("You can add files to LameXP via Drag&amp;Drop here!"), tr("(Right-click to close the DropBox)")));
135         }
136 }
137
138 void DropBox::showEvent(QShowEvent *event)
139 {
140         m_screenGeometry = SCREEN_GEOMETRY();
141         setFixedSize(ui->dropBoxLabel->pixmap()->size());
142         
143         m_counterLabel->setGeometry(0, ui->dropBoxLabel->height() - 30, ui->dropBoxLabel->width(), 25);
144
145         if(m_firstShow)
146         {
147                 m_firstShow = false;
148                 int pos_x = m_settings->dropBoxWidgetPositionX();
149                 int pos_y = m_settings->dropBoxWidgetPositionY();
150                 if((pos_x < 0) && (pos_y < 0))
151                 {
152                         QWidget *const parentWidget = dynamic_cast<QWidget*>(this->parent());
153                         QWidget *const targetWidget = parentWidget ? parentWidget : this;
154                         QRect availGeometry = QApplication::desktop()->availableGeometry(targetWidget);
155                         pos_x = availGeometry.width()  - frameGeometry().width()  + availGeometry.left();
156                         pos_y = availGeometry.height() - frameGeometry().height() + availGeometry.top();
157                 }
158                 else
159                 {
160                         pos_x = qBound(0, pos_x, m_screenGeometry.width()  - frameGeometry().width());
161                         pos_y = qBound(0, pos_y, m_screenGeometry.height() - frameGeometry().height());
162                 }
163                 move(pos_x, pos_y);
164         }
165
166         if(m_moving)
167         {
168                 QApplication::restoreOverrideCursor();
169                 m_moving = false;
170                 setWindowOpacity(LOW_OPACITY);
171         }
172
173         boundWidget(this);
174 }
175
176 void DropBox::keyPressEvent(QKeyEvent *event)
177 {
178         event->ignore();
179 }
180
181 void DropBox::keyReleaseEvent(QKeyEvent *event)
182 {
183         event->ignore();
184 }
185
186 void DropBox::closeEvent(QCloseEvent *event)
187 {
188         if(!m_canClose)
189         {
190                 event->ignore();
191         }
192 }
193
194 void DropBox::mousePressEvent(QMouseEvent *event)
195 {
196         if(m_moving)
197         {
198                 event->ignore();
199                 return;
200         }
201         
202         if(event->button() == Qt::RightButton)
203         {
204                 hide();
205                 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
206                 return;
207         }
208         
209         m_screenGeometry = SCREEN_GEOMETRY();
210         QApplication::setOverrideCursor(Qt::SizeAllCursor);
211         *m_windowReferencePoint = this->pos();
212         *m_mouseReferencePoint = event->globalPos();
213         m_moving = true;
214         setWindowOpacity(1.0);
215 }
216
217 void DropBox::mouseReleaseEvent(QMouseEvent *event)
218 {
219         if(m_moving && event->button() != Qt::RightButton)
220         {
221                 boundWidget(this);
222                 QApplication::restoreOverrideCursor();
223                 setWindowOpacity(LOW_OPACITY);
224                 m_settings->dropBoxWidgetPositionX(this->x());
225                 m_settings->dropBoxWidgetPositionY(this->y());
226                 m_moving = false;
227         }
228 }
229
230 void DropBox::mouseMoveEvent(QMouseEvent *event)
231 {
232         if(!m_moving)
233         {
234                 return;
235         }
236                 
237         const int delta_x = m_mouseReferencePoint->x() - event->globalX();
238         const int delta_y = m_mouseReferencePoint->y() - event->globalY();
239
240         const int max_x = m_screenGeometry.width() -  frameGeometry().width() + m_screenGeometry.left();
241         const int max_y = m_screenGeometry.height() - frameGeometry().height() + m_screenGeometry.top();
242
243         const int new_x = qBound(m_screenGeometry.left(), m_windowReferencePoint->x() - delta_x, max_x);
244         const int new_y = qBound(m_screenGeometry.top(),  m_windowReferencePoint->y() - delta_y, max_y);
245
246         move(new_x, new_y);
247 }
248
249 void DropBox::showToolTip(void)
250 {
251         QToolTip::showText(ui->dropBoxLabel->mapToGlobal(ui->dropBoxLabel->pos()), ui->dropBoxLabel->toolTip());
252 }
253
254 bool DropBox::event(QEvent *event)
255 {
256         switch(event->type())
257         {
258         case QEvent::Leave:
259         case QEvent::WindowDeactivate:
260                 if(m_moving)
261                 {
262                         QApplication::restoreOverrideCursor();
263                         m_moving = false;
264                 }
265                 break;
266         }
267
268         return QDialog::event(event);
269 }
270
271 ////////////////////////////////////////////////////////////
272 // PRIVATE
273 ////////////////////////////////////////////////////////////
274
275 void DropBox::boundWidget(QWidget *widget)
276 {
277         static const int magnetic = 24;
278         QRect availGeometry = QApplication::desktop()->availableGeometry(widget);
279
280         const int max_x = availGeometry.width()  - widget->frameGeometry().width()  + availGeometry.left();
281         const int max_y = availGeometry.height() - widget->frameGeometry().height() + availGeometry.top();
282
283         int new_x = qBound(availGeometry.left(), widget->x(), max_x);
284         int new_y = qBound(availGeometry.top(),  widget->y(), max_y);
285
286         if(new_x - availGeometry.left() < magnetic) new_x = availGeometry.left();
287         if(new_y - availGeometry.top()  < magnetic) new_y = availGeometry.top();
288
289         if(max_x - new_x < magnetic) new_x = max_x;
290         if(max_y - new_y < magnetic) new_y = max_y;
291
292         if((widget->x() != new_x) || (widget->y() != new_y))
293         {
294                 widget->move(new_x, new_y);
295         }
296 }