• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • workspace API Reference
  • KDE Home
  • Contact Us
 

liblancelot

  • sources
  • kde-4.14
  • workspace
  • kdeplasma-addons
  • libs
  • lancelot
  • widgets
PopupMenu.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser/Library General Public License version 2,
6  * or (at your option) any later version, as published by the Free
7  * Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser/Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Lesser/Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "PopupMenu.h"
21 
22 #include <QApplication>
23 #include <QSignalMapper>
24 
25 #include <lancelot/models/StandardActionListModel.h>
26 #include <lancelot/widgets/ActionListView.h>
27 
28 #define ITEM_HEIGHT 32
29 
30 namespace Lancelot {
31 
32 // ActionsModel
33 class ActionsModel:
34  public StandardActionListModel {
35 public:
36  ActionsModel();
37  ~ActionsModel();
38 
39  QAction * addAction(const QIcon & icon, const QString & title);
40  void addAction(QAction * action);
41 
42  L_Override void activate(int index);
43 
44  QList < QAction * > m_actions;
45 };
46 
47 ActionsModel::ActionsModel()
48 {
49 }
50 
51 ActionsModel::~ActionsModel()
52 {
53  qDeleteAll(m_actions);
54 }
55 
56 QAction * ActionsModel::addAction(const QIcon & icon, const QString & title)
57 {
58  QAction * action = new QAction(icon, title, NULL);
59  m_actions << action;
60  add(
61  title, QString(), icon, QString()
62  );
63  return action;
64 }
65 
66 void ActionsModel::addAction(QAction * action)
67 {
68  m_actions << action;
69 }
70 
71 void ActionsModel::activate(int index)
72 {
73  if (index < 0 || index >= m_actions.size()) {
74  return;
75  }
76 
77  m_actions.at(index)->trigger();
78 }
79 
80 // PopupMenu
81 
82 class PopupMenu::Private {
83 public:
84  Private(PopupMenu * parent)
85  {
86  Q_UNUSED(parent);
87  };
88 
89  ~Private()
90  {
91  }
92 
93  ActionsModel * model;
94  QSignalMapper mapper;
95  QAction * chosenAction;
96 };
97 
98 PopupMenu::PopupMenu(QWidget * parent, Qt::WindowFlags f)
99  : PopupList(parent, f),
100  d(new Private(this))
101 {
102  setModel(d->model = new ActionsModel());
103  connect(&d->mapper, SIGNAL(mapped(int)),
104  this, SLOT(actionChosen(int)));
105 
106 }
107 
108 PopupMenu::~PopupMenu()
109 {
110  delete d;
111 }
112 
113 void PopupMenu::addAction(QAction * action)
114 {
115  d->model->addAction(action);
116 }
117 
118 QAction * PopupMenu::addAction(const QIcon & icon, const QString & title)
119 {
120  QAction * result = d->model->addAction(icon, title);
121  connect(result, SIGNAL(triggered()),
122  &d->mapper, SLOT(map()));
123  d->mapper.setMapping(result, d->model->m_actions.size() - 1);
124  return result;
125 }
126 
127 QAction * PopupMenu::exec(const QPoint & p, QAction * action)
128 {
129  Q_UNUSED(action);
130  d->chosenAction = NULL;
131  PopupList::exec(p);
132 
133  while (isVisible()) {
134  QApplication::processEvents();
135  }
136  return d->chosenAction;
137 }
138 
139 void PopupMenu::actionChosen(int index)
140 {
141  d->chosenAction = action(index);
142  close();
143 }
144 
145 QAction * PopupMenu::action(int index)
146 {
147  if (index < 0 || index >= d->model->m_actions.size()) {
148  return NULL;
149  }
150  return d->model->m_actions.at(index);
151 }
152 
153 } // namespace Lancelot
154 
QWidget
Lancelot::PopupList::setModel
void setModel(ActionListModel *model)
Sets the model for the popup list.
Definition: PopupList.cpp:199
QPoint
L_Override
#define L_Override
Definition: lancelot_export.h:41
Lancelot::PopupMenu::action
QAction * action(int index)
Definition: PopupMenu.cpp:145
QAction::trigger
void trigger()
Lancelot::PopupList
The list that pops up in its own window.
Definition: PopupList.h:41
StandardActionListModel.h
Lancelot::PopupList::exec
void exec(const QPoint &p)
Pops out the list.
Definition: PopupList.cpp:326
QCoreApplication::processEvents
void processEvents(QFlags< QEventLoop::ProcessEventsFlag > flags)
Lancelot::PopupMenu::~PopupMenu
virtual ~PopupMenu()
Destroys Lancelot::PopupMenu.
Definition: PopupMenu.cpp:108
Lancelot::PopupMenu::actionChosen
void actionChosen(int index)
Definition: PopupMenu.cpp:139
QString
QList< QAction * >
Lancelot::PopupMenu::addAction
QAction * addAction(const QIcon &icon, const QString &title)
Definition: PopupMenu.cpp:118
ActionListView.h
Lancelot::PopupMenu::PopupMenu
PopupMenu(QWidget *parent=0, Qt::WindowFlags f=Qt::Window)
Creates a new Lancelot::PopupMenu.
Definition: PopupMenu.cpp:98
QAction
Qt::WindowFlags
typedef WindowFlags
PopupMenu.h
Lancelot::PopupMenu::exec
QAction * exec(const QPoint &p, QAction *action=0)
Definition: PopupMenu.cpp:127
QSignalMapper
QIcon
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:43:01 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

liblancelot

Skip menu "liblancelot"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

workspace API Reference

Skip menu "workspace API Reference"
  • kdeplasma-addons
  •       GroupingDesktop
  •     liblancelot

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal