-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding helper class to launch qtassistan from ApplicationWindow.
Re #6630. The helper class is rhobust, but only tested on linux so far. Still needs help with other operating systems and working on installed versions.
- Loading branch information
1 parent
dea7a30
commit f27ab8b
Showing
5 changed files
with
258 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
#include "HelpWindow.h" | ||
#include "MantidKernel/ConfigService.h" | ||
#include <boost/make_shared.hpp> | ||
#include <iostream> | ||
#include <Poco/File.h> | ||
#include <Poco/Path.h> | ||
#include <QByteArray> | ||
#include <QDesktopServices> | ||
#include <stdexcept> | ||
#include <strstream> | ||
|
||
using std::string; | ||
|
||
/// Base url for all of the files in the project | ||
const string BASEURL("qthelp://org.mantidproject/doc/"); | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
HelpWindow::HelpWindow() : | ||
m_collectionFile(""), | ||
m_cacheFile(""), | ||
m_log(Mantid::Kernel::Logger::get("HelpWindow")) | ||
{ | ||
this->determineFileLocs(); | ||
this->start(); | ||
} | ||
|
||
/// Destructor does nothing. | ||
HelpWindow::~HelpWindow() | ||
{ | ||
// do nothing | ||
} | ||
|
||
/** | ||
* Have the help window show a specific url. If the url doesn't exist | ||
* this just pops up the default view for the help. | ||
* | ||
* \param url The url to open. This should start with \link BASEURL. | ||
*/ | ||
void HelpWindow::showURL(const string &url) | ||
{ | ||
// make sure the process is going | ||
this->start(); | ||
|
||
m_log.debug() << "open help url \"" << url << "\"\n"; | ||
string temp("setSource " + url + "\n"); | ||
QByteArray ba; | ||
ba.append(QLatin1String(temp.c_str())); | ||
m_process->write(ba); | ||
} | ||
|
||
/** | ||
* Show the help page for a particular algorithm. The page is picked | ||
* using matching naming conventions. | ||
* | ||
* @param name The name of the algorithm to show. | ||
* @param version The version of the algorithm to jump do. The default | ||
* value (-1) will show the top of the page. | ||
*/ | ||
void HelpWindow::showAlgorithm(const string &name, const int version) | ||
{ | ||
// TODO jump to the version within the page | ||
(void)version; | ||
|
||
string url(BASEURL + "Algo_" + name + ".html"); | ||
this->showURL(url); | ||
} | ||
|
||
/** | ||
* Show the help page for a particular fit function. The page is | ||
* picked using matching naming conventions. | ||
* | ||
* @param name The name of the fit function to show. | ||
*/ | ||
void HelpWindow::showFitFunction(const std::string &name) | ||
{ | ||
string url(BASEURL + "FitFunc_" + name + ".html"); | ||
} | ||
|
||
/** | ||
* Start up the help browser in a separate process. | ||
* | ||
* This will only do something if the browser is not already | ||
* running. Due to a bug in qt 4.8.1 this will delete the | ||
* cache file every time the browser is started. | ||
*/ | ||
void HelpWindow::start() | ||
{ | ||
// check if it is already started | ||
if (this->isRunning()) | ||
{ | ||
m_log.debug() << "helpwindow process already running\n"; | ||
return; | ||
} | ||
|
||
// see if chache file exists and remove it | ||
if ((!m_cacheFile.empty()) && (Poco::File(m_cacheFile).exists())) | ||
{ | ||
m_log.debug() << "Removing help cache file \"" << m_cacheFile << "\"\n"; | ||
Poco::File(m_cacheFile).remove(); | ||
} | ||
|
||
// start the process | ||
m_process = boost::make_shared<QProcess>(); | ||
QStringList args; | ||
args << QLatin1String("-collectionFile") | ||
<< QLatin1String(m_collectionFile.c_str()) | ||
<< QLatin1String("-enableRemoteControl"); | ||
m_process->start(QLatin1String("/usr/bin/assistant"), args); | ||
if (!m_process->waitForStarted()) | ||
return; | ||
m_log.debug() << QString("/usr/bin/assistant").toStdString() | ||
<< " " << args.join(QString(" ")).toStdString() | ||
<< " (state = " << m_process->state() << ")\n"; | ||
} | ||
|
||
/** | ||
* @return True if the browser is running. | ||
*/ | ||
bool HelpWindow::isRunning() | ||
{ | ||
// NULL pointer definitely isn't running | ||
if (!m_process) | ||
return false; | ||
|
||
// ask the process for its state | ||
if (m_process->state() == QProcess::NotRunning) | ||
return false; | ||
else | ||
return true; | ||
} | ||
|
||
/** | ||
* Determine the location of the collection file, "mantid.qhc". This | ||
* checks in multiple locations and can throw an exception. | ||
* | ||
* @param binDir The location of the mantid executable. | ||
*/ | ||
void HelpWindow::findCollectionFile(std::string &binDir) | ||
{ | ||
const std::string COLLECTION("mantid.qhc"); | ||
|
||
// try next to the executable | ||
Poco::Path path(binDir, COLLECTION); | ||
if (Poco::File(path).exists()) | ||
{ | ||
m_collectionFile =path.absolute().toString(); | ||
return; | ||
} | ||
|
||
// try one level up | ||
path = Poco::Path(binDir, "../"+COLLECTION); | ||
if (Poco::File(path).exists()) | ||
{ | ||
m_collectionFile = path.absolute().toString(); | ||
return; | ||
} | ||
|
||
// try where the builds will put it | ||
path = Poco::Path(binDir, "../qtassistant/"+COLLECTION); | ||
if (Poco::File(path).exists()) | ||
{ | ||
m_collectionFile = path.absolute().toString(); | ||
return; | ||
} | ||
|
||
// try in a good linux install location | ||
path = Poco::Path(binDir, "../share/doc/" + COLLECTION); | ||
if (Poco::File(path).exists()) | ||
{ | ||
m_collectionFile = path.absolute().toString(); | ||
return; | ||
} | ||
|
||
// all tries have failed | ||
std::strstream msg; | ||
msg << "Failed to find help system collection file \"" << COLLECTION << "\""; | ||
throw std::runtime_error(msg.str()); | ||
} | ||
|
||
/** | ||
* Determine the location of the collection and cache files. | ||
*/ | ||
void HelpWindow::determineFileLocs() | ||
{ | ||
// determine collection file location | ||
string binDir = Mantid::Kernel::ConfigService::Instance().getDirectoryOfExecutable(); | ||
this->findCollectionFile(binDir); | ||
m_log.debug() << "using collection file \"" << m_collectionFile << "\"\n"; | ||
|
||
// determine cache file location | ||
m_cacheFile = "mantid.qhc"; | ||
QString dataLoc = QDesktopServices::storageLocation(QDesktopServices::DataLocation); | ||
if (dataLoc.endsWith("mantidproject")) | ||
{ | ||
Poco::Path path (dataLoc.toStdString(), m_cacheFile); | ||
m_cacheFile = path.absolute().toString(); | ||
} | ||
else if (dataLoc.endsWith("MantidPlot")) // understood to end in "ISIS/MantidPlot" | ||
{ | ||
Poco::Path path(dataLoc.toStdString()); | ||
path = path.parent(); // drop off "MantidPlot" | ||
path = path.parent(); // drop off "ISIS" | ||
path = Poco::Path(path, "mantidproject"); | ||
path = Poco::Path(path, m_cacheFile); | ||
m_cacheFile = path.absolute().toString(); | ||
} | ||
else | ||
{ | ||
m_log.debug() << "Failed to determine help cache file location\n"; // REMOVE | ||
m_cacheFile = ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef HELPWINDOW_H | ||
#define HELPWINDOW_H | ||
#include <boost/shared_ptr.hpp> | ||
#include <QProcess> | ||
#include <string> | ||
#include "MantidKernel/Logger.h" | ||
|
||
class HelpWindow | ||
{ | ||
public: | ||
HelpWindow(); | ||
virtual ~HelpWindow(); | ||
void showURL(const std::string & url); | ||
void showAlgorithm(const std::string &name, const int version=-1); | ||
void showFitFunction(const std::string &name); | ||
|
||
private: | ||
/// Shared pointer to the process running qt assistant. | ||
boost::shared_ptr<QProcess> m_process; | ||
/// The full path of the collection file. | ||
std::string m_collectionFile; | ||
/** The full path of the cache file. If it is not | ||
determined this is an empty string. */ | ||
std::string m_cacheFile; | ||
/// The logger for the class. | ||
Mantid::Kernel::Logger& m_log; | ||
|
||
void start(); | ||
bool isRunning(); | ||
void findCollectionFile(std::string & binDir); | ||
void determineFileLocs(); | ||
}; | ||
#endif // HELPWINDOW_H |