1 /***************************************************************************
2 kmagselrect.cpp - description
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 ***************************************************************************/
11 /***************************************************************************
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. *
18 ***************************************************************************/
20 #include "kmagselrect.h"
21 #include "kmagselrect.moc"
23 #include <QApplication>
28 #include <QMouseEvent>
30 #include <QDesktopWidget>
33 static uchar line_bits
[] = {0x2d, 0x96, 0x4b, 0xa5, 0xd2, 0x69, 0xb4, 0x5a};
35 static QColor titleColor
= QColor (0,0,128);
36 static QColor titleBtnColor
= QColor (255,255,0);
37 static QColor textColor
= QColor (255,255,255);
39 static int frameSize
= 10;
40 static int titleSize
= 24;
42 void setTitleColors (const QColor
&title
, const QColor
&text
, const QColor
&titleBtn
)
45 titleBtnColor
= titleBtn
;
49 void setFrameSize (int size
)
54 void setTitleSize (int size
)
59 QColor
getTitleColor ()
64 QColor
getTitleBtnColor ()
69 QColor
getTextColor ()
81 if (titleSize
> frameSize
)
87 //--------------------------------------------------------------------------
89 //--------------------------------------------------------------------------
91 KMagSelRect::KMagSelRect(QWidget
*parent
) :
97 KMagSelRect::KMagSelRect(const QPoint
&topLeft
, const QPoint
&bottomRight
,
99 QRect(topLeft
, bottomRight
)
104 KMagSelRect::KMagSelRect(const QPoint
&topLeft
, const QSize
&size
,
111 KMagSelRect::KMagSelRect(int left
, int top
, int width
, int height
,
113 QRect(left
, top
, width
, height
)
118 void KMagSelRect::init(QWidget
*parent
)
120 // Make sure parent is the window itself, not a widget within the window
122 while (parent
->parentWidget (true) != 0)
123 parent
=parent
->parentWidget (true);
126 selWindowParent
= parent
;
128 m_alwaysVisible
= false;
131 KMagSelRect::~KMagSelRect()
135 //--------------------------------------------------------------------------
137 //--------------------------------------------------------------------------
139 bool KMagSelRect::visible()
141 return (selectionwindow
!= 0);
144 void KMagSelRect::alwaysVisible(bool visible
)
146 m_alwaysVisible
= visible
;
150 //--------------------------------------------------------------------------
152 //--------------------------------------------------------------------------
154 void KMagSelRect::show()
156 if (selectionwindow
== 0) {
157 selectionwindow
= new KMagSelWin (selWindowParent
);
158 selectionwindow
->setObjectName("selectionwindow");
159 connect (selectionwindow
, SIGNAL (resized ()), this, SLOT (selWinResized ()));
162 selectionwindow
->show();
163 selWindowParent
->setActiveWindow();
167 void KMagSelRect::hide()
171 if (selectionwindow
!= 0) {
172 selectionwindow
->hide();
173 delete selectionwindow
;
178 void KMagSelRect::update()
180 if (selectionwindow
!= 0)
181 selectionwindow
->setSelRect (QRect (topLeft(), bottomRight()));
184 void KMagSelRect::selWinResized()
186 if (selectionwindow
!= 0)
188 QRect newRect
= selectionwindow
->getSelRect();
189 setRect (newRect
.x(), newRect
.y(), newRect
.width(), newRect
.height());
193 //--------------------------------------------------------------------------
195 //--------------------------------------------------------------------------
197 KMagSelWin::KMagSelWin ( QWidget
* parent
, Qt::WFlags
) :
198 QWidget (parent
, Qt::WStyle_Customize
| Qt::WStyle_NoBorder
| Qt::WStyle_StaysOnTop
| Qt::WType_TopLevel
| Qt::WX11BypassWM
)
200 QBitmap line
= QBitmap::fromData( QSize(8, 8), line_bits
);
201 setPaletteBackgroundPixmap (line
);
202 setBackgroundOrigin (QWidget::WindowOrigin
);
204 titleBar
= new KMagSelWinCorner (this);
205 titleBar
->setObjectName("titlebar");
206 titleBar
->setPaletteBackgroundColor (getTitleColor ());
207 titleBar
->setPaletteForegroundColor (getTextColor ());
208 titleBar
->setText(i18n("Selection Window")+" - "+i18n("KMagnifier"));
209 connect (titleBar
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
210 connect (titleBar
, SIGNAL (resized (QPoint
)), this, SLOT (titleMoved (QPoint
)));
212 topLeftCorner
= new KMagSelWinCorner (this);
213 topLeftCorner
->setObjectName("topleft");
214 topLeftCorner
->setCursor (Qt::SizeFDiagCursor
);
215 topLeftCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
216 connect (topLeftCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
217 connect (topLeftCorner
, SIGNAL (resized (QPoint
)), this, SLOT (topLeftResized (QPoint
)));
219 topRightCorner
= new KMagSelWinCorner (this);
220 topRightCorner
->setObjectName("topright");
221 topRightCorner
->setCursor (Qt::SizeBDiagCursor
);
222 topRightCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
223 connect (topRightCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
224 connect (topRightCorner
, SIGNAL (resized (QPoint
)), this, SLOT (topRightResized (QPoint
)));
226 bottomLeftCorner
= new KMagSelWinCorner (this);
227 bottomLeftCorner
->setObjectName("bottomleft");
228 bottomLeftCorner
->setCursor (Qt::SizeBDiagCursor
);
229 bottomLeftCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
230 connect (bottomLeftCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
231 connect (bottomLeftCorner
, SIGNAL (resized (QPoint
)), this, SLOT (bottomLeftResized (QPoint
)));
233 bottomRightCorner
= new KMagSelWinCorner (this);
234 bottomRightCorner
->setObjectName("bottomright");
235 bottomRightCorner
->setCursor (Qt::SizeFDiagCursor
);
236 bottomRightCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
237 connect (bottomRightCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
238 connect (bottomRightCorner
, SIGNAL (resized (QPoint
)), this, SLOT (bottomRightResized (QPoint
)));
241 KMagSelWin::~KMagSelWin()
244 delete topLeftCorner
;
245 delete topRightCorner
;
246 delete bottomLeftCorner
;
247 delete bottomRightCorner
;
250 void KMagSelWin::setSelRect (const QRect
&_selRect
)
252 QRect selRect
= _selRect
.normalized();
254 if (selRect
.left() < 0)
256 if (selRect
.top() < 0)
258 if (selRect
.right() > QApplication::desktop()->width())
259 selRect
.setRight (QApplication::desktop()->width());
260 if (selRect
.bottom() > QApplication::desktop()->height())
261 selRect
.setBottom (QApplication::desktop()->height());
264 selRect
.left() - getFrameSize(),
265 selRect
.top() - getTitleSize() - 2,
266 selRect
.width() + getFrameSize() + getFrameSize(),
267 selRect
.height() + getFrameSize() + getTitleSize()+2);
269 int w
= getFrameSize();
270 if (selRect
.width() < w
+w
)
271 w
= static_cast<int>(selRect
.width()/2);
273 int h
= getFrameSize();
274 if (selRect
.height() < h
+h
)
275 h
= static_cast<int>(selRect
.height()/2);
277 setMask (QRegion (QRect (0, 0, width(), height ()))
278 - QRegion (QRect (getFrameSize(), getTitleSize()+2, selRect
.width(), selRect
.height()))
279 - QRegion (QRect (0, 0, getFrameSize()+w
, getTitleSize()+2-getFrameSize()))
280 - QRegion (QRect (width()-getFrameSize()-w
, 0, getFrameSize()+w
, getTitleSize()+2-getFrameSize()))
281 - QRegion (QRect (0, getTitleSize()+2+h
, getFrameSize()-2, selRect
.height()-h
-h
))
282 - QRegion (QRect (width()-getFrameSize()+2, getTitleSize()+2+h
, getFrameSize()-2, selRect
.height()-h
-h
))
283 - QRegion (QRect (getFrameSize()+w
, height()-getFrameSize()+2, selRect
.width()-w
-w
, getFrameSize()-2)));
285 titleBar
->setGeometry (getFrameSize()+w
, 0, selRect
.width()-h
-h
, getTitleSize());
286 topLeftCorner
->setGeometry (0, getTitleSize()+2-getFrameSize(), getFrameSize()+w
, getFrameSize()+h
);
287 topRightCorner
->setGeometry (width()-getFrameSize()-w
, getTitleSize()+2-getFrameSize(), getFrameSize()+w
, getFrameSize()+h
);
288 bottomLeftCorner
->setGeometry (0, height()-getFrameSize()-h
, getFrameSize()+w
, getFrameSize()+h
);
289 bottomRightCorner
->setGeometry (width()-getFrameSize()-w
, height()-getFrameSize()-h
, getFrameSize()+w
, getFrameSize()+h
);
292 QRect
KMagSelWin::getSelRect ()
295 x() + getFrameSize(),
296 y() + getTitleSize()+2,
297 width() - getFrameSize() - getFrameSize(),
298 height() - getFrameSize() - getTitleSize()-2);
301 void KMagSelWin::startResizing ()
303 oldSelRect
= getSelRect();
306 void KMagSelWin::titleMoved (const QPoint
&offset
)
308 QRect selRect
= oldSelRect
;
309 selRect
.translate (offset
.x(), offset
.y());
310 setSelRect (selRect
);
314 void KMagSelWin::topLeftResized (const QPoint
&offset
)
316 setSelRect (QRect(oldSelRect
.topLeft() + offset
, oldSelRect
.bottomRight ()));
320 void KMagSelWin::topRightResized (const QPoint
&offset
)
322 setSelRect (QRect(oldSelRect
.topRight() + offset
, oldSelRect
.bottomLeft ()));
326 void KMagSelWin::bottomLeftResized (const QPoint
&offset
)
328 setSelRect (QRect(oldSelRect
.bottomLeft() + offset
, oldSelRect
.topRight ()));
332 void KMagSelWin::bottomRightResized (const QPoint
&offset
)
334 setSelRect (QRect(oldSelRect
.bottomRight() + offset
, oldSelRect
.topLeft()));
339 //--------------------------------------------------------------------------
341 //--------------------------------------------------------------------------
343 KMagSelWinCorner::KMagSelWinCorner ( QWidget
* parent
, Qt::WFlags f
) :
346 setBackgroundMode (Qt::FixedColor
);
347 setFrameStyle (QFrame::WinPanel
| QFrame::Raised
);
351 KMagSelWinCorner::~KMagSelWinCorner()
355 void KMagSelWinCorner::mousePressEvent ( QMouseEvent
* e
)
357 oldPos
= e
->globalPos ();
358 emit
startResizing ();
361 void KMagSelWinCorner::mouseReleaseEvent ( QMouseEvent
* e
)
363 setFrameShadow (QFrame::Raised
);
364 emit
resized (e
->globalPos () - oldPos
);
367 void KMagSelWinCorner::mouseMoveEvent ( QMouseEvent
* e
)
369 emit
resized (e
->globalPos () - oldPos
);