dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
actioncontainer.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "actioncontainer_p.h"
29 #include "actionmanager_p.h"
30 
31 #include "command_p.h"
32 
33 #include "coreconstants.h"
34 #include "uniqueidmanager.h"
35 
36 #include <QtCore/QDebug>
37 #include <QAction>
38 #include <QMenuBar>
39 
41 
42 using namespace Core;
43 using namespace Core::Internal;
44 
145 // ---------- ActionContainerPrivate ------------
146 
153  : m_data(0)
154  , m_id(id)
155 {
156 }
157 
158 void ActionContainerPrivate::setEmptyAction(EmptyAction ea)
159 {
160  m_data = ((m_data & ~EA_Mask) | ea);
161 }
162 
164 {
165  return (m_data & EA_Mask) == ea;
166 }
167 
168 void ActionContainerPrivate::appendGroup(const QString &group)
169 {
170  int gid = UniqueIDManager::instance()->uniqueIdentifier(group);
171  m_groups << gid;
172 }
173 
174 QAction *ActionContainerPrivate::insertLocation(const QString &group) const
175 {
176  int grpid = UniqueIDManager::instance()->uniqueIdentifier(group);
177  int prevKey = 0;
178  int pos = ((grpid << 16) | 0xFFFF);
179  return beforeAction(pos, &prevKey);
180 }
181 
182 void ActionContainerPrivate::addAction(Command *action, const QString &group)
183 {
184  if (!canAddAction(action))
185  return;
186 
189  int grpid = idmanager->uniqueIdentifier(Constants::G_DEFAULT_TWO);
190  if (!group.isEmpty())
191  grpid = idmanager->uniqueIdentifier(group);
192  if (!m_groups.contains(grpid) && !am->defaultGroups().contains(grpid))
193  qWarning() << "*** addAction(): Unknown group: " << group;
194  int pos = ((grpid << 16) | 0xFFFF);
195  addAction(action, pos, true);
196 }
197 
198 void ActionContainerPrivate::addMenu(ActionContainer *menu, const QString &group)
199 {
200  ActionContainerPrivate *container = static_cast<ActionContainerPrivate *>(menu);
201  if (!container->canBeAddedToMenu())
202  return;
203 
206  int grpid = idmanager->uniqueIdentifier(Constants::G_DEFAULT_TWO);
207  if (!group.isEmpty())
208  grpid = idmanager->uniqueIdentifier(group);
209  if (!m_groups.contains(grpid) && !am->defaultGroups().contains(grpid))
210  qWarning() << "*** addMenu(): Unknown group: " << group;
211  int pos = ((grpid << 16) | 0xFFFF);
212  addMenu(menu, pos, true);
213 }
214 
216 {
217  return m_id;
218 }
219 
221 {
222  return nullptr;
223 }
224 
226 {
227  return nullptr;
228 }
229 
231 {
232  return (action->action() != nullptr);
233 }
234 
235 void ActionContainerPrivate::addAction(Command *action, int pos, bool setpos)
236 {
237  Action *a = static_cast<Action *>(action);
238 
239  int prevKey = 0;
240  QAction *ba = beforeAction(pos, &prevKey);
241 
242  if (setpos) {
243  pos = calcPosition(pos, prevKey);
244  CommandLocation loc;
245  loc.m_container = m_id;
246  loc.m_position = pos;
247  QList<CommandLocation> locs = a->locations();
248  locs.append(loc);
249  a->setLocations(locs);
250  }
251 
252  m_commands.append(action);
253  m_posmap.insert(pos, action->id());
254  insertAction(ba, a->action());
255 }
256 
257 void ActionContainerPrivate::addMenu(ActionContainer *menu, int pos, bool setpos)
258 {
259  MenuActionContainer *mc = static_cast<MenuActionContainer *>(menu);
260 
261  int prevKey = 0;
262  QAction *ba = beforeAction(pos, &prevKey);
263 
264  if (setpos) {
265  pos = calcPosition(pos, prevKey);
266  CommandLocation loc;
267  loc.m_container = m_id;
268  loc.m_position = pos;
269  mc->setLocation(loc);
270  }
271 
272  m_subContainers.append(menu);
273  m_posmap.insert(pos, menu->id());
274  insertMenu(ba, mc->menu());
275 }
276 
277 QAction *ActionContainerPrivate::beforeAction(int pos, int *prevKey) const
278 {
280 
281  int baId = -1;
282 
283  (*prevKey) = -1;
284 
285  QMap<int, int>::const_iterator i = m_posmap.constBegin();
286  while (i != m_posmap.constEnd()) {
287  if (i.key() > pos) {
288  baId = i.value();
289  break;
290  }
291  (*prevKey) = i.key();
292  ++i;
293  }
294 
295  if (baId == -1)
296  return nullptr;
297 
298  if (Command *cmd = am->command(baId))
299  return cmd->action();
300  if (ActionContainer *container = am->actionContainer(baId))
301  if (QMenu *menu = container->menu())
302  return menu->menuAction();
303 
304  return nullptr;
305 }
306 
307 int ActionContainerPrivate::calcPosition(int pos, int prevKey) const
308 {
309  int grp = (pos & 0xFFFF0000);
310  if (prevKey == -1)
311  return grp;
312 
313  int prevgrp = (prevKey & 0xFFFF0000);
314 
315  if (grp != prevgrp)
316  return grp;
317 
318  return grp + (prevKey & 0xFFFF) + 10;
319 }
320 
321 // ---------- MenuActionContainer ------------
322 
330  , m_menu(nullptr)
331 {
333 }
334 
336 {
337  m_menu = menu;
338 
339  QVariant v;
340  qVariantSetValue<MenuActionContainer *>(v, this);
341 
342  m_menu->menuAction()->setData(v);
343 }
344 
346 {
347  return m_menu;
348 }
349 
350 void MenuActionContainer::insertAction(QAction *before, QAction *action)
351 {
352  m_menu->insertAction(before, action);
353 }
354 
355 void MenuActionContainer::insertMenu(QAction *before, QMenu *menu)
356 {
357  m_menu->insertMenu(before, menu);
358 }
359 
361 {
362  m_location = location;
363 }
364 
366 {
367  return m_location;
368 }
369 
371 {
372  if (hasEmptyAction(EA_None))
373  return true;
374 
375  bool hasitems = false;
376 
377  foreach (ActionContainer *container, subContainers()) {
378  if (container == this) {
379  qWarning() << Q_FUNC_INFO << "container" << (this->menu() ? this->menu()->title() : "")
380  << "contains itself as subcontainer";
381  continue;
382  }
383  if (container->update()) {
384  hasitems = true;
385  break;
386  }
387  }
388  if (!hasitems) {
389  foreach (Command *command, commands()) {
390  if (command->isActive()) {
391  hasitems = true;
392  break;
393  }
394  }
395  }
396 
397  if (hasEmptyAction(EA_Hide))
398  m_menu->setVisible(hasitems);
399  else if (hasEmptyAction(EA_Disable))
400  m_menu->setEnabled(hasitems);
401 
402  return hasitems;
403 }
404 
406 {
407  return true;
408 }
409 
410 // ---------- MenuBarActionContainer ------------
411 
419  , m_menuBar(nullptr)
420 {
422 }
423 
424 void MenuBarActionContainer::setMenuBar(QMenuBar *menuBar)
425 {
426  m_menuBar = menuBar;
427 }
428 
430 {
431  return m_menuBar;
432 }
433 
434 void MenuBarActionContainer::insertAction(QAction *before, QAction *action)
435 {
436  m_menuBar->insertAction(before, action);
437 }
438 
439 void MenuBarActionContainer::insertMenu(QAction *before, QMenu *menu)
440 {
441  m_menuBar->insertMenu(before, menu);
442 }
443 
445 {
446  if (hasEmptyAction(EA_None))
447  return true;
448 
449  bool hasitems = false;
450  QList<QAction *> actions = m_menuBar->actions();
451  for (int i = 0; i < actions.size(); ++i) {
452  if (actions.at(i)->isVisible()) {
453  hasitems = true;
454  break;
455  }
456  }
457 
458  if (hasEmptyAction(EA_Hide))
459  m_menuBar->setVisible(hasitems);
460  else if (hasEmptyAction(EA_Disable))
461  m_menuBar->setEnabled(hasitems);
462 
463  return hasitems;
464 }
465 
467 {
468  return false;
469 }
static UniqueIDManager * instance()
virtual void insertAction(QAction *before, QAction *action)=0
int uniqueIdentifier(const QString &id)
virtual bool canBeAddedToMenu() const =0
void insertMenu(QAction *before, QMenu *menu)
void insertAction(QAction *before, QAction *action)
void setLocation(const CommandLocation &location)
virtual QAction * action() const =0
QAction * insertLocation(const QString &group) const
QList< Command * > commands() const
Command * command(int uid) const
void addAction(Command *action, const QString &group=QString())
for i
Definition: OPPlots.m:140
void addMenu(ActionContainer *menu, const QString &group=QString())
void insertAction(QAction *before, QAction *action)
static ActionManagerPrivate * instance()
end a
Definition: OPPlots.m:98
QList< CommandLocation > locations() const
Definition: command.cpp:381
QList< ActionContainer * > subContainers() const
virtual void insertMenu(QAction *before, QMenu *menu)=0
void appendGroup(const QString &group)
void setLocations(const QList< CommandLocation > &locations)
Definition: command.cpp:376
bool canAddAction(Command *action) const
virtual bool isActive() const =0
void insertMenu(QAction *before, QMenu *menu)
Q_DECLARE_METATYPE(Core::Internal::MenuActionContainer *) using namespace Core
ActionContainer * actionContainer(int uid) const
bool hasEmptyAction(EmptyAction ea) const
virtual int id() const =0
const char *const G_DEFAULT_TWO
virtual int id() const =0
QAction * action() const
Definition: command.cpp:371
The class Command represents an action like a menu item, tool button, or shortcut. You don't create Command objects directly, instead use {ActionManager::registerAction()} to register an action and retrieve a Command. The Command object represents the user visible action and its properties. If multiple actions are registered with the same ID (but different contexts) the returned Command is the shared one between these actions.
Definition: command.h:43
virtual bool update()=0
The ActionContainer class represents a menu or menu bar in the Tau Labs GCS.