@@ -63,6 +63,49 @@ void MainWindow::gotoPreviousImage() {
6363 }
6464}
6565
66+
67+ bool MainWindow::isEmptyLine (QImage& img, int line) {
68+ QColor white (255 , 255 , 255 , 255 );
69+ for (int x = 0 ; x < img.width (); x++) {
70+ if (img.pixelColor (x, line) != white) {
71+ return false ;
72+ }
73+ }
74+ return true ;
75+ }
76+
77+ bool MainWindow::isEmptyCol (QImage& img, int col) {
78+ QColor white (255 , 255 , 255 , 255 );
79+ for (int y = 0 ; y < img.height (); y++) {
80+ if (img.pixelColor (col, y) != white) {
81+ return false ;
82+ }
83+ }
84+ return true ;
85+ }
86+
87+ void MainWindow::exportPNG () {
88+ int top = 0 ;
89+ while (isEmptyLine (image, top) && top < image.height () - 1 ) top++;
90+ if (top > 0 ) top--;
91+ int bottom = image.height () - 1 ;
92+ while (isEmptyLine (image, bottom) && bottom > 0 ) bottom--;
93+ if (bottom < image.height () - 1 ) bottom++;
94+ int left = 0 ;
95+ while (isEmptyCol (image, left) && left < image.width () - 1 ) left++;
96+ if (left > 0 ) left--;
97+ int right = image.width () - 1 ;
98+ while (isEmptyCol (image, right) && right > 0 ) right--;
99+ if (right < image.width () - 1 ) right++;
100+
101+ QImage crop;
102+ crop = image.copy (left, top, right - left + 1 , bottom - top + 1 );
103+ QString fname = QFileDialog::getSaveFileUrl ().toLocalFile ();
104+ std::cout << " Export as " << fname.toStdString () << std::endl;
105+ crop.save (fname, " png" , -1 );
106+
107+ }
108+
66109void MainWindow::render (double scale) {
67110 if (currentDoc && display) {
68111 if (currentPage >= currentDoc->numPages ()) {
@@ -74,8 +117,9 @@ void MainWindow::render(double scale) {
74117 prevImage->setVisible (currentDoc->numPages () > 1 );
75118 nextImage->setVisible (currentDoc->numPages () > 1 );
76119
77- QImage image = currentDoc->page (currentPage)->renderToImage (scale * physicalDpiX (), scale * physicalDpiY ());
120+ image = currentDoc->page (currentPage)->renderToImage (scale * physicalDpiX (), scale * physicalDpiY ());
78121 display->setImage (image);
122+
79123 }
80124}
81125
@@ -284,6 +328,7 @@ MainWindow::MainWindow() : currentDoc(NULL), renderProcess(NULL), currentPage(0)
284328
285329 connect (prevImage, SIGNAL (triggered ()), this , SLOT (gotoPreviousImage ()));
286330 connect (nextImage, SIGNAL (triggered ()), this , SLOT (gotoNextImage ()));
331+ connect (exportImgPng, SIGNAL (triggered ()), this , SLOT (exportPNG ()));
287332
288333 connect ((QObject *)doc, SIGNAL (textChanged (KTextEditor::Document *)), this ,
289334 SLOT (textChanged (KTextEditor::Document *)));
@@ -427,6 +472,8 @@ void MainWindow::setupUI() {
427472 setupGUI (ToolBar | Keys | StatusBar | Save);
428473 createGUI (katePart);
429474
475+ exportImgPng = toolBar ()->addAction (QIcon::fromTheme (" image" ), " Save as PNG" );
476+
430477 toolBar ()->addSeparator ();
431478 prevImage = toolBar ()->addAction (QIcon::fromTheme (" go-previous" ), " Previous image" );
432479 nextImage = toolBar ()->addAction (QIcon::fromTheme (" go-next" ), " Next image" );
0 commit comments