Skip to content

Commit

Permalink
Re #6907. First pass at making qt-assistant optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed May 2, 2013
1 parent 30a0d3c commit 9442590
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/HelpWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DLLExport HelpWindowImpl
{
public:
void showURL(const std::string & url=std::string());
void showWikiPage(const std::string &page=std::string());
void showAlgorithm(const std::string &name=std::string(), const int version=-1);
void showAlgorithm(const QString &name, const int version=-1);
void showFitFunction(const std::string &name=std::string());
Expand All @@ -29,6 +30,8 @@ class DLLExport HelpWindowImpl
/// Destructor
virtual ~HelpWindowImpl();

void openWebpage(const std::string &url);

/// Shared pointer to the process running qt assistant.
boost::shared_ptr<QProcess> m_process;
/// The full path of the collection file.
Expand Down
74 changes: 73 additions & 1 deletion Code/Mantid/MantidQt/API/src/HelpWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Poco/Thread.h>
#include <QByteArray>
#include <QDesktopServices>
#include <QUrl>
#include <stdexcept>

namespace MantidQt
Expand All @@ -15,11 +16,20 @@ namespace API
{
using std::string;

#ifndef __APPLE__
#define ENABLE_QTASSISTANT 1
#endif

/// Base url for all of the files in the project.
const string BASE_URL("qthelp://org.mantidproject/doc/");
/// Url to display if nothing else is suggested.
const string DEFAULT_URL(BASE_URL + "html/index.html");

/// Base url for all of the wiki links
const string WIKI_BASE_URL("http://mantidproject.org/");
/// Url to display if nothing else is suggested.
const string WIKI_DEFAULT_URL(WIKI_BASE_URL + "MantidPlot");

/**
* Default constructor shows the \link MantidQt::API::DEFAULT_URL.
*/
Expand All @@ -39,6 +49,12 @@ HelpWindowImpl::~HelpWindowImpl()
// do nothing
}

void HelpWindowImpl::openWebpage(const string &url)
{
m_log.debug() << "open url \"" << url << "\"\n";
QDesktopServices::openUrl(QUrl::QUrl(QLatin1String(url.c_str())));
}

/**
* Have the help window show a specific url. If the url doesn't exist
* this just pops up the default view for the help.
Expand All @@ -48,6 +64,7 @@ HelpWindowImpl::~HelpWindowImpl()
*/
void HelpWindowImpl::showURL(const string &url)
{
#ifdef ENABLE_QTASSISTANT
std::string urlToShow(url);
if (urlToShow.empty())
urlToShow = DEFAULT_URL;
Expand All @@ -61,6 +78,20 @@ void HelpWindowImpl::showURL(const string &url)
QByteArray ba;
ba.append(QLatin1String(temp.c_str()));
m_process->write(ba);
#else // ENABLE_QTASSISTANT
if (url.empty())
this->openWebpage(WIKI_DEFAULT_URL);
else
this->openWebpage(url);
#endif // ENABLE_QTASSISTANT
}

void HelpWindowImpl::showWikiPage(const string &page)
{
if (page.empty())
this->openWebpage(WIKI_DEFAULT_URL);
else
this->openWebpage(WIKI_BASE_URL + page);
}

/**
Expand All @@ -77,10 +108,17 @@ void HelpWindowImpl::showAlgorithm(const string &name, const int version)
// TODO jump to the version within the page
(void)version;

#ifdef ENABLE_QTASSISTANT
string url(BASE_URL + "html/Algo_" + name + ".html");
if (name.empty())
url = BASE_URL + "html/algorithms_index.html";
this->showURL(url);
#else // ENABLE_QTASSISTANT
if (name.empty())
this->showWikiPage("Category:Algorithms");
else
this->showWikiPage(name);
#endif // ENABLE_QTASSISTANT
}

/**
Expand All @@ -105,10 +143,19 @@ void HelpWindowImpl::showAlgorithm(const QString &name, const int version)
*/
void HelpWindowImpl::showFitFunction(const std::string &name)
{
#ifdef ENABLE_QTASSISTANT
string url(BASE_URL + "html/FitFunc_" + name + ".html");
if (name.empty())
{
url = BASE_URL + "html/fitfunctions_index.html";
}
this->showURL(url);
#else // ENABLE_QTASSISTANT
if (name.empty())
this->showWikiPage("Category:Fit_functions");
else
this->showWikiPage(name);
#endif // ENABLE_QTASSISTANT
}

/**
Expand Down Expand Up @@ -156,6 +203,7 @@ void HelpWindowImpl::start(const std::string &url)
Poco::File(m_cacheFile).remove();
}

#ifdef ENABLE_QTASSISTANT
// start the process
m_process = boost::make_shared<QProcess>();
QStringList args;
Expand Down Expand Up @@ -186,6 +234,10 @@ void HelpWindowImpl::start(const std::string &url)
m_log.debug() << m_assistantExe
<< " " << args.join(QString(" ")).toStdString()
<< " (state = " << m_process->state() << ")\n";
#else // ENABLE_QTASSISTANT
UNUSED_ARG(url);
// do not start qt-assistant if the feature is compiled out
#endif // ENABLE_QTASSISTANT
}

/**
Expand All @@ -212,6 +264,9 @@ bool HelpWindowImpl::isRunning()
*/
void HelpWindowImpl::findCollectionFile(std::string &binDir)
{
// notes the feature being disabled
m_collectionFile = "";
#ifdef ENABLE_QTASSISTANT
const std::string COLLECTION("mantid.qhc");

// try next to the executable
Expand Down Expand Up @@ -241,15 +296,31 @@ void HelpWindowImpl::findCollectionFile(std::string &binDir)
return;
}

// try a special place for mac/osx
path = Poco::Path(binDir, "../../share/doc/" + COLLECTION);
if (Poco::File(path).exists())
{
m_collectionFile = path.absolute().toString();
return;
}

// all tries have failed
throw std::runtime_error("Failed to find help system collection file \"" + COLLECTION + "\"");
m_log.warning("Failed to find help system collection file \"" + COLLECTION + "\"");
#else // ENABLE_QTASSISTANT
UNUSED_ARG(binDir);
#endif // ENABLE_QTASSISTANT
}

/**
* Determine the location of the collection and cache files.
*/
void HelpWindowImpl::determineFileLocs()
{
#ifndef ENABLE_QTASSISTANT
m_assistantExe = "";
m_cacheFile = "";
return;
#else // ENABLE_QTASSISTANT
// determine collection file location
string binDir = Mantid::Kernel::ConfigService::Instance().getDirectoryOfExecutable();
this->findCollectionFile(binDir);
Expand Down Expand Up @@ -313,6 +384,7 @@ void HelpWindowImpl::determineFileLocs()
m_log.debug() << "Failed to determine help cache file location\n"; // REMOVE
m_cacheFile = "";
}
#endif // ENABLE_QTASSISTANT
}

} // namespace API
Expand Down

0 comments on commit 9442590

Please sign in to comment.