Make sure the "queued" slots in the FileAnalyzer thread are really executed in the...
[LameXP.git] / src / Dialog_DropBox.cpp
blobe722100e44704a9c68fc76260301497b65f57595
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Dialog_DropBox.h"
25 #include "../tmp/UIC_DropBox.h"
27 #include "Global.h"
28 #include "Model_Settings.h"
30 #include <QThread>
31 #include <QMovie>
32 #include <QKeyEvent>
33 #include <QDesktopWidget>
34 #include <QToolTip>
35 #include <QTimer>
37 #define EPS (1.0E-5)
38 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = (WIDGET)->font(); _font.setBold(BOLD); (WIDGET)->setFont(_font); }
40 static QRect SCREEN_GEOMETRY(void)
42 QDesktopWidget *desktop = QApplication::desktop();
43 return (desktop->isVirtualDesktop() ? desktop->screen()->geometry() : desktop->availableGeometry());
46 static const double LOW_OPACITY = 0.85;
48 ////////////////////////////////////////////////////////////
49 // Constructor
50 ////////////////////////////////////////////////////////////
52 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
54 QDialog(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
55 ui(new Ui::DropBox),
56 m_model(model),
57 m_settings(settings),
58 m_moving(false),
59 m_screenGeometry(SCREEN_GEOMETRY()),
60 m_firstShow(true)
62 //Init the dialog, from the .ui file
63 ui->setupUi(this);
65 //Init counter
66 m_counterLabel = new QLabel(this);
67 m_counterLabel->setParent(ui->dropBoxLabel);
68 m_counterLabel->setText("0");
69 m_counterLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
70 SET_FONT_BOLD(m_counterLabel, true);
72 m_windowReferencePoint = new QPoint;
73 m_mouseReferencePoint = new QPoint;
75 //Prevent close
76 m_canClose = false;
78 //Make transparent
79 setAttribute(Qt::WA_TranslucentBackground);
80 setWindowOpacity(LOW_OPACITY);
82 //Translate UI
83 QEvent languageChangeEvent(QEvent::LanguageChange);
84 changeEvent(&languageChangeEvent);
87 ////////////////////////////////////////////////////////////
88 // Destructor
89 ////////////////////////////////////////////////////////////
91 DropBox::~DropBox(void)
93 if(!m_firstShow)
95 m_settings->dropBoxWidgetPositionX(this->x());
96 m_settings->dropBoxWidgetPositionY(this->y());
99 LAMEXP_DELETE(m_counterLabel);
100 LAMEXP_DELETE(m_windowReferencePoint);
101 LAMEXP_DELETE(m_mouseReferencePoint);
102 LAMEXP_DELETE(ui);
105 ////////////////////////////////////////////////////////////
106 // PUBLIC SLOTS
107 ////////////////////////////////////////////////////////////
109 void DropBox::modelChanged(void)
111 if(m_model)
113 m_counterLabel->setText(QString::number(m_model->rowCount()));
117 ////////////////////////////////////////////////////////////
118 // EVENTS
119 ////////////////////////////////////////////////////////////
122 * Re-translate the UI
124 void DropBox::changeEvent(QEvent *e)
126 if(e->type() == QEvent::LanguageChange)
128 ui->retranslateUi(this);
129 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)")));
133 void DropBox::showEvent(QShowEvent *event)
135 m_screenGeometry = SCREEN_GEOMETRY();
136 setFixedSize(ui->dropBoxLabel->pixmap()->size());
138 m_counterLabel->setGeometry(0, ui->dropBoxLabel->height() - 30, ui->dropBoxLabel->width(), 25);
140 if(m_firstShow)
142 m_firstShow = false;
143 int pos_x = m_settings->dropBoxWidgetPositionX();
144 int pos_y = m_settings->dropBoxWidgetPositionY();
145 if((pos_x < 0) && (pos_y < 0))
147 QWidget *const parentWidget = dynamic_cast<QWidget*>(this->parent());
148 QWidget *const targetWidget = parentWidget ? parentWidget : this;
149 QRect availGeometry = QApplication::desktop()->availableGeometry(targetWidget);
150 pos_x = availGeometry.width() - frameGeometry().width() + availGeometry.left();
151 pos_y = availGeometry.height() - frameGeometry().height() + availGeometry.top();
153 else
155 pos_x = qBound(0, pos_x, m_screenGeometry.width() - frameGeometry().width());
156 pos_y = qBound(0, pos_y, m_screenGeometry.height() - frameGeometry().height());
158 move(pos_x, pos_y);
161 if(m_moving)
163 QApplication::restoreOverrideCursor();
164 m_moving = false;
165 setWindowOpacity(LOW_OPACITY);
168 boundWidget(this);
171 void DropBox::keyPressEvent(QKeyEvent *event)
173 event->ignore();
176 void DropBox::keyReleaseEvent(QKeyEvent *event)
178 event->ignore();
181 void DropBox::closeEvent(QCloseEvent *event)
183 if(!m_canClose)
185 event->ignore();
189 void DropBox::mousePressEvent(QMouseEvent *event)
191 if(m_moving)
193 event->ignore();
194 return;
197 if(event->button() == Qt::RightButton)
199 hide();
200 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
201 return;
204 m_screenGeometry = SCREEN_GEOMETRY();
205 QApplication::setOverrideCursor(Qt::SizeAllCursor);
206 *m_windowReferencePoint = this->pos();
207 *m_mouseReferencePoint = event->globalPos();
208 m_moving = true;
209 setWindowOpacity(1.0);
212 void DropBox::mouseReleaseEvent(QMouseEvent *event)
214 if(m_moving && event->button() != Qt::RightButton)
216 boundWidget(this);
217 QApplication::restoreOverrideCursor();
218 setWindowOpacity(LOW_OPACITY);
219 m_settings->dropBoxWidgetPositionX(this->x());
220 m_settings->dropBoxWidgetPositionY(this->y());
221 m_moving = false;
225 void DropBox::mouseMoveEvent(QMouseEvent *event)
227 if(!m_moving)
229 return;
232 const int delta_x = m_mouseReferencePoint->x() - event->globalX();
233 const int delta_y = m_mouseReferencePoint->y() - event->globalY();
235 const int max_x = m_screenGeometry.width() - frameGeometry().width() + m_screenGeometry.left();
236 const int max_y = m_screenGeometry.height() - frameGeometry().height() + m_screenGeometry.top();
238 const int new_x = qBound(m_screenGeometry.left(), m_windowReferencePoint->x() - delta_x, max_x);
239 const int new_y = qBound(m_screenGeometry.top(), m_windowReferencePoint->y() - delta_y, max_y);
241 move(new_x, new_y);
244 void DropBox::showToolTip(void)
246 QToolTip::showText(ui->dropBoxLabel->mapToGlobal(ui->dropBoxLabel->pos()), ui->dropBoxLabel->toolTip());
249 bool DropBox::event(QEvent *event)
251 switch(event->type())
253 case QEvent::Leave:
254 case QEvent::WindowDeactivate:
255 if(m_moving)
257 QApplication::restoreOverrideCursor();
258 m_moving = false;
260 break;
263 return QDialog::event(event);
266 ////////////////////////////////////////////////////////////
267 // PRIVATE
268 ////////////////////////////////////////////////////////////
270 void DropBox::boundWidget(QWidget *widget)
272 static const int magnetic = 24;
273 QRect availGeometry = QApplication::desktop()->availableGeometry(widget);
275 const int max_x = availGeometry.width() - widget->frameGeometry().width() + availGeometry.left();
276 const int max_y = availGeometry.height() - widget->frameGeometry().height() + availGeometry.top();
278 int new_x = qBound(availGeometry.left(), widget->x(), max_x);
279 int new_y = qBound(availGeometry.top(), widget->y(), max_y);
281 if(new_x - availGeometry.left() < magnetic) new_x = availGeometry.left();
282 if(new_y - availGeometry.top() < magnetic) new_y = availGeometry.top();
284 if(max_x - new_x < magnetic) new_x = max_x;
285 if(max_y - new_y < magnetic) new_y = max_y;
287 if((widget->x() != new_x) || (widget->y() != new_y))
289 widget->move(new_x, new_y);