1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 "../tmp/UIC_DropBox.h"
28 #include "Model_Settings.h"
33 #include <QDesktopWidget>
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 ////////////////////////////////////////////////////////////
50 ////////////////////////////////////////////////////////////
52 DropBox::DropBox(QWidget
*parent
, QAbstractItemModel
*model
, SettingsModel
*settings
)
54 QDialog(parent
, Qt::FramelessWindowHint
| Qt::WindowStaysOnTopHint
),
59 m_screenGeometry(SCREEN_GEOMETRY()),
62 //Init the dialog, from the .ui file
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
;
79 setAttribute(Qt::WA_TranslucentBackground
);
80 setWindowOpacity(LOW_OPACITY
);
83 QEvent
languageChangeEvent(QEvent::LanguageChange
);
84 changeEvent(&languageChangeEvent
);
87 ////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////
91 DropBox::~DropBox(void)
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
);
105 ////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////
109 void DropBox::modelChanged(void)
113 m_counterLabel
->setText(QString::number(m_model
->rowCount()));
117 ////////////////////////////////////////////////////////////
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&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);
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();
155 pos_x
= qBound(0, pos_x
, m_screenGeometry
.width() - frameGeometry().width());
156 pos_y
= qBound(0, pos_y
, m_screenGeometry
.height() - frameGeometry().height());
163 QApplication::restoreOverrideCursor();
165 setWindowOpacity(LOW_OPACITY
);
171 void DropBox::keyPressEvent(QKeyEvent
*event
)
176 void DropBox::keyReleaseEvent(QKeyEvent
*event
)
181 void DropBox::closeEvent(QCloseEvent
*event
)
189 void DropBox::mousePressEvent(QMouseEvent
*event
)
197 if(event
->button() == Qt::RightButton
)
200 if(m_settings
) m_settings
->dropBoxWidgetEnabled(false);
204 m_screenGeometry
= SCREEN_GEOMETRY();
205 QApplication::setOverrideCursor(Qt::SizeAllCursor
);
206 *m_windowReferencePoint
= this->pos();
207 *m_mouseReferencePoint
= event
->globalPos();
209 setWindowOpacity(1.0);
212 void DropBox::mouseReleaseEvent(QMouseEvent
*event
)
214 if(m_moving
&& event
->button() != Qt::RightButton
)
217 QApplication::restoreOverrideCursor();
218 setWindowOpacity(LOW_OPACITY
);
219 m_settings
->dropBoxWidgetPositionX(this->x());
220 m_settings
->dropBoxWidgetPositionY(this->y());
225 void DropBox::mouseMoveEvent(QMouseEvent
*event
)
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
);
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())
254 case QEvent::WindowDeactivate
:
257 QApplication::restoreOverrideCursor();
263 return QDialog::event(event
);
266 ////////////////////////////////////////////////////////////
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
);