Skip to content

Commit

Permalink
Adding helper class to launch qtassistan from ApplicationWindow.
Browse files Browse the repository at this point in the history
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
peterfpeterson committed Mar 4, 2013
1 parent dea7a30 commit f27ab8b
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/MantidPlot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set ( QTIPLOT_SRCS src/ApplicationWindow.cpp
src/Graph3D.cpp
src/Graph.cpp
src/Grid.cpp
src/HelpWindow.cpp
src/ImageDialog.cpp
src/ImageExportDialog.cpp
src/ImageMarker.cpp
Expand Down Expand Up @@ -277,6 +278,7 @@ set ( QTIPLOT_HDRS src/ApplicationWindow.h
src/Graph3D.h
src/Graph.h
src/Grid.h
src/HelpWindow.h
src/ImageDialog.h
src/ImageExportDialog.h
src/ImageMarker.h
Expand Down
8 changes: 6 additions & 2 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14606,7 +14606,9 @@ void ApplicationWindow::showMantidConcepts()
}
void ApplicationWindow::showalgorithmDescriptions()
{
QDesktopServices::openUrl(QUrl("http://www.mantidproject.org/Category:Algorithms"));
if (!m_helpWindow)
m_helpWindow = boost::make_shared<HelpWindow>();
m_helpWindow->showURL("qthelp://org.mantidproject/doc/algorithms_index.html");
}

void ApplicationWindow::showSetupParaview()
Expand All @@ -14630,7 +14632,9 @@ void ApplicationWindow::showFirstTimeSetup()
*/
void ApplicationWindow::showmantidplotHelp()
{
QDesktopServices::openUrl(QUrl("http://www.mantidproject.org/MantidPlot:_Help"));
if (!m_helpWindow)
m_helpWindow = boost::make_shared<HelpWindow>();
m_helpWindow->showURL("qthelp://org.mantidproject/doc/index.html");
}

//
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Description : QtiPlot's main window
#include <QSet>
#include <QSettings>

#include "HelpWindow.h"
#include "Table.h"
#include "ScriptingEnv.h"
#include "Scripted.h"
Expand Down Expand Up @@ -1367,6 +1368,8 @@ public slots:
// Flag telling if table values should be automatically recalculated when values in a column are modified.
bool d_auto_update_table_values;
int d_matrix_undo_stack_size;
/// Smart pointer to the help window
boost::shared_ptr<HelpWindow> m_helpWindow;

/// A method to populate the CurveLayout struct on loading a project
CurveLayout fillCurveSettings(const QStringList & curve, unsigned int offset = 0);
Expand Down
214 changes: 214 additions & 0 deletions Code/Mantid/MantidPlot/src/HelpWindow.cpp
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 = "";
}
}
33 changes: 33 additions & 0 deletions Code/Mantid/MantidPlot/src/HelpWindow.h
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

0 comments on commit f27ab8b

Please sign in to comment.