1/***************************************************************************
2 kmagselrect.cpp - description
3 -------------------
4 begin : Mon Feb 12 23:45:41 EST 2001
5 copyright : (C) 2001-2003 by Sarang Lakare
6 email : sarang#users.sf.net
7 copyright : (C) 2003-2004 by Olaf Schmidt
8 email : ojschmidt@kde.org
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20#include "kmagselrect.h"
21#include "kmagselrect.moc"
22
23#include <QtGui/QApplication>
24#include <QtGui/QCursor>
25#include <QtGui/QPixmap>
26#include <QtGui/QBitmap>
27#include <QtGui/QMouseEvent>
28#include <QtGui/QLabel>
29#include <QtGui/QDesktopWidget>
30
31#include <klocale.h>
32
33static uchar line_bits[] = {0x2d, 0x96, 0x4b, 0xa5, 0xd2, 0x69, 0xb4, 0x5a};
34
35static QColor titleColor = QColor (0,0,128);
36static QColor titleBtnColor = QColor (255,255,0);
37static QColor textColor = QColor (255,255,255);
38
39static int frameSize = 10;
40static int titleSize = 24;
41
42void setTitleColors (const QColor &title, const QColor &text, const QColor &titleBtn)
43{
44 titleColor = title;
45 titleBtnColor = titleBtn;
46 textColor = text;
47}
48
49void setFrameSize (int size)
50{
51 frameSize = size;
52}
53
54void setTitleSize (int size)
55{
56 titleSize = size;
57}
58
59QColor getTitleColor ()
60{
61 return titleColor;
62}
63
64QColor getTitleBtnColor ()
65{
66 return titleBtnColor;
67}
68
69QColor getTextColor ()
70{
71 return textColor;
72}
73
74int getFrameSize ()
75{
76 return frameSize;
77}
78
79int getTitleSize ()
80{
81 if (titleSize > frameSize)
82 return titleSize;
83 else
84 return frameSize;
85}
86
87//--------------------------------------------------------------------------
88// Construction
89//--------------------------------------------------------------------------
90
91KMagSelRect::KMagSelRect(QWidget *parent) :
92 QRect()
93{
94 init(parent);
95}
96
97KMagSelRect::KMagSelRect(const QPoint &topLeft, const QPoint &bottomRight,
98 QWidget *parent) :
99QRect(topLeft, bottomRight)
100{
101 init(parent);
102}
103
104KMagSelRect::KMagSelRect(const QPoint &topLeft, const QSize &size,
105 QWidget *parent) :
106QRect(topLeft, size)
107{
108 init(parent);
109}
110
111KMagSelRect::KMagSelRect(int left, int top, int width, int height,
112 QWidget *parent) :
113QRect(left, top, width, height)
114{
115 init(parent);
116}
117
118void KMagSelRect::init(QWidget *parent)
119{
120 // Make sure parent is the window itself, not a widget within the window
121 if (parent != 0)
122 while (parent->parentWidget() != 0)
123 parent=parent->parentWidget();
124
125 selectionwindow = 0;
126 selWindowParent = parent;
127
128 m_alwaysVisible = false;
129}
130
131KMagSelRect::~KMagSelRect()
132{
133}
134
135//--------------------------------------------------------------------------
136//
137//--------------------------------------------------------------------------
138
139bool KMagSelRect::visible()
140{
141 return (selectionwindow != 0);
142}
143
144void KMagSelRect::alwaysVisible(bool visible)
145{
146 m_alwaysVisible = visible;
147}
148
149
150//--------------------------------------------------------------------------
151// Slots
152//--------------------------------------------------------------------------
153
154void KMagSelRect::show()
155{
156 if (selectionwindow == 0) {
157 selectionwindow = new KMagSelWin (selWindowParent);
158 selectionwindow->setObjectName( QLatin1String("selectionwindow" ));
159 connect (selectionwindow, SIGNAL (resized()), this, SLOT (selWinResized()));
160
161 update();
162 selectionwindow->show();
163 selWindowParent->activateWindow();
164 }
165}
166
167void KMagSelRect::hide()
168{
169 if(m_alwaysVisible)
170 return;
171 if (selectionwindow != 0) {
172 selectionwindow->hide();
173 delete selectionwindow;
174 selectionwindow = 0;
175 }
176}
177
178void KMagSelRect::update()
179{
180 if (selectionwindow != 0)
181 selectionwindow->setSelRect (QRect (topLeft(), bottomRight()));
182}
183
184void KMagSelRect::selWinResized()
185{
186 if (selectionwindow != 0)
187 {
188 QRect newRect = selectionwindow->getSelRect();
189 setRect (newRect.x(), newRect.y(), newRect.width(), newRect.height());
190 }
191}
192
193//--------------------------------------------------------------------------
194// KMagSelWin
195//--------------------------------------------------------------------------
196
197void setPaletteColor(QWidget* w, QPalette::ColorRole r, const QColor& c)
198{
199 QPalette p = w->palette();
200 p.setColor(r, c);
201 w->setPalette(p);
202}
203
204KMagSelWin::KMagSelWin ( QWidget * parent, Qt::WFlags ) :
205 QWidget(parent) //Qt::WStyle_Customize | Qt::WStyle_NoBorder | Qt::WStyle_StaysOnTop | Qt::WType_TopLevel | Qt::WX11BypassWM)
206{
207 setWindowFlags( Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
208
209 QPalette p = palette();
210 p.setBrush(backgroundRole(), QBrush(QBitmap::fromData( QSize(8, 8), line_bits)));
211 setPalette(p);
212
213 titleBar = new KMagSelWinCorner (this);
214 titleBar->setObjectName( QLatin1String("titlebar" ));
215 setPaletteColor(titleBar, QPalette::Background, getTitleColor());
216 setPaletteColor(titleBar, QPalette::Foreground, getTextColor());
217 titleBar->setText(i18n("Selection Window")+QLatin1String( " - " )+i18n("KMagnifier"));
218 connect (titleBar, SIGNAL (startResizing()), this, SLOT (startResizing()));
219 connect (titleBar, SIGNAL (resized(QPoint)), this, SLOT (titleMoved(QPoint)));
220
221 topLeftCorner = new KMagSelWinCorner (this);
222 topLeftCorner->setObjectName( QLatin1String("topleft" ));
223 topLeftCorner->setCursor (Qt::SizeFDiagCursor);
224 setPaletteColor(topLeftCorner, QPalette::Background, getTitleBtnColor());
225 connect (topLeftCorner, SIGNAL (startResizing()), this, SLOT (startResizing()));
226 connect (topLeftCorner, SIGNAL (resized(QPoint)), this, SLOT (topLeftResized(QPoint)));
227
228 topRightCorner = new KMagSelWinCorner (this);
229 topRightCorner->setObjectName( QLatin1String("topright" ));
230 topRightCorner->setCursor (Qt::SizeBDiagCursor);
231 setPaletteColor(topRightCorner, QPalette::Background, getTitleBtnColor ());
232 connect (topRightCorner, SIGNAL (startResizing()), this, SLOT (startResizing()));
233 connect (topRightCorner, SIGNAL (resized(QPoint)), this, SLOT (topRightResized(QPoint)));
234
235 bottomLeftCorner = new KMagSelWinCorner (this);
236 bottomLeftCorner->setObjectName( QLatin1String("bottomleft" ));
237 bottomLeftCorner->setCursor (Qt::SizeBDiagCursor);
238 setPaletteColor(bottomLeftCorner, QPalette::Background, getTitleBtnColor());
239 connect (bottomLeftCorner, SIGNAL (startResizing()), this, SLOT (startResizing()));
240 connect (bottomLeftCorner, SIGNAL (resized(QPoint)), this, SLOT (bottomLeftResized(QPoint)));
241
242 bottomRightCorner = new KMagSelWinCorner (this);
243 bottomRightCorner->setObjectName( QLatin1String("bottomright" ));
244 bottomRightCorner->setCursor (Qt::SizeFDiagCursor);
245 setPaletteColor(bottomRightCorner, QPalette::Background, getTitleBtnColor ());
246 connect (bottomRightCorner, SIGNAL (startResizing()), this, SLOT (startResizing()));
247 connect (bottomRightCorner, SIGNAL (resized(QPoint)), this, SLOT (bottomRightResized(QPoint)));
248}
249
250KMagSelWin::~KMagSelWin()
251{
252 delete titleBar;
253 delete topLeftCorner;
254 delete topRightCorner;
255 delete bottomLeftCorner;
256 delete bottomRightCorner;
257}
258
259void KMagSelWin::setSelRect (const QRect &_selRect)
260{
261 QRect selRect = _selRect.normalized();
262
263 if (selRect.left() < 0)
264 selRect.setLeft (0);
265 if (selRect.top() < 0)
266 selRect.setTop (0);
267 if (selRect.right() > QApplication::desktop()->width())
268 selRect.setRight (QApplication::desktop()->width());
269 if (selRect.bottom() > QApplication::desktop()->height())
270 selRect.setBottom (QApplication::desktop()->height());
271
272 setGeometry (
273 selRect.left() - getFrameSize(),
274 selRect.top() - getTitleSize() - 2,
275 selRect.width() + getFrameSize() + getFrameSize(),
276 selRect.height() + getFrameSize() + getTitleSize()+2);
277
278 int w = getFrameSize();
279 if (selRect.width() < w+w)
280 w = static_cast<int>(selRect.width()/2);
281
282 int h = getFrameSize();
283 if (selRect.height() < h+h)
284 h = static_cast<int>(selRect.height()/2);
285
286 setMask (QRegion (QRect (0, 0, width(), height ()))
287 - QRegion (QRect (getFrameSize(), getTitleSize()+2, selRect.width(), selRect.height()))
288 - QRegion (QRect (0, 0, getFrameSize()+w, getTitleSize()+2-getFrameSize()))
289 - QRegion (QRect (width()-getFrameSize()-w, 0, getFrameSize()+w, getTitleSize()+2-getFrameSize()))
290 - QRegion (QRect (0, getTitleSize()+2+h, getFrameSize()-2, selRect.height()-h-h))
291 - QRegion (QRect (width()-getFrameSize()+2, getTitleSize()+2+h, getFrameSize()-2, selRect.height()-h-h))
292 - QRegion (QRect (getFrameSize()+w, height()-getFrameSize()+2, selRect.width()-w-w, getFrameSize()-2)));
293
294 titleBar->setGeometry (getFrameSize()+w, 0, selRect.width()-h-h, getTitleSize());
295 topLeftCorner->setGeometry (0, getTitleSize()+2-getFrameSize(), getFrameSize()+w, getFrameSize()+h);
296 topRightCorner->setGeometry (width()-getFrameSize()-w, getTitleSize()+2-getFrameSize(), getFrameSize()+w, getFrameSize()+h);
297 bottomLeftCorner->setGeometry (0, height()-getFrameSize()-h, getFrameSize()+w, getFrameSize()+h);
298 bottomRightCorner->setGeometry (width()-getFrameSize()-w, height()-getFrameSize()-h, getFrameSize()+w, getFrameSize()+h);
299}
300
301QRect KMagSelWin::getSelRect ()
302{
303 return QRect (
304 x() + getFrameSize(),
305 y() + getTitleSize()+2,
306 width() - getFrameSize() - getFrameSize(),
307 height() - getFrameSize() - getTitleSize()-2);
308}
309
310void KMagSelWin::startResizing ()
311{
312 oldSelRect = getSelRect();
313}
314
315void KMagSelWin::titleMoved (const QPoint &offset)
316{
317 QRect selRect = oldSelRect;
318 selRect.translate (offset.x(), offset.y());
319 setSelRect (selRect);
320 emit resized ();
321}
322
323void KMagSelWin::topLeftResized (const QPoint &offset)
324{
325 setSelRect (QRect(oldSelRect.topLeft() + offset, oldSelRect.bottomRight ()));
326 emit resized();
327}
328
329void KMagSelWin::topRightResized (const QPoint &offset)
330{
331 setSelRect (QRect(oldSelRect.topRight() + offset, oldSelRect.bottomLeft ()));
332 emit resized();
333}
334
335void KMagSelWin::bottomLeftResized (const QPoint &offset)
336{
337 setSelRect (QRect(oldSelRect.bottomLeft() + offset, oldSelRect.topRight ()));
338 emit resized();
339}
340
341void KMagSelWin::bottomRightResized (const QPoint &offset)
342{
343 setSelRect (QRect(oldSelRect.bottomRight() + offset, oldSelRect.topLeft()));
344 emit resized();
345}
346
347
348//--------------------------------------------------------------------------
349// KMagSelWinCorner
350//--------------------------------------------------------------------------
351
352KMagSelWinCorner::KMagSelWinCorner ( QWidget * parent, Qt::WFlags f ) :
353 QLabel (parent, f)
354{
355 setFrameStyle (QFrame::WinPanel | QFrame::Raised);
356 setLineWidth (1);
357}
358
359KMagSelWinCorner::~KMagSelWinCorner()
360{
361}
362
363void KMagSelWinCorner::mousePressEvent ( QMouseEvent * e )
364{
365 oldPos = e->globalPos ();
366 emit startResizing ();
367}
368
369void KMagSelWinCorner::mouseReleaseEvent ( QMouseEvent * e )
370{
371 setFrameShadow (QFrame::Raised);
372 emit resized (e->globalPos () - oldPos);
373}
374
375void KMagSelWinCorner::mouseMoveEvent ( QMouseEvent * e )
376{
377 emit resized (e->globalPos () - oldPos);
378}
379