summaryrefslogtreecommitdiff
path: root/muse2/muse/widgets/popupmenu.cpp
blob: 6cda1987406b28b236e719b1cd29e6a7280744c4 (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
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: popupmenu.cpp,v 1.1.1.1 2010/07/18 03:21:00 terminator356 Exp $
//
//  (C) Copyright 1999-2010 Werner Schweer (ws@seh.de)
//
//  PopupMenu sub-class of QMenu created by Tim.
//  (C) Copyright 2010-2011 Tim E. Real (terminator356 A T sourceforge D O T net)
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================

#include <QMouseEvent>
#include <QHoverEvent>
#include <QAction>
#include <QPoint>
#include <QDesktopWidget>
#include <QApplication>

#include "popupmenu.h"
#include "gconfig.h"
#include "route.h"
 
 
namespace MusEGui {

//======================
// PopupMenu
//======================

PopupMenu::PopupMenu(bool stayOpen) 
          : _stayOpen(stayOpen)
{
  init();
}

PopupMenu::PopupMenu(QWidget* parent, bool stayOpen) 
          : QMenu(parent), _stayOpen(stayOpen)
{
  init();
}

PopupMenu::PopupMenu(const QString& title, QWidget* parent, bool stayOpen)
          : QMenu(title, parent), _stayOpen(stayOpen)
{
  init();        
}

void PopupMenu::init()
{
  // Menus will trigger! Set to make sure our trigger handlers ignore menus.
  menuAction()->setData(-1);
  
  _cur_menu = this;
  _cur_menu_count = 1;
  _cur_item_width = 0;
  _cur_col_count = 0;
  
  //_stayOpen = false;
  moveDelta = 0;
  
  #ifndef POPUP_MENU_DISABLE_AUTO_SCROLL  
  timer = new QTimer(this);
  timer->setInterval(100);
  timer->setSingleShot(false);
  connect(this, SIGNAL(hovered(QAction*)), SLOT(popHovered(QAction*)));
  connect(timer, SIGNAL(timeout()), SLOT(timerHandler()));
  #endif   // POPUP_MENU_DISABLE_AUTO_SCROLL
}

// NOTE: Tested all RoutePopupMenu and PopupMenu dtors and a couple of action dtors from our 
//  PixmapButtonsHeaderWidgetAction and PixmapButtonsWidgetAction: 
// This does not appear to be required any more. All submenus and actions are being deleted now.  
/*
void PopupMenu::clear()
{
  //printf("PopupMenu::clear this:%p\n", this); 

  QList<QAction*> list = actions();
  for(int i = 0; i < list.size(); ++i)
  {
    QAction* act = list[i];
    QMenu* menu = act->menu();
    if(menu)
    {
      menu->clear();    // Recursive.
      act->setMenu(0);  // CHECK: Is this OK?
      //printf("  deleting menu:%p\n", menu); 
      delete menu;
    }
  }
  
  // Now let QT remove and delete this menu's actions.
  QMenu::clear();
  
  #ifndef POPUP_MENU_DISABLE_AUTO_SCROLL  
  connect(this, SIGNAL(hovered(QAction*)), SLOT(popHovered(QAction*)));
  connect(timer, SIGNAL(timeout()), SLOT(timerHandler()));
  #endif    // POPUP_MENU_DISABLE_AUTO_SCROLL
}
*/

void PopupMenu::clearAllChecks() const
{
  QList<QAction*> list = actions();
  for(int i = 0; i < list.size(); ++i)
  {
    QAction* act = list[i];
    PopupMenu* menu = static_cast <PopupMenu*>(act->menu());
    if(menu)
      menu->clearAllChecks();     // Recursive.
    if(act->isCheckable())
    {
      act->blockSignals(true);
      act->setChecked(false);
      act->blockSignals(false);
    }  
  }
}

QAction* PopupMenu::findActionFromData(const QVariant& v) const
{
  QList<QAction*> list = actions();
  for(int i = 0; i < list.size(); ++i)
  {
    QAction* act = list[i];
    PopupMenu* menu = (PopupMenu*)act->menu();
    if(menu)
    {
      if(QAction* actm = menu->findActionFromData(v))      // Recursive.
        return actm;
    }
    
    // "Operator == Compares this QVariant with v and returns true if they are equal, 
    //   otherwise returns false. In the case of custom types, their equalness operators 
    //   are not called. Instead the values' addresses are compared."
    //
    // Take care of struct Route first. Insert other future custom structures here too !
    if(act->data().canConvert<MusECore::Route>() && v.canConvert<MusECore::Route>())
    {
      if(act->data().value<MusECore::Route>() == v.value<MusECore::Route>())
        return act;    
    }
    else
    if(act->data() == v)
      return act;
  }
  return 0;
}
    
bool PopupMenu::event(QEvent* event)
{
  switch(event->type())
  {
    #ifndef POPUP_MENU_DISABLE_STAY_OPEN  
    case QEvent::MouseButtonDblClick:
    {  
      if(_stayOpen)
      //if(_stayOpen && MusEGlobal::config.popupsDefaultStayOpen)
      {
        QMouseEvent* e = static_cast<QMouseEvent*>(event);
        if(e->modifiers() == Qt::NoModifier)
        {
          event->accept();
          // Convert into a return press, which selects the item and closes the menu.
          // Note that with double click, it's a press followed by release followed by double click.
          // That would toggle our item twice eg on->off->on, which is hopefully OK.
          QKeyEvent ke(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier); 
          //ke.ignore();   // Pass it on
          return QMenu::event(&ke);
        }  
      }  
    }  
    break;
    case QEvent::KeyPress:
    {
      if(_stayOpen)
      //if(_stayOpen && MusEGlobal::config.popupsDefaultStayOpen)
      {
        QKeyEvent* e = static_cast<QKeyEvent*>(event);
        if(e->modifiers() == Qt::NoModifier && e->key() == Qt::Key_Space)
        {
          QAction* act = activeAction();
          if(act)
          {
            act->trigger();
            event->accept();
            return true;    // We handled it.
          }
        }  
      }
    }
    break;
    #endif   // POPUP_MENU_DISABLE_STAY_OPEN
    
    #ifndef POPUP_MENU_DISABLE_AUTO_SCROLL  
    case QEvent::MouseMove:
    {
      QMouseEvent* e = static_cast<QMouseEvent*>(event);
      QPoint globPos = e->globalPos();
      int dw = QApplication::desktop()->width();  // We want the whole thing if multiple monitors.
      if(x() < 0 && globPos.x() <= 0)   // If on the very first pixel (or beyond)
      {
        moveDelta = 32;
        if(!timer->isActive())
          timer->start();
        event->accept();
        return true;        
      }  
      else
      if(x() + width() >= dw && globPos.x() >= (dw -1))   // If on the very last pixel (or beyond)
      {
        moveDelta = -32;
        if(!timer->isActive())
          timer->start();
        event->accept();
        return true;        
      }
        
      if(timer->isActive())
          timer->stop();
      
      //event->accept();
      //return true;        
      
      event->ignore();               // Pass it on
      //return QMenu::event(event);  
    }  
    break;
    #endif  // POPUP_MENU_DISABLE_AUTO_SCROLL  
    
    default:
    break;
  }
  
  return QMenu::event(event);
}

#ifndef POPUP_MENU_DISABLE_AUTO_SCROLL  
void PopupMenu::timerHandler()
{
  //if(!isVisible() || !hasFocus())
  if(!isVisible())
  {
    timer->stop();
    return;
  }  

  int dw = QApplication::desktop()->width();  // We want the whole thing if multiple monitors.
  int nx = x() + moveDelta;
  if(moveDelta < 0 && nx + width() < dw)
  {
    timer->stop();
    nx = dw - width();
  }
  else
  if(moveDelta > 0 && nx > 0) 
  {
    timer->stop();
    nx = 0;
  }
  
  move(nx, y());  
}

void PopupMenu::popHovered(QAction* action)
{
  if(action)
  {  
    int dw = QApplication::desktop()->width();  // We want the whole thing if multiple monitors.
    QRect r = actionGeometry(action);
    if(x() + r.x() < 0)
      move(-r.x(), y());   
    else
    if(r.x() + r.width() + x() > dw)
      move(dw - r.x() - r.width(), y());  
  }
}
#endif    // POPUP_MENU_DISABLE_AUTO_SCROLL

void PopupMenu::mouseReleaseEvent(QMouseEvent *e)
{
    QAction* action = actionAt(e->pos());
    if (!(action && action == activeAction() && !action->isSeparator() && action->isEnabled()))
      action=NULL;

    #ifdef POPUP_MENU_DISABLE_STAY_OPEN
    if (action && action->menu() != NULL  &&  action->isCheckable())
      action->activate(QAction::Trigger);

    QMenu::mouseReleaseEvent(e);
    
    if (action && action->menu() != NULL  &&  action->isCheckable())
      close();
      
    return;
    
    #else
    // Check for Ctrl to stay open.
    if(!_stayOpen || (!MusEGlobal::config.popupsDefaultStayOpen && (e->modifiers() & Qt::ControlModifier) == 0))  
    {
      if (action && action->menu() != NULL  &&  action->isCheckable())
        action->activate(QAction::Trigger);

      QMenu::mouseReleaseEvent(e);

      if (action && action->menu() != NULL  &&  action->isCheckable())
        close();

      return;
    }  
    
    if (action) 
      action->activate(QAction::Trigger);
    else 
      QMenu::mouseReleaseEvent(e);
      
    #endif   // POPUP_MENU_DISABLE_STAY_OPEN 
}

//-----------------------------------------
// getMenu
// Auto-breakup a too-wide menu.
// NOTE This is a necessary catch-all because X doesn't like too-wide menus, but risky because
//       some callers might depend on all the items being in one menu (using ::actions or ::addActions).
//      So try to avoid that. The overwhelming rule of thumb is that too-wide menus are bad design anyway.
//------------------------------------------
PopupMenu* PopupMenu::getMenu()
{
  // We want the whole thing if multiple monitors.
  // Resonable to assume if X can show this desktop, it can show a menu with the same width?
  int dw = QApplication::desktop()->width();
  // If we're still only at one column, not much we can do - some item(s) must have had reeeeally long text.
  // Not to worry. Hopefully the auto-scroll will handle it!
  // Use columnCount() + 2 to catch well BEFORE it widens beyond the edge, and leave room for many <More...>
  // TESTED: Not only does the system not appear to like too-wide menus, but also the column count was observed
  //          rolling over to zero repeatedly after it reached 15, simply when adding actions! The action width was 52
  //          the number of items when it first rolled over was around 480 = 884, well below my desktop width of 1366.
  //         Apparently there is a limit on the number of columns - whatever, it made the col count limit necessary:
  if((_cur_col_count > 1 && ((_cur_col_count + 2) * _cur_item_width) >= dw) || _cur_col_count >= 8)
  {
    // This menu is too wide. So make a new one...
    _cur_item_width = 0;
    _cur_col_count = 1;
    QString s(tr("<More...> %1").arg(_cur_menu_count));
    _cur_menu = new PopupMenu(s, this, _stayOpen);
    ++_cur_menu_count;
    addMenu(_cur_menu);
  }
  return _cur_menu;
}

//----------------------------------------------------
// Need to catch these to auto-breakup a too-big menu...
//----------------------------------------------------

QAction* PopupMenu::addAction(const QString& text)
{
  QAction* act = static_cast<QMenu*>(getMenu())->addAction(text);
  int w = _cur_menu->actionGeometry(act).width();
  if(w > _cur_item_width)
    _cur_item_width = w;
  int c = _cur_menu->columnCount();
  if(c > _cur_col_count)
    _cur_col_count = c;
  return act;
}

QAction* PopupMenu::addAction(const QIcon& icon, const QString& text)
{
  QAction* act = static_cast<QMenu*>(getMenu())->addAction(icon, text);
  int w = _cur_menu->actionGeometry(act).width();
  if(w > _cur_item_width)
    _cur_item_width = w;
  int c = _cur_menu->columnCount();
  if(c > _cur_col_count)
    _cur_col_count = c;
  return act;
}

QAction* PopupMenu::addAction(const QString& text, const QObject* receiver, const char* member, const QKeySequence& shortcut)
{
  QAction* act = static_cast<QMenu*>(getMenu())->addAction(text, receiver, member, shortcut);
  int w = _cur_menu->actionGeometry(act).width();
  if(w > _cur_item_width)
    _cur_item_width = w;
  int c = _cur_menu->columnCount();
  if(c > _cur_col_count)
    _cur_col_count = c;
  return act;
}

QAction* PopupMenu::addAction(const QIcon& icon, const QString& text, const QObject* receiver, const char* member, const QKeySequence& shortcut)
{
  QAction* act = static_cast<QMenu*>(getMenu())->addAction(icon, text, receiver, member, shortcut);
  int w = _cur_menu->actionGeometry(act).width();
  if(w > _cur_item_width)
    _cur_item_width = w;
  int c = _cur_menu->columnCount();
  if(c > _cur_col_count)
    _cur_col_count = c;
  return act;
}

void PopupMenu::addAction(QAction* action)
{
  static_cast<QMenu*>(getMenu())->addAction(action);
  int w = _cur_menu->actionGeometry(action).width();
  if(w > _cur_item_width)
    _cur_item_width = w;
  int c = _cur_menu->columnCount();
  if(c > _cur_col_count)
    _cur_col_count = c;
}

/*
//======================
// PopupView
//======================

PopupView::PopupView(QWidget* parent) 
          : QColumnView(parent)
{
  _model= new QStandardItemModel(this); 
  // FIXME: After clearing, then re-filling, no items seen. 
  // But if setModel is called FOR THE FIRST TIME after clearing the model,
  //  then it works. Calling setModel any time after that does not work.
  setModel(_model);
}

PopupView::~PopupView()
{
  // Make sure to clear the popup so that any child popups are also deleted !
  //popup->clear();
}

void PopupView::clear()
{
  _model->clear();
}
*/ 
 
} // namespace MusEGui