00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "applettitlebar.h"
00021 #include "newspaper.h"
00022
00023 #include <QGraphicsGridLayout>
00024 #include <QGraphicsSceneMouseEvent>
00025 #include <QGraphicsSceneResizeEvent>
00026 #include <QLabel>
00027 #include <QPainter>
00028 #include <QTimer>
00029
00030
00031 #include <KIconLoader>
00032
00033 #include <Plasma/Animator>
00034 #include <Plasma/IconWidget>
00035 #include <Plasma/Label>
00036 #include <Plasma/Separator>
00037 #include <Plasma/Svg>
00038 #include <Plasma/Theme>
00039
00040
00041 AppletTitleBar::AppletTitleBar(Plasma::Applet *applet)
00042 : QGraphicsWidget(applet),
00043 m_applet(applet),
00044 m_pressedButton(NoButton),
00045 m_maximizeButtonAnimationId(0),
00046 m_configureButtonAnimationId(0),
00047 m_closeButtonAnimationId(0),
00048 m_separator(0),
00049 m_background(0),
00050 m_savedAppletTopMargin(0),
00051 m_underMouse(false),
00052 m_showButtons(false)
00053 {
00054 m_maximizeButtonRect = m_configureButtonRect = m_closeButtonRect = QRect(0, 0, KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium);
00055
00056 m_icons = new Plasma::Svg(this);
00057 m_icons->setImagePath("widgets/configuration-icons");
00058 m_icons->setContainsMultipleImages(true);
00059
00060
00061 if (applet->backgroundHints() & Plasma::Applet::StandardBackground) {
00062 m_separator = new Plasma::Svg(this);
00063 m_separator->setImagePath("widgets/line");
00064 m_separator->setContainsMultipleImages(true);
00065 } else {
00066 m_background = new Plasma::FrameSvg(this);
00067 m_background->setImagePath("widgets/background");
00068 }
00069
00070 applet->installEventFilter(this);
00071 syncMargins();
00072 syncSize();
00073
00074 if (applet->containment()) {
00075 connect(applet->containment(), SIGNAL(appletRemoved(Plasma::Applet *)), this, SLOT(appletRemoved(Plasma::Applet *)));
00076 }
00077 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
00078
00079 connect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)), this, SLOT(animationFinished(int)));
00080 }
00081
00082 AppletTitleBar::~AppletTitleBar()
00083 {
00084 }
00085
00086
00087 void AppletTitleBar::syncMargins()
00088 {
00089 const int extraMargin = 2;
00090 syncIconRects();
00091
00092
00093 if (m_background) {
00094 qreal left, top, right, bottom;
00095
00096 m_background->getMargins(left, top, right, bottom);
00097 setContentsMargins(left, top, right, bottom);
00098 setMinimumHeight(m_maximizeButtonRect.height() + extraMargin + top + bottom);
00099 setMaximumHeight(m_maximizeButtonRect.height() + extraMargin + top + bottom);
00100 } else {
00101 setMinimumHeight(m_maximizeButtonRect.height() + extraMargin);
00102 setMaximumHeight(m_maximizeButtonRect.height() + extraMargin);
00103 }
00104
00105 qreal left, right, bottom;
00106 m_applet->getContentsMargins(&left, &m_savedAppletTopMargin, &right, &bottom);
00107 m_applet->setContentsMargins(left, m_savedAppletTopMargin + size().height() + extraMargin, right, bottom);
00108 }
00109
00110 void AppletTitleBar::syncSize()
00111 {
00112 setGeometry(QRectF(QPointF(m_applet->contentsRect().left(), m_savedAppletTopMargin),
00113 QSizeF(m_applet->contentsRect().size().width(),
00114 size().height())));
00115 }
00116
00117 void AppletTitleBar::syncIconRects()
00118 {
00119 m_maximizeButtonRect.moveTopLeft(contentsRect().topLeft());
00120 m_configureButtonRect.moveTopLeft(contentsRect().topLeft());
00121
00122 if (m_applet->hasValidAssociatedApplication()) {
00123 m_configureButtonRect.moveLeft(contentsRect().left() + m_maximizeButtonRect.width() + 2);
00124 }
00125
00126 m_closeButtonRect.moveTopRight(contentsRect().topRight());
00127 }
00128
00129 bool AppletTitleBar::eventFilter(QObject *watched, QEvent *event)
00130 {
00131 Q_UNUSED(watched)
00132 if (event->type() == QEvent::GraphicsSceneResize) {
00133 syncSize();
00134 } else if (event->type() == QEvent::GraphicsSceneHoverEnter) {
00135 m_underMouse = true;
00136 m_showButtons = true;
00137 syncIconRects();
00138
00139 if (m_maximizeButtonAnimationId) {
00140 Plasma::Animator::self()->stopElementAnimation(m_maximizeButtonAnimationId);
00141 }
00142 m_maximizeButtonAnimationId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
00143 Plasma::Animator::self()->setInitialPixmap(m_maximizeButtonAnimationId, m_icons->pixmap("maximize"));
00144
00145 if (m_configureButtonAnimationId) {
00146 Plasma::Animator::self()->stopElementAnimation(m_maximizeButtonAnimationId);
00147 }
00148 m_configureButtonAnimationId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
00149 Plasma::Animator::self()->setInitialPixmap(m_configureButtonAnimationId, m_icons->pixmap("configure"));
00150
00151 if (m_closeButtonAnimationId) {
00152 Plasma::Animator::self()->stopElementAnimation(m_maximizeButtonAnimationId);
00153 }
00154 m_closeButtonAnimationId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
00155 Plasma::Animator::self()->setInitialPixmap(m_closeButtonAnimationId, m_icons->pixmap("close"));
00156
00157 update();
00158 } else if (event->type() == QEvent::GraphicsSceneHoverLeave) {
00159 m_underMouse = false;
00160 if (m_maximizeButtonAnimationId) {
00161 Plasma::Animator::self()->stopElementAnimation(m_maximizeButtonAnimationId);
00162 }
00163 m_maximizeButtonAnimationId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
00164 Plasma::Animator::self()->setInitialPixmap(m_maximizeButtonAnimationId, m_icons->pixmap("maximize"));
00165
00166 if (m_configureButtonAnimationId) {
00167 Plasma::Animator::self()->stopElementAnimation(m_maximizeButtonAnimationId);
00168 }
00169 m_configureButtonAnimationId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
00170 Plasma::Animator::self()->setInitialPixmap(m_configureButtonAnimationId, m_icons->pixmap("configure"));
00171
00172 if (m_closeButtonAnimationId) {
00173 Plasma::Animator::self()->stopElementAnimation(m_maximizeButtonAnimationId);
00174 }
00175 m_closeButtonAnimationId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
00176 Plasma::Animator::self()->setInitialPixmap(m_closeButtonAnimationId, m_icons->pixmap("close"));
00177
00178 update();
00179 }
00180
00181 return false;
00182 }
00183
00184 void AppletTitleBar::mousePressEvent(QGraphicsSceneMouseEvent *event)
00185 {
00186 if (m_applet->hasValidAssociatedApplication() &&
00187 m_maximizeButtonRect.contains(event->pos())) {
00188 m_pressedButton = MaximizeButton;
00189 m_maximizeButtonRect.translate(1, 1);
00190 update(m_maximizeButtonRect);
00191 } else if (m_configureButtonRect.contains(event->pos())) {
00192 m_configureButtonRect.translate(1, 1);
00193 m_pressedButton = ConfigureButton;
00194 update(m_configureButtonRect);
00195 } else if (m_closeButtonRect.contains(event->pos())) {
00196 m_closeButtonRect.translate(1, 1);
00197 m_pressedButton = CloseButton;
00198 update(m_closeButtonRect);
00199 } else {
00200 event->ignore();
00201 }
00202 }
00203
00204 void AppletTitleBar::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
00205 {
00206 event->ignore();
00207 }
00208
00209 void AppletTitleBar::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
00210 {
00211 if (m_pressedButton == MaximizeButton && m_maximizeButtonRect.contains(event->pos())) {
00212 if (m_applet->hasValidAssociatedApplication()) {
00213 m_applet->runAssociatedApplication();
00214 }
00215 } else if (m_pressedButton == ConfigureButton && m_configureButtonRect.contains(event->pos())) {
00216 if (m_applet->hasConfigurationInterface()) {
00217 m_applet->showConfigurationInterface();
00218 }
00219 } else if (m_pressedButton == CloseButton && m_closeButtonRect.contains(event->pos())) {
00220 if (m_applet->immutability() == Plasma::Mutable) {
00221 m_applet->destroy();
00222 }
00223 } else {
00224 event->ignore();
00225 }
00226
00227 switch (m_pressedButton) {
00228 case MaximizeButton:
00229 m_maximizeButtonRect.translate(-1, -1);
00230 update(m_maximizeButtonRect);
00231 break;
00232 case ConfigureButton:
00233 m_configureButtonRect.translate(-1, -1);
00234 update(m_configureButtonRect);
00235 break;
00236 case CloseButton:
00237 m_closeButtonRect.translate(-1, -1);
00238 update(m_closeButtonRect);
00239 break;
00240 default:
00241 break;
00242 }
00243
00244 m_pressedButton = NoButton;
00245 }
00246
00247 void AppletTitleBar::resizeEvent(QGraphicsSceneResizeEvent *event)
00248 {
00249 if (m_background) {
00250 m_background->resizeFrame(event->newSize());
00251 }
00252
00253 syncIconRects();
00254 }
00255
00256 void AppletTitleBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00257 {
00258 Q_UNUSED(option)
00259 Q_UNUSED(widget)
00260
00261 if (m_background) {
00262 m_background->paintFrame(painter);
00263 }
00264
00265 if (m_showButtons) {
00266 if (m_applet->hasValidAssociatedApplication()) {
00267 if (m_maximizeButtonAnimationId) {
00268 QPixmap animPixmap = Plasma::Animator::self()->currentPixmap(m_maximizeButtonAnimationId);
00269 painter->drawPixmap(m_maximizeButtonRect, animPixmap, animPixmap.rect());
00270 } else {
00271 m_icons->paint(painter, m_maximizeButtonRect, "maximize");
00272 }
00273 }
00274 if (m_applet->hasConfigurationInterface()) {
00275 if (m_maximizeButtonAnimationId) {
00276 QPixmap animPixmap = Plasma::Animator::self()->currentPixmap(m_configureButtonAnimationId);
00277 painter->drawPixmap(m_configureButtonRect, animPixmap, animPixmap.rect());
00278 } else {
00279 m_icons->paint(painter, m_configureButtonRect, "configure");
00280 }
00281 }
00282 if (m_applet->immutability() == Plasma::Mutable) {
00283 if (m_maximizeButtonAnimationId) {
00284 QPixmap animPixmap = Plasma::Animator::self()->currentPixmap(m_closeButtonAnimationId);
00285 painter->drawPixmap(m_closeButtonRect, animPixmap, animPixmap.rect());
00286 } else {
00287 m_icons->paint(painter, m_closeButtonRect, "close");
00288 }
00289 }
00290 }
00291
00292 painter->save();
00293 painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
00294 painter->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
00295 painter->drawText(contentsRect(), Qt::AlignCenter, m_applet->name());
00296 painter->restore();
00297
00298 if (m_separator) {
00299 QRectF lineRect = contentsRect();
00300 lineRect.setTop(lineRect.height() - m_separator->elementSize("horizontal-line").height());
00301 m_separator->paint(painter, lineRect, "horizontal-line");
00302 }
00303 }
00304
00305 void AppletTitleBar::appletRemoved(Plasma::Applet *applet)
00306 {
00307 if (applet == m_applet) {
00308 qreal left, top, right, bottom;
00309 m_applet->getContentsMargins(&left, &top, &right, &bottom);
00310 m_applet->setContentsMargins(left, m_savedAppletTopMargin + size().height(), right, bottom);
00311 deleteLater();
00312 }
00313 }
00314
00315 void AppletTitleBar::themeChanged()
00316 {
00317
00318
00319 QTimer::singleShot(0, this, SLOT(syncMargins()));
00320 }
00321
00322 void AppletTitleBar::animationFinished(int id)
00323 {
00324 if (!m_underMouse) {
00325 m_showButtons = false;
00326 }
00327
00328 if (id == m_maximizeButtonAnimationId) {
00329 m_maximizeButtonAnimationId = 0;
00330 } else if (id == m_configureButtonAnimationId) {
00331 m_configureButtonAnimationId = 0;
00332 } else if (id == m_closeButtonAnimationId) {
00333 m_closeButtonAnimationId = 0;
00334 }
00335 }
00336
00337 #include <applettitlebar.moc>