KOffice – TDE office suite
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.

kis_gradient_slider_widget.cc 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
  3. * 2004 Sven Langkamp <longamp@reallygood.de>
  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 Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "kis_gradient_slider_widget.h"
  20. #include <tqpainter.h>
  21. #include <kdebug.h>
  22. #include <kpopupmenu.h>
  23. #include <klocale.h>
  24. #include "kis_autogradient_resource.h"
  25. #define MARGIN 5
  26. #define HANDLE_SIZE 10
  27. KisGradientSliderWidget::KisGradientSliderWidget(TQWidget *parent, const char* name, WFlags f )
  28. : TQWidget( parent, name, f),
  29. m_currentSegment(0),
  30. m_selectedSegment(0),
  31. m_drag(0)
  32. {
  33. setMinimumHeight(30);
  34. m_segmentMenu = new KPopupMenu();
  35. m_segmentMenu->insertItem(i18n("Split Segment"), SPLIT_SEGMENT);
  36. m_segmentMenu->insertItem(i18n("Duplicate Segment"), DUPLICATE_SEGMENT);
  37. m_segmentMenu->insertItem(i18n("Mirror Segment"), MIRROR_SEGMENT);
  38. m_segmentMenu->insertItem(i18n("Remove Segment"), REMOVE_SEGMENT);
  39. connect( m_segmentMenu, TQT_SIGNAL( activated(int) ), TQT_SLOT( slotMenuAction(int) ) );
  40. }
  41. void KisGradientSliderWidget::setGradientResource( KisAutogradientResource* agr)
  42. {
  43. m_autogradientResource = agr;
  44. m_selectedSegment = m_autogradientResource->segmentAt(0.0);
  45. emit sigSelectedSegment( m_selectedSegment );
  46. }
  47. void KisGradientSliderWidget::paintEvent ( TQPaintEvent* pe )
  48. {
  49. TQWidget::paintEvent( pe );
  50. TQPixmap pixmap( width(), height() );
  51. pixmap.fill( colorGroup().background() );
  52. TQPainter painter( &pixmap );
  53. painter.setPen( TQt::black );
  54. painter.drawRect( MARGIN, MARGIN, width() - 2 * MARGIN, height()- 2 * MARGIN - HANDLE_SIZE );
  55. if(m_autogradientResource)
  56. {
  57. TQImage img = m_autogradientResource->generatePreview(width()- 2* MARGIN - 2, height()- 2* MARGIN - HANDLE_SIZE - 2);
  58. TQPixmap pixmap(img.width(), img.height());
  59. if (!img.isNull()) {
  60. m_pixmapIO.putImage(&pixmap, 0, 0, &img);
  61. painter.drawPixmap( MARGIN + 1, MARGIN + 1, pixmap, 0, 0, pixmap.width(), pixmap.height());
  62. }
  63. painter.fillRect( MARGIN + 1, height()- MARGIN - HANDLE_SIZE, width() - 2 * MARGIN, HANDLE_SIZE, TQBrush( TQt::white ) );
  64. if( m_selectedSegment )
  65. {
  66. TQRect selection( tqRound( m_selectedSegment->startOffset()*(double)(width()- 2 * MARGIN - 2) ) + 6,
  67. height()- HANDLE_SIZE - MARGIN,
  68. tqRound( ( m_selectedSegment->endOffset() - m_selectedSegment->startOffset() )*(double)(width()-12) ),
  69. HANDLE_SIZE );
  70. painter.fillRect( selection, TQBrush( colorGroup().highlight() ) );
  71. }
  72. TQPointArray triangle(3);
  73. TQValueVector<double> handlePositions = m_autogradientResource->getHandlePositions();
  74. int position;
  75. painter.setBrush( TQBrush( TQt::black) );
  76. for (uint i = 0; i < handlePositions.count(); i++)
  77. {
  78. position = tqRound( handlePositions[i] * (double)( width()-12) ) + 6;
  79. triangle[0] = TQPoint(position, height() - HANDLE_SIZE - MARGIN );
  80. triangle[1] = TQPoint(position + (HANDLE_SIZE / 2 - 1), height() - MARGIN );
  81. triangle[2] = TQPoint(position - (HANDLE_SIZE / 2 - 1), height() - MARGIN );
  82. painter.drawPolygon(triangle);
  83. }
  84. painter.setBrush( TQBrush( TQt::white ) );
  85. TQValueVector<double> middleHandlePositions = m_autogradientResource->getMiddleHandlePositions();
  86. for (uint i = 0; i < middleHandlePositions.count(); i++)
  87. {
  88. position = tqRound( middleHandlePositions[i] * (double)(width()-12) ) + 6;
  89. triangle[0] = TQPoint(position, height()-HANDLE_SIZE - MARGIN);
  90. triangle[1] = TQPoint(position + (HANDLE_SIZE / 2 - 2), height() - MARGIN);
  91. triangle[2] = TQPoint(position - (HANDLE_SIZE / 2 - 2), height() - MARGIN);
  92. painter.drawPolygon(triangle);
  93. }
  94. }
  95. bitBlt( this, 0, 0, &pixmap, 0, 0, pixmap.width(), pixmap.height(), TQt::CopyROP);
  96. }
  97. void KisGradientSliderWidget::mousePressEvent( TQMouseEvent * e )
  98. {
  99. TQWidget::mousePressEvent( e );
  100. if( ( e->y() < MARGIN || e->y() > height() - MARGIN ) || ( e->x() < MARGIN || e->x() > width() - MARGIN ) || e-> button() != Qt::LeftButton )
  101. return;
  102. double t = (double)(e->x() - MARGIN) / (double)(width() - 2 * MARGIN);
  103. KisGradientSegment* segment = 0;
  104. segment = m_autogradientResource->segmentAt(t);
  105. if(segment != 0)
  106. {
  107. m_currentSegment = segment;
  108. TQRect leftHandle( tqRound(m_currentSegment->startOffset() * (double)(width()-2*MARGIN-2)+ MARGIN - (HANDLE_SIZE/2 - 1 )),
  109. height() - HANDLE_SIZE,
  110. HANDLE_SIZE - 1,
  111. HANDLE_SIZE);
  112. TQRect middleHandle( tqRound(m_currentSegment->middleOffset() * (double)(width()-2*MARGIN-2)+ MARGIN - (HANDLE_SIZE/2 -2) ),
  113. height() - HANDLE_SIZE - MARGIN,
  114. HANDLE_SIZE - 1,
  115. HANDLE_SIZE);
  116. TQRect rightHandle( tqRound(m_currentSegment->endOffset() * (double)(width()-2*MARGIN-2)+ MARGIN - (HANDLE_SIZE/2 - 1 )),
  117. height() - HANDLE_SIZE,
  118. HANDLE_SIZE - 1,
  119. HANDLE_SIZE);
  120. // Change the activation order of the handles to avoid deadlocks
  121. if( t > 0.5 )
  122. {
  123. if( leftHandle.contains( e->pos() ) )
  124. m_drag = LEFT_DRAG;
  125. else if( middleHandle.contains( e->pos() ) )
  126. m_drag = MIDDLE_DRAG;
  127. else if( rightHandle.contains( e->pos() ) )
  128. m_drag = RIGHT_DRAG;
  129. }
  130. else
  131. {
  132. if( rightHandle.contains( e->pos() ) )
  133. m_drag = RIGHT_DRAG;
  134. else if( middleHandle.contains( e->pos() ) )
  135. m_drag = MIDDLE_DRAG;
  136. else if( leftHandle.contains( e->pos() ) )
  137. m_drag = LEFT_DRAG;
  138. }
  139. if( m_drag == NO_DRAG )
  140. {
  141. m_selectedSegment = m_currentSegment;
  142. emit sigSelectedSegment( m_selectedSegment );
  143. }
  144. }
  145. tqrepaint(false);
  146. }
  147. void KisGradientSliderWidget::mouseReleaseEvent ( TQMouseEvent * e )
  148. {
  149. TQWidget::mouseReleaseEvent( e );
  150. m_drag = NO_DRAG;
  151. }
  152. void KisGradientSliderWidget::mouseMoveEvent( TQMouseEvent * e )
  153. {
  154. TQWidget::mouseMoveEvent( e );
  155. if( ( e->y() < MARGIN || e->y() > height() - MARGIN ) || ( e->x() < MARGIN || e->x() > width() - MARGIN ) )
  156. return;
  157. double t = (double)(e->x() - MARGIN) / (double)(width() - 2 * MARGIN);
  158. switch( m_drag )
  159. {
  160. case RIGHT_DRAG:
  161. m_autogradientResource->moveSegmentEndOffset( m_currentSegment, t );
  162. break;
  163. case LEFT_DRAG:
  164. m_autogradientResource->moveSegmentStartOffset( m_currentSegment, t );
  165. break;
  166. case MIDDLE_DRAG:
  167. m_autogradientResource->moveSegmentMiddleOffset( m_currentSegment, t );
  168. break;
  169. }
  170. if ( m_drag != NO_DRAG)
  171. emit sigChangedSegment( m_currentSegment );
  172. tqrepaint(false);
  173. }
  174. void KisGradientSliderWidget::contextMenuEvent( TQContextMenuEvent * e )
  175. {
  176. m_segmentMenu->setItemEnabled( REMOVE_SEGMENT, m_autogradientResource->removeSegmentPossible() );
  177. m_segmentMenu->popup( e->globalPos());
  178. }
  179. void KisGradientSliderWidget::slotMenuAction( int id )
  180. {
  181. switch( id )
  182. {
  183. case SPLIT_SEGMENT:
  184. m_autogradientResource->splitSegment( m_selectedSegment );
  185. break;
  186. case DUPLICATE_SEGMENT:
  187. m_autogradientResource->duplicateSegment( m_selectedSegment );
  188. break;
  189. case MIRROR_SEGMENT:
  190. m_autogradientResource->mirrorSegment( m_selectedSegment );
  191. break;
  192. case REMOVE_SEGMENT:
  193. m_selectedSegment = m_autogradientResource->removeSegment( m_selectedSegment );
  194. break;
  195. }
  196. emit sigSelectedSegment( m_selectedSegment );
  197. tqrepaint(false);
  198. }
  199. #include "kis_gradient_slider_widget.moc"