KOffice – TDE office suite
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * kis_tool_ellipse.cc - part of Krayon
  3. *
  4. * Copyright (c) 2000 John Califf <jcaliff@compuzone.net>
  5. * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
  6. * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
  7. * Copyright (c) 2004 Clarence Dang <dang@kde.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <tqpainter.h>
  24. #include <tdeaction.h>
  25. #include <kdebug.h>
  26. #include <tdelocale.h>
  27. #include "kis_painter.h"
  28. #include "kis_canvas_subject.h"
  29. #include "kis_canvas_controller.h"
  30. #include "kis_tool_ellipse.h"
  31. #include "kis_button_press_event.h"
  32. #include "kis_button_release_event.h"
  33. #include "kis_move_event.h"
  34. #include "kis_paintop_registry.h"
  35. #include "kis_undo_adapter.h"
  36. #include "kis_canvas.h"
  37. #include "kis_canvas_painter.h"
  38. #include "kis_cursor.h"
  39. KisToolEllipse::KisToolEllipse()
  40. : super(i18n ("Ellipse")),
  41. m_dragging (false),
  42. m_currentImage (0)
  43. {
  44. setName("tool_ellipse");
  45. setCursor(KisCursor::load("tool_ellipse_cursor.png", 6, 6));
  46. }
  47. KisToolEllipse::~KisToolEllipse()
  48. {
  49. }
  50. void KisToolEllipse::update (KisCanvasSubject *subject)
  51. {
  52. super::update (subject);
  53. if (m_subject)
  54. m_currentImage = m_subject->currentImg ();
  55. }
  56. void KisToolEllipse::buttonPress(KisButtonPressEvent *event)
  57. {
  58. if (m_currentImage && event->button() == Qt::LeftButton) {
  59. m_dragging = true;
  60. m_dragStart = m_dragCenter = m_dragEnd = event->pos();
  61. draw(m_dragStart, m_dragEnd);
  62. }
  63. }
  64. void KisToolEllipse::move(KisMoveEvent *event)
  65. {
  66. if (m_dragging) {
  67. // erase old lines on canvas
  68. draw(m_dragStart, m_dragEnd);
  69. // move (alt) or resize ellipse
  70. if (event->state() & TQt::AltButton) {
  71. KisPoint trans = event->pos() - m_dragEnd;
  72. m_dragStart += trans;
  73. m_dragEnd += trans;
  74. } else {
  75. KisPoint diag = event->pos() - (event->state() & TQt::ControlButton
  76. ? m_dragCenter : m_dragStart);
  77. // circle?
  78. if (event->state() & TQt::ShiftButton) {
  79. double size = TQMAX(fabs(diag.x()), fabs(diag.y()));
  80. double w = diag.x() < 0 ? -size : size;
  81. double h = diag.y() < 0 ? -size : size;
  82. diag = KisPoint(w, h);
  83. }
  84. // resize around center point?
  85. if (event->state() & TQt::ControlButton) {
  86. m_dragStart = m_dragCenter - diag;
  87. m_dragEnd = m_dragCenter + diag;
  88. } else {
  89. m_dragEnd = m_dragStart + diag;
  90. }
  91. }
  92. // draw new lines on canvas
  93. draw(m_dragStart, m_dragEnd);
  94. m_dragCenter = KisPoint((m_dragStart.x() + m_dragEnd.x()) / 2,
  95. (m_dragStart.y() + m_dragEnd.y()) / 2);
  96. }
  97. }
  98. void KisToolEllipse::buttonRelease(KisButtonReleaseEvent *event)
  99. {
  100. if (!m_subject || !m_currentImage)
  101. return;
  102. if (m_dragging && event->button() == Qt::LeftButton) {
  103. // erase old lines on canvas
  104. draw(m_dragStart, m_dragEnd);
  105. m_dragging = false;
  106. if (m_dragStart == m_dragEnd)
  107. return;
  108. if (!m_currentImage)
  109. return;
  110. if (!m_currentImage->activeDevice())
  111. return;
  112. KisPaintDeviceSP device = m_currentImage->activeDevice ();
  113. KisPainter painter (device);
  114. if (m_currentImage->undo()) painter.beginTransaction (i18n ("Ellipse"));
  115. painter.setPaintColor(m_subject->fgColor());
  116. painter.setBackgroundColor(m_subject->bgColor());
  117. painter.setFillStyle(fillStyle());
  118. painter.setBrush(m_subject->currentBrush());
  119. painter.setPattern(m_subject->currentPattern());
  120. painter.setOpacity(m_opacity);
  121. painter.setCompositeOp(m_compositeOp);
  122. KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp(m_subject->currentPaintop(), m_subject->currentPaintopSettings(), &painter);
  123. painter.setPaintOp(op); // Painter takes ownership
  124. painter.paintEllipse(m_dragStart, m_dragEnd, PRESSURE_DEFAULT/*event->pressure()*/, event->xTilt(), event->yTilt());
  125. device->setDirty( painter.dirtyRect() );
  126. notifyModified();
  127. KisUndoAdapter *adapter = m_currentImage->undoAdapter();
  128. if (adapter) {
  129. adapter->addCommand(painter.endTransaction());
  130. }
  131. }
  132. }
  133. void KisToolEllipse::draw(const KisPoint& start, const KisPoint& end )
  134. {
  135. if (!m_subject || !m_currentImage)
  136. return;
  137. KisCanvasController *controller = m_subject->canvasController ();
  138. KisCanvas *canvas = controller->kiscanvas();
  139. KisCanvasPainter p (canvas);
  140. p.setRasterOp (TQt::NotROP);
  141. p.drawEllipse (TQRect (controller->windowToView (start).floorTQPoint(), controller->windowToView (end).floorTQPoint()));
  142. p.end ();
  143. }
  144. void KisToolEllipse::setup(TDEActionCollection *collection)
  145. {
  146. m_action = static_cast<TDERadioAction *>(collection->action(name()));
  147. if (m_action == 0) {
  148. TDEShortcut shortcut(TQt::Key_Plus);
  149. shortcut.append(TDEShortcut(TQt::Key_F7));
  150. m_action = new TDERadioAction(i18n("&Ellipse"),
  151. "tool_ellipse",
  152. shortcut,
  153. this,
  154. TQT_SLOT(activate()),
  155. collection,
  156. name());
  157. m_action->setToolTip(i18n("Draw an ellipse"));
  158. m_action->setExclusiveGroup("tools");
  159. m_ownAction = true;
  160. }
  161. }
  162. #include "kis_tool_ellipse.moc"