summaryrefslogtreecommitdiffstats
path: root/digikam/libs/widgets/common/dcursortracker.cpp
blob: c2fe358d46598d034b2b2488ed33d0b9d48e53f9 (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2007-23-03
 * Description : a tool tip widget witch follow cursor movements 
 *               Tool tip content is displayed without delay.
 * 
 * Copyright (C) 2007 by Gilles Caulier  <caulier dot gilles at gmail dot com>
 *
 * 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, or (at your option)
 * any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * ============================================================ */

// TQt includes.

#include <tqevent.h>
#include <tqtooltip.h>

// Local includes.

#include "dcursortracker.h"

namespace Digikam 
{

DCursorTracker::DCursorTracker(const TQString& txt, TQWidget *tqparent)
              : TQLabel(txt, 0, "", WX11BypassWM) 
{
    tqparent->setMouseTracking(true);
    tqparent->installEventFilter(this);
    setEnable(true);
}

/**
 * Overload to make sure the widget size is correct
 */
void DCursorTracker::setText(const TQString& txt) 
{
    TQLabel::setText(txt);
    adjustSize();
}

void DCursorTracker::setEnable(bool b) 
{
    m_enable = b;
}

bool DCursorTracker::eventFilter(TQObject *object, TQEvent *e) 
{
    TQWidget *widget = TQT_TQWIDGET(object);

    switch (e->type()) 
    {
        case TQEvent::MouseMove: 
        {
            TQMouseEvent *event = TQT_TQMOUSEEVENT(e);
            if (m_enable && (TQT_TQRECT_OBJECT(widget->rect()).tqcontains(event->pos()) || 
                            (event->stateAfter() & Qt::LeftButton))) 
            {
                show();
                TQPoint p = widget->mapToGlobal(TQPoint(widget->width()/2, 0));
                move(p.x()-width()/2, p.y()-height());
            }
            else 
            {
                hide();
            }
            break;
        }

        case TQEvent::MouseButtonRelease: 
        {
            TQMouseEvent* event = TQT_TQMOUSEEVENT(e);
            if ( !TQT_TQRECT_OBJECT(widget->rect()).tqcontains(event->pos()) ) 
            {
                hide();
            }
            break;
        }

        default:
            break;
    }

    return false;
}


DTipTracker::DTipTracker(const TQString& txt, TQWidget *tqparent)
           : DCursorTracker(txt, tqparent) 
{
    setPalette(TQToolTip::palette());
    setFrameStyle(TQFrame::Plain | TQFrame::Box);
    setLineWidth(1);
    tqsetAlignment(AlignAuto | AlignTop);
}

} // namespace Digikam