23 #include <QKeySequence> 24 #include <QMessageBox> 27 #include <QDomDocument> 28 #include <QFileDialog> 29 #include <QTextStream> 35 , mGettingShortcut( false )
44 connect( btnChangeShortcut, SIGNAL( clicked() ),
this, SLOT( changeShortcut() ) );
45 connect( btnResetShortcut, SIGNAL( clicked() ),
this, SLOT( resetShortcut() ) );
46 connect( btnSetNoShortcut, SIGNAL( clicked() ),
this, SLOT( setNoShortcut() ) );
47 connect( btnSaveShortcuts, SIGNAL( clicked() ),
this, SLOT( saveShortcuts() ) );
48 connect( btnLoadShortcuts, SIGNAL( clicked() ),
this, SLOT( loadShortcuts() ) );
66 void QgsConfigureShortcutsDialog::saveState()
75 void QgsConfigureShortcutsDialog::restoreState()
81 void QgsConfigureShortcutsDialog::populateActions()
87 Q_FOREACH (
QObject* obj, objects )
93 if (
QAction* action = qobject_cast< QAction* >( obj ) )
95 actionText = action->text();
97 sequence = action->shortcut().toString();
98 icon = action->icon();
100 else if (
QShortcut* shortcut = qobject_cast< QShortcut* >( obj ) )
102 actionText = shortcut->whatsThis();
103 sequence = shortcut->key().toString();
111 lst << actionText << sequence;
114 item->
setData( 0, Qt::UserRole, qVariantFromValue( obj ) );
118 treeActions->addTopLevelItems( items );
121 treeActions->resizeColumnToContents( 0 );
122 treeActions->sortItems( 0, Qt::AscendingOrder );
124 actionChanged( treeActions->currentItem(), nullptr );
127 void QgsConfigureShortcutsDialog::saveShortcuts()
130 tr(
"XML file" ) +
" (*.xml);;" +
tr(
"All files" ) +
" (*)" );
136 if ( !fileName.
endsWith(
".xml", Qt::CaseInsensitive ) )
141 QFile file( fileName );
142 if ( !file.
open( QIODevice::WriteOnly | QIODevice::Text ) )
145 tr(
"Cannot write file %1:\n%2." )
165 for (
int i = 0; i < keys.
count(); ++i )
167 actionText = keys[ i ];
168 actionShortcut = settings.
value( actionText,
"" ).
toString();
180 void QgsConfigureShortcutsDialog::loadShortcuts()
183 tr(
"XML file" ) +
" (*.xml);;" +
tr(
"All files" ) +
" (*)" );
190 QFile file( fileName );
191 if ( !file.
open( QIODevice::ReadOnly | QIODevice::Text ) )
194 tr(
"Cannot read file %1:\n%2." )
205 if ( !doc.
setContent( &file,
true, &errorStr, &errorLine, &errorColumn ) )
208 tr(
"Parse error at line %1, column %2:\n%3" )
216 if ( root.
tagName() !=
"qgsshortcuts" )
219 tr(
"The file is not an shortcuts exchange file." ) );
226 bool localeOverrideFlag = settings.
value(
"locale/overrideFlag",
false ).
toBool();
227 if ( localeOverrideFlag )
229 currentLocale = settings.
value(
"locale/userLocale",
"en_US" ).
toString();
236 if ( root.
attribute(
"locale" ) != currentLocale )
239 tr(
"The file contains shortcuts created with different locale, so you can't use it." ) );
250 actionShortcut = child.
attribute(
"shortcut" );
256 treeActions->
clear();
260 void QgsConfigureShortcutsDialog::changeShortcut()
263 setGettingShortcut(
true );
266 void QgsConfigureShortcutsDialog::resetShortcut()
268 QObject*
object = currentObject();
270 setCurrentActionShortcut( sequence );
273 void QgsConfigureShortcutsDialog::setNoShortcut()
278 QAction* QgsConfigureShortcutsDialog::currentAction()
280 return qobject_cast<
QAction*>( currentObject() );
283 QShortcut* QgsConfigureShortcutsDialog::currentShortcut()
285 return qobject_cast<
QShortcut*>( currentObject() );
291 Q_UNUSED( previous );
293 setGettingShortcut(
false );
297 if (
QAction* action = currentAction() )
301 sequence = action->shortcut();
303 else if (
QShortcut*
object = currentShortcut() )
307 sequence =
object->key();
315 shortcut =
tr(
"None" );
316 btnResetShortcut->setText(
tr(
"Set default (%1)" ).arg( shortcut ) );
319 btnSetNoShortcut->setEnabled( !sequence.
isEmpty() );
321 btnResetShortcut->setEnabled( sequence !=
QKeySequence( shortcut ) );
326 if ( !mGettingShortcut )
332 int key =
event->key();
337 mModifiers |= Qt::META;
338 updateShortcutText();
341 mModifiers |= Qt::ALT;
342 updateShortcutText();
344 case Qt::Key_Control:
345 mModifiers |= Qt::CTRL;
346 updateShortcutText();
349 mModifiers |= Qt::SHIFT;
350 updateShortcutText();
355 setGettingShortcut(
false );
360 updateShortcutText();
366 if ( !mGettingShortcut )
372 int key =
event->key();
377 mModifiers &= ~
Qt::META;
378 updateShortcutText();
381 mModifiers &= ~
Qt::ALT;
382 updateShortcutText();
384 case Qt::Key_Control:
385 mModifiers &= ~
Qt::CTRL;
386 updateShortcutText();
389 mModifiers &= ~
Qt::SHIFT;
390 updateShortcutText();
399 setCurrentActionShortcut(
QKeySequence( mModifiers + mKey ) );
400 setGettingShortcut(
false );
405 QObject* QgsConfigureShortcutsDialog::currentObject()
407 if ( !treeActions->currentItem() )
410 QObject*
object = treeActions->currentItem()->data( 0, Qt::UserRole ).value<
QObject*>();
414 void QgsConfigureShortcutsDialog::updateShortcutText()
418 btnChangeShortcut->setText(
tr(
"Input: " ) + s.
toString() );
421 void QgsConfigureShortcutsDialog::setGettingShortcut(
bool getting )
425 mGettingShortcut = getting;
428 btnChangeShortcut->setChecked(
false );
429 btnChangeShortcut->setText(
tr(
"Change" ) );
433 updateShortcutText();
437 void QgsConfigureShortcutsDialog::setCurrentActionShortcut(
const QKeySequence& s )
439 QObject*
object = currentObject();
445 if ( otherObject ==
object )
451 if (
QAction* otherAction = qobject_cast< QAction* >( otherObject ) )
453 otherText = otherAction->text();
456 else if (
QShortcut* otherShortcut = qobject_cast< QShortcut* >( otherObject ) )
458 otherText = otherShortcut->whatsThis();
462 tr(
"This shortcut is already assigned to action %1. Reassign?" ).arg( otherText ),
463 QMessageBox::Yes | QMessageBox::No );
465 if ( res != QMessageBox::Yes )
472 items[0]->setText( 1,
QString() );
479 treeActions->currentItem()->setText( 1, s.
toString() );
481 actionChanged( treeActions->currentItem(), nullptr );
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QString objectDefaultKeySequence(QObject *object) const
Returns the default sequence for an object (either a QAction or QShortcut).
QByteArray toByteArray() const
virtual bool event(QEvent *e)
QList< QObject * > listAll() const
Returns a list of both actions and shortcuts in the manager.
QDomNode appendChild(const QDomNode &newChild)
QStringList childKeys() const
QString attribute(const QString &name, const QString &defValue) const
QString errorString() const
QDomElement nextSiblingElement(const QString &tagName) const
Shortcuts manager is a class that contains a list of QActions and QShortcuts that have been registere...
QDomElement documentElement() const
bool setObjectKeySequence(QObject *object, const QString &sequence)
Modifies an object's (either a QAction or a QShortcut) key sequence.
QString & remove(int position, int n)
QString tr(const char *sourceText, const char *disambiguation, int n)
static QgsShortcutsManager * instance()
Return the singleton instance of the manager.
void setValue(const QString &key, const QVariant &value)
int count(const T &value) const
void append(const T &value)
bool setKeySequence(const QString &name, const QString &sequence)
Modifies an action or shortcut's key sequence.
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void setAttribute(const QString &name, const QString &value)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QString settingsPath() const
Returns the root settings path used to store shortcut customisation.
QString defaultKeySequence(QAction *action) const
Returns the default sequence for an action.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual void keyPressEvent(QKeyEvent *e)
QVariant value(const QString &key, const QVariant &defaultValue) const
void save(QTextStream &str, int indent) const
QString toString(SequenceFormat format) const
QDomElement firstChildElement(const QString &tagName) const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
QDomElement createElement(const QString &tagName)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void beginGroup(const QString &prefix)
QObject * objectForSequence(const QKeySequence &sequence) const
Returns the object (QAction or QShortcut) matching the specified key sequence,.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)