Changeset View
Changeset View
Standalone View
Standalone View
ui/signaturepanel.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 by Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | #include "signaturepanel.h" | ||||
11 | | ||||
12 | #include "pageview.h" | ||||
13 | #include "signatureguiutils.h" | ||||
14 | #include "signaturemodel.h" | ||||
15 | #include "revisionviewer.h" | ||||
16 | #include "signaturepropertiesdialog.h" | ||||
17 | | ||||
18 | #include <KLocalizedString> | ||||
19 | | ||||
20 | #include <QMenu> | ||||
21 | #include <QTreeView> | ||||
22 | #include <QVBoxLayout> | ||||
23 | #include <QHeaderView> | ||||
24 | #include <QApplication> | ||||
25 | | ||||
26 | #include "core/form.h" | ||||
27 | #include "core/document.h" | ||||
28 | | ||||
29 | class SignaturePanelPrivate | ||||
30 | { | ||||
31 | public: | ||||
32 | Okular::FormFieldSignature *getSignatureFromId( int formId ); | ||||
33 | | ||||
34 | QHash<int, Okular::FormFieldSignature *> m_signatureForms; | ||||
35 | Okular::Document *m_document; | ||||
36 | Okular::FormFieldSignature *m_currentForm; | ||||
37 | QTreeView *m_view; | ||||
38 | SignatureModel *m_model; | ||||
39 | PageView *m_pageView; | ||||
40 | }; | ||||
41 | | ||||
42 | SignaturePanel::SignaturePanel( QWidget *parent, Okular::Document *document ) | ||||
43 | : QWidget( parent ), d_ptr( new SignaturePanelPrivate ) | ||||
44 | { | ||||
45 | Q_D( SignaturePanel ); | ||||
46 | d->m_view = new QTreeView( this ); | ||||
47 | d->m_view->setAlternatingRowColors( true ); | ||||
48 | d->m_view->setSelectionMode( QAbstractItemView::ExtendedSelection ); | ||||
49 | d->m_view->header()->hide(); | ||||
50 | | ||||
51 | d->m_document = document; | ||||
52 | d->m_model = new SignatureModel( d->m_document, this ); | ||||
53 | | ||||
54 | d->m_view->setModel( d->m_model ); | ||||
55 | connect( d->m_view->selectionModel(), &QItemSelectionModel::currentChanged, this, &SignaturePanel::activated ); | ||||
56 | connect( d->m_view, &QTreeView::pressed, this, &SignaturePanel::slotShowContextMenu ); | ||||
57 | | ||||
58 | auto vLayout = new QVBoxLayout( this ); | ||||
59 | vLayout->setMargin( 0 ); | ||||
60 | vLayout->setSpacing( 6 ); | ||||
61 | vLayout->addWidget( d->m_view ); | ||||
62 | } | ||||
63 | | ||||
64 | Okular::FormFieldSignature *SignaturePanelPrivate::getSignatureFromId( int formId ) | ||||
65 | { | ||||
66 | if ( formId == -1 ) | ||||
67 | return nullptr; | ||||
68 | return m_signatureForms[formId]; | ||||
69 | } | ||||
70 | | ||||
71 | void SignaturePanel::activated( const QModelIndex &index ) | ||||
72 | { | ||||
73 | Q_D( SignaturePanel ); | ||||
74 | int formId = d->m_model->data( index, SignatureModel::FormIdRole ).toInt(); | ||||
75 | d->m_currentForm = d->getSignatureFromId( formId ); | ||||
76 | if ( !d->m_currentForm ) | ||||
77 | return; | ||||
78 | Okular::NormalizedRect nr = d->m_currentForm->rect(); | ||||
79 | Okular::DocumentViewport vp; | ||||
80 | vp.pageNumber = d->m_model->data( index, SignatureModel::PageRole ).toInt(); | ||||
81 | vp.rePos.enabled = true; | ||||
82 | vp.rePos.pos = Okular::DocumentViewport::Center; | ||||
83 | vp.rePos.normalizedX = ( nr.right + nr.left ) / 2.0; | ||||
84 | vp.rePos.normalizedY = ( nr.bottom + nr.top ) / 2.0; | ||||
85 | d->m_document->setViewport( vp, nullptr ); | ||||
86 | d->m_pageView->highlightSignatureFormWidget( formId ); | ||||
87 | } | ||||
88 | | ||||
89 | void SignaturePanel::slotShowContextMenu() | ||||
90 | { | ||||
91 | Q_D( SignaturePanel ); | ||||
92 | if ( !d->m_currentForm ) | ||||
93 | return; | ||||
94 | | ||||
95 | Qt::MouseButtons buttons = QApplication::mouseButtons(); | ||||
96 | if ( buttons == Qt::RightButton ) | ||||
97 | { | ||||
98 | QMenu *menu = new QMenu( this ); | ||||
99 | QAction *sigRev = new QAction( i18n("View Revision"), menu ); | ||||
100 | QAction *sigProp = new QAction( i18n("View Properties"), menu ); | ||||
101 | connect( sigRev, &QAction::triggered, this, &SignaturePanel::slotViewRevision ); | ||||
102 | connect( sigProp, &QAction::triggered, this, &SignaturePanel::slotViewProperties ); | ||||
103 | menu->addAction( sigRev ); | ||||
104 | menu->addAction( sigProp ); | ||||
105 | menu->exec( QCursor::pos() ); | ||||
106 | delete menu; | ||||
107 | } | ||||
108 | } | ||||
109 | | ||||
110 | void SignaturePanel::slotViewRevision() | ||||
111 | { | ||||
112 | Q_D( SignaturePanel ); | ||||
113 | QByteArray data; | ||||
114 | d->m_document->requestSignedRevisionData( d->m_currentForm->validate(), &data ); | ||||
115 | RevisionViewer viewer( data, this ); | ||||
116 | viewer.viewRevision(); | ||||
117 | } | ||||
118 | | ||||
119 | void SignaturePanel::slotViewProperties() | ||||
120 | { | ||||
121 | Q_D( SignaturePanel ); | ||||
122 | SignaturePropertiesDialog propDlg( d->m_document, d->m_currentForm, this ); | ||||
123 | propDlg.exec(); | ||||
124 | } | ||||
125 | | ||||
126 | void SignaturePanel::notifySetup( const QVector<Okular::Page *> &/*pages*/, int setupFlags ) | ||||
127 | { | ||||
128 | if ( !( setupFlags & Okular::DocumentObserver::UrlChanged ) ) | ||||
129 | return; | ||||
130 | | ||||
131 | Q_D( SignaturePanel ); | ||||
132 | QVector<Okular::FormFieldSignature *> signatureForms = SignatureGuiUtils::getSignatureFormFields( d->m_document, true, -1 ); | ||||
133 | foreach ( auto sf, signatureForms ) | ||||
134 | { | ||||
135 | d->m_signatureForms[sf->id()] = sf; | ||||
136 | } | ||||
137 | } | ||||
138 | | ||||
139 | SignaturePanel::~SignaturePanel() | ||||
140 | { | ||||
141 | Q_D( SignaturePanel ); | ||||
142 | d->m_document->removeObserver( this ); | ||||
143 | delete d->m_model; | ||||
144 | } | ||||
145 | | ||||
146 | void SignaturePanel::setPageView( PageView *pv ) | ||||
147 | { | ||||
148 | Q_D( SignaturePanel ); | ||||
149 | d->m_pageView = pv; | ||||
150 | } | ||||
151 | | ||||
152 | #include "moc_signaturepanel.cpp" |