TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kptoolcolorpicker.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #define DEBUG_KP_TOOL_COLOR_PICKER 0
  24. #include <kptoolcolorpicker.h>
  25. #include <tqimage.h>
  26. #include <tqpixmap.h>
  27. #include <tqpoint.h>
  28. #include <kdebug.h>
  29. #include <tdelocale.h>
  30. #include <kpcolortoolbar.h>
  31. #include <kpcommandhistory.h>
  32. #include <kpdefs.h>
  33. #include <kpdocument.h>
  34. #include <kpmainwindow.h>
  35. #include <kppixmapfx.h>
  36. /*
  37. * kpToolColorPicker
  38. */
  39. kpToolColorPicker::kpToolColorPicker (kpMainWindow *mainWindow)
  40. : kpTool (i18n ("Color Picker"), i18n ("Lets you select a color from the image"),
  41. TQt::Key_C,
  42. mainWindow, "tool_color_picker")
  43. {
  44. }
  45. kpToolColorPicker::~kpToolColorPicker ()
  46. {
  47. }
  48. kpColor kpToolColorPicker::colorAtPixel (const TQPoint &p)
  49. {
  50. #if DEBUG_KP_TOOL_COLOR_PICKER && 0
  51. kdDebug () << "kpToolColorPicker::colorAtPixel" << p << endl;
  52. #endif
  53. return kpPixmapFX::getColorAtPixel (*document ()->pixmap (), p);
  54. }
  55. TQString kpToolColorPicker::haventBegunDrawUserMessage () const
  56. {
  57. return i18n ("Click to select a color.");
  58. }
  59. void kpToolColorPicker::begin ()
  60. {
  61. setUserMessage (haventBegunDrawUserMessage ());
  62. }
  63. // virtual
  64. void kpToolColorPicker::beginDraw ()
  65. {
  66. m_oldColor = color (m_mouseButton);
  67. setUserMessage (cancelUserMessage ());
  68. }
  69. // virtual
  70. void kpToolColorPicker::draw (const TQPoint &thisPoint, const TQPoint &, const TQRect &)
  71. {
  72. const kpColor color = colorAtPixel (thisPoint);
  73. if (color.isValid ())
  74. {
  75. mainWindow ()->colorToolBar ()->setColor (m_mouseButton, color);
  76. setUserShapePoints (thisPoint);
  77. }
  78. else
  79. {
  80. mainWindow ()->colorToolBar ()->setColor (m_mouseButton, m_oldColor);
  81. setUserShapePoints ();
  82. }
  83. }
  84. // virtual
  85. void kpToolColorPicker::cancelShape ()
  86. {
  87. mainWindow ()->colorToolBar ()->setColor (m_mouseButton, m_oldColor);
  88. setUserMessage (i18n ("Let go of all the mouse buttons."));
  89. }
  90. void kpToolColorPicker::releasedAllButtons ()
  91. {
  92. setUserMessage (haventBegunDrawUserMessage ());
  93. }
  94. // virtual
  95. void kpToolColorPicker::endDraw (const TQPoint &thisPoint, const TQRect &)
  96. {
  97. const kpColor color = colorAtPixel (thisPoint);
  98. if (color.isValid ())
  99. {
  100. kpToolColorPickerCommand *cmd = new kpToolColorPickerCommand (
  101. m_mouseButton,
  102. color, m_oldColor,
  103. mainWindow ());
  104. mainWindow ()->commandHistory ()->addCommand (cmd, false /* no exec */);
  105. setUserMessage (haventBegunDrawUserMessage ());
  106. }
  107. else
  108. {
  109. cancelShape ();
  110. }
  111. }
  112. /*
  113. * kpToolColorPickerCommand
  114. */
  115. kpToolColorPickerCommand::kpToolColorPickerCommand (int mouseButton,
  116. const kpColor &newColor,
  117. const kpColor &oldColor,
  118. kpMainWindow *mainWindow)
  119. : kpCommand (mainWindow),
  120. m_mouseButton (mouseButton),
  121. m_newColor (newColor),
  122. m_oldColor (oldColor)
  123. {
  124. }
  125. kpToolColorPickerCommand::~kpToolColorPickerCommand ()
  126. {
  127. }
  128. // public virtual [base kpCommand]
  129. TQString kpToolColorPickerCommand::name () const
  130. {
  131. return i18n ("Color Picker");
  132. }
  133. // public virtual [base kpCommand]
  134. int kpToolColorPickerCommand::size () const
  135. {
  136. return 0;
  137. }
  138. // public virtual [base kpCommand]
  139. void kpToolColorPickerCommand::execute ()
  140. {
  141. colorToolBar ()->setColor (m_mouseButton, m_newColor);
  142. }
  143. // public virtual [base kpCommand]
  144. void kpToolColorPickerCommand::unexecute ()
  145. {
  146. colorToolBar ()->setColor (m_mouseButton, m_oldColor);
  147. }
  148. // private
  149. kpColorToolBar *kpToolColorPickerCommand::colorToolBar () const
  150. {
  151. return m_mainWindow ? m_mainWindow->colorToolBar () : 0;
  152. }
  153. #include <kptoolcolorpicker.moc>