Updated MediaInfo binary to v0.7.48 (2011-08-17), compiled with MSVC 10.0
[LameXP.git] / src / Dialog_DropBox.cpp
blob0693f50cecda079d63f9182421f9bb4bc70803da
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Dialog_DropBox.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QThread>
28 #include <QMovie>
29 #include <QKeyEvent>
30 #include <QDesktopWidget>
31 #include <QToolTip>
32 #include <QTimer>
34 #define EPS (1.0E-5)
35 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET.font(); _font.setBold(BOLD); WIDGET.setFont(_font); }
37 ////////////////////////////////////////////////////////////
38 // Constructor
39 ////////////////////////////////////////////////////////////
41 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
43 QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint),
44 m_counterLabel(this),
45 m_model(model),
46 m_settings(settings),
47 m_moving(false),
48 m_firstShow(true)
50 //Init the dialog, from the .ui file
51 setupUi(this);
53 //Init counter
54 m_counterLabel.setParent(dropBoxLabel);
55 m_counterLabel.setText("0");
56 m_counterLabel.setAlignment(Qt::AlignHCenter | Qt::AlignTop);
57 SET_FONT_BOLD(m_counterLabel, true);
59 //Prevent close
60 m_canClose = false;
62 //Make transparent
63 setWindowOpacity(0.8);
65 //Translate UI
66 QEvent languageChangeEvent(QEvent::LanguageChange);
67 changeEvent(&languageChangeEvent);
70 ////////////////////////////////////////////////////////////
71 // Destructor
72 ////////////////////////////////////////////////////////////
74 DropBox::~DropBox(void)
78 ////////////////////////////////////////////////////////////
79 // PUBLIC SLOTS
80 ////////////////////////////////////////////////////////////
82 void DropBox::modelChanged(void)
84 if(m_model)
86 m_counterLabel.setText(QString::number(m_model->rowCount()));
90 ////////////////////////////////////////////////////////////
91 // EVENTS
92 ////////////////////////////////////////////////////////////
95 * Re-translate the UI
97 void DropBox::changeEvent(QEvent *e)
99 if(e->type() == QEvent::LanguageChange)
101 Ui::DropBox::retranslateUi(this);
102 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)")));
106 void DropBox::showEvent(QShowEvent *event)
108 QRect screenGeometry = QApplication::desktop()->availableGeometry();
110 resize(dropBoxLabel->pixmap()->size());
111 setMaximumSize(dropBoxLabel->pixmap()->size());
113 m_counterLabel.setGeometry(0, dropBoxLabel->height() - 30, dropBoxLabel->width(), 25);
115 if(m_firstShow)
117 m_firstShow = false;
118 int max_x = screenGeometry.width() - frameGeometry().width() + screenGeometry.left();
119 int max_y = screenGeometry.height() - frameGeometry().height() + screenGeometry.top();
120 move(max_x, max_y);
121 QTimer::singleShot(333, this, SLOT(showToolTip()));
124 if(m_moving)
126 QApplication::restoreOverrideCursor();
127 m_moving = false;
131 void DropBox::keyPressEvent(QKeyEvent *event)
133 event->ignore();
136 void DropBox::keyReleaseEvent(QKeyEvent *event)
138 event->ignore();
141 void DropBox::closeEvent(QCloseEvent *event)
143 if(!m_canClose) event->ignore();
146 void DropBox::mousePressEvent(QMouseEvent *event)
148 if(m_moving)
150 event->ignore();
151 return;
154 if(event->button() == Qt::RightButton)
156 hide();
157 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
158 return;
161 QApplication::setOverrideCursor(Qt::SizeAllCursor);
162 m_moving = true;
163 m_windowReferencePoint = this->pos();
164 m_mouseReferencePoint = event->globalPos();
167 void DropBox::mouseReleaseEvent(QMouseEvent *event)
169 if(m_moving && event->button() != Qt::RightButton)
171 QApplication::restoreOverrideCursor();
172 m_moving = false;
176 void DropBox::mouseMoveEvent(QMouseEvent *event)
178 if(!m_moving)
180 return;
183 static const int magnetic = 22;
184 QRect screenGeometry = QApplication::desktop()->availableGeometry();
186 const int delta_x = m_mouseReferencePoint.x() - event->globalX();
187 const int delta_y = m_mouseReferencePoint.y() - event->globalY();
188 const int max_x = screenGeometry.width() - frameGeometry().width() + screenGeometry.left();
189 const int max_y = screenGeometry.height() - frameGeometry().height() + screenGeometry.top();
191 int new_x = min(max_x, max(screenGeometry.left(), m_windowReferencePoint.x() - delta_x));
192 int new_y = min(max_y, max(screenGeometry.top(), m_windowReferencePoint.y() - delta_y));
194 if(new_x < magnetic)
196 new_x = 0;
198 else if(max_x - new_x < magnetic)
200 new_x = max_x;
203 if(new_y < magnetic)
205 new_y = 0;
207 else if(max_y - new_y < magnetic)
209 new_y = max_y;
212 move(new_x, new_y);
215 void DropBox::showToolTip(void)
217 QToolTip::showText(dropBoxLabel->mapToGlobal(dropBoxLabel->pos()), dropBoxLabel->toolTip());
220 bool DropBox::event(QEvent *event)
222 switch(event->type())
224 case QEvent::Leave:
225 case QEvent::WindowDeactivate:
226 if(m_moving)
228 QApplication::restoreOverrideCursor();
229 m_moving = false;
231 break;
234 return QDialog::event(event);