OSDN Git Service

Handle two more plurals correctly.
[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 "../tmp/UIC_DropBox.h"
25
26 #include "Global.h"
27 #include "Model_Settings.h"
28
29 #include <QThread>
30 #include <QMovie>
31 #include <QKeyEvent>
32 #include <QDesktopWidget>
33 #include <QToolTip>
34 #include <QTimer>
35
36 #define EPS (1.0E-5)
37 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = (WIDGET)->font(); _font.setBold(BOLD); (WIDGET)->setFont(_font); }
38
39 ////////////////////////////////////////////////////////////
40 // Constructor
41 ////////////////////////////////////////////////////////////
42
43 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
44 :
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)
51 {
52         //Init the dialog, from the .ui file
53         ui->setupUi(this);
54
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);
61
62         m_windowReferencePoint = new QPoint;
63         m_mouseReferencePoint = new QPoint;
64
65         //Prevent close
66         m_canClose = false;
67
68         //Make transparent
69         setWindowOpacity(0.8);
70         
71         //Translate UI
72         QEvent languageChangeEvent(QEvent::LanguageChange);
73         changeEvent(&languageChangeEvent);
74 }
75
76 ////////////////////////////////////////////////////////////
77 // Destructor
78 ////////////////////////////////////////////////////////////
79
80 DropBox::~DropBox(void)
81 {
82         LAMEXP_DELETE(m_counterLabel);
83         LAMEXP_DELETE(m_windowReferencePoint);
84         LAMEXP_DELETE(m_mouseReferencePoint);
85         LAMEXP_DELETE(ui);
86 }
87
88 ////////////////////////////////////////////////////////////
89 // PUBLIC SLOTS
90 ////////////////////////////////////////////////////////////
91
92 void DropBox::modelChanged(void)
93 {
94         if(m_model)
95         {
96                 m_counterLabel->setText(QString::number(m_model->rowCount()));
97         }
98 }
99
100 ////////////////////////////////////////////////////////////
101 // EVENTS
102 ////////////////////////////////////////////////////////////
103
104 /*
105  * Re-translate the UI
106  */
107 void DropBox::changeEvent(QEvent *e)
108 {
109         if(e->type() == QEvent::LanguageChange)
110         {
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)")));
113         }
114 }
115
116 void DropBox::showEvent(QShowEvent *event)
117 {
118         QRect screenGeometry = QApplication::desktop()->availableGeometry();
119
120         resize(ui->dropBoxLabel->pixmap()->size());
121         setMaximumSize(ui->dropBoxLabel->pixmap()->size());
122         
123         m_counterLabel->setGeometry(0, ui->dropBoxLabel->height() - 30, ui->dropBoxLabel->width(), 25);
124
125         if(m_firstShow)
126         {
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()));
132         }
133
134         if(m_moving)
135         {
136                 QApplication::restoreOverrideCursor();
137                 m_moving = false;
138         }
139 }
140
141 void DropBox::keyPressEvent(QKeyEvent *event)
142 {
143         event->ignore();
144 }
145
146 void DropBox::keyReleaseEvent(QKeyEvent *event)
147 {
148         event->ignore();
149 }
150
151 void DropBox::closeEvent(QCloseEvent *event)
152 {
153         if(!m_canClose) event->ignore();
154 }
155
156 void DropBox::mousePressEvent(QMouseEvent *event)
157 {
158         if(m_moving)
159         {
160                 event->ignore();
161                 return;
162         }
163         
164         if(event->button() == Qt::RightButton)
165         {
166                 hide();
167                 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
168                 return;
169         }
170
171         QApplication::setOverrideCursor(Qt::SizeAllCursor);
172         *m_windowReferencePoint = this->pos();
173         *m_mouseReferencePoint = event->globalPos();
174         m_moving = true;
175 }
176
177 void DropBox::mouseReleaseEvent(QMouseEvent *event)
178 {
179         if(m_moving && event->button() != Qt::RightButton)
180         {
181                 QApplication::restoreOverrideCursor();
182                 m_moving = false;
183         }
184 }
185
186 void DropBox::mouseMoveEvent(QMouseEvent *event)
187 {
188         if(!m_moving)
189         {
190                 return;
191         }
192         
193         static const int magnetic = 22;
194         QRect screenGeometry = QApplication::desktop()->availableGeometry();
195         
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();
200
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));
203
204         if(new_x < magnetic)
205         {
206                 new_x = 0;
207         }
208         else if(max_x - new_x < magnetic)
209         {
210                 new_x = max_x;
211         }
212
213         if(new_y < magnetic)
214         {
215                 new_y = 0;
216         }
217         else if(max_y - new_y < magnetic)
218         {
219                 new_y = max_y;
220         }
221
222         move(new_x, new_y);
223 }
224
225 void DropBox::showToolTip(void)
226 {
227         QToolTip::showText(ui->dropBoxLabel->mapToGlobal(ui->dropBoxLabel->pos()), ui->dropBoxLabel->toolTip());
228 }
229
230 bool DropBox::event(QEvent *event)
231 {
232         switch(event->type())
233         {
234         case QEvent::Leave:
235         case QEvent::WindowDeactivate:
236                 if(m_moving)
237                 {
238                         QApplication::restoreOverrideCursor();
239                         m_moving = false;
240                 }
241                 break;
242         }
243
244         return QDialog::event(event);
245 }