summaryrefslogtreecommitdiff
path: root/muse2/muse/widgets/popupmenu.cpp
blob: 263c84759a409fd76b6b8f5378f3e48fd5fa22af (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
//=========================================================
//  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)
//=========================================================

//#include <stdio.h>
#include <QMouseEvent>
#include <QHoverEvent>
#include <QAction>
#include <QPoint>
#include <QDesktopWidget>
#include <QApplication>
//#include <QTimer>

#include <stdio.h>
//#include <QStandardItemModel>

#include "popupmenu.h"
 
 
//======================
// PopupMenu
//======================

//PopupMenu::PopupMenu() 
//{
//  init();
//}

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();        
}

PopupMenu::~PopupMenu()
{
  //printf("PopupMenu::~PopupMenu\n");  
}

void PopupMenu::init()
{
  // Menus will trigger! Set to make sure our trigger handlers ignore menus.
  menuAction()->setData(-1);
  
  //_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
}

void PopupMenu::clear()
{
  QList<QAction*> list = actions();
  for(int i = 0; i < list.size(); ++i)
  {
    QAction* act = list[i];
    QMenu* menu = act->menu();
    if(menu)
    {
      menu->clear();
      act->setMenu(0);  // CHECK: Is this OK?
      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
}

QAction* PopupMenu::findActionFromData(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))
        return actm;
    }
    if(act->data() == v)
      return act;
  }
  return 0;
}
    
bool PopupMenu::event(QEvent* event)
{
  //printf("PopupMenu::event type:%d\n", event->type());   // REMOVE Tim.
  
  switch(event->type())
  {
    #ifndef POPUP_MENU_DISABLE_STAY_OPEN  
    case QEvent::MouseButtonDblClick:
    {  
      if(_stayOpen)
      {
        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)
      {
        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();
      //QPoint pos = e->pos();
      int dw = QApplication::desktop()->width();  // We want the whole thing if multiple monitors.
      
      //printf("PopupMenu::event MouseMove: pos x:%d y:%d  globPos x:%d y:%d\n", 
      //      pos.x(), pos.y(), globPos.x(), globPos.y());  // REMOVE Tim.
      
      /*
      //QAction* action = actionAt(globPos);
      QAction* action = actionAt(pos);
      if(action)
      { 
        QRect r = actionGeometry(action);
        //printf(" act x:%d y:%d w:%d h:%d  popup px:%d py:%d pw:%d ph:%d\n", 
        //      r.x(), r.y(), r.width(), r.height(), x(), y(), width(), height());  // REMOVE Tim.
              
        //action->hover();      
      }
      */
      
      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()
{
  // printf("PopupMenu::timerHandler\n");   // REMOVE Tim.
  
  //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)
{
  //timer->stop();
  
  //moveDelta = 0;
  if(action)
  {  
    int dw = QApplication::desktop()->width();  // We want the whole thing if multiple monitors.
    
    QRect r = actionGeometry(action);
    //printf("PopupMenu::popHovered x:%d y:%d w:%d h:%d px:%d py:%d pw:%d ph:%d\n", 
    //       r.x(), r.y(), r.width(), r.height(), x(), y(), width(), height());  // REMOVE Tim.
    //printf("PopupMenu::popHovered x:%d y:%d w:%d h:%d px:%d py:%d pw:%d ph:%d dtw:%d\n", 
    //      r.x(), r.y(), r.width(), r.height(), x(), y(), width(), height(), dw);  // REMOVE Tim.
    //int x = r.x() + ctrlSubPop->x();
    if(x() + r.x() < 0)
      //setGeometry(0, y(), width(), height());      
      //scroll(-x, 0);      
      //move(-r.x() + 32, y());   // Allow some of left column to show so that mouse can move over it.  
      //move(-r.x() + r.width(), y());   // Allow some of left column to show so that mouse can move over it.  
      //moveDelta = x() - r.x() + 32;
      move(-r.x(), y());   
    else
    if(r.x() + r.width() + x() > dw)
      //setGeometry(1200 - r.x() - r.width(), y(), width(), height());      
      //scroll(-x + 1200, 0);      
      //move(dw - r.x() - r.width() - 32, y());  // Allow some of right column to show so that mouse can move over it.       
      //move(dw - r.x(), y());  // Allow some of right column to show so that mouse can move over it.       
      //moveDelta = x() + dw - r.x() - r.width() - 32;
      move(dw - r.x() - r.width(), y());  
  }
      
  //if(moveDelta == 0)
  //  timer->stop();
    
}
#endif    // POPUP_MENU_DISABLE_AUTO_SCROLL

void PopupMenu::mouseReleaseEvent(QMouseEvent *e)
{
    #ifdef POPUP_MENU_DISABLE_STAY_OPEN    
    QMenu::mouseReleaseEvent(e);
    return;
    
    #else
    if(!_stayOpen)
    {
      QMenu::mouseReleaseEvent(e);
      return;
    }  
    
    //printf("PopupMenu::mouseReleaseEvent\n");  // REMOVE Tim.
    
    //Q_D(QMenu);
    //if (d->mouseEventTaken(e))
    //    return;

    //d->mouseDown = false;
    //QAction *action = d->actionAt(e->pos());
    QAction *action = actionAt(e->pos());
    
    //for(QWidget *caused = this; caused;) {
    //    if (QMenu *m = qobject_cast<QMenu*>(caused)) {
    //        QAction *currentAction = d->currentAction;
    //        if(currentAction && (!currentAction->isEnabled() || currentAction->menu() || currentAction->isSeparator()))
    //            currentAction = 0;
    //        caused = m->d_func()->causedPopup.widget;
    //        if (m->d_func()->eventLoop)
    //            m->d_func()->syncAction = currentAction; // synchronous operation
    //    } else {
    //        break;
    //    }
    //}
    
    //if (action && action == d->currentAction) {
    if (action && action == activeAction() && !action->isSeparator() && action->isEnabled()) 
    {
        //if (action->menu())
        //    action->menu()->d_func()->setFirstActionActive();
        //else
            //d->activateAction(action, QAction::Trigger);
            action->activate(QAction::Trigger);
    }
    else 
    //if (d->motions > 6) {
    //    d->hideUpToMenuBar();
    //  }
      QMenu::mouseReleaseEvent(e);
    #endif   // POPUP_MENU_DISABLE_STAY_OPEN 
}

/*
//======================
// 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();
}
*/