File indexing completed on 2024-05-19 15:27:52

0001 /*
0002     This file is part of KGraphViewer.
0003     Copyright (C) 2010  Gael de Chalendar <kleag@free.fr>
0004 
0005     This program is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU General Public License as
0007     published by the Free Software Foundation; either version 2 of
0008     the License, or (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 
0018 */
0019 
0020 #include "layoutagraphthread.h"
0021 #include "kgraphviewerlib_debug.h"
0022 
0023 #include <QMutex>
0024 
0025 #include <QDebug>
0026 
0027 static QMutex gv_mutex;
0028 
0029 // Not sure just wrapping these two is really enough, but it
0030 // fixed all crashes when loading many files at once for me.
0031 
0032 int threadsafe_wrap_gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
0033 {
0034     QMutexLocker locker(&gv_mutex);
0035     return gvLayout(gvc, g, engine);
0036 }
0037 
0038 int threadsafe_wrap_gvRender(GVC_t *gvc, graph_t *g, const char *format, FILE *out)
0039 {
0040     QMutexLocker locker(&gv_mutex);
0041     return gvRender(gvc, g, format, out);
0042 }
0043 
0044 LayoutAGraphThread::LayoutAGraphThread()
0045     : sem(1)
0046 {
0047     m_gvc = gvContext();
0048 }
0049 
0050 LayoutAGraphThread::~LayoutAGraphThread()
0051 {
0052     gvFreeContext(m_gvc);
0053 }
0054 
0055 void LayoutAGraphThread::run()
0056 {
0057     if (!m_g) {
0058         qCWarning(KGRAPHVIEWERLIB_LOG) << "No graph loaded, skipping layout";
0059         return;
0060     }
0061     threadsafe_wrap_gvLayout(m_gvc, m_g, m_layoutCommand.toUtf8().data());
0062     threadsafe_wrap_gvRender(m_gvc, m_g, "xdot", nullptr);
0063 }
0064 
0065 void LayoutAGraphThread::layoutGraph(graph_t *graph, const QString &layoutCommand)
0066 {
0067     sem.acquire();
0068     m_g = graph;
0069     m_layoutCommand = layoutCommand;
0070     start();
0071 }
0072 
0073 #include "moc_layoutagraphthread.cpp"