summaryrefslogtreecommitdiffstats
path: root/src/komposesystray.cpp
blob: 41df920b7154f6f0dc595ed86989c1cc20e35fe8 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// C++ Implementation: komposesystray
//
// Description:
//
//
// Author: Hans Oischinger <hans.oischinger@kde-mail.net>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "komposesystray.h"

#include "komposeviewmanager.h"
#include "komposefullscreenwidget.h"
#include "komposesettings.h"
#include "komposeglobal.h"
#include "komposetaskmanager.h"

#include <qpixmap.h>
#include <qbitmap.h>
#include <qpainter.h>

#include <kapplication.h>
#include <kaction.h>
#include <kpopupmenu.h>
#include <kiconeffect.h>
#include <kglobalsettings.h>

KomposeSysTray::KomposeSysTray(QWidget *parent, const char *name)
    : KSystemTray(parent, name)
{
  // Create Menu
  menu = contextMenu();
  move( -1000, -1000 );
  // Fill Menu
  KomposeGlobal::instance()->getActShowWorldView()->plug(menu);
  KomposeGlobal::instance()->getActShowVirtualDesktopView()->plug(menu);
  KomposeGlobal::instance()->getActShowCurrentDesktopView()->plug(menu);
  menu->insertSeparator();
  KomposeGlobal::instance()->getActPreferencesDialog()->plug(menu);
  KomposeGlobal::instance()->getActConfigGlobalShortcuts()->plug(menu);
  KomposeGlobal::instance()->getActAboutDlg()->plug(menu);

  slotConfigChanged();
  connect( KomposeSettings::instance(), SIGNAL(settingsChanged()), this, SLOT(slotConfigChanged()) );
}


KomposeSysTray::~KomposeSysTray()
{}

void KomposeSysTray::slotConfigChanged( )
{
  // set the icon here
  QPixmap iconPixmap = loadIcon("kompose");
  setPixmap(iconPixmap);
  icon = iconPixmap.convertToImage();
  currentDesktopChanged(KomposeTaskManager::instance()->getCurrentDesktopNum());
}

void KomposeSysTray::mouseReleaseEvent (QMouseEvent * )
{}

void KomposeSysTray::mousePressEvent ( QMouseEvent * e )
{
  if ( !rect().contains( e->pos() ) )
    return;

  switch ( e->button() )
  {
  case LeftButton:
    KomposeViewManager::instance()->createView( KomposeSettings::instance()->getDefaultView() );
    break;
  case MidButton:
    // fall through
  case RightButton:
    contextMenuAboutToShow( menu );
    menu->popup( e->globalPos() );
    break;
  default:
    // nothing
    break;
  }
}

void KomposeSysTray::currentDesktopChanged(int desktop)
{
  if (!KomposeSettings::instance()->getShowDesktopNum())
    return;
  // update the icon to display the current desktop number
  // qDebug("Displaying current desktop number on the tray icon....\n");

  // copying from aKregator/src/trayicon.cpp
  // from KMSystemTray
  int oldW = pixmap()->size().width();
  int oldH = pixmap()->size().height();

  QString uStr=QString::number( desktop );
  QFont f=KGlobalSettings::generalFont();
  f.setBold(true);
  f.setItalic(true);
  float pointSize=f.pointSizeFloat();
  QFontMetrics fm(f);
  int w=fm.width(uStr);
  if( w > (oldW) )
  {
    pointSize *= float(oldW) / float(w);
    f.setPointSizeFloat(pointSize);
  }
  QPixmap pix(oldW, oldH);
  pix.fill(Qt::white);
  QPainter p(&pix);
  p.setFont(f);
  p.setPen(Qt::black);
  p.drawText(pix.rect(), Qt::AlignCenter, uStr);
  pix.setMask(pix.createHeuristicMask());
  QImage img=pix.convertToImage();
  // overlay
  QImage overlayImg=icon.copy();
  KIconEffect::overlay(overlayImg, img);
  QPixmap newIcon;
  newIcon.convertFromImage(overlayImg);
  setPixmap(newIcon);
}

#include "komposesystray.moc"