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
|
/***************************************************************************
* Copyright (C) 2003-2005 by David Saxton *
* [email protected] *
* *
* 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 "canvasitemparts.h"
#include "cells.h"
#include "cnitem.h"
#include "icndocument.h"
#include <tqpainter.h>
//BEGIN Class GuiPart
GuiPart::GuiPart( CNItem *parent, const TQRect & r, TQCanvas * canvas )
: TQObject(parent),
TQCanvasRectangle( r, canvas ),
m_angleDegrees(0),
p_parent(parent),
b_pointsAdded(false),
m_originalRect(r)
{
connect( parent, TQT_SIGNAL(movedBy(double, double )), this, TQT_SLOT(slotMoveBy(double, double )) );
setZ( parent->z() + 0.5 );
}
GuiPart::~GuiPart()
{
hide();
}
void GuiPart::setAngleDegrees( int angleDegrees )
{
m_angleDegrees = angleDegrees;
posChanged();
if (canvas())
canvas()->setChanged( boundingRect() );
}
void GuiPart::setGuiPartSize( int width, int height )
{
updateConnectorPoints(false);
setSize( width, height );
posChanged();
}
void GuiPart::initPainter( TQPainter &p )
{
if ( (m_angleDegrees%180) == 0 )
return;
p.translate( int(x()+(width()/2)), int(y()+(height()/2)) );
p.rotate(m_angleDegrees);
p.translate( -int(x()+(width()/2)), -int(y()+(height()/2)) );
}
void GuiPart::deinitPainter( TQPainter &p )
{
if ( (m_angleDegrees%180) == 0 )
return;
p.translate( int(x()+(width()/2)), int(y()+(height()/2)) );
p.rotate(-m_angleDegrees);
p.translate( -int(x()+(width()/2)), -int(y()+(height()/2)) );
}
void GuiPart::slotMoveBy( double dx, double dy )
{
if ( dx==0 && dy==0 )
return;
moveBy( dx, dy );
posChanged();
}
void GuiPart::updateConnectorPoints( bool add )
{
ICNDocument *icnd = dynamic_cast<ICNDocument*>(p_parent->itemDocument());
if ( !icnd)
return;
Cells * cells = icnd->cells();
if (!cells)
return;
if ( add == b_pointsAdded )
return;
b_pointsAdded = add;
int mult = add ? 1 : -1;
int sx = int(x()/8);
int sy = int(y()/8);
int ex = int((x()+width())/8);
int ey = int((y()+height())/8);
for ( int x=sx; x<=ex; ++x )
{
for ( int y=sy; y<=ey; ++y )
{
if ( icnd->isValidCellReference( x, y ) )
(*cells)[x][y].CIpenalty += mult*ICNDocument::hs_item/2;
}
}
}
TQRect GuiPart::drawRect()
{
TQRect dr = rect();
if ( m_angleDegrees%180 != 0 )
{
TQWMatrix m;
m.translate( int(x()+(width()/2)), int(y()+(height()/2)) );
if ( (m_angleDegrees%180) != 0 )
m.rotate(-m_angleDegrees);
m.translate( -int(x()+(width()/2)), -int(y()+(height()/2)) );
dr = m.mapRect(dr);
}
return dr;
}
//END Class GuiPart
//BEGIN Class Text
Text::Text( const TQString &text, CNItem *parent, const TQRect & r, TQCanvas * canvas, int flags )
: GuiPart( parent, r, canvas )
{
m_flags = flags;
setText(text);
}
Text::~Text()
{
}
bool Text::setText( const TQString & text )
{
if ( m_text == text )
return false;
updateConnectorPoints(false);
m_text = text;
return true;
}
void Text::setFlags( int flags )
{
updateConnectorPoints( false );
m_flags = flags;
}
void Text::drawShape( TQPainter & p )
{
initPainter(p);
p.setFont( p_parent->font() );
p.drawText( drawRect(), m_flags, m_text );
deinitPainter(p);
}
TQRect Text::recommendedRect() const
{
return TQFontMetrics( p_parent->font() ).boundingRect( m_originalRect.x(), m_originalRect.y(), m_originalRect.width(), m_originalRect.height(), m_flags, m_text );
}
//END Class Text
//BEGIN Class Widget
Widget::Widget( const TQString & id, CNItem * parent, const TQRect & r, TQCanvas * canvas )
: GuiPart( parent, r, canvas )
{
m_id = id;
show();
}
Widget::~Widget()
{
}
int Widget::rtti() const
{
return ItemDocument::RTTI::Widget;
}
void Widget::setEnabled( bool enabled )
{
widget()->setEnabled(enabled);
}
void Widget::posChanged()
{
// Swap around the width / height if we are rotated at a non-half way around
if ( m_angleDegrees%90 != 0 )
widget()->setFixedSize( TQSize( height(), width() ) );
else
widget()->setFixedSize( size() );
widget()->move( int(x()), int(y()) );
}
void Widget::drawShape( TQPainter &p )
{
// initPainter(p);
p.drawPixmap( int(x()), int(y()), TQPixmap::grabWidget( widget() ) );
// deinitPainter(p);
}
//END Class Widget
//BEGIN Class ToolButton
ToolButton::ToolButton( TQWidget *parent )
: TQToolButton(parent)
{
m_angleDegrees = 0;
if ( TQFontInfo(m_font).pixelSize() > 11 ) // It has to be > 11, not > 12, as (I think) pixelSize() rounds off the actual size
m_font.setPixelSize(12);
}
void ToolButton::drawButtonLabel( TQPainter * p )
{
if ( m_angleDegrees % 180 == 0 || text().isEmpty() )
{
TQToolButton::drawButtonLabel(p);
return;
}
double dx = size().width()/2;
double dy = size().height()/2;
p->translate( dx, dy );
p->rotate( m_angleDegrees );
p->translate( -dx, -dy );
p->translate( -dy+dx, 0 );
int m = width() > height() ? width() : height();
p->setPen( TQt::black );
p->drawText( isDown()?1:0, isDown()?1:0, m, m, TQt::AlignVCenter | TQt::AlignHCenter, text() );
p->translate( dy-dx, 0 );
p->translate( dx, dy );
p->rotate( -m_angleDegrees );
p->translate( -dx, -dy );
}
//END Class ToolButton
//BEGIN Class Button
Button::Button( const TQString & id, CNItem * parent, bool isToggle, const TQRect & r, TQCanvas * canvas )
: Widget( id, parent, r, canvas )
{
b_isToggle = isToggle;
m_button = new ToolButton(0l);
m_button->setUsesTextLabel(false);
m_button->setToggleButton(b_isToggle);
connect( m_button, TQT_SIGNAL(pressed()), this, TQT_SLOT(slotStateChanged()) );
connect( m_button, TQT_SIGNAL(released()), this, TQT_SLOT(slotStateChanged()) );
posChanged();
}
Button::~Button()
{
delete m_button;
}
void Button::setToggle( bool toggle )
{
if ( b_isToggle == toggle )
return;
if (b_isToggle)
{
// We must first untoggle it, else it'll be forever stuck...
setState(false);
}
b_isToggle = toggle;
m_button->setToggleButton(b_isToggle);
}
void Button::posChanged()
{
Widget::posChanged();
m_button->setAngleDegrees(m_angleDegrees);
}
void Button::slotStateChanged()
{
parent()->buttonStateChanged( id(), m_button->isDown() || m_button->isOn() );
}
TQWidget* Button::widget() const
{
return m_button;
}
void Button::setPixmap( const TQPixmap &p )
{
m_button->setPixmap(p);
}
void Button::setState( bool state )
{
if ( this->state() == state )
return;
if ( isToggle() )
m_button->setOn(state);
else
m_button->setDown(state);
slotStateChanged();
}
bool Button::state() const
{
if ( isToggle() )
return m_button->state();
else
return m_button->isDown();
}
TQRect Button::recommendedRect() const
{
TQSize tqsizeHint = m_button->tqsizeHint();
if ( tqsizeHint.width() < m_originalRect.width() )
tqsizeHint.setWidth( m_originalRect.width() );
// Hmm...for now, lets just keep the recomended rect the same height as the original rect
tqsizeHint.setHeight( m_originalRect.height() );
int hdw = (tqsizeHint.width() - m_originalRect.width())/2;
int hdh = (tqsizeHint.height() - m_originalRect.height())/2;
return TQRect( m_originalRect.x()-hdw, m_originalRect.y()-hdh, tqsizeHint.width(), tqsizeHint.height() );
}
void Button::setText( const TQString &text )
{
if ( m_button->text() == text )
return;
updateConnectorPoints(false);
m_button->setUsesTextLabel(true);
m_button->setText(text);
m_button->setTextLabel(text);
canvas()->setChanged( rect() );
p_parent->updateAttachedPositioning();
}
void Button::mousePressEvent( TQMouseEvent *e )
{
if ( !m_button->isEnabled() )
return;
TQMouseEvent event( TQEvent::MouseButtonPress, e->pos()-TQPoint(int(x()),int(y())), e->button(), e->state() );
m_button->mousePressEvent(&event);
if (event.isAccepted())
e->accept();
canvas()->setChanged( rect() );
}
void Button::mouseReleaseEvent( TQMouseEvent *e )
{
TQMouseEvent event( TQEvent::MouseButtonRelease, e->pos()-TQPoint(int(x()),int(y())), e->button(), e->state() );
m_button->mouseReleaseEvent(&event);
if (event.isAccepted())
e->accept();
canvas()->setChanged( rect() );
}
void Button::enterEvent()
{
m_button->enterEvent();
// m_button->setFocus();
// bool hasFocus = m_button->hasFocus();
// m_button->setAutoRaise(true);
// m_button->setOn(true);
}
void Button::leaveEvent()
{
m_button->leaveEvent();
// m_button->clearFocus();
// bool hasFocus = m_button->hasFocus();
// m_button->setAutoRaise(false);
// m_button->setOn(false);
}
//END Class Button
//BEGIN Class SliderWidget
SliderWidget::SliderWidget( TQWidget *parent )
: TQSlider(parent)
{
setWFlags(WNoAutoErase|WRepaintNoErase);
}
//END Class SliderWidget
//BEGIN Class Slider
Slider::Slider( const TQString & id, CNItem * parent, const TQRect & r, TQCanvas * canvas )
: Widget( id, parent, r, canvas )
{
m_orientation = Qt::Vertical;
m_slider = new SliderWidget(0l);
m_slider->setPaletteBackgroundColor(TQt::white);
m_slider->setPaletteForegroundColor(TQt::white);
m_slider->setEraseColor(TQt::white);
m_slider->setBackgroundMode( TQt::NoBackground );
connect( m_slider, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotValueChanged(int)) );
posChanged();
}
Slider::~Slider()
{
delete m_slider;
}
TQWidget* Slider::widget() const
{
return m_slider;
}
int Slider::value() const
{
return m_slider->value();
}
void Slider::setValue( int value )
{
m_slider->setValue(value);
}
void Slider::mousePressEvent( TQMouseEvent *e )
{
TQMouseEvent event( TQEvent::MouseButtonPress, e->pos()-TQPoint(int(x()),int(y())), e->button(), e->state() );
m_slider->mousePressEvent(&event);
if (event.isAccepted())
e->accept();
canvas()->setChanged( rect() );
}
void Slider::mouseReleaseEvent( TQMouseEvent *e )
{
TQMouseEvent event( TQEvent::MouseButtonRelease, e->pos()-TQPoint(int(x()),int(y())), e->button(), e->state() );
m_slider->mouseReleaseEvent(&event);
if (event.isAccepted())
e->accept();
canvas()->setChanged( rect() );
}
void Slider::mouseDoubleClickEvent ( TQMouseEvent *e )
{
TQMouseEvent event( TQEvent::MouseButtonDblClick, e->pos()-TQPoint(int(x()),int(y())), e->button(), e->state() );
m_slider->mouseDoubleClickEvent(&event);
if (event.isAccepted())
e->accept();
canvas()->setChanged( rect() );
}
void Slider::mouseMoveEvent( TQMouseEvent *e )
{
TQMouseEvent event( TQEvent::MouseMove, e->pos()-TQPoint(int(x()),int(y())), e->button(), e->state() );
m_slider->mouseMoveEvent(&event);
if (event.isAccepted())
e->accept();
}
void Slider::wheelEvent( TQWheelEvent *e )
{
TQWheelEvent event( e->pos()-TQPoint(int(x()),int(y())), e->delta(), e->state(), e->orientation() );
m_slider->wheelEvent(&event);
if (event.isAccepted())
e->accept();
canvas()->setChanged( rect() );
}
void Slider::enterEvent()
{
m_slider->enterEvent();
}
void Slider::leaveEvent()
{
m_slider->leaveEvent();
}
void Slider::slotValueChanged( int value )
{
parent()->itemDocument()->setModified(true);
parent()->sliderValueChanged(id(),value);
}
void Slider::setOrientation( Qt::Orientation o )
{
m_orientation = o;
posChanged();
}
void Slider::posChanged()
{
Widget::posChanged();
if ( m_orientation == Qt::Vertical )
m_slider->setOrientation( (m_angleDegrees%180 == 0) ? Qt::Vertical : Qt::Horizontal );
else
m_slider->setOrientation( (m_angleDegrees%180 == 0) ? Qt::Horizontal : Qt::Vertical );
}
//END Class Slider
#include "canvasitemparts.moc"
|