File: showgraph.cpp

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (64 lines) | stat: -rw-r--r-- 2,281 bytes parent folder | download | duplicates (5)
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

/*
 * CodeQuery
 * Copyright (C) 2013-2016 ruben2020 https://github.com/ruben2020/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

#include "asrt.h"
#include "core_iface.h"
#include "showgraph.h"

/** Magnifying factor for getting more detailed images */
const int IMAGE_EXPORT_SCALE_FACTOR = 2;

/** Adjust value for image's bounding rectangle on scene rendering */
const qreal IMAGE_RECT_ADJUST = 10;

QImage showgraph::convertToImage(QString grpxml)
{

    /** Create graph instance */
    GraphView* graph_view = new GraphView();
    graph_view->setGraph( new GGraph( graph_view, true));

    /** Read graph from XML */
    graph_view->graph()->readFromXML( grpxml);
 
    /**
     * Perform layout in single thread.
     * Multi thread wouldn't work since we do not run QApplication::exec() and there is not event loop
     */
    graph_view->graph()->doLayoutSingle();

    /** Get scene rectangle */
    QRectF scene_rect( graph_view->scene()->itemsBoundingRect()
                       .adjusted( -IMAGE_RECT_ADJUST, -IMAGE_RECT_ADJUST,
                                   IMAGE_RECT_ADJUST, IMAGE_RECT_ADJUST));

    /** Render to image */
        QImage image( scene_rect.width() * IMAGE_EXPORT_SCALE_FACTOR,
                          scene_rect.height() * IMAGE_EXPORT_SCALE_FACTOR,
                                   QImage::Format_RGB32);
    image.fill( graph_view->palette().base().color().rgb());
    QPainter pp( &image);
        pp.setRenderHints( graph_view->renderHints());
    graph_view->scene()->render( &pp, image.rect(), scene_rect);

    delete graph_view;
    return image;
}