File indexing completed on 2024-04-28 07:27:19
0001 // SPDX-FileCopyrightText: 2001-2003 Sarang Lakare <sarang@users.sourceforge.net> 0002 // SPDX-FileCopyrightText: 2003-2004 Olaf Schmidt <ojschmidt@kde.org> 0003 // SPDX-License-Identifier: GPL-2.0-or-later 0004 0005 #include "kmagselrect.h" 0006 0007 // Qt 0008 #include <QBitmap> 0009 #include <QScreen> 0010 #include <QMouseEvent> 0011 // KF5 0012 #include <KLocalizedString> 0013 0014 static const uchar line_bits[] = {0x2d, 0x96, 0x4b, 0xa5, 0xd2, 0x69, 0xb4, 0x5a}; 0015 0016 static QColor titleColor = QColor (0,0,128); 0017 static QColor titleBtnColor = QColor (255,255,0); 0018 static QColor textColor = QColor (255,255,255); 0019 0020 static int frameSize = 10; 0021 static int titleSize = 24; 0022 0023 void setTitleColors (const QColor &title, const QColor &text, const QColor &titleBtn) 0024 { 0025 titleColor = title; 0026 titleBtnColor = titleBtn; 0027 textColor = text; 0028 } 0029 0030 void setFrameSize (int size) 0031 { 0032 frameSize = size; 0033 } 0034 0035 void setTitleSize (int size) 0036 { 0037 titleSize = size; 0038 } 0039 0040 QColor getTitleColor () 0041 { 0042 return titleColor; 0043 } 0044 0045 QColor getTitleBtnColor () 0046 { 0047 return titleBtnColor; 0048 } 0049 0050 QColor getTextColor () 0051 { 0052 return textColor; 0053 } 0054 0055 int getFrameSize () 0056 { 0057 return frameSize; 0058 } 0059 0060 int getTitleSize () 0061 { 0062 if (titleSize > frameSize) 0063 return titleSize; 0064 else 0065 return frameSize; 0066 } 0067 0068 //-------------------------------------------------------------------------- 0069 // Construction 0070 //-------------------------------------------------------------------------- 0071 0072 KMagSelRect::KMagSelRect(QWidget *parent) : 0073 QRect() 0074 { 0075 init(parent); 0076 } 0077 0078 KMagSelRect::KMagSelRect(const QPoint &topLeft, const QPoint &bottomRight, 0079 QWidget *parent) : 0080 QRect(topLeft, bottomRight) 0081 { 0082 init(parent); 0083 } 0084 0085 KMagSelRect::KMagSelRect(const QPoint &topLeft, const QSize &size, 0086 QWidget *parent) : 0087 QRect(topLeft, size) 0088 { 0089 init(parent); 0090 } 0091 0092 KMagSelRect::KMagSelRect(int left, int top, int width, int height, 0093 QWidget *parent) : 0094 QRect(left, top, width, height) 0095 { 0096 init(parent); 0097 } 0098 0099 void KMagSelRect::init(QWidget *parent) 0100 { 0101 // Make sure parent is the window itself, not a widget within the window 0102 if (parent != nullptr) 0103 while (parent->parentWidget() != nullptr) 0104 parent=parent->parentWidget(); 0105 0106 selectionwindow = nullptr; 0107 selWindowParent = parent; 0108 0109 m_alwaysVisible = false; 0110 } 0111 0112 KMagSelRect::~KMagSelRect() 0113 { 0114 } 0115 0116 //-------------------------------------------------------------------------- 0117 // 0118 //-------------------------------------------------------------------------- 0119 0120 bool KMagSelRect::visible() const 0121 { 0122 return (selectionwindow != nullptr); 0123 } 0124 0125 void KMagSelRect::alwaysVisible(bool visible) 0126 { 0127 m_alwaysVisible = visible; 0128 } 0129 0130 0131 //-------------------------------------------------------------------------- 0132 // Slots 0133 //-------------------------------------------------------------------------- 0134 0135 void KMagSelRect::show() 0136 { 0137 if (selectionwindow == nullptr) { 0138 selectionwindow = new KMagSelWin (selWindowParent); 0139 selectionwindow->setObjectName( QStringLiteral("selectionwindow" )); 0140 connect (selectionwindow, &KMagSelWin::resized, this, &KMagSelRect::selWinResized); 0141 0142 update(); 0143 selectionwindow->show(); 0144 selWindowParent->activateWindow(); 0145 } 0146 } 0147 0148 void KMagSelRect::hide() 0149 { 0150 if(m_alwaysVisible) 0151 return; 0152 if (selectionwindow != nullptr) { 0153 selectionwindow->hide(); 0154 delete selectionwindow; 0155 selectionwindow = nullptr; 0156 } 0157 } 0158 0159 void KMagSelRect::update() 0160 { 0161 if (selectionwindow) 0162 selectionwindow->setSelRect (QRect (topLeft(), bottomRight())); 0163 } 0164 0165 void KMagSelRect::selWinResized() 0166 { 0167 if (selectionwindow) 0168 { 0169 const QRect newRect = selectionwindow->getSelRect(); 0170 setRect (newRect.x(), newRect.y(), newRect.width(), newRect.height()); 0171 } 0172 } 0173 0174 //-------------------------------------------------------------------------- 0175 // KMagSelWin 0176 //-------------------------------------------------------------------------- 0177 0178 void setPaletteColor(QWidget* w, QPalette::ColorRole r, const QColor& c) 0179 { 0180 QPalette p = w->palette(); 0181 p.setColor(r, c); 0182 w->setPalette(p); 0183 } 0184 0185 KMagSelWin::KMagSelWin ( QWidget * parent ) : 0186 QWidget(parent) //Qt::WStyle_Customize | Qt::WStyle_NoBorder | Qt::WStyle_StaysOnTop | Qt::WType_TopLevel | Qt::WX11BypassWM) 0187 { 0188 setWindowFlags( Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); 0189 0190 QPalette p = palette(); 0191 p.setBrush(backgroundRole(), QBrush(QBitmap::fromData( QSize(8, 8), line_bits))); 0192 setPalette(p); 0193 0194 titleBar = new KMagSelWinCorner (this); 0195 titleBar->setObjectName( QStringLiteral("titlebar" )); 0196 setPaletteColor(titleBar, QPalette::Window, getTitleColor()); 0197 setPaletteColor(titleBar, QPalette::WindowText, getTextColor()); 0198 titleBar->setText(i18n("Selection Window")+QLatin1String( " - " )+i18n("KMagnifier")); 0199 connect (titleBar, &KMagSelWinCorner::startResizing, this, &KMagSelWin::startResizing); 0200 connect (titleBar, &KMagSelWinCorner::resized, this, &KMagSelWin::titleMoved); 0201 0202 topLeftCorner = new KMagSelWinCorner (this); 0203 topLeftCorner->setObjectName( QStringLiteral("topleft" )); 0204 topLeftCorner->setCursor (Qt::SizeFDiagCursor); 0205 setPaletteColor(topLeftCorner, QPalette::Window, getTitleBtnColor()); 0206 connect (topLeftCorner, &KMagSelWinCorner::startResizing, this, &KMagSelWin::startResizing); 0207 connect (topLeftCorner, &KMagSelWinCorner::resized, this, &KMagSelWin::topLeftResized); 0208 0209 topRightCorner = new KMagSelWinCorner (this); 0210 topRightCorner->setObjectName( QStringLiteral("topright" )); 0211 topRightCorner->setCursor (Qt::SizeBDiagCursor); 0212 setPaletteColor(topRightCorner, QPalette::Window, getTitleBtnColor ()); 0213 connect (topRightCorner, &KMagSelWinCorner::startResizing, this, &KMagSelWin::startResizing); 0214 connect (topRightCorner, &KMagSelWinCorner::resized, this, &KMagSelWin::topRightResized); 0215 0216 bottomLeftCorner = new KMagSelWinCorner (this); 0217 bottomLeftCorner->setObjectName( QStringLiteral("bottomleft" )); 0218 bottomLeftCorner->setCursor (Qt::SizeBDiagCursor); 0219 setPaletteColor(bottomLeftCorner, QPalette::Window, getTitleBtnColor()); 0220 connect (bottomLeftCorner, &KMagSelWinCorner::startResizing, this, &KMagSelWin::startResizing); 0221 connect (bottomLeftCorner, &KMagSelWinCorner::resized, this, &KMagSelWin::bottomLeftResized); 0222 0223 bottomRightCorner = new KMagSelWinCorner (this); 0224 bottomRightCorner->setObjectName( QStringLiteral("bottomright" )); 0225 bottomRightCorner->setCursor (Qt::SizeFDiagCursor); 0226 setPaletteColor(bottomRightCorner, QPalette::Window, getTitleBtnColor ()); 0227 connect (bottomRightCorner, &KMagSelWinCorner::startResizing, this, &KMagSelWin::startResizing); 0228 connect (bottomRightCorner, &KMagSelWinCorner::resized, this, &KMagSelWin::bottomRightResized); 0229 } 0230 0231 KMagSelWin::~KMagSelWin() 0232 { 0233 delete titleBar; 0234 delete topLeftCorner; 0235 delete topRightCorner; 0236 delete bottomLeftCorner; 0237 delete bottomRightCorner; 0238 } 0239 0240 void KMagSelWin::setSelRect (const QRect &_selRect) 0241 { 0242 QRect selRect = _selRect.normalized(); 0243 0244 if (selRect.left() < 0) 0245 selRect.setLeft (0); 0246 if (selRect.top() < 0) 0247 selRect.setTop (0); 0248 const QSize screenSize = screen()->virtualSize(); 0249 if (selRect.right() > screenSize.width()) 0250 selRect.setRight (screenSize.width()); 0251 if (selRect.bottom() > screenSize.height()) 0252 selRect.setBottom (screenSize.height()); 0253 0254 setGeometry ( 0255 selRect.left() - getFrameSize(), 0256 selRect.top() - getTitleSize() - 2, 0257 selRect.width() + getFrameSize() + getFrameSize(), 0258 selRect.height() + getFrameSize() + getTitleSize()+2); 0259 0260 int w = getFrameSize(); 0261 if (selRect.width() < w+w) 0262 w = static_cast<int>(selRect.width()/2); 0263 0264 int h = getFrameSize(); 0265 if (selRect.height() < h+h) 0266 h = static_cast<int>(selRect.height()/2); 0267 0268 setMask (QRegion (QRect (0, 0, width(), height ())) 0269 - QRegion (QRect (getFrameSize(), getTitleSize()+2, selRect.width(), selRect.height())) 0270 - QRegion (QRect (0, 0, getFrameSize()+w, getTitleSize()+2-getFrameSize())) 0271 - QRegion (QRect (width()-getFrameSize()-w, 0, getFrameSize()+w, getTitleSize()+2-getFrameSize())) 0272 - QRegion (QRect (0, getTitleSize()+2+h, getFrameSize()-2, selRect.height()-h-h)) 0273 - QRegion (QRect (width()-getFrameSize()+2, getTitleSize()+2+h, getFrameSize()-2, selRect.height()-h-h)) 0274 - QRegion (QRect (getFrameSize()+w, height()-getFrameSize()+2, selRect.width()-w-w, getFrameSize()-2))); 0275 0276 titleBar->setGeometry (getFrameSize()+w, 0, selRect.width()-h-h, getTitleSize()); 0277 topLeftCorner->setGeometry (0, getTitleSize()+2-getFrameSize(), getFrameSize()+w, getFrameSize()+h); 0278 topRightCorner->setGeometry (width()-getFrameSize()-w, getTitleSize()+2-getFrameSize(), getFrameSize()+w, getFrameSize()+h); 0279 bottomLeftCorner->setGeometry (0, height()-getFrameSize()-h, getFrameSize()+w, getFrameSize()+h); 0280 bottomRightCorner->setGeometry (width()-getFrameSize()-w, height()-getFrameSize()-h, getFrameSize()+w, getFrameSize()+h); 0281 } 0282 0283 QRect KMagSelWin::getSelRect () 0284 { 0285 return QRect ( 0286 x() + getFrameSize(), 0287 y() + getTitleSize()+2, 0288 width() - getFrameSize() - getFrameSize(), 0289 height() - getFrameSize() - getTitleSize()-2); 0290 } 0291 0292 void KMagSelWin::startResizing () 0293 { 0294 oldSelRect = getSelRect(); 0295 } 0296 0297 void KMagSelWin::titleMoved (const QPoint &offset) 0298 { 0299 QRect selRect = oldSelRect; 0300 selRect.translate (offset.x(), offset.y()); 0301 setSelRect (selRect); 0302 Q_EMIT resized (); 0303 } 0304 0305 void KMagSelWin::topLeftResized (const QPoint &offset) 0306 { 0307 setSelRect (QRect(oldSelRect.topLeft() + offset, oldSelRect.bottomRight ())); 0308 Q_EMIT resized(); 0309 } 0310 0311 void KMagSelWin::topRightResized (const QPoint &offset) 0312 { 0313 setSelRect (QRect(oldSelRect.topRight() + offset, oldSelRect.bottomLeft ())); 0314 Q_EMIT resized(); 0315 } 0316 0317 void KMagSelWin::bottomLeftResized (const QPoint &offset) 0318 { 0319 setSelRect (QRect(oldSelRect.bottomLeft() + offset, oldSelRect.topRight ())); 0320 Q_EMIT resized(); 0321 } 0322 0323 void KMagSelWin::bottomRightResized (const QPoint &offset) 0324 { 0325 setSelRect (QRect(oldSelRect.bottomRight() + offset, oldSelRect.topLeft())); 0326 Q_EMIT resized(); 0327 } 0328 0329 0330 //-------------------------------------------------------------------------- 0331 // KMagSelWinCorner 0332 //-------------------------------------------------------------------------- 0333 0334 KMagSelWinCorner::KMagSelWinCorner ( QWidget * parent ) : 0335 QLabel (parent) 0336 { 0337 setFrameStyle (QFrame::WinPanel | QFrame::Raised); 0338 setLineWidth (1); 0339 } 0340 0341 KMagSelWinCorner::~KMagSelWinCorner() 0342 { 0343 } 0344 0345 void KMagSelWinCorner::mousePressEvent ( QMouseEvent * e ) 0346 { 0347 oldPos = e->globalPosition().toPoint(); 0348 Q_EMIT startResizing (); 0349 } 0350 0351 void KMagSelWinCorner::mouseReleaseEvent ( QMouseEvent * e ) 0352 { 0353 setFrameShadow (QFrame::Raised); 0354 QPoint p = e->globalPosition().toPoint(); 0355 Q_EMIT resized (p - oldPos); 0356 } 0357 0358 void KMagSelWinCorner::mouseMoveEvent ( QMouseEvent * e ) 0359 { 0360 QPoint p = e->globalPosition().toPoint(); 0361 Q_EMIT resized (p - oldPos); 0362 } 0363 0364 #include "moc_kmagselrect.cpp"