1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
3 * slaout@linux62.org *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "colorpicker.h"
22
23 #include <QtCore/QTimer>
24 #include <QtGui/QKeyEvent>
25 #include <QtGui/QMouseEvent>
26 #include <QColorDialog>
27 #include <QApplication>
28
29 /// ///
30
31 /** DektopColorPicker */
32
33 /* From Qt documentation:
34 * " Note that only visible widgets can grab mouse input.
35 * If isVisible() returns FALSE for a widget, that widget cannot call grabMouse(). "
36 * So, we should use an always visible widget to be able to pick a color from screen,
37 * even by first hidding the main window (user seldomly want to grab a color from BasKet!)
38 * or use a global shortcut (main window can be hidden when hitting that shortcut).
39 */
40
DesktopColorPicker()41 DesktopColorPicker::DesktopColorPicker()
42 : QDesktopWidget()
43 {
44 setObjectName("DesktopColorPicker");
45 m_gettingColorFromScreen = false;
46 }
47
~DesktopColorPicker()48 DesktopColorPicker::~DesktopColorPicker()
49 {
50 }
51
pickColor()52 void DesktopColorPicker::pickColor()
53 {
54 m_gettingColorFromScreen = true;
55 // Global::mainContainer->setActive(false);
56 QTimer::singleShot(50, this, SLOT(slotDelayedPick()));
57 }
58
59 /* When firered from basket context menu, and not from menu, grabMouse doesn't work!
60 * It's perhapse because context menu call slotColorFromScreen() and then
61 * ungrab the mouse (since menus grab the mouse).
62 * But why isn't there such bug with normal menus?...
63 * By calling this method with a QTimer::singleShot, we are sure context menu code is
64 * finished and we can grab the mouse without loosing the grab:
65 */
slotDelayedPick()66 void DesktopColorPicker::slotDelayedPick()
67 {
68 grabKeyboard();
69 grabMouse(Qt::CrossCursor);
70 }
71
72 /* Validate the color
73 */
mouseReleaseEvent(QMouseEvent * event)74 void DesktopColorPicker::mouseReleaseEvent(QMouseEvent *event)
75 {
76 if (m_gettingColorFromScreen) {
77 m_gettingColorFromScreen = false;
78 releaseMouse();
79 releaseKeyboard();
80
81 //Grab color of pixel
82 QPoint p = event->globalPos();
83 QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), p.x(), p.y(), 1, 1);
84 QColor color(pixmap.toImage().pixel(0, 0));
85
86 emit pickedColor(color);
87 } else
88 QDesktopWidget::mouseReleaseEvent(event);
89 }
90
91 /* Cancel the mode
92 */
keyPressEvent(QKeyEvent * event)93 void DesktopColorPicker::keyPressEvent(QKeyEvent *event)
94 {
95 if (m_gettingColorFromScreen)
96 if (event->key() == Qt::Key_Escape) {
97 m_gettingColorFromScreen = false;
98 releaseMouse();
99 releaseKeyboard();
100 emit canceledPick();
101 }
102 QDesktopWidget::keyPressEvent(event);
103 }
104
105