00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "tool_text.h"
00021
00022 #include <qcursor.h>
00023 #include <kiconloader.h>
00024 #include <kstandarddirs.h>
00025 #include <kdebug.h>
00026 #include <KoPoint.h>
00027 #include <klocale.h>
00028 #include <KoZoomHandler.h>
00029 #include <kinputdialog.h>
00030
00031 #include "kivio_view.h"
00032 #include "kivio_canvas.h"
00033 #include "kivio_page.h"
00034 #include "kivio_doc.h"
00035
00036 #include "kivio_stencil_spawner_set.h"
00037 #include "kivio_stencil_spawner.h"
00038 #include "kivio_custom_drag_data.h"
00039 #include "kivio_layer.h"
00040 #include "kivio_point.h"
00041 #include "kivio_stencil.h"
00042 #include "kivio_factory.h"
00043 #include "kivio_command.h"
00044 #include "kivio_pluginmanager.h"
00045 #include "mousetoolaction.h"
00046 #include "stenciltexteditor.h"
00047
00048
00049 TextTool::TextTool( KivioView* parent ) : Kivio::MouseTool(parent, "Text Mouse Tool")
00050 {
00051 m_textAction = new Kivio::MouseToolAction( i18n("Text Tool"), "text", Key_F2, actionCollection(), "text" );
00052 connect(m_textAction, SIGNAL(toggled(bool)), this, SLOT(setActivated(bool)));
00053 connect(m_textAction, SIGNAL(doubleClicked()), this, SLOT(makePermanent()));
00054
00055 m_permanent = false;
00056 m_mode = stmNone;
00057
00058 QPixmap pix = BarIcon("kivio_text_cursor",KivioFactory::global());
00059 m_pTextCursor = new QCursor(pix,2,2);
00060 }
00061
00062 TextTool::~TextTool()
00063 {
00064 delete m_pTextCursor;
00065 }
00066
00067
00074 bool TextTool::processEvent(QEvent* e)
00075 {
00076 switch (e->type())
00077 {
00078 case QEvent::MouseButtonPress:
00079 mousePress( (QMouseEvent*)e );
00080 return true;
00081 break;
00082
00083 case QEvent::MouseButtonRelease:
00084 mouseRelease( (QMouseEvent*)e );
00085 return true;
00086 break;
00087
00088 case QEvent::MouseMove:
00089 mouseMove( (QMouseEvent*)e );
00090 return true;
00091 break;
00092
00093 default:
00094 break;
00095 }
00096
00097 return false;
00098 }
00099
00100 void TextTool::setActivated(bool a)
00101 {
00102 if(a) {
00103 emit activated(this);
00104 m_textAction->setChecked(true);
00105 view()->canvasWidget()->setCursor(*m_pTextCursor);
00106 m_mode = stmNone;
00107 } else {
00108 m_textAction->setChecked(false);
00109 m_permanent = false;
00110 }
00111 }
00112
00113 void TextTool::text(QRect r)
00114 {
00115
00116 KoPoint startPoint = view()->canvasWidget()->mapFromScreen( QPoint( r.x(), r.y() ) );
00117 KoPoint releasePoint = view()->canvasWidget()->mapFromScreen( QPoint( r.x() + r.width(), r.y() + r.height() ) );
00118
00119
00120 float x = startPoint.x() < releasePoint.x() ? startPoint.x() : releasePoint.x();
00121 float y = startPoint.y() < releasePoint.y() ? startPoint.y() : releasePoint.y();
00122
00123
00124 float w = releasePoint.x() - startPoint.x();
00125
00126 if( w < 0.0 ) {
00127 w *= -1.0;
00128 }
00129
00130 float h = releasePoint.y() - startPoint.y();
00131
00132 if( h < 0.0 ) {
00133 h *= -1.0;
00134 }
00135
00136 KivioDoc* doc = view()->doc();
00137 KivioPage* page = view()->activePage();
00138
00139 KivioStencilSpawner* ss = doc->findInternalStencilSpawner("Dave Marotti - Text");
00140
00141 if (!ss) {
00142 return;
00143 }
00144
00145 KivioStencil* stencil = ss->newStencil();
00146 stencil->setType(kstText);
00147 stencil->setPosition(x,y);
00148 stencil->setDimensions(w,h);
00149 stencil->setText("");
00150 stencil->setTextFont(doc->defaultFont());
00151 page->unselectAllStencils();
00152 page->addStencil(stencil);
00153 page->selectStencil(stencil);
00154
00155 doc->updateView(page);
00156
00157 applyToolAction(page->selectedStencils());
00158
00159 if (stencil->text().isEmpty()) {
00160 page->deleteSelectedStencils();
00161 doc->updateView(page);
00162 }
00163 }
00164
00165 void TextTool::mousePress( QMouseEvent *e )
00166 {
00167 if(e->button() == LeftButton) {
00168 KoPoint pagePoint = view()->canvasWidget()->mapFromScreen(e->pos());
00169 int colType;
00170 KivioPage *page = view()->activePage();
00171 KivioStencil* stencil = page->checkForStencil( &pagePoint, &colType, 0.0, false);
00172
00173 if(stencil) {
00174 applyToolAction(stencil, pagePoint);
00175 } else if(startRubberBanding(e)) {
00176 m_mode = stmDrawRubber;
00177 }
00178 }
00179 }
00180
00181
00185 bool TextTool::startRubberBanding( QMouseEvent *e )
00186 {
00187 view()->canvasWidget()->startRectDraw( e->pos(), KivioCanvas::Rubber );
00188 view()->canvasWidget()->repaint();
00189 m_startPoint = e->pos();
00190
00191 return true;
00192 }
00193
00194 void TextTool::mouseMove( QMouseEvent * e )
00195 {
00196 switch( m_mode )
00197 {
00198 case stmDrawRubber:
00199 continueRubberBanding(e);
00200 break;
00201
00202 default:
00203 break;
00204 }
00205 }
00206
00207 void TextTool::continueRubberBanding( QMouseEvent *e )
00208 {
00209 view()->canvasWidget()->continueRectDraw( e->pos(), KivioCanvas::Rubber );
00210 }
00211
00212 void TextTool::mouseRelease( QMouseEvent *e )
00213 {
00214 m_releasePoint = e->pos();
00215
00216 switch( m_mode )
00217 {
00218 case stmDrawRubber:
00219 endRubberBanding(e);
00220 break;
00221 }
00222
00223 m_mode = stmNone;
00224
00225 view()->canvasWidget()->repaint();
00226 }
00227
00228 void TextTool::endRubberBanding(QMouseEvent *)
00229 {
00230
00231 view()->canvasWidget()->endRectDraw();
00232 QRect rect;
00233
00234 if( m_startPoint != m_releasePoint ) {
00235 rect = view()->canvasWidget()->rect();
00236 } else {
00237 rect.setTopLeft(m_startPoint);
00238 rect.setWidth(view()->zoomHandler()->zoomItX(100));
00239 rect.setHeight(view()->zoomHandler()->zoomItY(20));
00240 }
00241
00242 text(rect);
00243
00244 if(!m_permanent) {
00245 view()->pluginManager()->activateDefaultTool();
00246 }
00247 }
00248
00249 void TextTool::applyToolAction(QPtrList<KivioStencil>* stencils)
00250 {
00251 if(stencils->isEmpty()) {
00252 return;
00253 }
00254
00255 KivioStencil* stencil = stencils->first();
00256 bool ok = false;
00257
00258 while(stencil) {
00259 if(stencil->hasTextBox()) {
00260 ok = true;
00261 break;
00262 }
00263
00264 stencil = stencils->next();
00265 }
00266
00267 if(!ok) {
00268 return;
00269 }
00270
00271 Kivio::StencilTextEditor editor(i18n("Edit Text"), view(), "StencilTextEditor");
00272 editor.setFont(stencil->textFont());
00273 editor.setFontColor(stencil->textColor());
00274 editor.setBackgroundColor(stencil->bgColor());
00275 editor.setText(stencil->text());
00276 editor.setHorizontalAlign(static_cast<Qt::AlignmentFlags>(stencil->hTextAlign()));
00277 editor.setVerticalAlign(static_cast<Qt::AlignmentFlags>(stencil->vTextAlign()));
00278
00279 if(editor.exec() == QDialog::Accepted) {
00280 KMacroCommand* macroCmd = new KMacroCommand(i18n("Change Stencil Text and Formatting"));
00281 bool changed = false;
00282 QString text = editor.text();
00283 QFont font = editor.font();
00284 QColor textColor = editor.fontColor();
00285 int halignment = editor.horizontalAlignment();
00286 int valignment = editor.verticalAlignment();
00287 bool changeFont = (stencil->textFont() != font);
00288 bool changeTextColor = (stencil->textColor() != textColor);
00289 bool changeHAlignment = (stencil->hTextAlign() != halignment);
00290 bool changeVAlignment = (stencil->vTextAlign() != valignment);
00291
00292
00293 while( stencil )
00294 {
00295 if(stencil->text() != text)
00296 {
00297 KivioChangeStencilTextCommand *cmd = new KivioChangeStencilTextCommand(i18n("Change Stencil Text"),
00298 stencil, stencil->text(), text, view()->activePage());
00299 stencil->setText(text);
00300 macroCmd->addCommand(cmd);
00301 changed = true;
00302 }
00303
00304 if(changeFont && (stencil->textFont() != font)) {
00305 KivioChangeStencilFontCommand* cmd = new KivioChangeStencilFontCommand(i18n("Change Stencil Font"),
00306 view()->activePage(), stencil, stencil->textFont(), font);
00307 stencil->setTextFont(font);
00308 macroCmd->addCommand(cmd);
00309 changed = true;
00310 }
00311
00312 if(changeTextColor && (stencil->textColor() != textColor)) {
00313 KivioChangeStencilColorCommand* cmd = new KivioChangeStencilColorCommand(i18n("Change Stencil Text Color"),
00314 view()->activePage(), stencil, stencil->textColor(), textColor, KivioChangeStencilColorCommand::CT_TEXTCOLOR);
00315 stencil->setTextColor(textColor);
00316 macroCmd->addCommand(cmd);
00317 changed = true;
00318 }
00319
00320 if(changeHAlignment && (stencil->hTextAlign() != halignment)) {
00321 KivioChangeStencilHAlignmentCommand* cmd = new KivioChangeStencilHAlignmentCommand(
00322 i18n("Change Stencil Horizontal Alignment"), view()->activePage(), stencil, stencil->hTextAlign(), halignment);
00323 stencil->setHTextAlign(halignment);
00324 macroCmd->addCommand(cmd);
00325 changed = true;
00326 }
00327
00328 if(changeVAlignment && (stencil->vTextAlign() != valignment)) {
00329 KivioChangeStencilVAlignmentCommand* cmd = new KivioChangeStencilVAlignmentCommand(
00330 i18n("Change Stencil Vertical Alignment"), view()->activePage(), stencil, stencil->vTextAlign(), valignment);
00331 stencil->setVTextAlign(valignment);
00332 macroCmd->addCommand(cmd);
00333 changed = true;
00334 }
00335
00336 stencil = stencils->next();
00337 }
00338
00339 if(changed) {
00340 view()->doc()->addCommand(macroCmd);
00341 view()->doc()->updateView(view()->activePage());
00342 } else {
00343 delete macroCmd;
00344 }
00345 }
00346 }
00347
00348 void TextTool::applyToolAction(KivioStencil* stencil, const KoPoint& pos)
00349 {
00350 if(!stencil) {
00351 return;
00352 }
00353
00354 QString name = stencil->getTextBoxName(pos);
00355
00356 if(name.isEmpty()) {
00357 return;
00358 }
00359
00360 Kivio::StencilTextEditor editor(i18n("Edit Text"), view(), "StencilTextEditor");
00361 editor.setFont(stencil->textFont(name));
00362 editor.setFontColor(stencil->textColor(name));
00363 editor.setBackgroundColor(stencil->bgColor());
00364 editor.setText(stencil->text(name));
00365 editor.setHorizontalAlign(static_cast<Qt::AlignmentFlags>(stencil->hTextAlign(name)));
00366 editor.setVerticalAlign(static_cast<Qt::AlignmentFlags>(stencil->vTextAlign(name)));
00367
00368 if(editor.exec() == QDialog::Accepted) {
00369 KMacroCommand* macroCmd = new KMacroCommand(i18n("Change Stencil Text and Formatting"));
00370 bool changed = false;
00371 QString text = editor.text();
00372
00373 if(stencil->text(name) != text)
00374 {
00375 KivioChangeStencilTextCommand *cmd = new KivioChangeStencilTextCommand(i18n("Change Stencil Text"),
00376 stencil, stencil->text(name), text, view()->activePage(), name);
00377 stencil->setText(text, name);
00378 macroCmd->addCommand(cmd);
00379 changed = true;
00380 }
00381
00382 QFont font = editor.font();
00383
00384 if(stencil->textFont(name) != font) {
00385 KivioChangeStencilFontCommand* cmd = new KivioChangeStencilFontCommand(i18n("Change Stencil Font"),
00386 view()->activePage(), stencil, stencil->textFont(name), font, name);
00387 stencil->setTextFont(name, font);
00388 macroCmd->addCommand(cmd);
00389 changed = true;
00390 }
00391
00392 QColor textColor = editor.fontColor();
00393
00394 if(stencil->textColor(name) != textColor) {
00395 KivioChangeStencilColorCommand* cmd = new KivioChangeStencilColorCommand(i18n("Change Stencil Text Color"),
00396 view()->activePage(), stencil, stencil->textColor(name), textColor,
00397 KivioChangeStencilColorCommand::CT_TEXTCOLOR, name);
00398 stencil->setTextColor(name, textColor);
00399 macroCmd->addCommand(cmd);
00400 changed = true;
00401 }
00402
00403 int halignment = editor.horizontalAlignment();
00404
00405 if(stencil->hTextAlign(name) != halignment) {
00406 KivioChangeStencilHAlignmentCommand* cmd = new KivioChangeStencilHAlignmentCommand(
00407 i18n("Change Stencil Horizontal Alignment"), view()->activePage(), stencil,
00408 stencil->hTextAlign(name), halignment, name);
00409 stencil->setHTextAlign(name, halignment);
00410 macroCmd->addCommand(cmd);
00411 changed = true;
00412 }
00413
00414 int valignment = editor.verticalAlignment();
00415
00416 if(stencil->vTextAlign(name) != valignment) {
00417 KivioChangeStencilVAlignmentCommand* cmd = new KivioChangeStencilVAlignmentCommand(
00418 i18n("Change Stencil Vertical Alignment"), view()->activePage(), stencil,
00419 stencil->vTextAlign(name), valignment, name);
00420 stencil->setVTextAlign(name, valignment);
00421 macroCmd->addCommand(cmd);
00422 changed = true;
00423 }
00424
00425 if(changed) {
00426 view()->doc()->addCommand(macroCmd);
00427 view()->doc()->updateView(view()->activePage());
00428 } else {
00429 delete macroCmd;
00430 }
00431 }
00432 }
00433
00434 void TextTool::makePermanent()
00435 {
00436 m_permanent = true;
00437 }
00438
00439 #include "tool_text.moc"