Updated Ukrainian translation.
[LameXP.git] / src / Dialog_DropBox.cpp
blob09c02f9a4fb69b9aa7b6fe9dd45180b1bf715e06
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 "../tmp/UIC_DropBox.h"
26 #include "Global.h"
27 #include "Model_Settings.h"
29 #include <QThread>
30 #include <QMovie>
31 #include <QKeyEvent>
32 #include <QDesktopWidget>
33 #include <QToolTip>
34 #include <QTimer>
36 #define EPS (1.0E-5)
37 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = (WIDGET)->font(); _font.setBold(BOLD); (WIDGET)->setFont(_font); }
39 ////////////////////////////////////////////////////////////
40 // Constructor
41 ////////////////////////////////////////////////////////////
43 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
45 QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint),
46 ui(new Ui::DropBox),
47 m_model(model),
48 m_settings(settings),
49 m_moving(false),
50 m_firstShow(true)
52 //Init the dialog, from the .ui file
53 ui->setupUi(this);
55 //Init counter
56 m_counterLabel = new QLabel(this);
57 m_counterLabel->setParent(ui->dropBoxLabel);
58 m_counterLabel->setText("0");
59 m_counterLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
60 SET_FONT_BOLD(m_counterLabel, true);
62 m_windowReferencePoint = new QPoint;
63 m_mouseReferencePoint = new QPoint;
65 //Prevent close
66 m_canClose = false;
68 //Make transparent
69 setWindowOpacity(0.8);
71 //Translate UI
72 QEvent languageChangeEvent(QEvent::LanguageChange);
73 changeEvent(&languageChangeEvent);
76 ////////////////////////////////////////////////////////////
77 // Destructor
78 ////////////////////////////////////////////////////////////
80 DropBox::~DropBox(void)
82 LAMEXP_DELETE(m_counterLabel);
83 LAMEXP_DELETE(m_windowReferencePoint);
84 LAMEXP_DELETE(m_mouseReferencePoint);
85 LAMEXP_DELETE(ui);
88 ////////////////////////////////////////////////////////////
89 // PUBLIC SLOTS
90 ////////////////////////////////////////////////////////////
92 void DropBox::modelChanged(void)
94 if(m_model)
96 m_counterLabel->setText(QString::number(m_model->rowCount()));
100 ////////////////////////////////////////////////////////////
101 // EVENTS
102 ////////////////////////////////////////////////////////////
105 * Re-translate the UI
107 void DropBox::changeEvent(QEvent *e)
109 if(e->type() == QEvent::LanguageChange)
111 ui->retranslateUi(this);
112 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)")));
116 void DropBox::showEvent(QShowEvent *event)
118 QRect screenGeometry = QApplication::desktop()->availableGeometry();
120 resize(ui->dropBoxLabel->pixmap()->size());
121 setMaximumSize(ui->dropBoxLabel->pixmap()->size());
123 m_counterLabel->setGeometry(0, ui->dropBoxLabel->height() - 30, ui->dropBoxLabel->width(), 25);
125 if(m_firstShow)
127 m_firstShow = false;
128 int max_x = screenGeometry.width() - frameGeometry().width() + screenGeometry.left();
129 int max_y = screenGeometry.height() - frameGeometry().height() + screenGeometry.top();
130 move(max_x, max_y);
131 QTimer::singleShot(333, this, SLOT(showToolTip()));
134 if(m_moving)
136 QApplication::restoreOverrideCursor();
137 m_moving = false;
141 void DropBox::keyPressEvent(QKeyEvent *event)
143 event->ignore();
146 void DropBox::keyReleaseEvent(QKeyEvent *event)
148 event->ignore();
151 void DropBox::closeEvent(QCloseEvent *event)
153 if(!m_canClose) event->ignore();
156 void DropBox::mousePressEvent(QMouseEvent *event)
158 if(m_moving)
160 event->ignore();
161 return;
164 if(event->button() == Qt::RightButton)
166 hide();
167 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
168 return;
171 QApplication::setOverrideCursor(Qt::SizeAllCursor);
172 *m_windowReferencePoint = this->pos();
173 *m_mouseReferencePoint = event->globalPos();
174 m_moving = true;
177 void DropBox::mouseReleaseEvent(QMouseEvent *event)
179 if(m_moving && event->button() != Qt::RightButton)
181 QApplication::restoreOverrideCursor();
182 m_moving = false;
186 void DropBox::mouseMoveEvent(QMouseEvent *event)
188 if(!m_moving)
190 return;
193 static const int magnetic = 22;
194 QRect screenGeometry = QApplication::desktop()->availableGeometry();
196 const int delta_x = m_mouseReferencePoint->x() - event->globalX();
197 const int delta_y = m_mouseReferencePoint->y() - event->globalY();
198 const int max_x = screenGeometry.width() - frameGeometry().width() + screenGeometry.left();
199 const int max_y = screenGeometry.height() - frameGeometry().height() + screenGeometry.top();
201 int new_x = qMin(max_x, qMax(screenGeometry.left(), m_windowReferencePoint->x() - delta_x));
202 int new_y = qMin(max_y, qMax(screenGeometry.top(), m_windowReferencePoint->y() - delta_y));
204 if(new_x < magnetic)
206 new_x = 0;
208 else if(max_x - new_x < magnetic)
210 new_x = max_x;
213 if(new_y < magnetic)
215 new_y = 0;
217 else if(max_y - new_y < magnetic)
219 new_y = max_y;
222 move(new_x, new_y);
225 void DropBox::showToolTip(void)
227 QToolTip::showText(ui->dropBoxLabel->mapToGlobal(ui->dropBoxLabel->pos()), ui->dropBoxLabel->toolTip());
230 bool DropBox::event(QEvent *event)
232 switch(event->type())
234 case QEvent::Leave:
235 case QEvent::WindowDeactivate:
236 if(m_moving)
238 QApplication::restoreOverrideCursor();
239 m_moving = false;
241 break;
244 return QDialog::event(event);