summaryrefslogtreecommitdiffstats
path: root/kugar/kudesigner_lib/reportitem.cpp
blob: 22234c053bfa1eba5cc2e5d12d4452e53a6c5892 (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
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
/* This file is part of the KDE project
 Copyright (C) 2002-2004 Alexander Dymo <[email protected]>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
#include "reportitem.h"

#include <tdelocale.h>
#include <tdeglobalsettings.h>

#include <tqrect.h>
#include <tqpainter.h>
#include <tqregexp.h>

#include <koproperty/property.h>

#include "canvas.h"
#include "band.h"
#include "propertyserializer.h"

#include <kdebug.h>

namespace Kudesigner
{

TQRect ReportItem::topLeftResizableRect()
{
    return TQRect( ( int ) x(), ( int ) y(), HolderSize, HolderSize );
}

TQRect ReportItem::bottomLeftResizableRect()
{
    return TQRect( ( int ) x(), ( int ) ( y() + height() - HolderSize ), HolderSize, HolderSize );
}

TQRect ReportItem::topRightResizableRect()
{
    return TQRect( ( int ) ( x() + width() - HolderSize ), ( int ) y(), HolderSize, HolderSize );
}

TQRect ReportItem::bottomRightResizableRect()
{
    return TQRect( ( int ) ( x() + width() - HolderSize ), ( int ) ( y() + height() - HolderSize ), HolderSize, HolderSize );
}

TQRect ReportItem::topMiddleResizableRect()
{
    return TQRect( ( int ) ( x() + width() / 2 - HolderSize / 2. ), ( int ) y(), HolderSize, HolderSize );
}

TQRect ReportItem::bottomMiddleResizableRect()
{
    return TQRect( ( int ) ( x() + width() / 2 - HolderSize / 2. ), ( int ) ( y() + height() - HolderSize ), HolderSize, HolderSize );
}

TQRect ReportItem::leftMiddleResizableRect()
{
    return TQRect( ( int ) x(), ( int ) ( y() + height() / 2 - HolderSize / 2. ), HolderSize, HolderSize );
}

TQRect ReportItem::rightMiddleResizableRect()
{
    return TQRect( ( int ) ( x() + width() - HolderSize ), ( int ) ( y() + height() / 2 - HolderSize / 2. ), HolderSize, HolderSize );
}

void ReportItem::updateGeomProps()
{
    if ( !section() )
        return ;
    props[ "X" ].setValue( ( int ) ( x() - section() ->x() ) );
    props[ "Y" ].setValue( ( int ) ( y() - section() ->y() ) );
    props[ "Width" ].setValue( width() );
    props[ "Height" ].setValue( height() );
}

Band *ReportItem::section()
{
    return parentSection;
}

void ReportItem::setSection( Band *section )
{
    props[ "X" ].setValue( ( int ) ( x() - section->x() ) );
    props[ "Y" ].setValue( ( int ) ( y() - section->y() ) );
    parentSection = section;
}

void ReportItem::setSectionUndestructive( Band *section )
{
    parentSection = section;
}

TQString ReportItem::getXml()
{
    TQString result = "";

    int i = 1;
    for ( Set::Iterator it( props ); it.current(); ++it )
    {
        if ( !( i % 3 ) )
            result += "\n\t\t  ";
        result += " " + TQString(it.currentKey()) + "=" + "\"" + escape( PropertySerializer::toString( it.current() ) ) + "\"";
        i++;
    }

    return result;
}

int ReportItem::isInHolder( const TQPoint p )
{
    if ( topLeftResizableRect().contains( p ) )
        return ( ResizeTop | ResizeLeft );
    if ( bottomLeftResizableRect().contains( p ) )
        return ( ResizeBottom | ResizeLeft );
    if ( leftMiddleResizableRect().contains( p ) )
        return ( ResizeLeft );
    if ( bottomMiddleResizableRect().contains( p ) )
        return ( ResizeBottom );
    if ( topMiddleResizableRect().contains( p ) )
        return ( ResizeTop );
    if ( topRightResizableRect().contains( p ) )
        return ( ResizeTop | ResizeRight );
    if ( bottomRightResizableRect().contains( p ) )
        return ( ResizeBottom | ResizeRight );
    if ( rightMiddleResizableRect().contains( p ) )
        return ( ResizeRight );

    return ResizeNothing;
}

void ReportItem::drawHolders( TQPainter &painter )
{
    painter.setPen( TQColor( 0, 0, 0 ) );

    painter.setBrush( TDEGlobalSettings::highlightColor() );

    TQCanvasItemList list = collisions( false );
    TQCanvasItemList::iterator it = list.begin();
    for ( ; it != list.end(); ++it )
    {
        switch ( ( *it )->rtti() )
        {
        case Rtti_Label:
        case Rtti_Field:
        case Rtti_Special:
        case Rtti_Calculated:
        case Rtti_Line:
            {
            ReportItem *item = static_cast<ReportItem*>( *it );
            if ( section() != item->section() )
                continue;
            if ( intersects( item ) )
                painter.setBrush( TQt::red );
            break;
            }
        default:
            break;
        }
    }

    if ( props["Height"].value().toInt() > section()->props["Height"].value().toInt() )
        painter.setBrush( TQt::red );

    painter.drawRect( topLeftResizableRect() );
    painter.drawRect( topRightResizableRect() );
    painter.drawRect( bottomLeftResizableRect() );
    painter.drawRect( bottomRightResizableRect() );
    painter.drawRect( topMiddleResizableRect() );
    painter.drawRect( bottomMiddleResizableRect() );
    painter.drawRect( leftMiddleResizableRect() );
    painter.drawRect( rightMiddleResizableRect() );
}

bool ReportItem::intersects( ReportItem *item )
{
    TQRect r1;
    TQRect r2;

    if ( rtti() == Rtti_Line /*line*/ )
    {
        int x1 = props["X1"].value().toInt();
        int x2 = props["X2"].value().toInt();
        int y1 = props["Y1"].value().toInt();
        int y2 = props["Y2"].value().toInt();
        int width = props["Width"].value().toInt();
        //TODO I'm not sure of a good fix for this, but for now I'm assuming lines
        // in reports are either horizontal or vertical.
        if ( x1 == x2 )
            r1 = TQRect( x1, y1, x2 + width, y2 );
        else if ( y1 == y2 )
            r1 = TQRect( x1, y1, x2, y2 + width );
    }
    else
        r1 = TQRect( props["X"].value().toInt(), props["Y"].value().toInt(),
                    props["Width"].value().toInt(), props["Height"].value().toInt() );

    if ( item->rtti() == Rtti_Line /*line*/ )
    {
        int x1 = item->props["X1"].value().toInt();
        int x2 = item->props["X2"].value().toInt();
        int y1 = item->props["Y1"].value().toInt();
        int y2 = item->props["Y2"].value().toInt();
        int width = item->props["Width"].value().toInt();
        //TODO I'm not sure of a good fix for this, but for now I'm assuming lines
        // in reports are either horizontal or vertical.
        if ( x1 == x2 )
            r2 = TQRect( x1, y1, x2 + width, y2 );
        else if ( y1 == y2 )
            r2 = TQRect( x1, y1, x2, y2 + width );
    }
    else
        r2 = TQRect( item->props["X"].value().toInt(), item->props["Y"].value().toInt(),
                    item->props["Width"].value().toInt(), item->props["Height"].value().toInt() );

    bool intersects = r1.intersects( r2 );

    if ( intersects )
        kdDebug(30001) << rttiName( rtti() ) << " " << r1 << "\n"
                  << "...is intersected by..." << "\n"
                  << rttiName( item->rtti() ) << " " << r2
                  << endl;

    return intersects;
}

TQString ReportItem::escape( TQString string )
{
    string.replace( TQRegExp( "&" ), "&amp;" );
    string.replace( TQRegExp( "<" ), "&lt;" );
    string.replace( TQRegExp( ">" ), "&gt;" );
    return string;
}

}