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.h>
28 #include <QMouseEvent>
30 #include <QDesktopWidget>
31 #include <kapplication.h>
34 static uchar line_bits
[] = {0x2d, 0x96, 0x4b, 0xa5, 0xd2, 0x69, 0xb4, 0x5a};
36 static QColor titleColor
= QColor (0,0,128);
37 static QColor titleBtnColor
= QColor (255,255,0);
38 static QColor textColor
= QColor (255,255,255);
40 static int frameSize
= 10;
41 static int titleSize
= 24;
43 void setTitleColors (QColor title
, QColor text
, QColor titleBtn
)
46 titleBtnColor
= titleBtn
;
50 void setFrameSize (int size
)
55 void setTitleSize (int size
)
60 QColor
getTitleColor ()
65 QColor
getTitleBtnColor ()
70 QColor
getTextColor ()
82 if (titleSize
> frameSize
)
88 //--------------------------------------------------------------------------
90 //--------------------------------------------------------------------------
92 KMagSelRect::KMagSelRect(QWidget
*parent
) :
98 KMagSelRect::KMagSelRect(const QPoint
&topLeft
, const QPoint
&bottomRight
,
100 QRect(topLeft
, bottomRight
)
105 KMagSelRect::KMagSelRect(const QPoint
&topLeft
, const QSize
&size
,
112 KMagSelRect::KMagSelRect(int left
, int top
, int width
, int height
,
114 QRect(left
, top
, width
, height
)
119 void KMagSelRect::init(QWidget
*parent
)
121 // Make sure parent is the window itself, not a widget within the window
123 while (parent
->parentWidget (true) != 0)
124 parent
=parent
->parentWidget (true);
127 selWindowParent
= parent
;
129 m_alwaysVisible
= false;
132 KMagSelRect::~KMagSelRect()
136 //--------------------------------------------------------------------------
138 //--------------------------------------------------------------------------
140 bool KMagSelRect::visible()
142 return (selectionwindow
!= 0);
145 void KMagSelRect::alwaysVisible(bool visible
)
147 m_alwaysVisible
= visible
;
151 //--------------------------------------------------------------------------
153 //--------------------------------------------------------------------------
155 void KMagSelRect::show()
157 if (selectionwindow
== 0) {
158 selectionwindow
= new KMagSelWin (selWindowParent
, "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
, const char * name
, Qt::WFlags
) :
198 QWidget (parent
, name
, Qt::WStyle_Customize
| Qt::WStyle_NoBorder
| Qt::WStyle_StaysOnTop
| Qt::WType_TopLevel
| Qt::WX11BypassWM
)
200 QBitmap
line (8, 8, line_bits
, true);
201 setPaletteBackgroundPixmap (line
);
202 setBackgroundOrigin (QWidget::WindowOrigin
);
204 titleBar
= new KMagSelWinCorner (this, "titlebar");
205 titleBar
->setPaletteBackgroundColor (getTitleColor ());
206 titleBar
->setPaletteForegroundColor (getTextColor ());
207 titleBar
->setText(i18n("Selection Window")+" - "+i18n("KMagnifier"));
208 connect (titleBar
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
209 connect (titleBar
, SIGNAL (resized (QPoint
)), this, SLOT (titleMoved (QPoint
)));
211 topLeftCorner
= new KMagSelWinCorner (this, "topleft");
212 topLeftCorner
->setCursor (Qt::SizeFDiagCursor
);
213 topLeftCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
214 connect (topLeftCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
215 connect (topLeftCorner
, SIGNAL (resized (QPoint
)), this, SLOT (topLeftResized (QPoint
)));
217 topRightCorner
= new KMagSelWinCorner (this, "topright");
218 topRightCorner
->setCursor (Qt::SizeBDiagCursor
);
219 topRightCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
220 connect (topRightCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
221 connect (topRightCorner
, SIGNAL (resized (QPoint
)), this, SLOT (topRightResized (QPoint
)));
223 bottomLeftCorner
= new KMagSelWinCorner (this, "bottomleft");
224 bottomLeftCorner
->setCursor (Qt::SizeBDiagCursor
);
225 bottomLeftCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
226 connect (bottomLeftCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
227 connect (bottomLeftCorner
, SIGNAL (resized (QPoint
)), this, SLOT (bottomLeftResized (QPoint
)));
229 bottomRightCorner
= new KMagSelWinCorner (this, "bottomright");
230 bottomRightCorner
->setCursor (Qt::SizeFDiagCursor
);
231 bottomRightCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
232 connect (bottomRightCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
233 connect (bottomRightCorner
, SIGNAL (resized (QPoint
)), this, SLOT (bottomRightResized (QPoint
)));
236 KMagSelWin::~KMagSelWin()
239 delete topLeftCorner
;
240 delete topRightCorner
;
241 delete bottomLeftCorner
;
242 delete bottomRightCorner
;
245 void KMagSelWin::setSelRect (QRect selRect
)
247 selRect
= selRect
.normalize();
249 if (selRect
.left() < 0)
251 if (selRect
.top() < 0)
253 if (selRect
.right() > QApplication::desktop()->width())
254 selRect
.setRight (QApplication::desktop()->width());
255 if (selRect
.bottom() > QApplication::desktop()->height())
256 selRect
.setBottom (QApplication::desktop()->height());
259 selRect
.left() - getFrameSize(),
260 selRect
.top() - getTitleSize() - 2,
261 selRect
.width() + getFrameSize() + getFrameSize(),
262 selRect
.height() + getFrameSize() + getTitleSize()+2);
264 int w
= getFrameSize();
265 if (selRect
.width() < w
+w
)
266 w
= static_cast<int>(selRect
.width()/2);
268 int h
= getFrameSize();
269 if (selRect
.height() < h
+h
)
270 h
= static_cast<int>(selRect
.height()/2);
272 setMask (QRegion (QRect (0, 0, width(), height ()))
273 - QRegion (QRect (getFrameSize(), getTitleSize()+2, selRect
.width(), selRect
.height()))
274 - QRegion (QRect (0, 0, getFrameSize()+w
, getTitleSize()+2-getFrameSize()))
275 - QRegion (QRect (width()-getFrameSize()-w
, 0, getFrameSize()+w
, getTitleSize()+2-getFrameSize()))
276 - QRegion (QRect (0, getTitleSize()+2+h
, getFrameSize()-2, selRect
.height()-h
-h
))
277 - QRegion (QRect (width()-getFrameSize()+2, getTitleSize()+2+h
, getFrameSize()-2, selRect
.height()-h
-h
))
278 - QRegion (QRect (getFrameSize()+w
, height()-getFrameSize()+2, selRect
.width()-w
-w
, getFrameSize()-2)));
280 titleBar
->setGeometry (getFrameSize()+w
, 0, selRect
.width()-h
-h
, getTitleSize());
281 topLeftCorner
->setGeometry (0, getTitleSize()+2-getFrameSize(), getFrameSize()+w
, getFrameSize()+h
);
282 topRightCorner
->setGeometry (width()-getFrameSize()-w
, getTitleSize()+2-getFrameSize(), getFrameSize()+w
, getFrameSize()+h
);
283 bottomLeftCorner
->setGeometry (0, height()-getFrameSize()-h
, getFrameSize()+w
, getFrameSize()+h
);
284 bottomRightCorner
->setGeometry (width()-getFrameSize()-w
, height()-getFrameSize()-h
, getFrameSize()+w
, getFrameSize()+h
);
287 QRect
KMagSelWin::getSelRect ()
290 x() + getFrameSize(),
291 y() + getTitleSize()+2,
292 width() - getFrameSize() - getFrameSize(),
293 height() - getFrameSize() - getTitleSize()-2);
296 void KMagSelWin::startResizing ()
298 oldSelRect
= getSelRect();
301 void KMagSelWin::titleMoved ( QPoint offset
)
303 QRect selRect
= oldSelRect
;
304 selRect
.moveBy (offset
.x(), offset
.y());
305 setSelRect (selRect
);
309 void KMagSelWin::topLeftResized ( QPoint offset
)
311 setSelRect (QRect(oldSelRect
.topLeft() + offset
, oldSelRect
.bottomRight ()));
315 void KMagSelWin::topRightResized ( QPoint offset
)
317 setSelRect (QRect(oldSelRect
.topRight() + offset
, oldSelRect
.bottomLeft ()));
321 void KMagSelWin::bottomLeftResized ( QPoint offset
)
323 setSelRect (QRect(oldSelRect
.bottomLeft() + offset
, oldSelRect
.topRight ()));
327 void KMagSelWin::bottomRightResized ( QPoint offset
)
329 setSelRect (QRect(oldSelRect
.bottomRight() + offset
, oldSelRect
.topLeft()));
334 //--------------------------------------------------------------------------
336 //--------------------------------------------------------------------------
338 KMagSelWinCorner::KMagSelWinCorner ( QWidget
* parent
, const char * name
, Qt::WFlags f
) :
339 QLabel (parent
, name
, f
)
341 setBackgroundMode (Qt::FixedColor
);
342 setFrameStyle (QFrame::WinPanel
| QFrame::Raised
);
346 KMagSelWinCorner::~KMagSelWinCorner()
350 void KMagSelWinCorner::mousePressEvent ( QMouseEvent
* e
)
352 oldPos
= e
->globalPos ();
353 emit
startResizing ();
356 void KMagSelWinCorner::mouseReleaseEvent ( QMouseEvent
* e
)
358 setFrameShadow (QFrame::Raised
);
359 emit
resized (e
->globalPos () - oldPos
);
362 void KMagSelWinCorner::mouseMoveEvent ( QMouseEvent
* e
)
364 emit
resized (e
->globalPos () - oldPos
);