1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
/***************************************************************************
* Copyright (C) 2005 by David Saxton *
* david@bluehaze.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "canvasmanipulator.h"
#include "cnitem.h"
#include "connector.h"
#include "docmanager.h"
#include "drawpart.h"
#include "ecnode.h"
#include "itemdocument.h"
#include "itemview.h"
#include "ktechlab.h"
#include "core/ktlconfig.h"
#include <tdeaccel.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kurldrag.h>
#include <cmath>
#include <tqcursor.h>
#include <tqtimer.h>
#include <tqwmatrix.h>
//BEGIN class ItemView
ItemView::ItemView( ItemDocument * itemDocument, ViewContainer *viewContainer, uint viewAreaId, const char *name )
: View( itemDocument, viewContainer, viewAreaId, name )
{
TDEActionCollection * ac = actionCollection();
KStdAction::selectAll( itemDocument, TQT_SLOT(selectAll()), ac );
KStdAction::zoomIn( TQT_TQOBJECT(this), TQT_SLOT(zoomIn()), ac );
KStdAction::zoomOut( TQT_TQOBJECT(this), TQT_SLOT(zoomOut()), ac );
KStdAction::actualSize( TQT_TQOBJECT(this), TQT_SLOT(actualSize()), ac )->setEnabled(false);
TDEAccel *pAccel = new TDEAccel(this);
pAccel->insert( "Cancel", i18n("Cancel"), i18n("Cancel the current operation"), TQt::Key_Escape, itemDocument, TQT_SLOT(cancelCurrentOperation()) );
pAccel->readSettings();
new TDEAction( i18n("Delete"), "editdelete", TQt::Key_Delete, itemDocument, TQT_SLOT(deleteSelection()), ac, "edit_delete" );
new TDEAction( i18n("Export as Image..."), 0, 0, itemDocument, TQT_SLOT(exportToImage()), ac, "file_export_image");
//BEGIN Item Alignment actions
new TDEAction( i18n("AlignQt::Horizontally"), 0, 0, itemDocument, TQT_SLOT(alignHorizontally()), ac, "align_horizontally" );
new TDEAction( i18n("AlignQt::Vertically"), 0, 0, itemDocument, TQT_SLOT(alignVertically()), ac, "align_vertically" );
new TDEAction( i18n("DistributeQt::Horizontally"), 0, 0, itemDocument, TQT_SLOT(distributeHorizontally()), ac, "distribute_horizontally" );
new TDEAction( i18n("DistributeQt::Vertically"), 0, 0, itemDocument, TQT_SLOT(distributeVertically()), ac, "distribute_vertically" );
//END Item Alignment actions
//BEGIN Draw actions
TDEToolBarPopupAction * pa = new TDEToolBarPopupAction( i18n("Draw"), "paintbrush", 0, 0, 0, ac, "edit_draw" );
pa->setDelayed(false);
TDEPopupMenu * m = pa->popupMenu();
m->insertTitle( i18n("Draw") );
m->insertItem( TDEGlobal::iconLoader()->loadIcon( "tool_text", TDEIcon::Small ), i18n("Text"), DrawPart::da_text );
m->insertItem( TDEGlobal::iconLoader()->loadIcon( "tool_line", TDEIcon::Small ), i18n("Line"), DrawPart::da_line );
m->insertItem( TDEGlobal::iconLoader()->loadIcon( "tool_arrow", TDEIcon::Small ), i18n("Arrow"), DrawPart::da_arrow );
m->insertItem( TDEGlobal::iconLoader()->loadIcon( "tool_ellipse", TDEIcon::Small ), i18n("Ellipse"), DrawPart::da_ellipse );
m->insertItem( TDEGlobal::iconLoader()->loadIcon( "tool_rectangle", TDEIcon::Small ), i18n("Rectangle"), DrawPart::da_rectangle );
connect( m, TQT_SIGNAL(activated(int)), itemDocument, TQT_SLOT(slotSetDrawAction(int)) );
//END Draw actions
//BEGIN Item Control actions
new TDEAction( i18n("Raise Selection"), "1uparrow", TQt::Key_PageUp, itemDocument, TQT_SLOT(raiseZ()), ac, "edit_raise" );
new TDEAction( i18n("Lower Selection"), "1downarrow", TQt::Key_PageDown, itemDocument, TQT_SLOT(lowerZ()), ac, "edit_lower" );
//END Item Control actions
TDEAction * na = new TDEAction( "", 0, 0, 0, 0, ac, "null_action" );
na->setEnabled(false);
setXMLFile( "ktechlabitemviewui.rc" );
m_pUpdateStatusTmr = new TQTimer(this);
connect( m_pUpdateStatusTmr, TQT_SIGNAL(timeout()), this, TQT_SLOT(updateStatus()) );
connect( this, TQT_SIGNAL(viewUnfocused()), this, TQT_SLOT(stopUpdatingStatus()) );
p_itemDocument = itemDocument;
m_zoomLevel = 1.;
m_CVBEditor = new CVBEditor( p_itemDocument->canvas(), this, "cvbEditor" );
m_CVBEditor->setLineWidth(1);
m_layout->insertWidget( 0, m_CVBEditor );
setAcceptDrops(true);
}
ItemView::~ItemView()
{
}
bool ItemView::canZoomIn() const
{
return true;
}
bool ItemView::canZoomOut() const
{
return int(std::floor((100*m_zoomLevel)+0.5)) > 25;
}
void ItemView::zoomIn()
{
int currentZoomPercent = int(std::floor((100*m_zoomLevel)+0.5));
int newZoom = currentZoomPercent;
if ( currentZoomPercent < 100 )
newZoom += 25;
else if ( currentZoomPercent < 200 )
newZoom += 50;
else
newZoom += 100;
m_zoomLevel = newZoom/100.;
TQWMatrix m( m_zoomLevel, 0.0, 0.0, m_zoomLevel, 1.0, 1.0 );
m_CVBEditor->setWorldMatrix(m);
p_itemDocument->requestEvent( ItemDocument::ItemDocumentEvent::ResizeCanvasToItems );
updateZoomActions();
}
void ItemView::zoomOut()
{
int currentZoomPercent = int(std::floor((100*m_zoomLevel)+0.5));
int newZoom = currentZoomPercent;
if ( currentZoomPercent <= 25 )
return;
if ( currentZoomPercent <= 100 )
newZoom -= 25;
else if ( currentZoomPercent <= 200 )
newZoom -= 50;
else
newZoom -= 100;
m_zoomLevel = newZoom/100.;
TQWMatrix m( m_zoomLevel, 0.0, 0.0, m_zoomLevel, 1.0, 1.0 );
m_CVBEditor->setWorldMatrix(m);
p_itemDocument->requestEvent( ItemDocument::ItemDocumentEvent::ResizeCanvasToItems );
updateZoomActions();
}
void ItemView::actualSize()
{
m_zoomLevel = 1.0;
TQWMatrix m( m_zoomLevel, 0.0, 0.0, m_zoomLevel, 1.0, 1.0 );
m_CVBEditor->setWorldMatrix(m);
p_itemDocument->requestEvent( ItemDocument::ItemDocumentEvent::ResizeCanvasToItems );
updateZoomActions();
}
void ItemView::updateZoomActions()
{
action("view_zoom_in")->setEnabled( canZoomIn() );
action("view_zoom_out")->setEnabled( canZoomOut() );
action("view_actual_size")->setEnabled( m_zoomLevel != 1.0 );
}
void ItemView::dropEvent( TQDropEvent *event )
{
KURL::List urls;
if ( KURLDrag::decode( event, urls ) )
{
// Then it is URLs that we can decode :)
const KURL::List::iterator end = urls.end();
for ( KURL::List::iterator it = urls.begin(); it != end; ++it )
{
DocManager::self()->openURL(*it);
}
return;
}
if ( !TQString(event->format()).startsWith("ktechlab/") )
return;
TQString text;
TQDataStream stream( event->encodedData(event->format()), IO_ReadOnly );
stream >> text;
TQPoint position = event->pos();
position.setX( int((position.x() + m_CVBEditor->contentsX())/m_zoomLevel) );
position.setY( int((position.y() + m_CVBEditor->contentsY())/m_zoomLevel) );
// Get a new component item
p_itemDocument->addItem( text, position, true );
setFocus();
}
void ItemView::scrollToMouse( const TQPoint &pos )
{
TQPoint position = pos * m_zoomLevel;
int left = m_CVBEditor->contentsX();
int top = m_CVBEditor->contentsY();
int right = left + m_CVBEditor->visibleWidth();
int bottom = top + m_CVBEditor->visibleHeight();
if( position.x() < left ) m_CVBEditor->scrollBy( position.x() - left, 0 );
else if( position.x() > right ) m_CVBEditor->scrollBy( position.x() - right, 0 );
if( position.y() < top ) m_CVBEditor->scrollBy( 0, position.y() - top );
else if( position.y() > bottom ) m_CVBEditor->scrollBy( 0, position.y() - bottom);
}
void ItemView::contentsMousePressEvent( TQMouseEvent *e )
{
if (!e)
return;
e->accept();
// For some reason, when we are initially unfocused, we only receive the
// release event if the user drags the mouse - not very often. So see if we
// were initially unfocused, and if so, do unclick as well.
bool wasFocused = isFocused();
setFocused();
if ( !p_itemDocument )
return;
p_itemDocument->canvas()->setMessage( TQString() );
p_itemDocument->m_cmManager->mousePressEvent( EventInfo( this, e ) );
if ( !wasFocused )
p_itemDocument->m_cmManager->mouseReleaseEvent( EventInfo( this, e ) );
}
void ItemView::contentsMouseDoubleClickEvent( TQMouseEvent *e )
{
if (!e)
return;
e->accept();
//HACK: Pass this of as a single press event if widget underneath
TQCanvasItem * atTop = p_itemDocument->itemAtTop( e->pos()/zoomLevel() );
if ( atTop && atTop->rtti() == ItemDocument::RTTI::Widget )
contentsMousePressEvent(e);
else
p_itemDocument->m_cmManager->mouseDoubleClickEvent( EventInfo( this, e ) );
}
void ItemView::contentsMouseMoveEvent( TQMouseEvent *e )
{
if ( !e || !p_itemDocument )
return;
e->accept();
p_itemDocument->m_cmManager->mouseMoveEvent( EventInfo( this, e ) );
if ( !m_pUpdateStatusTmr->isActive() )
startUpdatingStatus();
}
void ItemView::contentsMouseReleaseEvent( TQMouseEvent *e )
{
if (!e)
return;
e->accept();
p_itemDocument->m_cmManager->mouseReleaseEvent( EventInfo( this, e ) );
}
void ItemView::contentsWheelEvent( TQWheelEvent *e )
{
if (!e)
return;
e->accept();
p_itemDocument->m_cmManager->wheelEvent( EventInfo( this, e ) );
}
void ItemView::dragEnterEvent( TQDragEnterEvent *event )
{
startUpdatingStatus();
KURL::List urls;
if ( KURLDrag::decode( event, urls ) )
{
event->accept(true);
// Then it is URLs that we can decode later :)
return;
}
}
void ItemView::enterEvent( TQEvent * e )
{
Q_UNUSED(e);
startUpdatingStatus();
}
void ItemView::leaveEvent( TQEvent * e )
{
Q_UNUSED(e);
stopUpdatingStatus();
// Cleanup
setCursor(TQt::ArrowCursor);
if (p_ktechlab)
p_ktechlab->slotChangeStatusbar(TQString());
if ( p_itemDocument )
p_itemDocument->m_canvasTip->setVisible(false);
}
void ItemView::slotUpdateConfiguration()
{
// m_CVBEditor->setEraseColor( KTLConfig::bgColor() );
m_CVBEditor->setEraseColor( TQt::white );
if ( m_pUpdateStatusTmr->isActive() )
startUpdatingStatus();
}
void ItemView::startUpdatingStatus()
{
m_pUpdateStatusTmr->stop();
m_pUpdateStatusTmr->start( int(1000./KTLConfig::refreshRate()) );
}
void ItemView::stopUpdatingStatus()
{
m_pUpdateStatusTmr->stop();
}
void ItemView::updateStatus()
{
TQPoint pos = (m_CVBEditor->mapFromGlobal( TQCursor::pos() ) + TQPoint( m_CVBEditor->contentsX(), m_CVBEditor->contentsY() )) / zoomLevel();
ItemDocument * itemDocument = static_cast<ItemDocument*>(document());
if ( !itemDocument )
return;
CMManager * cmManager = itemDocument->m_cmManager;
CanvasTip * canvasTip = itemDocument->m_canvasTip;
bool displayTip = false;
TQCursor cursor = TQt::ArrowCursor;
TQString statusbar;
if ( cmManager->cmState() & CMManager::cms_repeated_add )
{
cursor = TQt::CrossCursor;
statusbar = i18n("Left click to add. Right click to resume normal editing");
}
else if ( cmManager->cmState() & CMManager::cms_draw )
{
cursor = TQt::CrossCursor;
statusbar = i18n("Click and hold to start drawing.");
}
else if ( cmManager->currentManipulator())
{
switch ( cmManager->currentManipulator()->type() )
{
case CanvasManipulator::RepeatedItemAdd:
cursor = TQt::CrossCursor;
statusbar = i18n("Left click to add. Right click to resume normal editing");
break;
case CanvasManipulator::ManualConnector:
statusbar = i18n("Right click to cancel the connector");
// no break
case CanvasManipulator::AutoConnector:
cursor = TQt::CrossCursor;
break;
case CanvasManipulator::ItemMove:
case CanvasManipulator::MechItemMove:
cursor = TQt::SizeAllCursor;
break;
case CanvasManipulator::Draw:
cursor = TQt::CrossCursor;
break;
default:
break;
}
}
else if ( TQCanvasItem *qcanvasItem = itemDocument->itemAtTop(pos) )
{
switch( qcanvasItem->rtti() )
{
case ItemDocument::RTTI::Connector:
{
cursor = TQt::CrossCursor;
if ( itemDocument->type() != Document::dt_circuit )
break;
canvasTip->displayVI( static_cast<Connector*>(qcanvasItem), pos );
displayTip = true;
break;
}
case ItemDocument::RTTI::Node:
{
cursor = TQt::CrossCursor;
ECNode * ecnode = dynamic_cast<ECNode*>(qcanvasItem);
if ( !ecnode )
break;
canvasTip->displayVI( ecnode, pos );
displayTip = true;
break;
}
case ItemDocument::RTTI::CNItem:
{
statusbar = (static_cast<CNItem*>(qcanvasItem))->name();
break;
}
default:
{
break;
}
}
}
setCursor(cursor);
if (p_ktechlab)
p_ktechlab->slotChangeStatusbar(statusbar);
canvasTip->setVisible(displayTip);
}
//END class ItemView
//BEGIN class CVBEditor
CVBEditor::CVBEditor( TQCanvas *canvas, ItemView *itemView, const char *name )
: TQCanvasView( canvas, itemView, name, WNoAutoErase | WStaticContents )
{
b_ignoreEvents = false;
b_passEventsToView = true;
p_itemView = itemView;
viewport()->setMouseTracking(true);
setAcceptDrops(true);
setFrameShape(NoFrame);
// setEraseColor( KTLConfig::bgColor() );
setEraseColor( TQt::white );
setPaletteBackgroundColor( TQt::white );
viewport()->setEraseColor( TQt::white );
viewport()->setPaletteBackgroundColor( TQt::white );
}
void CVBEditor::contentsMousePressEvent( TQMouseEvent* e )
{
if (b_passEventsToView)
p_itemView->contentsMousePressEvent(e);
else
TQCanvasView::contentsMousePressEvent(e);
}
void CVBEditor::contentsMouseReleaseEvent( TQMouseEvent* e )
{
if (b_passEventsToView)
p_itemView->contentsMouseReleaseEvent(e);
else
TQCanvasView::contentsMouseReleaseEvent(e);
}
void CVBEditor::contentsMouseDoubleClickEvent( TQMouseEvent* e )
{
if (b_passEventsToView)
p_itemView->contentsMouseDoubleClickEvent(e);
else
TQCanvasView::contentsMouseDoubleClickEvent(e);
}
void CVBEditor::contentsMouseMoveEvent( TQMouseEvent* e )
{
if (b_passEventsToView)
p_itemView->contentsMouseMoveEvent(e);
else
TQCanvasView::contentsMouseMoveEvent(e);
}
void CVBEditor::dragEnterEvent( TQDragEnterEvent* e )
{
if (b_passEventsToView)
p_itemView->dragEnterEvent(e);
else
TQCanvasView::dragEnterEvent(e);
}
void CVBEditor::dropEvent( TQDropEvent* e )
{
if (b_passEventsToView)
p_itemView->dropEvent(e);
else
TQCanvasView::dropEvent(e);
}
void CVBEditor::enterEvent( TQEvent * e )
{
if (b_passEventsToView)
p_itemView->enterEvent(e);
else
TQCanvasView::enterEvent(e);
}
void CVBEditor::leaveEvent( TQEvent* e )
{
if (b_passEventsToView)
p_itemView->leaveEvent(e);
else
TQCanvasView::leaveEvent(e);
}
void CVBEditor::contentsWheelEvent( TQWheelEvent *e )
{
if (b_ignoreEvents)
{
e->ignore();
return;
}
if (b_passEventsToView)
p_itemView->contentsWheelEvent(e);
else
{
b_ignoreEvents = true;
TQCanvasView::wheelEvent(e);
b_ignoreEvents = false;
}
}
void CVBEditor::viewportResizeEvent( TQResizeEvent * e )
{
TQCanvasView::viewportResizeEvent(e);
p_itemView->p_itemDocument->requestEvent( ItemDocument::ItemDocumentEvent::ResizeCanvasToItems );
}
//END class CVBEditor
#include "itemview.moc"
|