summaryrefslogtreecommitdiffstats
path: root/src/tastymenu.cpp
blob: 3dc3e7eaef613c41dfe7073bbdcbf8ed4ad7652d (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/***************************************************************************
 *   Copyright (C) 2006 by Marco Martin                                    *
 *   [email protected]                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the Lesser GNU General Public                   *
 *   License as published by the Free Software Foundation;                 *
 *   either version 2 of the License, 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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <kglobal.h>
#include <klocale.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kmessagebox.h>
#include <kconfigdialog.h>
#include <kaboutdata.h>
#include <kaboutapplication.h>
#include <kpopupmenu.h>
#include <kkeydialog.h>
#include <kuser.h>
#include <kstandarddirs.h>
#include <kdebug.h>
#include <tqstyle.h>


#include "tastymenu.h"
#include "appearance.h"
#include "behaviour.h"



TastyMenu::TastyMenu(const TQString& configFile, Type type, int actions, TQWidget *parent, const char *name)
  : KPanelApplet(configFile, type, actions, parent, name), numNewApplications(0)
{

    // Get the current application configuration handle
    kConfig = sharedConfig();
    prefSkel = new Prefs(kConfig);
    prefSkel->readConfig();

    kickerConf = KGlobal::config();
    kickerConf->setGroup("buttons");
    _showBigToolTip = kickerConf->readBoolEntry("EnableIconZoom", true);

    button = new TastyButton(this);

    menuHandler = new MenuHandler(this, prefSkel, "MenuHandler", 
                                  prefSkel->isNormalWindow() ?
                                      TQt::WType_Dialog :
                                      TQt::WType_Popup|TQt::WNoAutoErase);

    connect (button, SIGNAL (pressed()), this, SLOT (clickSlot ()));
    connect (menuHandler, SIGNAL(hidden()), this, SLOT(setButtonUp()));
    _menuButtonLabel = prefSkel->menuButtonLabel();
    if(_menuButtonLabel.isEmpty())
      button -> setTextLabel(i18n("Menu"), false);
    else
      button -> setTextLabel(_menuButtonLabel, false);

    button -> setUsesTextLabel(prefSkel->menuButtonLabelType()
                               != Prefs::EnumMenuButtonLabelType::MenuButtonNone);
    button -> setTextPosition(TQToolButton::BesideIcon);
    menuTip = new TastyToolTip(button);

    _toolTipTitle = prefSkel->toolTipTitle();

    if( !_toolTipTitle.isEmpty() )
      menuTip->setTitle(_toolTipTitle);

    if( height() >= KIcon::SizeMedium )
      button->setUsesBigPixmap(true);
    else
      button->setUsesBigPixmap(false);

    iconLoader = KGlobal::iconLoader();

    loadMenuButtonIcon();

    button->setAutoRaise(true);

    _newAppsNotification = prefSkel->newAppsNotification();
    if( _newAppsNotification )
    {
      setNewApplicationsMessage(prefSkel->newInstalledApps().count());
      connect( menuHandler, SIGNAL(newApplications(int)),
               this, SLOT(setNewApplicationsMessage(int)));
    }
    setGlobalAccel( prefSkel->overrideAltF1());

    connect( menuHandler, SIGNAL(kickerConfChanged()),
             this, SLOT(updateConfiguration()) );
}


TastyMenu::~TastyMenu()
{
  //delete prefSkel;
  KGlobal::locale()->removeCatalogue("tastymenu");
}

void TastyMenu::loadMenuButtonIcon()
{
  _menuButtonIcon = prefSkel->menuButtonIcon();
  //the tooltip has the same icon as the button
  menuTip->loadIcon( _menuButtonIcon );

  if( prefSkel->menuButtonIconType()
      == Prefs::EnumMenuButtonIconType::IconNone )
  {
    button->setIconSet(TQIconSet());
    return;
  }

  if( position() == pTop || position() == pBottom )
    _iconsize = height();
  else if(position() == pLeft || position() == pRight)
    _iconsize = width();

  TQPixmap btnPixmap(iconLoader->loadIcon(_menuButtonIcon, KIcon::Panel, _iconsize));
  if( !btnPixmap.isNull() )
    button->setIconSet(btnPixmap);
  else
  {
    kdDebug() << "Failed to load custom icon" << endl;
    button->setIconSet(iconLoader->loadIcon("kmenu", KIcon::Panel, height()));
  }
}

TQPoint TastyMenu::menupos(TQWidget *widget)
{
 //now should look decent on all positions
 switch( position() )
 {
   case pLeft:
   {
     return TQPoint( this->mapToGlobal( this->geometry().topRight() ));
     break;
   }
   case pRight:
   {
     TQPoint buttonCoord = this->mapToGlobal(this->geometry().topLeft());
     return TQPoint( buttonCoord.x()-widget->width(), buttonCoord.y() );
     break;
   }
   case pTop:
   {
     return TQPoint( this->mapToGlobal( this->geometry().bottomLeft() ));
     break;
   }
   default: //pBottom or floating
   {
     TQPoint buttonCoord = this->mapToGlobal(this->geometry().topLeft());
     return TQPoint( buttonCoord.x(), buttonCoord.y()-widget->height() );
     break;
   }
 }
}

void TastyMenu::clickSlot()
{
  menuTip->hideTip();

  menuHandler->popup(menupos(menuHandler));

  //TQTimer::singleShot( 10000, this, SLOT(setButtonUp()) );
}

void TastyMenu::about()
{
  KAboutData data("tastymenu",
                  I18N_NOOP("Tasty Menu"),
                  "1.0.6",
                  I18N_NOOP("KMenu replacement"),
                  KAboutData::License_LGPL_V2,
                  "(c) 2006-2007, Marco Martin",0,0,"[email protected]");

  data.addAuthor("Marco Martin",
                  I18N_NOOP("Maintainer"),
                  "[email protected]",
                  "http://www.notmart.org");

  data.setTranslator(I18N_NOOP("_: NAME OF TRANSLATORS\\nYour names")
               ,I18N_NOOP("_: EMAIL OF TRANSLATORS\\nYour emails"));

  data.addCredit("Yurkovsky Andrey",
                  I18N_NOOP("For the Russian translation"),
                  "[email protected]");
  data.addCredit("Jannick Kuhr",
                  I18N_NOOP("For the German translation"),
                  "[email protected]");
  data.addCredit("Jesús S Fernández Prieto (xgoan)",
                  I18N_NOOP("For the Spanish translation"),
                  "[email protected]");
  data.addCredit("Motsyo Vitaliy",
                  I18N_NOOP("For the Ukrainian translation"),
                  "[email protected]");
  data.addCredit("Laurent Hilsz",
                  I18N_NOOP("For the French translation"),
                  "[email protected]");
  data.addCredit("Tommi Nieminen",
                  I18N_NOOP("For the Finnish translation"),
                  "[email protected]");
  data.addCredit("Matija Šuklje",
                  I18N_NOOP("For the Slovenian translation"),
                  "[email protected]");
  data.addCredit("Tomasz Argasiński",
                  I18N_NOOP("For the Polish translation"),
                  "[email protected]");
  data.addCredit("Ewerton de A. Dutra"
                  I18N_NOOP("For the Polish translation"),
                  "[email protected]");


  data.addCredit("Oswald Buddenhagen and Stephan Kulow",
                  I18N_NOOP("For the Switch user code from KDM"),
                  "[email protected] and [email protected]");

  data.addCredit("The whole KBFX team",
                 I18N_NOOP("For some inspirations here and there."),
                 "http://www.kbfx.org");

  data.addCredit("Seb Ruiz",
                 I18N_NOOP("For some code taken from Amarok's statistics list view"),
                 "[email protected]");

  KIconLoader *iconLoader = KGlobal::iconLoader();
  data.setProgramLogo(iconLoader->loadIcon("kmenu", KIcon::Panel).convertToImage());

  KAboutApplication dialog(&data);
  dialog.exec();
}


void TastyMenu::help()
{
  KApplication::kdeinitExec("khelpcenter", "help:/tastymenu");
}


void TastyMenu::preferences()
{
  if(KConfigDialog::showDialog("settings"))
    return;

  Appearance *appearanceDialog = new Appearance(0, "appearance" );
  Behaviour *behaviourDialog = new Behaviour(0, "behaviour" );

  KConfigDialog *dialog = new KConfigDialog(this, "settings", prefSkel,
                                            KDialogBase::Tabbed,
                                            KConfigDialog::Default|
                                            KConfigDialog::Ok|
                                            KConfigDialog::Apply|
                                            KConfigDialog::Cancel );
  dialog->addPage(appearanceDialog, i18n("Appearance"), "appearance" );
  dialog->addPage(behaviourDialog, i18n("Behaviour"), "behaviour" );
  connect( dialog, SIGNAL(settingsChanged()),
           this, SLOT(updateConfiguration()) );
  dialog->show();
}

void TastyMenu::setGlobalAccel( bool global )
{
  globalAccel = new KGlobalAccel(this);
  globalAccel->insert("Toggle Tasty Menu", i18n("Toggle Tasty Menu"),
                      i18n("Toggle Tasty Menu"),
                      0, 0, this, SLOT(clickSlot()));
  globalAccel->readSettings();
  globalAccel->updateConnections();

  //deactivate or reactivate the global alt+f1 shotcut for kmenu
  if( global )
  {
    if( !kickerConf )
      kickerConf = KGlobal::config();

    kickerConf->setGroup("Global Shortcuts");
    TQString kmenuShortcut = kickerConf->readEntry("Popup Launch Menu", "default(Alt+F1)");
    if( kmenuShortcut=="none" )
      return;
    TQString tastyMenuShortcut = kickerConf->readEntry("Toggle Tasty Menu", "");
    KShortcut shortCutKey(tastyMenuShortcut);
    kickerConf->writeEntry("Popup Launch Menu", "none");
    kickerConf->writeEntry("Toggle Tasty Menu", kmenuShortcut);
    kickerConf->sync();

  }
  else
  {
    if( !kickerConf )
      kickerConf = KGlobal::config();
    kickerConf->setGroup("Global Shortcuts");
    kickerConf->deleteEntry("Popup Launch Menu");
    kickerConf->sync();
  }
}


void TastyMenu::updateConfiguration()
{
  kickerConf->setGroup("buttons");
  _showBigToolTip = kickerConf->readBoolEntry("EnableIconZoom", true);

  menuHandler->updateConfig();
  setGlobalAccel(prefSkel->overrideAltF1());
  _menuButtonLabel = prefSkel->menuButtonLabel();

  if(_menuButtonLabel.isEmpty())
    button -> setTextLabel(i18n("Menu"), false);
  else
    button -> setTextLabel(_menuButtonLabel, false);

  button -> setUsesTextLabel(prefSkel->menuButtonLabelType()
                               != Prefs::EnumMenuButtonLabelType::MenuButtonNone);
  loadMenuButtonIcon();

  _newAppsNotification = prefSkel->newAppsNotification();

  _toolTipTitle = prefSkel->toolTipTitle();

  if( !_toolTipTitle.isEmpty() )
    menuTip->setTitle(_toolTipTitle);
}


void TastyMenu::setNewApplicationsMessage( int number )
{
  if( number <= 0 )
  {
    menuTip ->setMessage("");
  }
  else
    menuTip ->setMessage(i18n("There is one new installed application",
                              "There are %n new installed applications", number));
  /*else if( number == 1)
    menuTip ->setMessage(i18n("There is one new installed application"));
  else
    menuTip ->setMessage(i18n("There are")+" " + TQString().setNum(number) +" "+ i18n("new installed applications"));*/

  if( _showBigToolTip && number > numNewApplications )
    menuTip->notify( menupos( menuTip ) );

  numNewApplications = number;
}


int TastyMenu::widthForHeight(int height) const
{
  int buttonWidth = width();
  if( position() == pTop || position() == pBottom )
  {
    button -> setTextPosition(TQToolButton::BesideIcon);
    if( prefSkel->menuButtonLabelType()
        != Prefs::EnumMenuButtonLabelType::MenuButtonNone )
      return ((button->fontMetrics()).width(button->textLabel())) + _iconsize + 10;
    else
      return height;
  }
  else
    return buttonWidth;
}

int TastyMenu::heightForWidth(int width) const
{
  int buttonHeight = height();
  if( position() == pLeft || position() == pRight )
  {
    if( prefSkel->menuButtonLabelType()
        != Prefs::EnumMenuButtonLabelType::MenuButtonNone )
    {
      button -> setTextPosition(TQToolButton::BelowIcon);
      return ((button->fontMetrics()).height()) + _iconsize + 10;
    }
    else
      return width;
  }
  else
    return buttonHeight;
}

void TastyMenu::resizeEvent(TQResizeEvent *e)
{
  int newHeight = e->size().height();
  button->setFixedHeight(newHeight);
  button->setFixedWidth(e->size().width());

  if( newHeight >= KIcon::SizeMedium )
    button->setUsesBigPixmap(true);
  else
    button->setUsesBigPixmap(false);


  loadMenuButtonIcon();
}

void TastyMenu::mousePressEvent(TQMouseEvent *e)
{
  menuTip->hideTip();

  if(e->button()==RightButton)
  {
    KPopupMenu menu(this);

    menu.insertTitle("Tasty Menu");
    menu.insertItem(SmallIcon("kmenu"), i18n("&About"), 1);
    menu.insertItem(SmallIcon("help"), i18n("&Help"), 2);
    menu.insertItem(SmallIcon("kmenuedit"), i18n("&Edit Menu"), 3);
    if( _newAppsNotification )
      menu.insertItem(SmallIcon("locationbar_erase"), i18n("&Clear recently installed applications list"), 4);
    menu.insertSeparator();
    menu.insertItem(SmallIcon("configure_shortcuts"), i18n("&Configure Global Shortcuts..."), 5);
    menu.insertItem(SmallIcon("configure"), i18n("&Configure..."), 6);

    int choice = menu.exec(this->mapToGlobal(e->pos()));

    switch( choice )
    {
      case 1:
        about();
        break;

      case 2:
        help();
        break;

      case 3:
        KRun::runCommand ("kmenuedit");
        break;

      case 4:
        menuHandler->clearNewInstalledApplications();
        setNewApplicationsMessage(0);
        menuHandler->slotUpdateApplications();
        break;
      case 5:
      {
        kickerConf->sync();
        KKeyDialog::configure(globalAccel);
        globalAccel->writeSettings(kickerConf);
        globalAccel->updateConnections();
        kickerConf->sync();
        break;
      }
      case 6:
       preferences();
       break;

      default:
        break;
    }
  }
}

void TastyMenu::enterEvent( TQEvent * e )
{
  e=e;
  if( _showBigToolTip && !menuHandler->isVisible() )
    menuTip->showTip(menupos(menuTip));

  KPanelApplet::enterEvent(e);
}

void TastyMenu::leaveEvent( TQEvent * e )
{
  e=e;
  menuTip->hideTip();

  KPanelApplet::leaveEvent(e);
}

extern "C"
{
    KPanelApplet* init( TQWidget *parent, const TQString& configFile)
    {
        KGlobal::locale()->insertCatalogue("tastymenu");
        return new TastyMenu(configFile, KPanelApplet::Normal,
                             KPanelApplet::About | KPanelApplet::Help | KPanelApplet::Preferences,
                             parent, "tastymenu");
    }
}