1 | /* |
---|
2 | |
---|
3 | Copyright 2012-2013 Adam Reichold |
---|
4 | |
---|
5 | This file is part of qpdfview. |
---|
6 | |
---|
7 | qpdfview is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation, either version 2 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | qpdfview is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #include "annotationwidgets.h" |
---|
23 | |
---|
24 | #include <QAction> |
---|
25 | #include <QDesktopServices> |
---|
26 | #include <QFileDialog> |
---|
27 | #include <QGraphicsProxyWidget> |
---|
28 | #include <QMenu> |
---|
29 | #include <QMessageBox> |
---|
30 | #include <QMutex> |
---|
31 | #include <QUrl> |
---|
32 | |
---|
33 | #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) |
---|
34 | |
---|
35 | #include <poppler-qt5.h> |
---|
36 | |
---|
37 | #else |
---|
38 | |
---|
39 | #include <poppler-qt4.h> |
---|
40 | |
---|
41 | #endif // QT_VERSION |
---|
42 | |
---|
43 | #include <poppler-annotation.h> |
---|
44 | |
---|
45 | #ifndef HAS_POPPLER_24 |
---|
46 | |
---|
47 | #define LOCK_ANNOTATION QMutexLocker mutexLocker(m_mutex); |
---|
48 | |
---|
49 | #else |
---|
50 | |
---|
51 | #define LOCK_ANNOTATION |
---|
52 | |
---|
53 | #endif // HAS_POPPLER_24 |
---|
54 | |
---|
55 | namespace |
---|
56 | { |
---|
57 | |
---|
58 | bool hideOnEscape(QWidget* widget, QKeyEvent* event) |
---|
59 | { |
---|
60 | if(event->key() == Qt::Key_Escape) |
---|
61 | { |
---|
62 | widget->hide(); |
---|
63 | |
---|
64 | event->accept(); |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | return false; |
---|
69 | } |
---|
70 | |
---|
71 | } // anonymous |
---|
72 | |
---|
73 | namespace qpdfview |
---|
74 | { |
---|
75 | |
---|
76 | AnnotationWidget::AnnotationWidget(QMutex* mutex, Poppler::Annotation* annotation, QWidget* parent) : QPlainTextEdit(parent), |
---|
77 | m_mutex(mutex), |
---|
78 | m_annotation(annotation) |
---|
79 | { |
---|
80 | LOCK_ANNOTATION |
---|
81 | |
---|
82 | setTabChangesFocus(true); |
---|
83 | setPlainText(m_annotation->contents()); |
---|
84 | |
---|
85 | connect(this, SIGNAL(textChanged()), SLOT(on_textChanged())); |
---|
86 | connect(this, SIGNAL(textChanged()), SIGNAL(wasModified())); |
---|
87 | |
---|
88 | moveCursor(QTextCursor::End); |
---|
89 | } |
---|
90 | |
---|
91 | void AnnotationWidget::keyPressEvent(QKeyEvent* event) |
---|
92 | { |
---|
93 | if(!hideOnEscape(this, event)) |
---|
94 | { |
---|
95 | QPlainTextEdit::keyPressEvent(event); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | void AnnotationWidget::on_textChanged() |
---|
100 | { |
---|
101 | LOCK_ANNOTATION |
---|
102 | |
---|
103 | m_annotation->setContents(toPlainText()); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | FileAttachmentAnnotationWidget::FileAttachmentAnnotationWidget(QMutex* mutex, Poppler::FileAttachmentAnnotation* annotation, QWidget* parent) : QToolButton(parent), |
---|
108 | m_mutex(mutex), |
---|
109 | m_annotation(annotation) |
---|
110 | { |
---|
111 | m_menu = new QMenu(this); |
---|
112 | m_saveAction = m_menu->addAction(tr("Save...")); |
---|
113 | m_saveAndOpenAction = m_menu->addAction(tr("Save and open...")); |
---|
114 | |
---|
115 | setMenu(m_menu); |
---|
116 | setPopupMode(QToolButton::InstantPopup); |
---|
117 | setIcon(QIcon::fromTheme(QLatin1String("mail-attachment"), QIcon(QLatin1String(":icons/mail-attachment.svg")))); |
---|
118 | |
---|
119 | connect(m_menu, SIGNAL(aboutToShow()), SLOT(on_aboutToShow())); |
---|
120 | connect(m_menu, SIGNAL(aboutToHide()), SLOT(on_aboutToHide())); |
---|
121 | |
---|
122 | connect(m_saveAction, SIGNAL(triggered()), SLOT(on_save_triggered())); |
---|
123 | connect(m_saveAndOpenAction, SIGNAL(triggered()), SLOT(on_saveAndOpen_triggered())); |
---|
124 | } |
---|
125 | |
---|
126 | void FileAttachmentAnnotationWidget::keyPressEvent(QKeyEvent *event) |
---|
127 | { |
---|
128 | if(!hideOnEscape(this, event)) |
---|
129 | { |
---|
130 | QToolButton::keyPressEvent(event); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | void FileAttachmentAnnotationWidget::on_aboutToShow() |
---|
135 | { |
---|
136 | graphicsProxyWidget()->setZValue(1.0); |
---|
137 | } |
---|
138 | |
---|
139 | void FileAttachmentAnnotationWidget::on_aboutToHide() |
---|
140 | { |
---|
141 | graphicsProxyWidget()->setZValue(0.0); |
---|
142 | } |
---|
143 | |
---|
144 | void FileAttachmentAnnotationWidget::on_save_triggered() |
---|
145 | { |
---|
146 | save(false); |
---|
147 | } |
---|
148 | |
---|
149 | void FileAttachmentAnnotationWidget::on_saveAndOpen_triggered() |
---|
150 | { |
---|
151 | save(true); |
---|
152 | } |
---|
153 | |
---|
154 | void FileAttachmentAnnotationWidget::save(bool open) |
---|
155 | { |
---|
156 | LOCK_ANNOTATION |
---|
157 | |
---|
158 | Poppler::EmbeddedFile* embeddedFile = m_annotation->embeddedFile(); |
---|
159 | |
---|
160 | QString filePath = QFileDialog::getSaveFileName(0, tr("Save file attachment"), embeddedFile->name()); |
---|
161 | |
---|
162 | if(!filePath.isEmpty()) |
---|
163 | { |
---|
164 | QFile file(filePath); |
---|
165 | |
---|
166 | if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
---|
167 | { |
---|
168 | file.write(embeddedFile->data()); |
---|
169 | |
---|
170 | file.close(); |
---|
171 | |
---|
172 | if(open) |
---|
173 | { |
---|
174 | if(!QDesktopServices::openUrl(QUrl::fromLocalFile(filePath))) |
---|
175 | { |
---|
176 | QMessageBox::warning(0, tr("Warning"), tr("Could not open file attachment saved to '%1'.").arg(filePath)); |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | QMessageBox::warning(0, tr("Warning"), tr("Could not save file attachment to '%1'.").arg(filePath)); |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | } // qpdfview |
---|