1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
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 "UIC_DropBox.h"
29 #include "Model_Settings.h"
32 #include <MUtils/Global.h>
38 #include <QDesktopWidget>
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 ////////////////////////////////////////////////////////////
55 ////////////////////////////////////////////////////////////
57 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
59 QDialog(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
64 m_screenGeometry(SCREEN_GEOMETRY()),
67 //Init the dialog, from the .ui file
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;
84 setAttribute(Qt::WA_TranslucentBackground);
85 setWindowOpacity(LOW_OPACITY);
88 QEvent languageChangeEvent(QEvent::LanguageChange);
89 changeEvent(&languageChangeEvent);
92 ////////////////////////////////////////////////////////////
94 ////////////////////////////////////////////////////////////
96 DropBox::~DropBox(void)
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);
110 ////////////////////////////////////////////////////////////
112 ////////////////////////////////////////////////////////////
114 void DropBox::modelChanged(void)
118 m_counterLabel->setText(QString::number(m_model->rowCount()));
122 ////////////////////////////////////////////////////////////
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&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);
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();
160 pos_x = qBound(0, pos_x, m_screenGeometry.width() - frameGeometry().width());
161 pos_y = qBound(0, pos_y, m_screenGeometry.height() - frameGeometry().height());
168 QApplication::restoreOverrideCursor();
170 setWindowOpacity(LOW_OPACITY);
176 void DropBox::keyPressEvent(QKeyEvent *event)
181 void DropBox::keyReleaseEvent(QKeyEvent *event)
186 void DropBox::closeEvent(QCloseEvent *event)
194 void DropBox::mousePressEvent(QMouseEvent *event)
202 if(event->button() == Qt::RightButton)
205 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
209 m_screenGeometry = SCREEN_GEOMETRY();
210 QApplication::setOverrideCursor(Qt::SizeAllCursor);
211 *m_windowReferencePoint = this->pos();
212 *m_mouseReferencePoint = event->globalPos();
214 setWindowOpacity(1.0);
217 void DropBox::mouseReleaseEvent(QMouseEvent *event)
219 if(m_moving && event->button() != Qt::RightButton)
222 QApplication::restoreOverrideCursor();
223 setWindowOpacity(LOW_OPACITY);
224 m_settings->dropBoxWidgetPositionX(this->x());
225 m_settings->dropBoxWidgetPositionY(this->y());
230 void DropBox::mouseMoveEvent(QMouseEvent *event)
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);
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())
259 case QEvent::WindowDeactivate:
262 QApplication::restoreOverrideCursor();
268 return QDialog::event(event);
271 ////////////////////////////////////////////////////////////
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);