1 // Copyright (c) 2016 The SigViewer Development Team
2 // Licensed under the GNU General Public License (GPL)
3 // https://www.gnu.org/licenses/gpl
4 
5 
6 #include "event_context_menu.h"
7 #include "signal_browser_model_4.h"
8 #include "gui/gui_action_factory.h"
9 
10 #include <QGraphicsSceneContextMenuEvent>
11 #include <QAction>
12 #include <QDebug>
13 
14 namespace sigviewer
15 {
16 
17 //-----------------------------------------------------------------------------
EventContextMenu(SignalVisualisationModel & browser_model,QSharedPointer<EventManager> event_manager)18 EventContextMenu::EventContextMenu (SignalVisualisationModel& browser_model,
19                                     QSharedPointer<EventManager> event_manager)
20     : browser_model_ (browser_model),
21       event_manager_ (event_manager)
22 {
23     // nothing to do here
24 }
25 
26 //-----------------------------------------------------------------------------
~EventContextMenu()27 EventContextMenu::~EventContextMenu ()
28 {
29 
30 }
31 
32 //-------------------------------------------------------------------------
addEvent(EventID event)33 void EventContextMenu::addEvent (EventID event)
34 {
35     QVector<QMenu*>::iterator it = sub_menus_.begin();
36     while (it != sub_menus_.end())
37     {
38         (*it)->clear();
39         delete (*it);
40         ++it;
41     }
42     sub_menus_.clear();
43 
44     clear();
45     QObject::disconnect(this, 0);
46     event_ids_.append (event);
47 }
48 
49 //-------------------------------------------------------------------------
getNumberOfEvents() const50 unsigned EventContextMenu::getNumberOfEvents () const
51 {
52     return event_ids_.size();
53 }
54 
55 
56 //-------------------------------------------------------------------------
finaliseAndShowContextMenu(QGraphicsSceneContextMenuEvent * context_event,QMenu * channel_menu)57 void EventContextMenu::finaliseAndShowContextMenu (QGraphicsSceneContextMenuEvent* context_event,
58                                                    QMenu* channel_menu)
59 {
60     QVector<EventID>::iterator it = event_ids_.begin();
61     if (event_ids_.size() > 1)
62     {
63         while (it != event_ids_.end())
64         {
65             QString text (event_manager_->getNameOfEvent(*it));
66             QMenu* submenu = new QMenu (text);
67             sub_menus_.append(submenu);
68 
69             // context-menu actions
70             addActionsToMenu (*submenu, *it);
71 
72             QAction* action = addMenu (submenu);
73             action->activate(QAction::Hover);
74             action->setData(*it);
75             addAction(action);
76             ++it;
77         }
78     }
79     else if (event_ids_.size() == 1)
80     {
81         QAction* title = addAction (event_manager_->getNameOfEvent(*it));
82         title->setEnabled (false);
83         addActionsToMenu (*this, *(event_ids_.begin ()));
84         browser_model_.selectEvent (*it);
85     }
86 
87     event_ids_.clear();
88     QObject::connect(this, SIGNAL(hovered(QAction*)), this, SLOT(selectEvent(QAction*)));
89     if (channel_menu)
90     {
91         addSeparator ();
92         addMenu (channel_menu);
93     }
94 
95     exec (context_event->screenPos());
96 }
97 
98 //-------------------------------------------------------------------------
finaliseAndShowSelectionMenu(QGraphicsSceneMouseEvent * event)99 void EventContextMenu::finaliseAndShowSelectionMenu (QGraphicsSceneMouseEvent* event)
100 {
101     QVector<EventID>::iterator it = event_ids_.begin();
102     if (event_ids_.size() > 1)
103     {
104         while (it != event_ids_.end())
105         {
106             QString text (event_manager_->getNameOfEvent (*it));
107             QAction* action = addAction(text);
108             //action->activate(QAction::Hover);
109             action->setData(*it);
110             //addAction(action);
111             ++it;
112         }
113         QObject::connect(this, SIGNAL(triggered(QAction*)), this, SLOT(selectEvent(QAction*)));
114         exec (event->screenPos());
115     }
116     else if (event_ids_.size() == 1)
117     {
118         browser_model_.selectEvent (*it);
119     }
120 
121     event_ids_.clear();
122 }
123 
124 
125 //-------------------------------------------------------------------------
selectEvent(QAction * q)126 void EventContextMenu::selectEvent (QAction* q)
127 {
128     bool ok = false;
129     int32 event_id = q->data().toInt(&ok);
130     if (q->data().isValid())
131         browser_model_.selectEvent (event_id);
132 }
133 
134 //-------------------------------------------------------------------------
addActionsToMenu(QMenu & menu,EventID event)135 void EventContextMenu::addActionsToMenu (QMenu& menu, EventID event)
136 {
137     QList<QAction*> actions;
138 
139     if (event_manager_->getEvent(event)->getChannel() != UNDEFINED_CHANNEL)
140     {
141         actions.append (GuiActionFactory::getInstance()->getQAction("To all Channels"));
142         actions.append (GuiActionFactory::getInstance()->getQAction("Copy to Channels..."));
143     }
144     actions.append (GuiActionFactory::getInstance()->getQAction("Delete"));
145     actions.append (GuiActionFactory::getInstance()->getQAction("Change Channel..."));
146     actions.append (GuiActionFactory::getInstance()->getQAction("Change Type..."));
147     actions.append (new QAction (this));
148     actions.last()->setSeparator(true);
149     actions.append (GuiActionFactory::getInstance()->getQAction("Insert Over"));
150     actions.append (new QAction (this));
151     actions.last()->setSeparator(true);
152     if (event_manager_->getNextEventOfSameType (event) != UNDEFINED_EVENT_ID)
153         actions.append (GuiActionFactory::getInstance()->getQAction("Goto and Select Next Event"));
154     if (event_manager_->getPreviousEventOfSameType (event) != UNDEFINED_EVENT_ID)
155         actions.append (GuiActionFactory::getInstance()->getQAction("Goto and Select Previous Event"));
156     if (!actions.last()->isSeparator())
157     {
158         actions.append (new QAction (this));
159         actions.last()->setSeparator(true);
160     }
161     if (browser_model_.getShownEventTypes().size() > 1)
162         actions.append (GuiActionFactory::getInstance()->getQAction("Hide Events of other Type"));
163     else
164         actions.append (GuiActionFactory::getInstance()->getQAction("Show all Events"));
165     actions.append (GuiActionFactory::getInstance()->getQAction("Fit View to Selected Event"));
166 
167     foreach (QAction* action, actions)
168         menu.addAction (action);
169 }
170 
171 }
172