karbon
vshapetool.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qcursor.h>
00021 #include <qevent.h>
00022 #include <qlabel.h>
00023
00024 #include "karbon_part.h"
00025 #include "karbon_view.h"
00026 #include "vcanvas.h"
00027 #include "vcomposite.h"
00028 #include "vglobal.h"
00029 #include "vpainter.h"
00030 #include "vpainterfactory.h"
00031 #include "vshapecmd.h"
00032 #include "vshapetool.h"
00033 #include "vselection.h"
00034 #include "vcursor.h"
00035
00036 VShapeTool::VShapeTool( KarbonView *view, const char *name, bool polar )
00037 : VTool( view, name )
00038 {
00039 m_cursor = new QCursor( VCursor::createCursor( VCursor::CrossHair ) );
00040
00041 m_isPolar = polar;
00042 m_isSquare = false;
00043 m_isCentered = false;
00044 }
00045
00046 VShapeTool::~VShapeTool()
00047 {
00048 delete m_cursor;
00049 }
00050
00051 QString
00052 VShapeTool::contextHelp()
00053 {
00054 QString s = i18n( "<qt><b>Shape tool</b><br>" );
00055 s += i18n( "<i>Click and drag</i> to place your own shape.<br>" );
00056 s += i18n( "<i>Click</i> to place a shape using the tool properties values.</qt>" );
00057 return s;
00058 }
00059
00060 void
00061 VShapeTool::activate()
00062 {
00063 VTool::activate();
00064 view()->setCursor( *m_cursor );
00065 view()->part()->document().selection()->showHandle( true );
00066 }
00067
00068 QString
00069 VShapeTool::statusText()
00070 {
00071 return uiname();
00072 }
00073
00074 void
00075 VShapeTool::draw()
00076 {
00077 VPainter* painter = view()->painterFactory()->editpainter();
00078 painter->setRasterOp( Qt::NotROP );
00079
00080 VPath* composite = shape();
00081 composite->setState( VPath::edit );
00082 composite->draw( painter, &composite->boundingBox() );
00083 delete( composite );
00084 }
00085
00086 void
00087 VShapeTool::mouseButtonPress()
00088 {
00089 recalc();
00090
00091
00092 draw();
00093 }
00094
00095 void
00096 VShapeTool::mouseButtonRelease()
00097 {
00098 draw();
00099
00100 recalc();
00101
00102 if( showDialog() )
00103 {
00104 VPath* composite = shape( true );
00105
00106 if( composite )
00107 {
00108 VShapeCmd* cmd = new VShapeCmd(
00109 &view()->part()->document(),
00110 uiname(), composite, icon() );
00111
00112 view()->part()->addCommand( cmd, true );
00113 }
00114 }
00115
00116 m_isSquare = false;
00117 m_isCentered = false;
00118 }
00119
00120 void
00121 VShapeTool::mouseDrag()
00122 {
00123
00124 draw();
00125
00126 recalc();
00127
00128
00129 draw();
00130 }
00131
00132 void
00133 VShapeTool::mouseDragRelease()
00134 {
00135
00136
00137 VShapeCmd* cmd = new VShapeCmd(
00138 &view()->part()->document(),
00139 uiname(), shape(), icon() );
00140
00141 view()->part()->addCommand( cmd, true );
00142
00143 m_isSquare = false;
00144 m_isCentered = false;
00145 }
00146
00147 void
00148 VShapeTool::mouseDragShiftPressed()
00149 {
00150
00151 draw();
00152
00153 m_isSquare = true;
00154 recalc();
00155
00156
00157 draw();
00158 }
00159
00160 void
00161 VShapeTool::mouseDragCtrlPressed()
00162 {
00163
00164 draw();
00165
00166 m_isCentered = true;
00167 recalc();
00168
00169
00170 draw();
00171 }
00172
00173 void
00174 VShapeTool::mouseDragShiftReleased()
00175 {
00176
00177 draw();
00178
00179 m_isSquare = false;
00180 recalc();
00181
00182
00183 draw();
00184 }
00185
00186 void
00187 VShapeTool::mouseDragCtrlReleased()
00188 {
00189
00190 draw();
00191
00192 m_isCentered = false;
00193 recalc();
00194
00195
00196 draw();
00197 }
00198
00199 void
00200 VShapeTool::cancel()
00201 {
00202
00203 if ( isDragging() )
00204 {
00205 draw();
00206 m_isSquare = false;
00207 m_isCentered = false;
00208 }
00209 }
00210
00211 void
00212 VShapeTool::recalc()
00213 {
00214 m_isSquare = shiftPressed();
00215 m_isCentered = ctrlPressed();
00216
00217 KoPoint _first = view()->canvasWidget()->snapToGrid( first() );
00218 KoPoint _last = view()->canvasWidget()->snapToGrid( last() );
00219
00220
00221 if( m_isPolar )
00222 {
00223
00224 m_d1 = sqrt(
00225 ( _last.x() - _first.x() ) * ( _last.x() - _first.x() ) +
00226 ( _last.y() - _first.y() ) * ( _last.y() - _first.y() ) );
00227
00228
00229 m_d2 = atan2( _last.y() - _first.y(), _last.x() - _first.x() );
00230
00231
00232 m_d2 -= VGlobal::pi_2;
00233
00234 m_p = _first;
00235 }
00236 else
00237
00238 {
00239 m_d1 = _last.x() - _first.x();
00240 m_d2 = _last.y() - _first.y();
00241
00242 const int m_sign1 = VGlobal::sign( m_d1 );
00243
00244 const int m_sign2 = VGlobal::sign( -m_d2 );
00245
00246
00247 if( m_d1 < 0.0 )
00248 m_d1 = -m_d1;
00249
00250 if( m_d2 < 0.0 )
00251 m_d2 = -m_d2;
00252
00253 if ( m_isSquare )
00254 {
00255 if ( m_d1 > m_d2 )
00256 m_d2 = m_d1;
00257 else
00258 m_d1 = m_d2;
00259 }
00260
00261 m_p.setX(
00262 _first.x() - ( m_sign1 == -1 ? m_d1 : 0.0 ) );
00263
00264 m_p.setY(
00265 _first.y() + ( m_sign2 == -1 ? m_d2 : 0.0 ) );
00266
00267 if ( m_isCentered )
00268 {
00269 m_p.setX( m_p.x() - m_sign1 * qRound( m_d1 * 0.5 ) );
00270 m_p.setY( m_p.y() + m_sign2 * qRound( m_d2 * 0.5 ) );
00271 }
00272 }
00273 }
00274
|