summaryrefslogtreecommitdiffstats
path: root/src/part/radialMap/segmentTip.cpp
blob: 3c8f3f4a8a345766f3bac7971992c61ef5da4a8e (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
//Author:    Max Howell <[email protected]>, (C) 2003-4
//Copyright: See COPYING file that comes with this distribution

#include "fileTree.h"
#include "segmentTip.h"

#include <cstdlib>
#include <tdeapplication.h>    //installing eventFilters
#include <kglobal.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kpixmapeffect.h>
#include <tqpainter.h>
#include <tqtooltip.h>        //for its palette



namespace RadialMap {


bool isBackingStoreActive()
{
    // # xdpyinfo | grep backing
    // options:    backing-store YES, save-unders YES

    char buffer[4096];
    FILE *xdpyinfo = popen( "xdpyinfo", "r" );
    int const N = fread( (void*)buffer, sizeof(char), 4096, xdpyinfo );
    buffer[ N ] = '\0';
    pclose( xdpyinfo );

    return TQString(TQString::fromLocal8Bit( buffer )).contains( "backing-store YES" );
}


SegmentTip::SegmentTip( uint h )
        : TQWidget( 0, 0, WNoAutoErase | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WStyle_StaysOnTop | WX11BypassWM )
        , m_cursorHeight( -h )
        , m_backing_store( isBackingStoreActive() )
{
    setBackgroundMode( TQt::NoBackground );
}

void
SegmentTip::moveTo( TQPoint p, const TQWidget &canvas, bool placeAbove )
{
    //**** this function is very slow and seems to be visibly influenced by operations like mapFromGlobal() (who knows why!)
    //  ** so any improvements are much desired

    //TODO uints could improve the class
    p.rx() -= rect().center().x();
    p.ry() -= (placeAbove ? 8 + height() : m_cursorHeight - 8);

    const TQRect screen = TDEGlobalSettings::desktopGeometry( parentWidget() );

    const int x  = p.x();
    const int y  = p.y();
    const int x2 = x + width();
    const int y2 = y + height(); //how's it ever gunna get below screen height?! (well you never know I spose)
    const int sw = screen.width();
    const int sh = screen.height();

    if( x  < 0  ) p.setX( 0 );
    if( y  < 0  ) p.setY( 0 );
    if( x2 > sw ) p.rx() -= x2 - sw;
    if( y2 > sh ) p.ry() -= y2 - sh;


    //I'm using this TQPoint to determine where to offset the bitBlt in m_pixmap
    TQPoint offset = canvas.mapToGlobal( TQPoint() ) - p;
    if( offset.x() < 0 ) offset.setX( 0 );
    if( offset.y() < 0 ) offset.setY( 0 );


    const TQRect alphaMaskRect( canvas.mapFromGlobal( p ), size() );
    const TQRect intersection( alphaMaskRect.intersect( canvas.rect() ) );

    m_pixmap.resize( size() ); //move to updateTip once you are sure it can never be null
    bitBlt( &m_pixmap, offset, &canvas, intersection, TQt::CopyROP );

    TQColor const c = TQToolTip::palette().color( TQPalette::Active, TQColorGroup::Background );
    if (!m_backing_store)
        m_pixmap.fill( c );

    TQPainter paint( &m_pixmap );
    paint.setPen( TQt::black );
    paint.setBrush( TQt::NoBrush );
    paint.drawRect( rect() );
    paint.end();

    if (m_backing_store)
        m_pixmap = KPixmapEffect::fade( m_pixmap, 0.6, c );

    paint.begin( &m_pixmap );
    paint.drawText( rect(), AlignCenter, m_text );
    paint.end();

    p += screen.topLeft(); //for Xinerama users

    move( x, y );
    show();
    update();
}

void
SegmentTip::updateTip( const File* const file, const Directory* const root )
{
    const TQString s1  = file->fullPath( root );
    TQString s2        = file->humanReadableSize();
    TDELocale *loc      = TDEGlobal::locale();
    const uint MARGIN = 3;
    const uint pc     = 100 * file->size() / root->size();
    uint maxw         = 0;
    uint h            = fontMetrics().height()*2 + 2*MARGIN;

    if( pc > 0 ) s2 += TQString( " (%1%)" ).arg( loc->formatNumber( pc, 0 ) );

    m_text  = s1;
    m_text += '\n';
    m_text += s2;

    if( file->isDirectory() )
    {
        double files  = static_cast<const Directory*>(file)->children();
        const uint pc = uint((100 * files) / (double)root->children());
        TQString s3    = i18n( "Files: %1" ).arg( loc->formatNumber( files, 0 ) );

        if( pc > 0 ) s3 += TQString( " (%1%)" ).arg( loc->formatNumber( pc, 0 ) );

        maxw    = fontMetrics().width( s3 );
        h      += fontMetrics().height();
        m_text += '\n';
        m_text += s3;
    }

    uint
    w = fontMetrics().width( s1 ); if( w > maxw ) maxw = w;
    w = fontMetrics().width( s2 ); if( w > maxw ) maxw = w;

    resize( maxw + 2 * MARGIN, h );
}

bool
SegmentTip::event( TQEvent *e )
{
    switch( e->type() )
    {
    case TQEvent::Show:
        kapp->installEventFilter( this );
        break;
    case TQEvent::Hide:
        kapp->removeEventFilter( this );
        break;
    case TQEvent::Paint:
    {
        //TQPainter( this ).drawPixmap( 0, 0, m_pixmap );
        bitBlt( this, 0, 0, &m_pixmap );
        return true;
    }
    default:
        ;
    }

    return false/*TQWidget::event( e )*/;
}

bool
SegmentTip::eventFilter( TQObject*, TQEvent *e )
{
    switch ( e->type() )
    {
    case TQEvent::Leave:
//     case TQEvent::MouseButtonPress:
//     case TQEvent::MouseButtonRelease:
    case TQEvent::KeyPress:
    case TQEvent::KeyRelease:
    case TQEvent::FocusIn:
    case TQEvent::FocusOut:
    case TQEvent::Wheel:
        hide(); //FALL THROUGH
    default:
        return false; //allow this event to passed to target
    }
}

} //namespace RadialMap