KDE3Support
k3panelapplet.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "k3panelapplet.h"
00025 #include "k3panelapplet.moc"
00026 #include <ksharedconfig.h>
00027 #include <kglobal.h>
00028 #include <QResizeEvent>
00029
00030 class K3PanelApplet::Private
00031 {
00032 public:
00033 Private()
00034 : position( K3PanelApplet::Bottom ),
00035 alignment( K3PanelApplet::LeftTop ),
00036 customMenu(0),
00037 hasFocus(false)
00038 {}
00039
00040 K3PanelApplet::Type type;
00041 K3PanelApplet::Position position;
00042 K3PanelApplet::Alignment alignment;
00043 int actions;
00044
00045 const QMenu* customMenu;
00046 KSharedConfig::Ptr sharedConfig;
00047 QList<QObject*> watchedForFocus;
00048 bool hasFocus;
00049 };
00050
00051 K3PanelApplet::K3PanelApplet(const QString& configFile, K3PanelApplet::Type type,
00052 int actions, QWidget *parent, Qt::WFlags f)
00053 : QFrame(parent, f),
00054 d(new Private())
00055 {
00056 d->type = type;
00057 d->actions = actions;
00058
00059 setFrameStyle(NoFrame);
00060 QPalette pal(palette());
00061 if(pal.active().mid() != pal.inactive().mid()){
00062 pal.setInactive(pal.active());
00063 setPalette(pal);
00064 }
00065 setBackgroundOrigin( AncestorOrigin );
00066
00067 d->sharedConfig = KSharedConfig::openConfig(configFile);
00068 }
00069
00070 K3PanelApplet::~K3PanelApplet()
00071 {
00072 d->watchedForFocus.clear();
00073 needsFocus(false);
00074 delete d;
00075 }
00076
00077 KConfig* K3PanelApplet::config() const
00078 {
00079 return d->sharedConfig.data();
00080 }
00081
00082 K3PanelApplet::Type K3PanelApplet::type() const
00083 {
00084 return d->type;
00085 }
00086
00087 int K3PanelApplet::actions() const
00088 {
00089 return d->actions;
00090 }
00091
00092 void K3PanelApplet::setPosition( K3PanelApplet::Position p )
00093 {
00094 if( d->position == p ) return;
00095 d->position = p;
00096 positionChange( p );
00097 }
00098
00099 void K3PanelApplet::setAlignment( K3PanelApplet::Alignment a )
00100 {
00101 if( d->alignment == a ) return;
00102 d->alignment = a;
00103 alignmentChange( a );
00104 }
00105
00106
00107 void K3PanelApplet::positionChange( K3PanelApplet::Position )
00108 {
00109 orientationChange( orientation() );
00110 QResizeEvent e( size(), size() );
00111 resizeEvent( &e );
00112 popupDirectionChange( popupDirection() );
00113 }
00114
00115
00116 K3PanelApplet::Position K3PanelApplet::popupDirection()
00117 {
00118 switch( d->position ) {
00119 case K3PanelApplet::Top:
00120 return K3PanelApplet::Down;
00121 case K3PanelApplet::Right:
00122 return K3PanelApplet::Left;
00123 case K3PanelApplet::Left:
00124 return K3PanelApplet::Right;
00125 case K3PanelApplet::Bottom:
00126 default:
00127 return K3PanelApplet::Up;
00128 }
00129 }
00130
00131 Qt::Orientation K3PanelApplet::orientation() const
00132 {
00133 if( d->position == K3PanelApplet::Top || d->position == K3PanelApplet::Bottom )
00134 {
00135 return Qt::Horizontal;
00136 }
00137 else
00138 {
00139 return Qt::Vertical;
00140 }
00141 }
00142
00143 K3PanelApplet::Position K3PanelApplet::position() const
00144 {
00145 return d->position;
00146 }
00147
00148 K3PanelApplet::Alignment K3PanelApplet::alignment() const
00149 {
00150 return d->alignment;
00151 }
00152
00153 void K3PanelApplet::action( K3PanelApplet::Action a )
00154 {
00155 if ( (a & K3PanelApplet::About) )
00156 {
00157 about();
00158 }
00159 if ( (a & K3PanelApplet::Help) )
00160 {
00161 help();
00162 }
00163 if ( (a & K3PanelApplet::Preferences) )
00164 {
00165 preferences();
00166 }
00167 if ( (a & K3PanelApplet::ReportBug) )
00168 {
00169 reportBug();
00170 }
00171 }
00172
00173 const QMenu* K3PanelApplet::customMenu() const
00174 {
00175 return d->customMenu;
00176 }
00177
00178 void K3PanelApplet::setCustomMenu(const QMenu* menu)
00179 {
00180 d->customMenu = menu;
00181 }
00182
00183 void K3PanelApplet::watchForFocus(QWidget* widget, bool watch)
00184 {
00185 if (!widget)
00186 {
00187 return;
00188 }
00189
00190 if (watch)
00191 {
00192 if (!d->watchedForFocus.contains(widget))
00193 {
00194 d->watchedForFocus.append(widget);
00195 widget->installEventFilter(this);
00196 }
00197 }
00198 else if (!d->watchedForFocus.contains(widget))
00199 {
00200 d->watchedForFocus.removeAll(widget);
00201 widget->removeEventFilter(this);
00202 }
00203 }
00204
00205 void K3PanelApplet::needsFocus(bool focus)
00206 {
00207 if (focus == d->hasFocus)
00208 {
00209 return;
00210 }
00211
00212 d->hasFocus = focus;
00213 emit requestFocus(focus);
00214 }
00215
00216 bool K3PanelApplet::eventFilter(QObject *o, QEvent * e)
00217 {
00218 if (!d->watchedForFocus.contains(o))
00219 {
00220 if (e->type() == QEvent::MouseButtonRelease ||
00221 e->type() == QEvent::FocusIn)
00222 {
00223 needsFocus(true);
00224 }
00225 else if (e->type() == QEvent::FocusOut)
00226 {
00227 needsFocus(false);
00228 }
00229 }
00230
00231 return QFrame::eventFilter(o, e);
00232 }
00233
00234 KSharedConfig::Ptr K3PanelApplet::sharedConfig() const
00235 {
00236 return d->sharedConfig;
00237 }