Debian Codegraph @

src/ApvlvImageWidget.cc

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE ApvlvImageWidget.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <QBuffer>
29 #include <QClipboard>
30 #include <QInputDialog>
31 #include <QMouseEvent>
32 #include <iostream>
33
34 #include "ApvlvImageWidget.h"
35
36 namespace apvlv
37 {
38 using namespace std;
39
40 #ifdef APVLV_WITH_OCR
41 TextContainer::TextContainer (QWidget *parent) : Editor (parent)
42 {
43 setReadOnly (false);
44 setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
45 setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
46 }
47 #endif
48
49 ImageContainer::ImageContainer (QWidget *parent) : QLabel (parent)
50 {
51 mCopyAction.setText (tr ("Copy"));
52 QObject::connect (&mCopyAction, SIGNAL (triggered (bool)), this,
53 SLOT (copy ()));
54 addAction (&mCopyAction);
55
56 mUnderlineAction.setText (tr ("Underline"));
57 QObject::connect (&mUnderlineAction, SIGNAL (triggered (bool)), this,
58 SLOT (underline ()));
59 addAction (&mUnderlineAction);
60
61 mCommentAction.setText (tr ("Comment"));
62 QObject::connect (&mCommentAction, SIGNAL (triggered (bool)), this,
63 SLOT (comment ()));
64 addAction (&mCommentAction);
65
66 setContextMenuPolicy (Qt::ContextMenuPolicy::ActionsContextMenu);
67 }
68
69 void
70 ImageContainer::mousePressEvent (QMouseEvent *event)
71 {
72 if (event->button () != Qt::MouseButton::LeftButton)
73 return;
74
75 redraw ();
76 mIsSelected = true;
77 mPressPosition = event->position ();
78 }
79
80 void
81 ImageContainer::mouseMoveEvent (QMouseEvent *event)
82 {
83 if (!mIsSelected)
84 return;
85
86 mMovePosition = event->position ();
87
88 auto range = selectionRange ();
89 auto rect_list = mImageWidget->file ()->pageHighlight (
90 mImageWidget->pageNumber (), range.first, range.second);
91 if (!rect_list)
92 return;
93
94 mImageWidget->setSelects (rect_list.value ());
95 redraw ();
96 }
97
98 void
99 ImageContainer::mouseReleaseEvent (QMouseEvent *event)
100 {
101 mImageWidget->setSelects ({});
102 mIsSelected = false;
103 }
104
105 bool
106 ImageContainer::renderImage (int pn, double zm, int rot)
107 {
108 return mImageWidget->file ()->pageRenderToImage (pn, zm, rot, &mImage);
109 }
110
111 void
112 ImageContainer::redraw ()
113 {
114 QImage img = mImage;
115 if (!mImageWidget->searchResults ().empty ())
116 {
117 imageSelectSearch (&img, mImageWidget->zoomrate (),
118 mImageWidget->searchSelect (),
119 mImageWidget->searchResults ());
120 }
121 else if (!mImageWidget->selects ().empty ())
122 {
123 imageSelect (&img, mImageWidget->zoomrate (), mImageWidget->selects ());
124 }
125 setPixmap (QPixmap::fromImage (img));
126 resize (img.size ());
127 }
128
129 pair<ApvlvPoint, ApvlvPoint>
130 ImageContainer::selectionRange ()
131 {
132 double left = mPressPosition.x () / mImageWidget->zoomrate ();
133 double top = mPressPosition.y () / mImageWidget->zoomrate ();
134 double right = mMovePosition.x () / mImageWidget->zoomrate ();
135 double bottom = mMovePosition.y () / mImageWidget->zoomrate ();
136 return { { left, top }, { right, bottom } };
137 }
138
139 vector<Rectangle>
140 ImageContainer::selectionArea ()
141 {
142 auto range = selectionRange ();
143 auto rect_list = mImageWidget->file ()->pageHighlight (
144 mImageWidget->pageNumber (), range.first, range.second);
145 if (!rect_list)
146 return {};
147 return rect_list.value ();
148 }
149
150 string
151 ImageContainer::selectionText ()
152 {
153 auto range = selectionRange ();
154 string text;
155 mImageWidget->file ()->pageText (
156 mImageWidget->pageNumber (),
157 { range.first.x, range.first.y, range.second.x, range.second.y }, text);
158 return text;
159 }
160
161 void
162 ImageContainer::copy ()
163 {
164 qDebug () << "copy text";
165 auto text = selectionText ();
166 auto clipboard = QGuiApplication::clipboard ();
167 clipboard->setText (QString::fromLocal8Bit (text));
168 mImageWidget->setSelects ({});
169 redraw ();
170 }
171
172 void
173 ImageContainer::underline ()
174 {
175 qDebug () << "underline text";
176 auto page = mImageWidget->pageNumber ();
177 auto range = selectionRange ();
178 auto text = selectionText ();
179 if (!text.empty ())
180 {
181 auto note = mImageWidget->file ()->getNote ();
182 Comment comment;
183 comment.quoteText = text;
184 comment.begin.set (page, &range.first);
185 comment.end.set (page, &range.second);
186 note->addComment (comment);
187 }
188
189 mImageWidget->setSelects ({});
190 redraw ();
191 }
192
193 void
194 ImageContainer::comment ()
195 {
196 qDebug () << "comment text";
197 do
198 {
199 auto text = selectionText ();
200 if (text.empty ())
201 break;
202
203 auto input_text = QInputDialog::getMultiLineText (this, tr ("Input"),
204 tr ("Comment"));
205 auto commentText = input_text.trimmed ();
206 if (commentText.isEmpty ())
207 break;
208
209 auto page = mImageWidget->pageNumber ();
210 auto range = selectionRange ();
211 auto note = mImageWidget->file ()->getNote ();
212 Comment comment;
213 comment.quoteText = text,
214 comment.commentText = commentText.toStdString ();
215 comment.begin.set (page, &range.first);
216 comment.end.set (page, &range.second);
217 note->addComment (comment);
218 }
219 while (false);
220
221 mImageWidget->setSelects ({});
222 redraw ();
223 }
224
225 ApvlvImage::ApvlvImage ()
226 {
227 setAlignment (Qt::AlignCenter);
228 setHorizontalScrollBarPolicy (Qt::ScrollBarPolicy::ScrollBarAsNeeded);
229 setVerticalScrollBarPolicy (Qt::ScrollBarPolicy::ScrollBarAsNeeded);
230
231 setWidget (&mImageContainer);
232 }
233
234 ApvlvImage::~ApvlvImage ()
235 {
236 qDebug () << "ApvlvImage: " << this << " be freed";
237 }
238
239 #ifdef APVLV_WITH_OCR
240 void
241 ApvlvImage::ocrDisplay (bool is_ocr)
242 {
243 if (is_ocr)
244 {
245 auto image = mImageContainer.pixmap ();
246 auto text = mOCR.getTextFromPixmap (image);
247 mTextContainer.setText (text.get ());
248 if (widget () != &mTextContainer)
249 {
250 takeWidget ();
251 setWidget (&mTextContainer);
252 }
253 }
254 else
255 {
256 if (widget () != &mImageContainer)
257 {
258 takeWidget ();
259 setWidget (&mImageContainer);
260 }
261 }
262 }
263
264 std::unique_ptr<char>
265 ApvlvImage::ocrGetText ()
266 {
267 auto image = mImageContainer.pixmap ();
268 auto text = mOCR.getTextFromPixmap (image);
269 return text;
270 }
271 #endif
272
273 void
274 ImageWidget::showPage (int p, double s)
275 {
276 if (p != mPageNumber)
277 {
278 if (!mImage.mImageContainer.renderImage (p, mZoomrate, mRotate))
279 return;
280 }
281 mImage.mImageContainer.redraw ();
282 #ifdef APVLV_WITH_OCR
283 mImage.mTextContainer.resize (mImage.mImageContainer.size ());
284 if (mImage.widget () == &mImage.mTextContainer)
285 {
286 mImage.mTextContainer.setZoomrate (mZoomrate);
287 mImage.ocrDisplay (true);
288 }
289 #endif
290 scrollTo (0.0, s);
291 mPageNumber = p;
292 }
293
294 void
295 ImageWidget::showPage (int p, const string &anchor)
296 {
297 showPage (p, 0.0f);
298 mAnchor = anchor;
299 }
300
301 void
302 ImageWidget::setSearchResults (const WordListRectangle &wlr)
303 {
304 mSearchResults = wlr;
305 mImage.mImageContainer.redraw ();
306 }
307
308 void
309 ImageWidget::setZoomrate (double zm)
310 {
311 if (mPageNumber != INVALID_PAGENUM)
312 {
313 if (mImage.mImageContainer.renderImage (mPageNumber, zm, mRotate))
314 {
315 mImage.mImageContainer.redraw ();
316 mZoomrate = zm;
317 }
318 #ifdef APVLV_WITH_OCR
319 mImage.mTextContainer.setZoomrate (zm);
320 #endif
321 }
322 else
323 {
324 mZoomrate = zm;
325 }
326 }
327
328 void
329 ImageWidget::setRotate (int rotate)
330 {
331 if (mPageNumber != INVALID_PAGENUM)
332 {
333 if (mImage.mImageContainer.renderImage (mPageNumber, mZoomrate, rotate))
334 {
335 mImage.mImageContainer.redraw ();
336 mRotate = rotate;
337 }
338 }
339 else
340 {
341 mRotate = rotate;
342 }
343 }
344
345 bool
346 imageSelect (QImage *pix, double zm, const vector<Rectangle> &rect_list)
347 {
348 for (auto const &rect : rect_list)
349 {
350 auto p1xz = static_cast<int> (rect.p1x * zm);
351 auto p2xz = static_cast<int> (rect.p2x * zm);
352 auto p1yz = static_cast<int> (rect.p1y * zm);
353 auto p2yz = static_cast<int> (rect.p2y * zm);
354
355 for (auto w = p1xz; w < p2xz; ++w)
356 {
357 for (auto h = p1yz; h < p2yz; ++h)
358 {
359 QColor c = pix->pixelColor (w, h);
360 c.setRgb (255 - c.red (), 255 - c.red (), 255 - c.red ());
361 pix->setPixelColor (w, h, c);
362 }
363 }
364 }
365
366 return true;
367 }
368
369 bool
370 imageUnderline (QImage *pix, double zm, const vector<Rectangle> &rect_list)
371 {
372 for (auto const &rect : rect_list)
373 {
374 auto p1xz = static_cast<int> (rect.p1x * zm);
375 auto p2xz = static_cast<int> (rect.p2x * zm);
376 auto p1yz = static_cast<int> (rect.p1y * zm);
377 auto p2yz = static_cast<int> (rect.p2y * zm);
378
379 for (auto w = p1xz; w < p2xz; ++w)
380 {
381 for (auto h = p1yz; h < p2yz; ++h)
382 {
383 QColor c = pix->pixelColor (w, h);
384 c.setRgb (255 - c.red (), 255 - c.red (), 255 - c.red ());
385 pix->setPixelColor (w, h, c);
386 }
387 }
388 }
389
390 return true;
391 }
392
393 bool
394 imageSelectSearch (QImage *pix, double zm, int select,
395 const WordListRectangle &wordlist)
396 {
397 for (auto itr = wordlist.begin (); itr != wordlist.end (); ++itr)
398 {
399 auto rectangles = *itr;
400
401 for (auto const &pos : rectangles.rect_list)
402 {
403 auto p1xz = static_cast<int> (pos.p1x * zm);
404 auto p2xz = static_cast<int> (pos.p2x * zm);
405 auto p1yz = static_cast<int> (pos.p2y * zm);
406 auto p2yz = static_cast<int> (pos.p1y * zm);
407
408 if (pix->format () == QImage::Format_ARGB32)
409 {
410 imageArgb32ToRgb32 (*pix, p1xz, p1yz, p2xz, p2yz);
411 }
412
413 if (std::distance (wordlist.begin (), itr) == select)
414 {
415 for (int w = p1xz; w < p2xz; ++w)
416 {
417 for (int h = p1yz; h < p2yz; ++h)
418 {
419 QColor c = pix->pixelColor (w, h);
420 c.setRgb (255 - c.red (), 255 - c.red (),
421 255 - c.red ());
422 pix->setPixelColor (w, h, c);
423 }
424 }
425 }
426 else
427 {
428 for (int w = p1xz; w < p2xz; ++w)
429 {
430 for (int h = p1yz; h < p2yz; ++h)
431 {
432 QColor c = pix->pixelColor (w, h);
433 c.setRgb (255 - c.red () / 2, 255 - c.red () / 2,
434 255 - c.red () / 2);
435 pix->setPixelColor (w, h, c);
436 }
437 }
438 }
439 }
440 }
441
442 return true;
443 }
444
445 }
446
447 // Local Variables:
448 // mode: c++
449 // End: