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>
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
);
159 selectionwindow
->setObjectName("selectionwindow");
160 connect (selectionwindow
, SIGNAL (resized ()), this, SLOT (selWinResized ()));
163 selectionwindow
->show();
164 selWindowParent
->setActiveWindow();
168 void KMagSelRect::hide()
172 if (selectionwindow
!= 0) {
173 selectionwindow
->hide();
174 delete selectionwindow
;
179 void KMagSelRect::update()
181 if (selectionwindow
!= 0)
182 selectionwindow
->setSelRect (QRect (topLeft(), bottomRight()));
185 void KMagSelRect::selWinResized()
187 if (selectionwindow
!= 0)
189 QRect newRect
= selectionwindow
->getSelRect();
190 setRect (newRect
.x(), newRect
.y(), newRect
.width(), newRect
.height());
194 //--------------------------------------------------------------------------
196 //--------------------------------------------------------------------------
198 KMagSelWin::KMagSelWin ( QWidget
* parent
, Qt::WFlags
) :
199 QWidget (parent
, Qt::WStyle_Customize
| Qt::WStyle_NoBorder
| Qt::WStyle_StaysOnTop
| Qt::WType_TopLevel
| Qt::WX11BypassWM
)
201 QBitmap line
= QBitmap::fromData( QSize(8, 8), line_bits
);
202 setPaletteBackgroundPixmap (line
);
203 setBackgroundOrigin (QWidget::WindowOrigin
);
205 titleBar
= new KMagSelWinCorner (this);
206 titleBar
->setObjectName("titlebar");
207 titleBar
->setPaletteBackgroundColor (getTitleColor ());
208 titleBar
->setPaletteForegroundColor (getTextColor ());
209 titleBar
->setText(i18n("Selection Window")+" - "+i18n("KMagnifier"));
210 connect (titleBar
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
211 connect (titleBar
, SIGNAL (resized (QPoint
)), this, SLOT (titleMoved (QPoint
)));
213 topLeftCorner
= new KMagSelWinCorner (this);
214 topLeftCorner
->setObjectName("topleft");
215 topLeftCorner
->setCursor (Qt::SizeFDiagCursor
);
216 topLeftCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
217 connect (topLeftCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
218 connect (topLeftCorner
, SIGNAL (resized (QPoint
)), this, SLOT (topLeftResized (QPoint
)));
220 topRightCorner
= new KMagSelWinCorner (this);
221 topRightCorner
->setObjectName("topright");
222 topRightCorner
->setCursor (Qt::SizeBDiagCursor
);
223 topRightCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
224 connect (topRightCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
225 connect (topRightCorner
, SIGNAL (resized (QPoint
)), this, SLOT (topRightResized (QPoint
)));
227 bottomLeftCorner
= new KMagSelWinCorner (this);
228 bottomLeftCorner
->setObjectName("bottomleft");
229 bottomLeftCorner
->setCursor (Qt::SizeBDiagCursor
);
230 bottomLeftCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
231 connect (bottomLeftCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
232 connect (bottomLeftCorner
, SIGNAL (resized (QPoint
)), this, SLOT (bottomLeftResized (QPoint
)));
234 bottomRightCorner
= new KMagSelWinCorner (this);
235 bottomRightCorner
->setObjectName("bottomright");
236 bottomRightCorner
->setCursor (Qt::SizeFDiagCursor
);
237 bottomRightCorner
->setPaletteBackgroundColor (getTitleBtnColor ());
238 connect (bottomRightCorner
, SIGNAL (startResizing ()), this, SLOT (startResizing ()));
239 connect (bottomRightCorner
, SIGNAL (resized (QPoint
)), this, SLOT (bottomRightResized (QPoint
)));
242 KMagSelWin::~KMagSelWin()
245 delete topLeftCorner
;
246 delete topRightCorner
;
247 delete bottomLeftCorner
;
248 delete bottomRightCorner
;
251 void KMagSelWin::setSelRect (QRect selRect
)
253 selRect
= selRect
.normalize();
255 if (selRect
.left() < 0)
257 if (selRect
.top() < 0)
259 if (selRect
.right() > QApplication::desktop()->width())
260 selRect
.setRight (QApplication::desktop()->width());
261 if (selRect
.bottom() > QApplication::desktop()->height())
262 selRect
.setBottom (QApplication::desktop()->height());
265 selRect
.left() - getFrameSize(),
266 selRect
.top() - getTitleSize() - 2,
267 selRect
.width() + getFrameSize() + getFrameSize(),
268 selRect
.height() + getFrameSize() + getTitleSize()+2);
270 int w
= getFrameSize();
271 if (selRect
.width() < w
+w
)
272 w
= static_cast<int>(selRect
.width()/2);
274 int h
= getFrameSize();
275 if (selRect
.height() < h
+h
)
276 h
= static_cast<int>(selRect
.height()/2);
278 setMask (QRegion (QRect (0, 0, width(), height ()))
279 - QRegion (QRect (getFrameSize(), getTitleSize()+2, selRect
.width(), selRect
.height()))
280 - QRegion (QRect (0, 0, getFrameSize()+w
, getTitleSize()+2-getFrameSize()))
281 - QRegion (QRect (width()-getFrameSize()-w
, 0, getFrameSize()+w
, getTitleSize()+2-getFrameSize()))
282 - QRegion (QRect (0, getTitleSize()+2+h
, getFrameSize()-2, selRect
.height()-h
-h
))
283 - QRegion (QRect (width()-getFrameSize()+2, getTitleSize()+2+h
, getFrameSize()-2, selRect
.height()-h
-h
))
284 - QRegion (QRect (getFrameSize()+w
, height()-getFrameSize()+2, selRect
.width()-w
-w
, getFrameSize()-2)));
286 titleBar
->setGeometry (getFrameSize()+w
, 0, selRect
.width()-h
-h
, getTitleSize());
287 topLeftCorner
->setGeometry (0, getTitleSize()+2-getFrameSize(), getFrameSize()+w
, getFrameSize()+h
);
288 topRightCorner
->setGeometry (width()-getFrameSize()-w
, getTitleSize()+2-getFrameSize(), getFrameSize()+w
, getFrameSize()+h
);
289 bottomLeftCorner
->setGeometry (0, height()-getFrameSize()-h
, getFrameSize()+w
, getFrameSize()+h
);
290 bottomRightCorner
->setGeometry (width()-getFrameSize()-w
, height()-getFrameSize()-h
, getFrameSize()+w
, getFrameSize()+h
);
293 QRect
KMagSelWin::getSelRect ()
296 x() + getFrameSize(),
297 y() + getTitleSize()+2,
298 width() - getFrameSize() - getFrameSize(),
299 height() - getFrameSize() - getTitleSize()-2);
302 void KMagSelWin::startResizing ()
304 oldSelRect
= getSelRect();
307 void KMagSelWin::titleMoved ( QPoint offset
)
309 QRect selRect
= oldSelRect
;
310 selRect
.translate (offset
.x(), offset
.y());
311 setSelRect (selRect
);
315 void KMagSelWin::topLeftResized ( QPoint offset
)
317 setSelRect (QRect(oldSelRect
.topLeft() + offset
, oldSelRect
.bottomRight ()));
321 void KMagSelWin::topRightResized ( QPoint offset
)
323 setSelRect (QRect(oldSelRect
.topRight() + offset
, oldSelRect
.bottomLeft ()));
327 void KMagSelWin::bottomLeftResized ( QPoint offset
)
329 setSelRect (QRect(oldSelRect
.bottomLeft() + offset
, oldSelRect
.topRight ()));
333 void KMagSelWin::bottomRightResized ( QPoint offset
)
335 setSelRect (QRect(oldSelRect
.bottomRight() + offset
, oldSelRect
.topLeft()));
340 //--------------------------------------------------------------------------
342 //--------------------------------------------------------------------------
344 KMagSelWinCorner::KMagSelWinCorner ( QWidget
* parent
, Qt::WFlags f
) :
347 setBackgroundMode (Qt::FixedColor
);
348 setFrameStyle (QFrame::WinPanel
| QFrame::Raised
);
352 KMagSelWinCorner::~KMagSelWinCorner()
356 void KMagSelWinCorner::mousePressEvent ( QMouseEvent
* e
)
358 oldPos
= e
->globalPos ();
359 emit
startResizing ();
362 void KMagSelWinCorner::mouseReleaseEvent ( QMouseEvent
* e
)
364 setFrameShadow (QFrame::Raised
);
365 emit
resized (e
->globalPos () - oldPos
);
368 void KMagSelWinCorner::mouseMoveEvent ( QMouseEvent
* e
)
370 emit
resized (e
->globalPos () - oldPos
);