Now kde doesn't use .la file
[kdeaccessibility.git] / kmag / kmagselrect.cpp
blobebac7bef6eacea8134891244800db9def50456e7
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 <QApplication>
24 #include <QCursor>
25 #include <QPixmap>
26 #include <QBitmap>
27 //Added by qt3to4:
28 #include <QMouseEvent>
29 #include <QLabel>
30 #include <QDesktopWidget>
31 #include <kapplication.h>
32 #include <klocale.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)
45 titleColor = title;
46 titleBtnColor = titleBtn;
47 textColor = text;
50 void setFrameSize (int size)
52 frameSize = size;
55 void setTitleSize (int size)
57 titleSize = size;
60 QColor getTitleColor ()
62 return titleColor;
65 QColor getTitleBtnColor ()
67 return titleBtnColor;
70 QColor getTextColor ()
72 return textColor;
75 int getFrameSize ()
77 return frameSize;
80 int getTitleSize ()
82 if (titleSize > frameSize)
83 return titleSize;
84 else
85 return frameSize;
88 //--------------------------------------------------------------------------
89 // Construction
90 //--------------------------------------------------------------------------
92 KMagSelRect::KMagSelRect(QWidget *parent) :
93 QRect()
95 init(parent);
98 KMagSelRect::KMagSelRect(const QPoint &topLeft, const QPoint &bottomRight,
99 QWidget *parent) :
100 QRect(topLeft, bottomRight)
102 init(parent);
105 KMagSelRect::KMagSelRect(const QPoint &topLeft, const QSize &size,
106 QWidget *parent) :
107 QRect(topLeft, size)
109 init(parent);
112 KMagSelRect::KMagSelRect(int left, int top, int width, int height,
113 QWidget *parent) :
114 QRect(left, top, width, height)
116 init(parent);
119 void KMagSelRect::init(QWidget *parent)
121 // Make sure parent is the window itself, not a widget within the window
122 if (parent != 0)
123 while (parent->parentWidget (true) != 0)
124 parent=parent->parentWidget (true);
126 selectionwindow = 0;
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 //--------------------------------------------------------------------------
152 // Slots
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 ()));
162 update();
163 selectionwindow->show();
164 selWindowParent->setActiveWindow();
168 void KMagSelRect::hide()
170 if(m_alwaysVisible)
171 return;
172 if (selectionwindow != 0) {
173 selectionwindow->hide();
174 delete selectionwindow;
175 selectionwindow = 0;
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 //--------------------------------------------------------------------------
195 // KMagSelWin
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()
244 delete titleBar;
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)
256 selRect.setLeft (0);
257 if (selRect.top() < 0)
258 selRect.setTop (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());
264 setGeometry (
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 ()
295 return QRect (
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);
312 emit resized ();
315 void KMagSelWin::topLeftResized ( QPoint offset )
317 setSelRect (QRect(oldSelRect.topLeft() + offset, oldSelRect.bottomRight ()));
318 emit resized();
321 void KMagSelWin::topRightResized ( QPoint offset )
323 setSelRect (QRect(oldSelRect.topRight() + offset, oldSelRect.bottomLeft ()));
324 emit resized();
327 void KMagSelWin::bottomLeftResized ( QPoint offset )
329 setSelRect (QRect(oldSelRect.bottomLeft() + offset, oldSelRect.topRight ()));
330 emit resized();
333 void KMagSelWin::bottomRightResized ( QPoint offset )
335 setSelRect (QRect(oldSelRect.bottomRight() + offset, oldSelRect.topLeft()));
336 emit resized();
340 //--------------------------------------------------------------------------
341 // KMagSelWinCorner
342 //--------------------------------------------------------------------------
344 KMagSelWinCorner::KMagSelWinCorner ( QWidget * parent, Qt::WFlags f ) :
345 QLabel (parent, f)
347 setBackgroundMode (Qt::FixedColor);
348 setFrameStyle (QFrame::WinPanel | QFrame::Raised);
349 setLineWidth (1);
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);