1 | /* |
2 | * Copyright 2005 by Aaron Seigo <aseigo@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU Library General Public License as |
6 | * published by the Free Software Foundation; either version 2, or |
7 | * (at your option) any later version. |
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 General Public License for more details |
13 | * |
14 | * You should have received a copy of the GNU 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 <plasma/plasma.h> |
21 | |
22 | #include <QAction> |
23 | #include <QGraphicsScene> |
24 | #include <QGraphicsView> |
25 | #include <QMenu> |
26 | |
27 | #include <plasma/containment.h> |
28 | #include <plasma/view.h> |
29 | |
30 | namespace Plasma |
31 | { |
32 | |
33 | qreal scalingFactor(ZoomLevel level) |
34 | { |
35 | switch (level) { |
36 | case DesktopZoom: |
37 | return 1; |
38 | break; |
39 | case GroupZoom: |
40 | return 0.5; |
41 | break; |
42 | case OverviewZoom: |
43 | return 0.2; |
44 | break; |
45 | } |
46 | |
47 | // to make odd compilers not warn like silly beasts |
48 | return 1; |
49 | } |
50 | |
51 | Direction locationToDirection(Location location) |
52 | { |
53 | switch (location) { |
54 | case Floating: |
55 | case Desktop: |
56 | case TopEdge: |
57 | case FullScreen: |
58 | //TODO: should we be smarter for floating and planer? |
59 | // perhaps we should take a QRect and/or QPos as well? |
60 | return Down; |
61 | case BottomEdge: |
62 | return Up; |
63 | case LeftEdge: |
64 | return Right; |
65 | case RightEdge: |
66 | return Left; |
67 | } |
68 | |
69 | return Down; |
70 | } |
71 | |
72 | Direction locationToInverseDirection(Location location) |
73 | { |
74 | switch (location) { |
75 | case Floating: |
76 | case Desktop: |
77 | case TopEdge: |
78 | case FullScreen: |
79 | //TODO: should we be smarter for floating and planer? |
80 | // perhaps we should take a QRect and/or QPos as well? |
81 | return Up; |
82 | case BottomEdge: |
83 | return Down; |
84 | case LeftEdge: |
85 | return Left; |
86 | case RightEdge: |
87 | return Right; |
88 | } |
89 | |
90 | return Up; |
91 | } |
92 | |
93 | QGraphicsView *viewFor(const QGraphicsItem *item) |
94 | { |
95 | if (!item || !item->scene()) { |
96 | return 0; |
97 | } |
98 | |
99 | QGraphicsView *found = 0; |
100 | foreach (QGraphicsView *view, item->scene()->views()) { |
101 | if (view->sceneRect().intersects(item->sceneBoundingRect()) || |
102 | view->sceneRect().contains(item->scenePos())) { |
103 | if (!found || view->isActiveWindow()) { |
104 | found = view; |
105 | } |
106 | } |
107 | } |
108 | |
109 | return found; |
110 | } |
111 | |
112 | QList<QAction*> (QMenu *, const QString &prefix, QObject *parent) |
113 | { |
114 | Q_ASSERT(menu); |
115 | |
116 | QList<QAction*> ret; |
117 | foreach (QAction *action, menu->actions()) { |
118 | if (QMenu * = action->menu()) { |
119 | //Flatten hierarchy and prefix submenu text to all actions in submenu |
120 | ret << actionsFromMenu(submenu, action->text(), parent); |
121 | } else if (!action->isSeparator() && action->isEnabled()) { |
122 | QString text = action->text(); |
123 | if (action->isCheckable()) { |
124 | if (action->isChecked()) { |
125 | text = QString("(%1) %2" ).arg(QChar(0x2613)).arg(text); |
126 | } else { |
127 | text = QString("( ) %1" ).arg(text); |
128 | } |
129 | } |
130 | |
131 | if (!prefix.isEmpty()) { |
132 | text = QString("%1: %2" ).arg(prefix).arg(text); |
133 | } |
134 | text = text.replace(QRegExp("&([\\S])" ), "\\1" ); |
135 | |
136 | QAction *a = new QAction(action->icon(), text, parent); |
137 | |
138 | QObject::connect(a, SIGNAL(triggered(bool)), action, SIGNAL(triggered(bool))); |
139 | ret << a; |
140 | } |
141 | } |
142 | return ret; |
143 | } |
144 | |
145 | } // Plasma namespace |
146 | |