dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
command.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 <QtCore/QDebug>
29 #include <QAction>
30 #include <QShortcut>
31 
32 #include "command_p.h"
33 
186 using namespace Core::Internal;
187 
194  : m_attributes(0)
195  , m_id(id)
196 {
197 }
198 
199 void CommandPrivate::setDefaultKeySequence(const QKeySequence &key)
200 {
201  m_defaultKey = key;
202 }
203 
205 {
206  return m_defaultKey;
207 }
208 
209 void CommandPrivate::setDefaultText(const QString &text)
210 {
211  m_defaultText = text;
212 }
213 
215 {
216  return m_defaultText;
217 }
218 
220 {
221  return m_id;
222 }
223 
224 QAction *CommandPrivate::action() const
225 {
226  return nullptr;
227 }
228 
229 QShortcut *CommandPrivate::shortcut() const
230 {
231  return nullptr;
232 }
233 
235 {
236  m_attributes |= attr;
237 }
238 
240 {
241  m_attributes &= ~attr;
242 }
243 
245 {
246  return (m_attributes & attr);
247 }
248 
249 QString CommandPrivate::stringWithAppendedShortcut(const QString &str) const
250 {
251  return QString("%1 <span style=\"color: gray; font-size: small\">%2</span>")
252  .arg(str)
253  .arg(keySequence().toString(QKeySequence::NativeText));
254 }
255 
256 // ---------- Shortcut ------------
257 
264  : CommandPrivate(id)
265  , m_shortcut(nullptr)
266 {
267 }
268 
269 QString Shortcut::name() const
270 {
271  if (!m_shortcut)
272  return QString();
273 
274  return m_shortcut->whatsThis();
275 }
276 
277 void Shortcut::setShortcut(QShortcut *shortcut)
278 {
279  m_shortcut = shortcut;
280 }
281 
282 QShortcut *Shortcut::shortcut() const
283 {
284  return m_shortcut;
285 }
286 
287 void Shortcut::setContext(const QList<int> &context)
288 {
289  m_context = context;
290 }
291 
293 {
294  return m_context;
295 }
296 
297 void Shortcut::setDefaultKeySequence(const QKeySequence &key)
298 {
299  if (m_shortcut->key().isEmpty())
300  setKeySequence(key);
302 }
303 
304 void Shortcut::setKeySequence(const QKeySequence &key)
305 {
306  m_shortcut->setKey(key);
307  emit keySequenceChanged();
308 }
309 
310 QKeySequence Shortcut::keySequence() const
311 {
312  return m_shortcut->key();
313 }
314 
315 void Shortcut::setDefaultText(const QString &text)
316 {
317  m_defaultText = text;
318 }
319 
320 QString Shortcut::defaultText() const
321 {
322  return m_defaultText;
323 }
324 
326 {
327  foreach (int ctxt, m_context) {
328  if (context.contains(ctxt)) {
329  m_shortcut->setEnabled(true);
330  return true;
331  }
332  }
333  m_shortcut->setEnabled(false);
334  return false;
335 }
336 
337 bool Shortcut::isActive() const
338 {
339  return m_shortcut->isEnabled();
340 }
341 
342 // ---------- Action ------------
343 
349  : CommandPrivate(id)
350  , m_action(nullptr)
351 {
352 }
353 
354 QString Action::name() const
355 {
356  if (!m_action)
357  return QString();
358 
359  return m_action->text();
360 }
361 
362 void Action::setAction(QAction *action)
363 {
364  m_action = action;
365  if (m_action) {
366  m_action->setParent(this);
367  m_toolTip = m_action->toolTip();
368  }
369 }
370 
371 QAction *Action::action() const
372 {
373  return m_action;
374 }
375 
377 {
379 }
380 
382 {
383  return m_locations;
384 }
385 
386 void Action::setDefaultKeySequence(const QKeySequence &key)
387 {
388  if (m_action->shortcut().isEmpty())
389  setKeySequence(key);
391 }
392 
393 void Action::setKeySequence(const QKeySequence &key)
394 {
395  m_action->setShortcut(key);
397  emit keySequenceChanged();
398 }
399 
401 {
402  if (m_action->shortcut().isEmpty())
403  m_action->setToolTip(m_toolTip);
404  else
406 }
407 
408 QKeySequence Action::keySequence() const
409 {
410  return m_action->shortcut();
411 }
412 
413 // ---------- OverrideableAction ------------
414 
421  : Action(id)
422  , m_currentAction(nullptr)
423  , m_active(false)
424  , m_contextInitialized(false)
425 {
426 }
427 
428 void OverrideableAction::setAction(QAction *action)
429 {
430  Action::setAction(action);
431 }
432 
434 {
435  m_context = context;
436 
437  QAction *oldAction = m_currentAction;
438  m_currentAction = nullptr;
439  for (int i = 0; i < m_context.size(); ++i) {
440  if (QAction *a = m_contextActionMap.value(m_context.at(i), nullptr)) {
441  m_currentAction = a;
442  break;
443  }
444  }
445 
446  if (m_currentAction == oldAction && m_contextInitialized)
447  return true;
448  m_contextInitialized = true;
449 
450  if (oldAction) {
451  disconnect(oldAction, SIGNAL(changed()), this, SLOT(actionChanged()));
452  disconnect(m_action, SIGNAL(triggered(bool)), oldAction, SIGNAL(triggered(bool)));
453  disconnect(m_action, SIGNAL(toggled(bool)), oldAction, SLOT(setChecked(bool)));
454  }
455  if (m_currentAction) {
456  connect(m_currentAction, SIGNAL(changed()), this, SLOT(actionChanged()));
457  // we want to avoid the toggling semantic on slot trigger(), so we just connect the signals
458  connect(m_action, SIGNAL(triggered(bool)), m_currentAction, SIGNAL(triggered(bool)));
459  // we need to update the checked state, so we connect to setChecked slot, which also fires a
460  // toggled signal
461  connect(m_action, SIGNAL(toggled(bool)), m_currentAction, SLOT(setChecked(bool)));
462  actionChanged();
463  m_active = true;
464  return true;
465  }
466  if (hasAttribute(CA_Hide))
467  m_action->setVisible(false);
468  m_action->setEnabled(false);
469  m_active = false;
470  return false;
471 }
472 
473 void OverrideableAction::addOverrideAction(QAction *action, const QList<int> &context)
474 {
475  if (context.isEmpty()) {
476  m_contextActionMap.insert(0, action);
477  } else {
478  for (int i = 0; i < context.size(); ++i) {
479  int k = context.at(i);
480  if (m_contextActionMap.contains(k))
481  qWarning() << QString("addOverrideAction: action already registered for context "
482  "when registering '%1'")
483  .arg(action->text());
484  m_contextActionMap.insert(k, action);
485  }
486  }
487 }
488 
489 void OverrideableAction::actionChanged()
490 {
492  m_action->setIcon(m_currentAction->icon());
493  m_action->setIconText(m_currentAction->iconText());
494  }
496  m_action->setText(m_currentAction->text());
497  m_toolTip = m_currentAction->toolTip();
499  m_action->setStatusTip(m_currentAction->statusTip());
500  m_action->setWhatsThis(m_currentAction->whatsThis());
501  }
502 
503  m_action->setCheckable(m_currentAction->isCheckable());
504  bool block = m_action->blockSignals(true);
505  m_action->setChecked(m_currentAction->isChecked());
506  m_action->blockSignals(block);
507 
508  m_action->setEnabled(m_currentAction->isEnabled());
509  m_action->setVisible(m_currentAction->isVisible());
510 }
511 
513 {
514  return m_active;
515 }
void addOverrideAction(QAction *action, const QList< int > &context)
Definition: command.cpp:473
void setKeySequence(const QKeySequence &key)
Definition: command.cpp:304
QKeySequence keySequence() const
Definition: command.cpp:310
void keySequenceChanged()
bool setCurrentContext(const QList< int > &context)
Definition: command.cpp:325
bool isActive() const
Definition: command.cpp:337
QKeySequence keySequence() const
Definition: command.cpp:408
for i
Definition: OPPlots.m:140
virtual QKeySequence keySequence() const =0
QString name() const
Definition: command.cpp:269
void setAction(QAction *action)
Definition: command.cpp:428
void setKeySequence(const QKeySequence &key)
Definition: command.cpp:393
void setContext(const QList< int > &context)
Definition: command.cpp:287
void setDefaultKeySequence(const QKeySequence &key)
Definition: command.cpp:386
bool setCurrentContext(const QList< int > &context)
Definition: command.cpp:433
QString stringWithAppendedShortcut(const QString &str) const
Definition: command.cpp:249
end a
Definition: OPPlots.m:98
QList< CommandLocation > locations() const
Definition: command.cpp:381
void setLocations(const QList< CommandLocation > &locations)
Definition: command.cpp:376
virtual void setAction(QAction *action)
Definition: command.cpp:362
void updateToolTipWithKeySequence()
Definition: command.cpp:400
virtual void setDefaultText(const QString &key)
Definition: command.cpp:315
QList< int > context() const
Definition: command.cpp:292
QShortcut * shortcut() const
Definition: command.cpp:282
void setDefaultKeySequence(const QKeySequence &key)
Definition: command.cpp:297
void setDefaultText(const QString &text)
Definition: command.cpp:209
virtual QString defaultText() const
Definition: command.cpp:320
QKeySequence defaultKeySequence() const
Definition: command.cpp:204
bool hasAttribute(CommandAttribute attr) const
Definition: command.cpp:244
QAction * action() const
Definition: command.cpp:224
void setAttribute(CommandAttribute attr)
Definition: command.cpp:234
QString defaultText() const
Definition: command.cpp:214
QAction * action() const
Definition: command.cpp:371
QString name() const
Definition: command.cpp:354
void setDefaultKeySequence(const QKeySequence &key)
Definition: command.cpp:199
void removeAttribute(CommandAttribute attr)
Definition: command.cpp:239
QList< CommandLocation > m_locations
Definition: command_p.h:130
void setShortcut(QShortcut *shortcut)
Definition: command.cpp:277
QShortcut * shortcut() const
Definition: command.cpp:229