Remove this line
[kdeaccessibility.git] / kmag / kmagselrect.cpp
blob4b8c626197816d11c04c7ce9b872d4c851ceea72
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 ***************************************************************************/
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 ***************************************************************************/
20 #include "kmagselrect.h"
21 #include "kmagselrect.moc"
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>
31 #include <klocale.h>
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)
44 titleColor = title;
45 titleBtnColor = titleBtn;
46 textColor = text;
49 void setFrameSize (int size)
51 frameSize = size;
54 void setTitleSize (int size)
56 titleSize = size;
59 QColor getTitleColor ()
61 return titleColor;
64 QColor getTitleBtnColor ()
66 return titleBtnColor;
69 QColor getTextColor ()
71 return textColor;
74 int getFrameSize ()
76 return frameSize;
79 int getTitleSize ()
81 if (titleSize > frameSize)
82 return titleSize;
83 else
84 return frameSize;
87 //--------------------------------------------------------------------------
88 // Construction
89 //--------------------------------------------------------------------------
91 KMagSelRect::KMagSelRect(QWidget *parent) :
92 QRect()
94 init(parent);
97 KMagSelRect::KMagSelRect(const QPoint &topLeft, const QPoint &bottomRight,
98 QWidget *parent) :
99 QRect(topLeft, bottomRight)
101 init(parent);
104 KMagSelRect::KMagSelRect(const QPoint &topLeft, const QSize &size,
105 QWidget *parent) :
106 QRect(topLeft, size)
108 init(parent);
111 KMagSelRect::KMagSelRect(int left, int top, int width, int height,
112 QWidget *parent) :
113 QRect(left, top, width, height)
115 init(parent);
118 void KMagSelRect::init(QWidget *parent)
120 // Make sure parent is the window itself, not a widget within the window
121 if (parent != 0)
122 while (parent->parentWidget (true) != 0)
123 parent=parent->parentWidget (true);
125 selectionwindow = 0;
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 //--------------------------------------------------------------------------
151 // Slots
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 ()));
161 update();
162 selectionwindow->show();
163 selWindowParent->setActiveWindow();
167 void KMagSelRect::hide()
169 if(m_alwaysVisible)
170 return;
171 if (selectionwindow != 0) {
172 selectionwindow->hide();
173 delete selectionwindow;
174 selectionwindow = 0;
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 //--------------------------------------------------------------------------
194 // KMagSelWin
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()
243 delete titleBar;
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)
255 selRect.setLeft (0);
256 if (selRect.top() < 0)
257 selRect.setTop (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());
263 setGeometry (
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 ()
294 return QRect (
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);
311 emit resized ();
314 void KMagSelWin::topLeftResized (const QPoint &offset)
316 setSelRect (QRect(oldSelRect.topLeft() + offset, oldSelRect.bottomRight ()));
317 emit resized();
320 void KMagSelWin::topRightResized (const QPoint &offset)
322 setSelRect (QRect(oldSelRect.topRight() + offset, oldSelRect.bottomLeft ()));
323 emit resized();
326 void KMagSelWin::bottomLeftResized (const QPoint &offset)
328 setSelRect (QRect(oldSelRect.bottomLeft() + offset, oldSelRect.topRight ()));
329 emit resized();
332 void KMagSelWin::bottomRightResized (const QPoint &offset)
334 setSelRect (QRect(oldSelRect.bottomRight() + offset, oldSelRect.topLeft()));
335 emit resized();
339 //--------------------------------------------------------------------------
340 // KMagSelWinCorner
341 //--------------------------------------------------------------------------
343 KMagSelWinCorner::KMagSelWinCorner ( QWidget * parent, Qt::WFlags f ) :
344 QLabel (parent, f)
346 setBackgroundMode (Qt::FixedColor);
347 setFrameStyle (QFrame::WinPanel | QFrame::Raised);
348 setLineWidth (1);
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);