summaryrefslogtreecommitdiffstats
path: root/src/libs/widgets/common/dcursortracker.cpp
blob: bb9490cbb3d2b7ffd860ac94d189e0f90030ab27 (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 *parent)
              : TQLabel(txt, 0, "", WX11BypassWM) 
{
    parent->setMouseTracking(true);
    parent->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 = static_cast<TQWidget*>(object);

    switch (e->type()) 
    {
        case TQEvent::MouseMove: 
        {
            TQMouseEvent *event = static_cast<TQMouseEvent*>(e);
            if (m_enable && (widget->rect().contains(event->pos()) ||
                            (event->stateAfter() & TQt::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 = static_cast<TQMouseEvent*>(e);
            if ( !widget->rect().contains(event->pos()) )
            {
                hide();
            }
            break;
        }

        default:
            break;
    }

    return false;
}


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

} // namespace Digikam