summaryrefslogtreecommitdiffstats
path: root/kbarcode/tcanvasitem.cpp
blob: 8259be269b79ebbecf6f695e6f531f377a94f774 (plain)
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
/***************************************************************************
                         tcanvasitem.cpp  -  description
                             -------------------
    begin                : Do Sep 2 2004
    copyright            : (C) 2004 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
                                                                          
    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 "tcanvasitem.h"
#include "mycanvasitem.h"
#include "mycanvasview.h"
#include "imageitem.h"

#include <tqpainter.h>
#include <labelutils.h>

TCanvasItem::TCanvasItem( MyCanvasView* cv )
    : TQCanvasRectangle( cv->canvas() ),
      ReferenceCounted()
{
    m_view = cv;
    m_item = NULL;
    show();
}

TCanvasItem::~TCanvasItem()
{
    if( m_item )
        delete m_item;
}

void TCanvasItem::setZ( double z )
{
    TQCanvasRectangle::setZ( z );

    if( m_item )
        m_item->setZ( (int)z );
}

void TCanvasItem::setSize( int width, int height )
{
    TQCanvasRectangle::setSize( width, height );
    
    if( m_item )
    {
        m_item->setSize( width, height );
        
        if( m_item->rtti() == eRtti_Image )
        {
            ImageItem* img = static_cast<ImageItem*>(m_item);
            img->updateImage();
        }
        
        update();
    }
}

void TCanvasItem::setSizeMM( int w, int h )
{
    if( m_item )
    {
        m_item->setSizeMM( w, h );
        TQCanvasRectangle::setSize( m_item->boundingRect().width(), m_item->boundingRect().height() );
    }
}

void TCanvasItem::moveBy( double dx, double dy )
{
    TQCanvasRectangle::moveBy( dx, dy );
    
    if( m_item )
    {
        m_item->move( (int)x() - m_view->getTranslation().x(), (int)y() - m_view->getTranslation().y() );
        update();
    }
}

void TCanvasItem::moveByMM( int x, int y )
{
    moveMM( m_item->rectMM().x() + x, m_item->rectMM().y() + y );
}

void TCanvasItem::moveMM( int x, int y )
{
    if( m_item )
    {
	LabelUtils l;
	// the TQCanvasRectangle::move is done before the real move
        // as we get dancing TCanvasItems on the screen otherwise
        TQCanvasRectangle::move( l.mmToPixel( x / 1000.0, m_view, LabelUtils::DpiX ) + m_view->getTranslation().x(), 
				l.mmToPixel( y / 1000.0, m_view, LabelUtils::DpiY ) + m_view->getTranslation().y() );
        m_item->moveMM( x, y );
    }
}

void TCanvasItem::drawShape (TQPainter & painter) 
{   
    painter.save();
    
    if( m_item )
    {
        if( z() != (int)m_item->z() )
            TQCanvasRectangle::setZ( m_item->z() );
        
        painter.save();
        painter.setClipRect( boundingRect(), TQPainter::CoordPainter );
        painter.translate( m_view->getTranslation().x(), m_view->getTranslation().y() );
        m_item->draw( &painter );
        painter.restore();
    }
    
    // draw edges
    if( isSelected() ) {
        const TQPixmap* spot = SpotProvider::getInstance()->spot();
        painter.translate( x(), y() );

        // top left
        painter.drawPixmap( 0, 0, *spot );
        // bottom left
        painter.drawPixmap( 0, height()-SPOTSIZE, *spot );
        // top right
        painter.drawPixmap( width()-SPOTSIZE, 0, *spot );
        // bottom left
        painter.drawPixmap( width()-SPOTSIZE, height()-SPOTSIZE, *spot );

        if( (width() - 2 * SPOTSIZE ) / 2 > SPOTSIZE ) {
            // top middle
            painter.drawPixmap( (width()-SPOTSIZE)/2, 0, *spot );
            // bottom middle
            painter.drawPixmap( (width()-SPOTSIZE)/2, height()-SPOTSIZE, *spot );
        }

        if( (height() - 2 * SPOTSIZE ) / 2 > SPOTSIZE ) {
            // left middle
            painter.drawPixmap( 0, (height()-SPOTSIZE)/2, *spot );
            // right middle
            painter.drawPixmap( width() - SPOTSIZE, (height()-SPOTSIZE)/2, *spot );
        }
    }
    painter.restore();
}

void TCanvasItem::setItem (DocumentItem* item) 
{
    if( m_item )
        m_item->setCanvasItem( NULL );
        
    m_item = item;    
    
    if( m_item )
    {
        m_item->setCanvasItem( this );
        this->setZ( m_item->z() );

        TQCanvasRectangle::move( m_item->boundingRect().x() + m_view->getTranslation().x(), m_item->boundingRect().y() + m_view->getTranslation().y() );
        TQCanvasRectangle::setSize( m_item->boundingRect().width(), m_item->boundingRect().height() );
        update();
    }
}

DocumentItem* TCanvasItem::item () const
{
    return m_item;
}

void TCanvasItem::update()
{
    TQCanvasRectangle::update();
}

int TCanvasItem::rtti() const
{
    if( m_item )
        return m_item->rtti();
    else
        return 0;
}

MyCanvasView* TCanvasItem::canvasView() const
{
    return m_view;
}



void TCanvasItem::show()
{
    if (!isVisible())
    {
        this->addRef();
        ((TQCanvasItem*) this)->show();
    }
}

void TCanvasItem::hide()
{
    if (refCount()==1)
    {
        if (isVisible())
        {
            this->remRef();
        }
    }
    else
    {
        if (isVisible())
        {
            ((TQCanvasItem*) this)->hide(); 
            this->remRef();
        }
    }
}