Bump version.
[LameXP.git] / src / Dialog_DropBox.cpp
blob13e5b526ebcc6a508dc808a4fd16f2deba2eb172
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2021 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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 "UIC_DropBox.h"
27 //Internal
28 #include "Global.h"
29 #include "Model_Settings.h"
31 //MUtils
32 #include <MUtils/Global.h>
34 //Qt
35 #include <QThread>
36 #include <QMovie>
37 #include <QKeyEvent>
38 #include <QDesktopWidget>
39 #include <QToolTip>
40 #include <QTimer>
42 #define EPS (1.0E-5)
43 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = (WIDGET)->font(); _font.setBold(BOLD); (WIDGET)->setFont(_font); }
45 static QRect SCREEN_GEOMETRY(void)
47 QDesktopWidget *desktop = QApplication::desktop();
48 return (desktop->isVirtualDesktop() ? desktop->screen()->geometry() : desktop->availableGeometry());
51 static const double LOW_OPACITY = 0.85;
53 ////////////////////////////////////////////////////////////
54 // Constructor
55 ////////////////////////////////////////////////////////////
57 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
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)
67 //Init the dialog, from the .ui file
68 ui->setupUi(this);
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);
77 m_windowReferencePoint = new QPoint;
78 m_mouseReferencePoint = new QPoint;
80 //Prevent close
81 m_canClose = false;
83 //Make transparent
84 setAttribute(Qt::WA_TranslucentBackground);
85 setWindowOpacity(LOW_OPACITY);
87 //Translate UI
88 QEvent languageChangeEvent(QEvent::LanguageChange);
89 changeEvent(&languageChangeEvent);
92 ////////////////////////////////////////////////////////////
93 // Destructor
94 ////////////////////////////////////////////////////////////
96 DropBox::~DropBox(void)
98 if(!m_firstShow)
100 m_settings->dropBoxWidgetPositionX(this->x());
101 m_settings->dropBoxWidgetPositionY(this->y());
104 MUTILS_DELETE(m_counterLabel);
105 MUTILS_DELETE(m_windowReferencePoint);
106 MUTILS_DELETE(m_mouseReferencePoint);
107 MUTILS_DELETE(ui);
110 ////////////////////////////////////////////////////////////
111 // PUBLIC SLOTS
112 ////////////////////////////////////////////////////////////
114 void DropBox::modelChanged(void)
116 if(m_model)
118 m_counterLabel->setText(QString::number(m_model->rowCount()));
122 ////////////////////////////////////////////////////////////
123 // EVENTS
124 ////////////////////////////////////////////////////////////
127 * Re-translate the UI
129 void DropBox::changeEvent(QEvent *e)
131 if(e->type() == QEvent::LanguageChange)
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)")));
138 void DropBox::showEvent(QShowEvent *event)
140 m_screenGeometry = SCREEN_GEOMETRY();
141 setFixedSize(ui->dropBoxLabel->pixmap()->size());
143 m_counterLabel->setGeometry(0, ui->dropBoxLabel->height() - 30, ui->dropBoxLabel->width(), 25);
145 if(m_firstShow)
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))
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();
158 else
160 pos_x = qBound(0, pos_x, m_screenGeometry.width() - frameGeometry().width());
161 pos_y = qBound(0, pos_y, m_screenGeometry.height() - frameGeometry().height());
163 move(pos_x, pos_y);
166 if(m_moving)
168 QApplication::restoreOverrideCursor();
169 m_moving = false;
170 setWindowOpacity(LOW_OPACITY);
173 boundWidget(this);
176 void DropBox::keyPressEvent(QKeyEvent *event)
178 event->ignore();
181 void DropBox::keyReleaseEvent(QKeyEvent *event)
183 event->ignore();
186 void DropBox::closeEvent(QCloseEvent *event)
188 if(!m_canClose)
190 event->ignore();
194 void DropBox::mousePressEvent(QMouseEvent *event)
196 if(m_moving)
198 event->ignore();
199 return;
202 if(event->button() == Qt::RightButton)
204 hide();
205 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
206 return;
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);
217 void DropBox::mouseReleaseEvent(QMouseEvent *event)
219 if(m_moving && event->button() != Qt::RightButton)
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;
230 void DropBox::mouseMoveEvent(QMouseEvent *event)
232 if(!m_moving)
234 return;
237 const int delta_x = m_mouseReferencePoint->x() - event->globalX();
238 const int delta_y = m_mouseReferencePoint->y() - event->globalY();
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();
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);
246 move(new_x, new_y);
249 void DropBox::showToolTip(void)
251 QToolTip::showText(ui->dropBoxLabel->mapToGlobal(ui->dropBoxLabel->pos()), ui->dropBoxLabel->toolTip());
254 bool DropBox::event(QEvent *event)
256 switch(event->type())
258 case QEvent::Leave:
259 case QEvent::WindowDeactivate:
260 if(m_moving)
262 QApplication::restoreOverrideCursor();
263 m_moving = false;
265 break;
268 return QDialog::event(event);
271 ////////////////////////////////////////////////////////////
272 // PRIVATE
273 ////////////////////////////////////////////////////////////
275 void DropBox::boundWidget(QWidget *widget)
277 static const int magnetic = 24;
278 QRect availGeometry = QApplication::desktop()->availableGeometry(widget);
280 const int max_x = availGeometry.width() - widget->frameGeometry().width() + availGeometry.left();
281 const int max_y = availGeometry.height() - widget->frameGeometry().height() + availGeometry.top();
283 int new_x = qBound(availGeometry.left(), widget->x(), max_x);
284 int new_y = qBound(availGeometry.top(), widget->y(), max_y);
286 if(new_x - availGeometry.left() < magnetic) new_x = availGeometry.left();
287 if(new_y - availGeometry.top() < magnetic) new_y = availGeometry.top();
289 if(max_x - new_x < magnetic) new_x = max_x;
290 if(max_y - new_y < magnetic) new_y = max_y;
292 if((widget->x() != new_x) || (widget->y() != new_y))
294 widget->move(new_x, new_y);