~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kicker/libkicker/kpanelapplet.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright (c) 2000 Matthias Elter
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
of this software and associated documentation files (the "Software"), to deal
 
7
in the Software without restriction, including without limitation the rights
 
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
copies of the Software, and to permit persons to whom the Software is
 
10
furnished to do so, subject to the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
18
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
21
 
 
22
******************************************************************/
 
23
 
 
24
#include "kpanelapplet.h"
 
25
#include "kpanelapplet.moc"
 
26
#include <ksharedconfig.h>
 
27
#include <kglobal.h>
 
28
#include <QResizeEvent>
 
29
 
 
30
class KPanelApplet::Private
 
31
{
 
32
public:
 
33
  Private()
 
34
    : position( Plasma::Bottom ),
 
35
      alignment( Plasma::LeftTop ),
 
36
      customMenu(0),
 
37
      hasFocus(false)
 
38
      {}
 
39
 
 
40
  Plasma::Type type;
 
41
  Plasma::Position position;
 
42
  Plasma::Alignment alignment;
 
43
  int actions;
 
44
 
 
45
  const QMenu* customMenu;
 
46
  KSharedConfig::Ptr sharedConfig;
 
47
  QList<QObject*> watchedForFocus;
 
48
  bool hasFocus;
 
49
};
 
50
 
 
51
KPanelApplet::KPanelApplet(const QString& configFile, Plasma::Type type,
 
52
                           int actions, QWidget *parent, Qt::WFlags f)
 
53
  : QFrame(parent, f),
 
54
    d(new Private())
 
55
{
 
56
  d->type = type;
 
57
  d->actions = actions;
 
58
 
 
59
  setFrameStyle(NoFrame);
 
60
  QPalette pal(palette());
 
61
  if(pal.active().mid() != pal.inactive().mid()){
 
62
    pal.setInactive(pal.active());
 
63
    setPalette(pal);
 
64
  }
 
65
  setBackgroundOrigin( AncestorOrigin );
 
66
 
 
67
  d->sharedConfig = KSharedConfig::openConfig(configFile);
 
68
}
 
69
 
 
70
KPanelApplet::~KPanelApplet()
 
71
{
 
72
  d->watchedForFocus.clear();
 
73
  needsFocus(false);
 
74
  delete d;
 
75
}
 
76
 
 
77
KConfig* KPanelApplet::config() const
 
78
{
 
79
  return d->sharedConfig.data();
 
80
}
 
81
 
 
82
Plasma::Type KPanelApplet::type() const
 
83
{
 
84
  return d->type;
 
85
}
 
86
 
 
87
int KPanelApplet::actions() const
 
88
{
 
89
  return d->actions;
 
90
}
 
91
 
 
92
void KPanelApplet::setPosition( Plasma::Position p )
 
93
{
 
94
  if( d->position == p ) return;
 
95
  d->position = p;
 
96
  positionChange( p );
 
97
}
 
98
 
 
99
void KPanelApplet::setAlignment( Plasma::Alignment a )
 
100
{
 
101
  if( d->alignment == a ) return;
 
102
  d->alignment = a;
 
103
  alignmentChange( a );
 
104
}
 
105
 
 
106
// FIXME: Remove implementation for KDE 4
 
107
void KPanelApplet::positionChange( Plasma::Position )
 
108
{
 
109
  orientationChange( orientation() );
 
110
  QResizeEvent e( size(), size() );
 
111
  resizeEvent( &e );
 
112
  popupDirectionChange( popupDirection() );
 
113
}
 
114
 
 
115
// FIXME: Remove for KDE 4
 
116
Plasma::Position KPanelApplet::popupDirection()
 
117
{
 
118
    switch( d->position ) {
 
119
        case Plasma::Top:
 
120
            return Plasma::Down;
 
121
        case Plasma::Right:
 
122
            return Plasma::Left;
 
123
        case Plasma::Left:
 
124
            return Plasma::Right;
 
125
        case Plasma::Bottom:
 
126
        default:
 
127
          return Plasma::Up;
 
128
    }
 
129
}
 
130
 
 
131
Qt::Orientation KPanelApplet::orientation() const
 
132
{
 
133
    if( d->position == Plasma::Top || d->position == Plasma::Bottom )
 
134
    {
 
135
        return Qt::Horizontal;
 
136
    }
 
137
    else
 
138
    {
 
139
        return Qt::Vertical;
 
140
    }
 
141
}
 
142
 
 
143
Plasma::Position KPanelApplet::position() const
 
144
{
 
145
    return d->position;
 
146
}
 
147
 
 
148
Plasma::Alignment KPanelApplet::alignment() const
 
149
{
 
150
    return d->alignment;
 
151
}
 
152
 
 
153
void KPanelApplet::action( Plasma::Action a )
 
154
{
 
155
    if ( (a & Plasma::About) )
 
156
    {
 
157
        about();
 
158
    }
 
159
    if ( (a & Plasma::Help) )
 
160
    {
 
161
        help();
 
162
    }
 
163
    if ( (a & Plasma::Preferences) )
 
164
    {
 
165
        preferences();
 
166
    }
 
167
    if ( (a & Plasma::ReportBug) )
 
168
    {
 
169
        reportBug();
 
170
    }
 
171
}
 
172
 
 
173
const QMenu* KPanelApplet::customMenu() const
 
174
{
 
175
    return d->customMenu;
 
176
}
 
177
 
 
178
void KPanelApplet::setCustomMenu(const QMenu* menu)
 
179
{
 
180
    d->customMenu = menu;
 
181
}
 
182
 
 
183
void KPanelApplet::watchForFocus(QWidget* widget, bool watch)
 
184
{
 
185
    if (!widget)
 
186
    {
 
187
        return;
 
188
    }
 
189
 
 
190
    if (watch)
 
191
    {
 
192
        if (!d->watchedForFocus.contains(widget))
 
193
        {
 
194
            d->watchedForFocus.append(widget);
 
195
            widget->installEventFilter(this);
 
196
        }
 
197
    }
 
198
    else if (!d->watchedForFocus.contains(widget))
 
199
    {
 
200
        d->watchedForFocus.removeAll(widget);
 
201
        widget->removeEventFilter(this);
 
202
    }
 
203
}
 
204
 
 
205
void KPanelApplet::needsFocus(bool focus)
 
206
{
 
207
    if (focus == d->hasFocus)
 
208
    {
 
209
        return;
 
210
    }
 
211
 
 
212
    d->hasFocus = focus;
 
213
    emit requestFocus(focus);
 
214
}
 
215
 
 
216
bool KPanelApplet::eventFilter(QObject *o, QEvent * e)
 
217
{
 
218
    if (!d->watchedForFocus.contains(o))
 
219
    {
 
220
        if (e->type() == QEvent::MouseButtonRelease ||
 
221
            e->type() == QEvent::FocusIn)
 
222
        {
 
223
            needsFocus(true);
 
224
        }
 
225
        else if (e->type() == QEvent::FocusOut)
 
226
        {
 
227
            needsFocus(false);
 
228
        }
 
229
    }
 
230
 
 
231
    return QFrame::eventFilter(o, e);
 
232
}
 
233
 
 
234
KSharedConfig::Ptr KPanelApplet::sharedConfig() const
 
235
{
 
236
    return d->sharedConfig;
 
237
}