1 /***************************************************************************
2 * Copyright (C) 2003 by S�astien Laot *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "colorpicker.h"
23 #include <kcolordialog.h>
27 /** DektopColorPicker */
29 /* From Qt documentation:
30 * " Note that only visible widgets can grab mouse input.
31 * If isVisible() returns FALSE for a widget, that widget cannot call grabMouse(). "
32 * So, we should use an always visible widget to be able to pick a color from screen,
33 * even by first hidding the main window (user seldomly want to grab a color from BasKet!)
34 * or use a global shortcut (main window can be hidden when hitting that shortcut).
37 DesktopColorPicker::DesktopColorPicker()
40 setName("DesktopColorPicker");
41 m_gettingColorFromScreen
= false;
44 DesktopColorPicker::~DesktopColorPicker()
48 void DesktopColorPicker::pickColor()
50 m_gettingColorFromScreen
= true;
51 // Global::mainContainer->setActive(false);
52 QTimer::singleShot( 50, this, SLOT(slotDelayedPick()) );
55 /* When firered from basket context menu, and not from menu, grabMouse doesn't work!
56 * It's perhapse because context menu call slotColorFromScreen() and then
57 * ungrab the mouse (since menus grab the mouse).
58 * But why isn't there such bug with normal menus?...
59 * By calling this method with a QTimer::singleShot, we are sure context menu code is
60 * finished and we can grab the mouse without loosing the grab:
62 void DesktopColorPicker::slotDelayedPick()
65 grabMouse(crossCursor
);
70 void DesktopColorPicker::mouseReleaseEvent(QMouseEvent
*event
)
72 if (m_gettingColorFromScreen
) {
73 m_gettingColorFromScreen
= false;
76 QColor color
= KColorDialog::grabColor(event
->globalPos());
77 emit
pickedColor(color
);
79 QDesktopWidget::mouseReleaseEvent(event
);
84 void DesktopColorPicker::keyPressEvent(QKeyEvent
*event
)
86 if (m_gettingColorFromScreen
)
87 if (event
->key() == Qt::Key_Escape
) {
88 m_gettingColorFromScreen
= false;
93 QDesktopWidget::keyPressEvent(event
);
96 #include "colorpicker.moc"