FreeCAD

Форк
0
/
Assistant.cpp 
187 строк · 6.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
# include <QCoreApplication>
28
# include <QDir>
29
# include <QFileInfo>
30
# include <QLibraryInfo>
31
# include <QMessageBox>
32
# include <QProcess>
33
# include <QTextStream>
34
#endif
35

36
#include <App/Application.h>
37
#include <Base/Console.h>
38
#include "Assistant.h"
39

40
using namespace Gui;
41

42
/* TRANSLATOR Gui::Assistant */
43

44
Assistant::Assistant()
45
    : proc(nullptr)
46
{
47
}
48

49
Assistant::~Assistant()
50
{
51
    if (proc && proc->state() == QProcess::Running) {
52
        proc->terminate();
53
        proc->waitForFinished(3000);
54
    }
55
}
56

57
void Assistant::showDocumentation(const QString &page)
58
{
59
    if (!startAssistant())
60
        return;
61
    if (!page.isEmpty()) {
62
        QTextStream str(proc);
63
        str << QLatin1String("setSource qthelp://org.freecad.usermanual/doc/")
64
            << page << QLatin1String("\n\n");
65
    }
66
}
67

68
bool Assistant::startAssistant()
69
{
70
    if (!proc) {
71
        proc = new QProcess();
72
        connect(proc, &QProcess::readyReadStandardOutput,
73
                this, &Assistant::readyReadStandardOutput);
74
        connect(proc, &QProcess::readyReadStandardError,
75
                this, &Assistant::readyReadStandardError);
76
    }
77

78
    if (proc->state() != QProcess::Running) {
79
#ifdef Q_OS_WIN
80
        QString app;
81
        app = QDir::toNativeSeparators(QString::fromStdString
82
            (App::Application::getHomePath()) + QLatin1String("bin/"));
83
#elif defined(Q_OS_MACOS)
84
        QString app = QCoreApplication::applicationDirPath() + QDir::separator();
85
#else
86
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
87
        QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator();
88
#else
89
        QString app = QLibraryInfo::path(QLibraryInfo::BinariesPath) + QDir::separator();
90
#endif
91
#endif
92
        app += QLatin1String("assistant");
93

94
        // get the name of the executable and the doc path
95
        QString exe = QString::fromStdString(App::Application::getExecutableName());
96
        QString doc = QString::fromStdString(App::Application::getHelpDir());
97
        QString qhc = doc + exe.toLower() + QLatin1String(".qhc");
98

99

100
        QFileInfo fi(qhc);
101
        if (!fi.isReadable()) {
102
            QMessageBox::critical(nullptr, tr("%1 Help").arg(exe),
103
                tr("%1 help files not found (%2). You might need to install the %1 documentation package.").arg(exe, qhc));
104
            return false;
105
        }
106

107
        static bool first = true;
108
        if (first) {
109
            Base::Console().Log("Help file at %s\n", (const char*)qhc.toUtf8());
110
            first = false;
111
        }
112

113
        // AppImage start
114
        // AppImage mount location changes on each start. Assistant caches freecad.qhc
115
        // file and sets an absolute path. As a result embedded documentation only works
116
        // on the first AppImage (help) run. Register the .gch file, to overcome the issue.
117
        static bool start = true;
118
        if (start) {
119
            char* appimage = getenv("APPIMAGE");
120
            if (appimage) {
121
                QString qch = doc + exe.toLower() + QLatin1String(".qch");
122
                QFileInfo fi(qch);
123
                if (fi.isReadable()) {
124
                    // Assume documentation is embedded
125
                    // Unregister qch file (path) from previous AppImage run
126
                    QStringList args;
127

128
                    args << QLatin1String("-collectionFile") << qhc
129
                         << QLatin1String("-unregister") << qch;
130

131
                    proc->start(app, args);
132

133
                    if (!proc->waitForFinished(50000)) {
134
                        QMessageBox::critical(nullptr, tr("%1 Help").arg(exe),
135
                            tr("Unable to launch Qt Assistant (%1)").arg(app));
136
                        return false;
137
                    }
138

139
                    // Register qch file (path) for current AppImage run
140
                    args.clear();
141

142
                    args << QLatin1String("-collectionFile") << qhc
143
                         << QLatin1String("-register") << qch;
144

145
                    proc->start(app, args);
146

147
                    if (!proc->waitForFinished(50000)) {
148
                        QMessageBox::critical(nullptr, tr("%1 Help").arg(exe),
149
                            tr("Unable to launch Qt Assistant (%1)").arg(app));
150
                        return false;
151
                    }
152
                }
153
            }
154
            start = false;
155
        }
156
        // AppImage end
157

158
        QStringList args;
159

160
        args << QLatin1String("-collectionFile") << qhc
161
             << QLatin1String("-enableRemoteControl");
162

163
        proc->start(app, args);
164

165
        if (!proc->waitForStarted()) {
166
            QMessageBox::critical(nullptr, tr("%1 Help").arg(exe),
167
                tr("Unable to launch Qt Assistant (%1)").arg(app));
168
            return false;
169
        }
170
    }
171

172
    return true;
173
}
174

175
void Assistant::readyReadStandardOutput()
176
{
177
    QByteArray data = proc->readAllStandardOutput();
178
    Base::Console().Log("Help view: %s\n", data.constData());
179
}
180

181
void Assistant::readyReadStandardError()
182
{
183
    QByteArray data = proc->readAllStandardError();
184
    Base::Console().Log("Help view: %s\n", data.constData());
185
}
186

187
#include "moc_Assistant.cpp"
188

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.