Skip to content
Permalink
Browse files
add ability to export shortcuts to PDF
  • Loading branch information
alexbruy committed Sep 5, 2021
1 parent fa5f088 commit 33949704f47dbeb291b7335790f998f11b1b64b9
Showing with 124 additions and 0 deletions.
  1. +122 −0 src/gui/qgsconfigureshortcutsdialog.cpp
  2. +2 −0 src/gui/qgsconfigureshortcutsdialog.h
@@ -30,6 +30,13 @@
#include <QTextStream>
#include <QMenu>
#include <QAction>
#include <QPrinter>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextTable>
#include <QTextTableFormat>
#include <QTextTableCellFormat>
#include <QTextCharFormat>

QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget *parent, QgsShortcutsManager *manager )
: QDialog( parent )
@@ -47,6 +54,10 @@ QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget *parent, QgsSh
mSaveMenu->addAction( mSaveAllShortcuts );
connect( mSaveAllShortcuts, &QAction::triggered, this, [this] { saveShortcuts(); } );

mSaveAsPdf = new QAction( tr( "Save as PDF…" ), this );
mSaveMenu->addAction( mSaveAsPdf );
connect( mSaveAsPdf, &QAction::triggered, this, &QgsConfigureShortcutsDialog::saveShortcutsPdf );

btnSaveShortcuts->setMenu( mSaveMenu );

connect( mLeFilter, &QgsFilterLineEdit::textChanged, this, &QgsConfigureShortcutsDialog::mLeFilter_textChanged );
@@ -519,3 +530,114 @@ void QgsConfigureShortcutsDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "introduction/qgis_configuration.html#keyboard-shortcuts" ) );
}

void QgsConfigureShortcutsDialog::saveShortcutsPdf()
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save Shortcuts" ), QDir::homePath(),
tr( "PDF file" ) + " (*.pdf);;" + tr( "All files" ) + " (*)" );

if ( fileName.isEmpty() )
return;

if ( !fileName.endsWith( QLatin1String( ".pdf" ), Qt::CaseInsensitive ) )
{
fileName += QLatin1String( ".pdf" );
}

QTextDocument *document = new QTextDocument;
QTextCursor cursor( document );

QTextTableFormat tableFormat;
tableFormat.setBorder( 0 );
tableFormat.setCellSpacing( 0 );
tableFormat.setCellPadding( 4 );
tableFormat.setHeaderRowCount( 1 );

QVector<QTextLength> constraints;
constraints << QTextLength( QTextLength::PercentageLength, 2 );
constraints << QTextLength( QTextLength::PercentageLength, 80 );
constraints << QTextLength( QTextLength::PercentageLength, 18 );
tableFormat.setColumnWidthConstraints( constraints );

QTextTableCellFormat headerFormat;
headerFormat.setFontWeight( QFont::Bold );
headerFormat.setBottomPadding( 4 );

QTextCharFormat rowFormat;
rowFormat.setVerticalAlignment( QTextCharFormat::AlignMiddle );

QTextCharFormat altRowFormat;
altRowFormat.setBackground( QBrush( QColor( 238, 238, 236 ) ) );
altRowFormat.setVerticalAlignment( QTextCharFormat::AlignMiddle );

int row = 0;
QTextTable *table = cursor.insertTable( 1, 3, tableFormat );
table->mergeCells( 0, 0, 1, 2 );
QTextCursor c = table->cellAt( row, 0 ).firstCursorPosition();
c.setCharFormat( headerFormat );
c.insertText( tr( "Action" ) );
c = table->cellAt( row, 2 ).firstCursorPosition();
c.setCharFormat( headerFormat );
c.insertText( tr( "Shortcut" ) );

const QList<QObject *> objects = mManager->listAll();
for ( QObject *obj : objects )
{
QString actionText;
QString sequence;
QIcon icon;

if ( QAction *action = qobject_cast< QAction * >( obj ) )
{
actionText = action->text().remove( '&' );
sequence = action->shortcut().toString( QKeySequence::NativeText );
icon = action->icon();
}
else if ( QShortcut *shortcut = qobject_cast< QShortcut * >( obj ) )
{
actionText = shortcut->whatsThis();
sequence = shortcut->key().toString( QKeySequence::NativeText );
icon = shortcut->property( "Icon" ).value<QIcon>();
}
else
{
continue;
}

// skip actions without shortcut and name
if ( actionText.isEmpty() || sequence.isEmpty() )
{
continue;
}

row += 1;
table->appendRows( 1 );

if ( row % 2 )
{
table->cellAt( row, 0 ).setFormat( altRowFormat );
table->cellAt( row, 1 ).setFormat( altRowFormat );
table->cellAt( row, 2 ).setFormat( altRowFormat );
}
else
{
table->cellAt( row, 0 ).setFormat( rowFormat );
table->cellAt( row, 1 ).setFormat( rowFormat );
table->cellAt( row, 2 ).setFormat( rowFormat );
}

if ( !icon.isNull() )
{
c = table->cellAt( row, 0 ).firstCursorPosition();
c.insertImage( icon.pixmap( QSize( 24, 24 ) ).toImage() );
}
table->cellAt( row, 1 ).firstCursorPosition().insertText( actionText );
table->cellAt( row, 2 ).firstCursorPosition().insertText( sequence );
}

QPrinter printer( QPrinter::PrinterResolution );
printer.setOutputFormat( QPrinter::PdfFormat );
printer.setPaperSize( QPrinter::A4 );
printer.setOutputFileName( fileName );
document->print( &printer );
}
@@ -56,6 +56,7 @@ class GUI_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsCo
void resetShortcut();
void setNoShortcut();
void loadShortcuts();
void saveShortcutsPdf();
void mLeFilter_textChanged( const QString &text );

void actionChanged( QTreeWidgetItem *current, QTreeWidgetItem *previous );
@@ -88,6 +89,7 @@ class GUI_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsCo
QMenu *mSaveMenu = nullptr;
QAction *mSaveUserShortcuts = nullptr;
QAction *mSaveAllShortcuts = nullptr;
QAction *mSaveAsPdf = nullptr;

bool mGettingShortcut = false;
int mModifiers = 0, mKey = 0;

0 comments on commit 3394970

Please sign in to comment.