OSDN Git Service

More internationalization and localization... (again)
[lamexp/LameXP.git] / src / Dialog_DropBox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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 #include <Windows.h>
34
35 #define EPS (1.0E-5)
36 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET.font(); _font.setBold(BOLD); WIDGET.setFont(_font); }
37
38 ////////////////////////////////////////////////////////////
39 // Constructor
40 ////////////////////////////////////////////////////////////
41
42 DropBox::DropBox(QWidget *parent, QAbstractItemModel *model, SettingsModel *settings)
43 :
44         QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint),
45         m_counterLabel(this),
46         m_model(model),
47         m_settings(settings),
48         m_moving(false),
49         m_firstShow(true)
50 {
51         //Init the dialog, from the .ui file
52         setupUi(this);
53         
54         //Init counter
55         m_counterLabel.setParent(dropBoxLabel);
56         m_counterLabel.setText("0");
57         m_counterLabel.setAlignment(Qt::AlignHCenter | Qt::AlignTop);
58         SET_FONT_BOLD(m_counterLabel, true);
59
60         //Prevent close
61         m_canClose = false;
62
63         //Make transparent
64         setWindowOpacity(0.8);
65         
66         //Translate UI
67         retranslateUi(this);
68 }
69
70 ////////////////////////////////////////////////////////////
71 // Destructor
72 ////////////////////////////////////////////////////////////
73
74 DropBox::~DropBox(void)
75 {
76 }
77
78 ////////////////////////////////////////////////////////////
79 // PUBLIC SLOTS
80 ////////////////////////////////////////////////////////////
81
82 void DropBox::doRetranslate(void)
83 {
84         retranslateUi(this);
85 }
86
87 void DropBox::modelChanged(void)
88 {
89         if(m_model)
90         {
91                 m_counterLabel.setText(QString::number(m_model->rowCount()));
92         }
93 }
94
95 ////////////////////////////////////////////////////////////
96 // EVENTS
97 ////////////////////////////////////////////////////////////
98
99 /*
100  * Re-translate the UI
101  */
102 void DropBox::changeEvent(QEvent *e)
103 {
104         if(e->type() == QEvent::LanguageChange)
105         {
106                 Ui::DropBox::retranslateUi(this);
107                 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)")));
108         }
109 }
110
111
112 void DropBox::showEvent(QShowEvent *event)
113 {
114         QRect screenGeometry = QApplication::desktop()->availableGeometry();
115
116         resize(dropBoxLabel->pixmap()->size());
117         setMaximumSize(dropBoxLabel->pixmap()->size());
118         
119         m_counterLabel.setGeometry(0, dropBoxLabel->height() - 30, dropBoxLabel->width(), 25);
120
121         if(m_firstShow)
122         {
123                 m_firstShow = false;
124                 int max_x = screenGeometry.width() - frameGeometry().width();
125                 int max_y = screenGeometry.height() - frameGeometry().height();
126                 move(max_x, max_y);
127                 QTimer::singleShot(333, this, SLOT(showToolTip()));
128         }
129
130         m_moving = false;
131 }
132
133 void DropBox::keyPressEvent(QKeyEvent *event)
134 {
135         event->ignore();
136 }
137
138 void DropBox::keyReleaseEvent(QKeyEvent *event)
139 {
140         event->ignore();
141 }
142
143 void DropBox::closeEvent(QCloseEvent *event)
144 {
145         if(!m_canClose) event->ignore();
146 }
147
148 void DropBox::mousePressEvent(QMouseEvent *event)
149 {
150         if(m_moving)
151         {
152                 event->ignore();
153                 return;
154         }
155         
156         if(event->button() == Qt::RightButton)
157         {
158                 hide();
159                 if(m_settings) m_settings->dropBoxWidgetEnabled(false);
160                 return;
161         }
162
163         QApplication::setOverrideCursor(Qt::SizeAllCursor);
164         m_moving = true;
165         m_windowReferencePoint = this->pos();
166         m_mouseReferencePoint = event->globalPos();
167 }
168
169 void DropBox::mouseReleaseEvent(QMouseEvent *event)
170 {
171         if(m_moving && event->button() != Qt::RightButton)
172         {
173                 QApplication::restoreOverrideCursor();
174                 m_moving = false;
175         }
176 }
177
178 void DropBox::mouseMoveEvent(QMouseEvent *event)
179 {
180         if(!m_moving)
181         {
182                 return;
183         }
184         
185         static const int magnetic = 22;
186         QRect screenGeometry = QApplication::desktop()->availableGeometry();
187         
188         int delta_x = m_mouseReferencePoint.x() - event->globalX();
189         int delta_y = m_mouseReferencePoint.y() - event->globalY();
190         
191         int max_x = screenGeometry.width() - frameGeometry().width();
192         int max_y = screenGeometry.height() - frameGeometry().height();
193
194         int new_x = min(max_x, max(0, m_windowReferencePoint.x() - delta_x));
195         int new_y = min(max_y, max(0, m_windowReferencePoint.y() - delta_y));
196
197         if(new_x < magnetic)
198         {
199                 new_x = 0;
200         }
201         else if(max_x - new_x < magnetic)
202         {
203                 new_x = max_x;
204         }
205
206         if(new_y < magnetic)
207         {
208                 new_y = 0;
209         }
210         else if(max_y - new_y < magnetic)
211         {
212                 new_y = max_y;
213         }
214
215         move(new_x, new_y);
216 }
217
218 void DropBox::showToolTip(void)
219 {
220         QToolTip::showText(dropBoxLabel->mapToGlobal(dropBoxLabel->pos()), dropBoxLabel->toolTip());
221 }