OSDN Git Service

Switched the method of how the Designer UI file is used in the UpdateDialog class...
[lamexp/LameXP.git] / src / Dialog_DropBox.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Dialog_DropBox.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QThread>
28 #include <QMovie>
29 #include <QKeyEvent>
30 #include <QDesktopWidget>
31 #include <QToolTip>
32 #include <QTimer>
33
34 #define EPS (1.0E-5)
35 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET.font(); _font.setBold(BOLD); WIDGET.setFont(_font); }
36
37 ////////////////////////////////////////////////////////////
38 // Constructor
39 ////////////////////////////////////////////////////////////
40
41 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
42 :
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)
49 {
50         //Init the dialog, from the .ui file
51         setupUi(this);
52         
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);
58
59         //Prevent close
60         m_canClose = false;
61
62         //Make transparent
63         setWindowOpacity(0.8);
64         
65         //Translate UI
66         QEvent languageChangeEvent(QEvent::LanguageChange);
67         changeEvent(&languageChangeEvent);
68 }
69
70 ////////////////////////////////////////////////////////////
71 // Destructor
72 ////////////////////////////////////////////////////////////
73
74 DropBox::~DropBox(void)
75 {
76 }
77
78 ////////////////////////////////////////////////////////////
79 // PUBLIC SLOTS
80 ////////////////////////////////////////////////////////////
81
82 void DropBox::modelChanged(void)
83 {
84         if(m_model)
85         {
86                 m_counterLabel.setText(QString::number(m_model->rowCount()));
87         }
88 }
89
90 ////////////////////////////////////////////////////////////
91 // EVENTS
92 ////////////////////////////////////////////////////////////
93
94 /*
95  * Re-translate the UI
96  */
97 void DropBox::changeEvent(QEvent *e)
98 {
99         if(e->type() == QEvent::LanguageChange)
100         {
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)")));
103         }
104 }
105
106 void DropBox::showEvent(QShowEvent *event)
107 {
108         QRect screenGeometry = QApplication::desktop()->availableGeometry();
109
110         resize(dropBoxLabel->pixmap()->size());
111         setMaximumSize(dropBoxLabel->pixmap()->size());
112         
113         m_counterLabel.setGeometry(0, dropBoxLabel->height() - 30, dropBoxLabel->width(), 25);
114
115         if(m_firstShow)
116         {
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()));
122         }
123
124         if(m_moving)
125         {
126                 QApplication::restoreOverrideCursor();
127                 m_moving = false;
128         }
129 }
130
131 void DropBox::keyPressEvent(QKeyEvent *event)
132 {
133         event->ignore();
134 }
135
136 void DropBox::keyReleaseEvent(QKeyEvent *event)
137 {
138         event->ignore();
139 }
140
141 void DropBox::closeEvent(QCloseEvent *event)
142 {
143         if(!m_canClose) event->ignore();
144 }
145
146 void DropBox::mousePressEvent(QMouseEvent *event)
147 {
148         if(m_moving)
149         {
150                 event->ignore();
151                 return;
152         }
153         
154         if(event->button() == Qt::RightButton)
155         {
156                 hide();
157                 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
158                 return;
159         }
160
161         QApplication::setOverrideCursor(Qt::SizeAllCursor);
162         m_moving = true;
163         m_windowReferencePoint = this->pos();
164         m_mouseReferencePoint = event->globalPos();
165 }
166
167 void DropBox::mouseReleaseEvent(QMouseEvent *event)
168 {
169         if(m_moving && event->button() != Qt::RightButton)
170         {
171                 QApplication::restoreOverrideCursor();
172                 m_moving = false;
173         }
174 }
175
176 void DropBox::mouseMoveEvent(QMouseEvent *event)
177 {
178         if(!m_moving)
179         {
180                 return;
181         }
182         
183         static const int magnetic = 22;
184         QRect screenGeometry = QApplication::desktop()->availableGeometry();
185         
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();
190
191         int new_x = qMin(max_x, qMax(screenGeometry.left(), m_windowReferencePoint.x() - delta_x));
192         int new_y = qMin(max_y, qMax(screenGeometry.top(), m_windowReferencePoint.y() - delta_y));
193
194         if(new_x < magnetic)
195         {
196                 new_x = 0;
197         }
198         else if(max_x - new_x < magnetic)
199         {
200                 new_x = max_x;
201         }
202
203         if(new_y < magnetic)
204         {
205                 new_y = 0;
206         }
207         else if(max_y - new_y < magnetic)
208         {
209                 new_y = max_y;
210         }
211
212         move(new_x, new_y);
213 }
214
215 void DropBox::showToolTip(void)
216 {
217         QToolTip::showText(dropBoxLabel->mapToGlobal(dropBoxLabel->pos()), dropBoxLabel->toolTip());
218 }
219
220 bool DropBox::event(QEvent *event)
221 {
222         switch(event->type())
223         {
224         case QEvent::Leave:
225         case QEvent::WindowDeactivate:
226                 if(m_moving)
227                 {
228                         QApplication::restoreOverrideCursor();
229                         m_moving = false;
230                 }
231                 break;
232         }
233
234         return QDialog::event(event);
235 }