Skip to content

Commit e3f62a9

Browse files
committed
export rendered tikz as png
1 parent fb49b32 commit e3f62a9

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

data/templates/template_beamer.pgs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
\documentclass{beamer}
2+
\beamertemplatenavigationsymbolsempty
23
\usepackage{tikz}
34
\usepackage{color}
45
\usepackage{amssymb}

mainwindow.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
66109
void 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");

mainwindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public slots:
5454
void gotoPreviousImage();
5555
void gotoNextImage();
5656
void updateTemplate(const QString& filename);
57+
void exportPNG();
5758

5859
private:
5960
void setupActions();
@@ -68,7 +69,11 @@ public slots:
6869
void appendLog(QString str);
6970

7071
void updateRootDirectory();
72+
73+
bool isEmptyLine(QImage& img, int line);
74+
bool isEmptyCol(QImage& img, int col);
7175

76+
QImage image;
7277
QWidget *window;
7378
QSplitter *splitView;
7479
QHBoxLayout *mainLayout;
@@ -112,6 +117,7 @@ public slots:
112117

113118
QAction* nextImage;
114119
QAction* prevImage;
120+
QAction* exportImgPng;
115121

116122
int currentPage;
117123
bool usersaved;

0 commit comments

Comments
 (0)