File: pdfannotation.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (279 lines) | stat: -rw-r--r-- 7,668 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifndef NO_POPPLER_PREVIEW
#include "PDFDocument.h"
#include "pdfannotation.h"
#include "poppler-annotation.h"
#include "utilsSystem.h"

/*
 * Wraps a Poppler::Annotation. This Object takes ownership of the referenced annotation
 */
PDFAnnotation::PDFAnnotation(Poppler::Annotation *ann, int pageNum, QObject *parent)
	: QObject(parent), m_popplerAnnotation(ann), m_pageNum(pageNum)
{

}

PDFAnnotation::~PDFAnnotation()
{
	delete m_popplerAnnotation;
}

QString PDFAnnotation::subTypeText() const
{
	return subTypeText(m_popplerAnnotation->subType());
}

QString PDFAnnotation::subTypeIconName() const
{
	return subTypeIconName(m_popplerAnnotation->subType());
}

QString PDFAnnotation::subTypeText(Poppler::Annotation::SubType subtype)
{
	switch (subtype) {
	case Poppler::Annotation::AText:
		return tr("Text");
	case Poppler::Annotation::ALine:
		return tr("Line");
	case Poppler::Annotation::AGeom:
		return tr("Geometric");
	case Poppler::Annotation::AHighlight:
		return tr("Highlight");
	case Poppler::Annotation::AStamp:
		return tr("Stamp");
	case Poppler::Annotation::AInk:
		return tr("Ink");
	case Poppler::Annotation::ALink:
		return tr("Link");
	case Poppler::Annotation::ACaret:
		return tr("Caret");
	case Poppler::Annotation::AFileAttachment:
		return tr("Attachment");
	case Poppler::Annotation::ASound:
		return tr("Sound");
	case Poppler::Annotation::AMovie:
		return tr("Movie");
	default:
		qDebug() << "no text for annotation subtype" << subtype;
	}
	return QString();
}

QString PDFAnnotation::subTypeIconName(Poppler::Annotation::SubType subtype)
{
	switch (subtype) {
	case Poppler::Annotation::AText:
		return ":/images-ng/annotation_text.svg";
	case Poppler::Annotation::AHighlight:
		return ":/images-ng/annotation_highlight.svg";
	case Poppler::Annotation::ACaret:
		return ":/images-ng/annotation_caret.svg";
	default:
		;
	}
	return QString();
}


/*** PDFAnnotations *********************************************************/

PDFAnnotations::PDFAnnotations(PDFDocument *doc)
	: QObject(doc), m_doc(doc)
{
	update();
}

const PDFAnnotationList &PDFAnnotations::annotationsInPage(int page) const
{
	return m_annotationsInPage[page];
}

PDFAnnotationModel *PDFAnnotations::createModel()
{
	return new PDFAnnotationModel(this);
}

const PDFAnnotation *PDFAnnotations::annotation(int absNumber) const
{
	Q_ASSERT(absNumber >= 0 && absNumber < m_annotations.length());
	return m_annotations[absNumber];
}

void PDFAnnotations::update()
{
	QList<Poppler::Annotation::SubType> excludedTypes = QList<Poppler::Annotation::SubType>()
	        << Poppler::Annotation::ALink
	        << Poppler::Annotation::AMovie;

	qDeleteAll(m_annotations);
	m_annotations.clear();
	m_annotationsInPage.clear();

	if (!m_doc || !m_doc->widget()) return;

	for (int pageNum = 0; pageNum < m_doc->widget()->realNumPages(); pageNum++) {
		Poppler::Page *page = m_doc->popplerDoc()->page(pageNum);
		if (!page) break;
		PDFAnnotationList annInPage;
		foreach (Poppler::Annotation *pa, page->annotations()) {
			if (excludedTypes.contains(pa->subType())) {
				delete pa;
				continue;
			}
			PDFAnnotation *ann = new PDFAnnotation(pa, pageNum, this);
			m_annotations.append(ann);
			annInPage.append(ann);
		}
		m_annotationsInPage.append(annInPage);
		delete page;
	}
}


/*** PDFAnnotationModel *****************************************************/

PDFAnnotationModel::PDFAnnotationModel(PDFAnnotations *parent)
	: QAbstractTableModel(parent), m_annotations(parent)
{
	columnTypes = QList<ColType>() << CT_TYPE << CT_PAGE << CT_AUTHOR << CT_CONTENTS << CT_MODIFICATON_DATE;
}

int PDFAnnotationModel::rowCount ( const QModelIndex &parent ) const
{
	Q_UNUSED(parent);
	return m_annotations->m_annotations.count();
}

int PDFAnnotationModel::columnCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);
	return columnTypes.length();
}

QVariant PDFAnnotationModel::data ( const QModelIndex &index, int role) const
{
	if (!index.isValid()) return QVariant();

	if (role != Qt::DisplayRole &&
	        role != Qt::ToolTipRole &&
	        role != Qt::DecorationRole &&
	        role != Qt::TextAlignmentRole)
		return QVariant(); // speed up a little bit by ignoring roles that are not handled below already here.

	const PDFAnnotation *ann = itemForIndex(index);
	if (!ann) return QVariant();

	ColType colType = columnTypes[index.column()];

	switch (colType) {
	case CT_AUTHOR:
		switch (role) {
		case Qt::DisplayRole:
			return ann->popplerAnnotation()->author();
		}
		break;
	case CT_CONTENTS:
		switch (role) {
		case Qt::DisplayRole:
			return ann->popplerAnnotation()->contents();
		}
		break;
	case CT_TYPE:
		switch (role) {
		case Qt::DecorationRole:
			return getRealIconCached(PDFAnnotation::subTypeIconName(ann->popplerAnnotation()->subType()));
		case Qt::ToolTipRole:
			return PDFAnnotation::subTypeText(ann->popplerAnnotation()->subType());
		}
		break;
	case CT_MODIFICATON_DATE:
		switch (role) {
		case Qt::DisplayRole:
			return ann->popplerAnnotation()->modificationDate().toString(Qt::DefaultLocaleShortDate);
		}
		break;
	case CT_CREATION_DATE:
		switch (role) {
		case Qt::DisplayRole:
			return ann->popplerAnnotation()->creationDate().toString(Qt::DefaultLocaleShortDate);
		}
	case CT_PAGE:
		switch (role) {
		case Qt::DisplayRole:
			return ann->pageNum();
		case Qt::TextAlignmentRole:
			return QVariant(Qt::AlignRight | Qt::AlignVCenter);
		}
	}

	return QVariant();
}

QVariant PDFAnnotationModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation == Qt::Vertical) return QVariant();

	if (role == Qt::DisplayRole) {
		ColType colType = columnTypes[section];
		return colTypeText(colType);
	}
	return QVariant();
}

const PDFAnnotation *PDFAnnotationModel::itemForIndex(const QModelIndex &index) const
{
	if (!index.isValid()) return NULL;
	return m_annotations->annotation(index.row());
}

QString PDFAnnotationModel::colTypeText(PDFAnnotationModel::ColType colType) const
{
	switch (colType) {
	case CT_AUTHOR:
		return tr("Author");
	case CT_CONTENTS:
		return tr("Text");
	case CT_TYPE:
		return tr("Type");
	case CT_MODIFICATON_DATE:
		return tr("Modified");
	case CT_CREATION_DATE:
		return tr("Created");
	case CT_PAGE:
		return tr("Page");
	default:
		qDebug() << "no column header text for colType" << colType;
	}
	return QString();
}


/*** PDFAnnotationTableView *************************************************/

PDFAnnotationTableView::PDFAnnotationTableView(QWidget *parent)
	: QTableView(parent)
{
	QFontMetrics fm(QApplication::font());
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setSelectionMode(QAbstractItemView::SingleSelection);
	//setColumnWidth(0,fm.width("> "));
	//setColumnWidth(1,20*fm.width("w"));
	//setColumnWidth(2,fm.width("WarningW"));
	//setColumnWidth(3,fm.width("Line WWWWW"));
	//setColumnWidth(4,20*fm.width("w"));
	//connect(errorTable, SIGNAL(clicked(const QModelIndex &)), this, SLOT(clickedOnLogModelIndex(const QModelIndex &)));

	horizontalHeader()->setStretchLastSection(true);
	setMinimumHeight(5 * (fm.lineSpacing() + 4));
	setFrameShape(QFrame::NoFrame);

	connect(this, SIGNAL(clicked(QModelIndex)), SLOT(onClick(QModelIndex)));
}

void PDFAnnotationTableView::onClick(const QModelIndex &index)
{
	PDFAnnotationModel *annModel = qobject_cast<PDFAnnotationModel *>(model());
	emit annotationClicked(annModel->itemForIndex(index));
}

#endif