Diffstat (limited to 'noncore/applets/keyhelper/keyhelperapplet/extension/MenuLauncher.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/applets/keyhelper/keyhelperapplet/extension/MenuLauncher.cpp | 324 |
1 files changed, 324 insertions, 0 deletions
diff --git a/noncore/applets/keyhelper/keyhelperapplet/extension/MenuLauncher.cpp b/noncore/applets/keyhelper/keyhelperapplet/extension/MenuLauncher.cpp new file mode 100644 index 0000000..0595a3e --- a/dev/null +++ b/noncore/applets/keyhelper/keyhelperapplet/extension/MenuLauncher.cpp | |||
@@ -0,0 +1,324 @@ | |||
1 | #include "MenuLauncher.h" | ||
2 | extern QWidget* g_Widget; | ||
3 | |||
4 | MenuLauncher::MenuLauncher(const QString& kind) : m_kind(kind) | ||
5 | { | ||
6 | qDebug("MenuLauncher::MenuLauncher()"); | ||
7 | m_pMenu = m_pTopMenu = NULL; | ||
8 | |||
9 | m_isShowing = false; | ||
10 | m_id = -1; | ||
11 | |||
12 | m_pTimer = new QTimer(this); | ||
13 | connect(m_pTimer, SIGNAL(timeout()), | ||
14 | this, SLOT(select())); | ||
15 | |||
16 | init(); | ||
17 | } | ||
18 | |||
19 | MenuLauncher::~MenuLauncher() | ||
20 | { | ||
21 | qDebug("MenuLauncher::~MenuLauncher()"); | ||
22 | delete m_pTopMenu; | ||
23 | delete m_pTimer; | ||
24 | } | ||
25 | |||
26 | void MenuLauncher::init() | ||
27 | { | ||
28 | buildMenu(); | ||
29 | } | ||
30 | |||
31 | QPopupMenu* MenuLauncher::initMenu(QWidget* parent, const QString& name) | ||
32 | { | ||
33 | QPopupMenu* pMenu; | ||
34 | pMenu = new QPopupMenuEx(parent, name); | ||
35 | pMenu->installEventFilter(this); | ||
36 | connect(pMenu, SIGNAL(activated(int)), this, SLOT(select(int))); | ||
37 | connect(pMenu, SIGNAL(highlighted(int)), this, SLOT(highlight(int))); | ||
38 | //connect(pMenu, SIGNAL(aboutToHide()), this, SLOT(execute())); | ||
39 | return(pMenu); | ||
40 | } | ||
41 | |||
42 | bool MenuLauncher::onKeyPress(int /*keycode*/) | ||
43 | { | ||
44 | if(m_isShowing){ | ||
45 | qDebug("showing ..."); | ||
46 | } else if(m_pMenu->isVisible()){ | ||
47 | next(); | ||
48 | } else { | ||
49 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
50 | cfg.setGroup("Global"); | ||
51 | int delay = cfg.readNumEntry("DelayPopup", 5); | ||
52 | QTimer::singleShot(delay, this, SLOT(show())); | ||
53 | m_isShowing = true; | ||
54 | } | ||
55 | return true; | ||
56 | } | ||
57 | |||
58 | bool MenuLauncher::onModRelease(int /*modcode*/) | ||
59 | { | ||
60 | if(m_pMenu->isVisible()){ | ||
61 | QTimer::singleShot(0, this, SLOT(select())); | ||
62 | return(true); | ||
63 | } else { | ||
64 | return(false); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | QString MenuLauncher::getMenuText(const QString& key, const QString& name) | ||
69 | { | ||
70 | QRegExp rx("^[0-9]+_"); | ||
71 | QString text; | ||
72 | QString ackey; | ||
73 | int len; | ||
74 | if(rx.match(key, 0, &len) == 0){ | ||
75 | ackey = key.mid(len); | ||
76 | } else { | ||
77 | ackey = key; | ||
78 | } | ||
79 | if(ackey.length() == 1){ | ||
80 | text = name; | ||
81 | text.append("(&"); | ||
82 | text.append(ackey); | ||
83 | text.append(")"); | ||
84 | } else { | ||
85 | text = ackey; | ||
86 | } | ||
87 | return(text); | ||
88 | } | ||
89 | |||
90 | int MenuLauncher::buildMenu(const QString& section, | ||
91 | QPopupMenu* pMenu, int& id) | ||
92 | { | ||
93 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
94 | |||
95 | if(m_oMenuList.contains(pMenu)){ | ||
96 | /* ̵¸Â¥ë¡¼¥×ËÉ»ß */ | ||
97 | return(0); | ||
98 | } | ||
99 | m_oMenuList.append(pMenu); | ||
100 | |||
101 | QString oldgroup = cfg.getGroup(); | ||
102 | |||
103 | QString group = section; | ||
104 | group[0] = group[0].upper(); | ||
105 | |||
106 | cfg.setGroup(group); | ||
107 | |||
108 | QStringList apps = cfg.getKeys(); | ||
109 | int cnt = 0; | ||
110 | if(apps.isEmpty() == false){ | ||
111 | for(QStringList::Iterator it=apps.begin(); | ||
112 | it!=apps.end(); ++it){ | ||
113 | QStringList args = cfg.readListEntry(*it, '\t'); | ||
114 | LnkWrapper lnk(args); | ||
115 | if(lnk.isValid()){ | ||
116 | cnt++; | ||
117 | QString text = getMenuText(*it, lnk.instance().name()); | ||
118 | if(args[0] == "@menu"){ | ||
119 | QPopupMenu* pSubMenu = initMenu(m_pTopMenu, args[1]); | ||
120 | pMenu->insertItem(lnk.instance().pixmap(), text, | ||
121 | pSubMenu, id); | ||
122 | m_oItemList.append(ItemInfo(section)); | ||
123 | id++; | ||
124 | buildMenu(args[1], pSubMenu, id); | ||
125 | } else { | ||
126 | pMenu->insertItem(lnk.instance().pixmap(), text, id); | ||
127 | m_oItemList.append(ItemInfo(section, *it)); | ||
128 | id++; | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | cfg.setGroup(oldgroup); | ||
134 | return(cnt); | ||
135 | } | ||
136 | |||
137 | void MenuLauncher::clearSubMenu() | ||
138 | { | ||
139 | for(QValueList<QPopupMenu*>::Iterator it=m_oMenuList.begin(); | ||
140 | it!=m_oMenuList.end(); ++it){ | ||
141 | if(*it != m_pTopMenu){ | ||
142 | delete *it; | ||
143 | } | ||
144 | } | ||
145 | m_oMenuList.clear(); | ||
146 | m_oItemList.clear(); | ||
147 | } | ||
148 | |||
149 | int MenuLauncher::buildMenu(bool force) | ||
150 | { | ||
151 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
152 | if(!force && m_lastmodify == cfg.lastRead()){ | ||
153 | return(m_pTopMenu->count()); | ||
154 | } | ||
155 | qDebug("buildMenu"); | ||
156 | |||
157 | QString oldgroup = cfg.getGroup(); | ||
158 | |||
159 | cfg.setGroup("Global"); | ||
160 | m_submenuTimeout = cfg.readNumEntry("SubMenuTimeout", 500); | ||
161 | |||
162 | if(m_pTopMenu){ | ||
163 | delete m_pTopMenu; | ||
164 | } | ||
165 | m_pMenu = m_pTopMenu = initMenu(g_Widget, kind()); | ||
166 | m_oLastId.clear(); | ||
167 | m_oMenuList.clear(); | ||
168 | m_oItemList.clear(); | ||
169 | |||
170 | MenuTitle* pTitle = new MenuTitle("MenuLauncher", | ||
171 | m_pTopMenu->font(), kind()); | ||
172 | m_pTopMenu->insertItem(pTitle); | ||
173 | |||
174 | int id = 0; | ||
175 | int cnt = buildMenu(kind(), m_pTopMenu, id); | ||
176 | if(cnt > 0){ | ||
177 | m_lastmodify = cfg.lastRead(); | ||
178 | } | ||
179 | |||
180 | cfg.setGroup(oldgroup); | ||
181 | return(cnt); | ||
182 | } | ||
183 | |||
184 | void MenuLauncher::show() | ||
185 | { | ||
186 | m_enablePopup = false; | ||
187 | int cnt = buildMenu(); | ||
188 | if(cnt > 0){ | ||
189 | m_pMenu = m_pTopMenu; | ||
190 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
191 | cfg.setGroup("Style"); | ||
192 | int x,y; | ||
193 | QString key = "Position_" + kind(); | ||
194 | if(cfg.hasKey(key)){ | ||
195 | const QStringList& list = cfg.readListEntry(key, ','); | ||
196 | x = list[0].toInt(); | ||
197 | y = list[1].toInt(); | ||
198 | } else { | ||
199 | x = (qt_screen->width() - m_pTopMenu->sizeHint().width()) / 2; | ||
200 | y = (qt_screen->height() - m_pTopMenu->sizeHint().height()) / 2; | ||
201 | } | ||
202 | QPoint pos(x, y); | ||
203 | m_pTopMenu->popup(pos); | ||
204 | m_pTopMenu->setActiveItem(1); | ||
205 | } | ||
206 | m_isShowing = false; | ||
207 | } | ||
208 | |||
209 | void MenuLauncher::next() | ||
210 | { | ||
211 | int index = m_pMenu->indexOf(m_id); | ||
212 | index++; | ||
213 | if(index >= (signed int)m_pMenu->count()){ | ||
214 | if(m_pMenu == m_pTopMenu){ | ||
215 | index = 1; | ||
216 | } else { | ||
217 | index = 0; | ||
218 | } | ||
219 | } | ||
220 | m_pMenu->setActiveItem(index); | ||
221 | m_id = m_pMenu->idAt(index); | ||
222 | } | ||
223 | |||
224 | void MenuLauncher::select() | ||
225 | { | ||
226 | if(m_pMenu->isVisible()){ | ||
227 | QMenuItem* item = m_pMenu->findItem(m_id); | ||
228 | int index = m_pMenu->indexOf(m_id); | ||
229 | QPopupMenu* p = m_pMenu; | ||
230 | //m_pMenu->activateItemAt(index); | ||
231 | if(item && item->popup()){ | ||
232 | m_pMenu = item->popup(); | ||
233 | } | ||
234 | p->activateItemAt(index); | ||
235 | } | ||
236 | } | ||
237 | |||
238 | void MenuLauncher::select(int id) | ||
239 | { | ||
240 | if(id >= 0 && m_oItemList[id].entry != QString::null){ | ||
241 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
242 | |||
243 | cfg.setGroup("Global"); | ||
244 | int delay = cfg.readNumEntry("DelayExec", 100); | ||
245 | |||
246 | QString group = m_oItemList[id].group; | ||
247 | group[0] = group[0].upper(); | ||
248 | cfg.setGroup(group); | ||
249 | |||
250 | //QStringList args = cfg.readListEntry(m_oItemList[id].entry, '\t'); | ||
251 | m_args = cfg.readListEntry(m_oItemList[id].entry, '\t'); | ||
252 | |||
253 | #if 0 | ||
254 | LnkWrapper lnk(args); | ||
255 | if(lnk.isValid()){ | ||
256 | lnk.instance().execute(); | ||
257 | } | ||
258 | #else | ||
259 | QTimer::singleShot(delay, this, SLOT(execute())); | ||
260 | #endif | ||
261 | } | ||
262 | m_pMenu = m_pTopMenu; | ||
263 | m_id = -1; | ||
264 | } | ||
265 | |||
266 | void MenuLauncher::execute() | ||
267 | { | ||
268 | LnkWrapper lnk(m_args); | ||
269 | if(lnk.isValid()){ | ||
270 | lnk.instance().execute(); | ||
271 | } | ||
272 | m_args.clear(); | ||
273 | } | ||
274 | |||
275 | void MenuLauncher::highlight(int id) | ||
276 | { | ||
277 | if(m_pMenu && m_pMenu->isVisible()){ | ||
278 | m_id = id; | ||
279 | if(m_enablePopup){ | ||
280 | QMenuItem* item = m_pMenu->findItem(m_id); | ||
281 | if(item && item->popup()){ | ||
282 | if(m_submenuTimeout > 0){ | ||
283 | m_pTimer->start(m_submenuTimeout, true); | ||
284 | } | ||
285 | } else { | ||
286 | m_pTimer->stop(); | ||
287 | } | ||
288 | } else { | ||
289 | /* ¥á¥Ë¥å¡¼É½¼¨Ä¾¸å¤Ï¥Ý¥Ã¥×¥¢¥Ã¥×¤·¤Ê¤¤ */ | ||
290 | m_enablePopup = true; | ||
291 | } | ||
292 | } | ||
293 | } | ||
294 | |||
295 | bool MenuLauncher::eventFilter(QObject* o, QEvent* e) | ||
296 | { | ||
297 | if(m_pTopMenu->isVisible()){ | ||
298 | QKeyEvent* ke = (QKeyEvent*)e; | ||
299 | switch(e->type()){ | ||
300 | case QEvent::Accel: | ||
301 | if(ke->key() == Qt::Key_Space | ||
302 | && ke->isAutoRepeat() == false){ | ||
303 | select(); | ||
304 | } | ||
305 | break; | ||
306 | case QEvent::FocusIn: | ||
307 | //qDebug("FocusIn[%p][%p]", o, m_pMenu); | ||
308 | m_pMenu = (QPopupMenu*)o; | ||
309 | if(m_oLastId.contains(o)){ | ||
310 | m_id = m_oLastId[o]; | ||
311 | } | ||
312 | m_pMenu->updateItem(m_id); | ||
313 | break; | ||
314 | case QEvent::FocusOut: | ||
315 | //qDebug("FocusOut[%p][%p]", o, m_pMenu); | ||
316 | m_oLastId[o] = m_id; | ||
317 | break; | ||
318 | default: | ||
319 | //qDebug(">>>>> [%p][%d] <<<<<", o, e->type()); | ||
320 | break; | ||
321 | } | ||
322 | } | ||
323 | return QObject::eventFilter(o, e); | ||
324 | } | ||