1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/VIEW/WIDGETS/helpViewer.h>
#include <BALL/VIEW/KERNEL/message.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/SYSTEM/path.h>
#include <BALL/FORMAT/lineBasedFile.h>
#include <QtWidgets/QMenu>
#include <QtGui/QMouseEvent>
#include <QtGui/QKeyEvent>
#include <QtCore/QEvent>
#include <QtGui/QTextCursor>
#include <QtWidgets/QToolBar>
using namespace std;
namespace BALL
{
namespace VIEW
{
MyTextBrowser::MyTextBrowser(QWidget* parent, const char*)
: QTextBrowser(parent),
forward_(false),
backward_(false)
{
setOpenExternalLinks(true);
}
HelpViewer::HelpViewer(QWidget* parent, const char* name)
: DockWidget(parent, name),
project_("BALLView"),
default_page_("index.html"),
browser_( new MyTextBrowser(this)),
whats_this_mode_(false),
ignore_event_(false),
whats_this_(true),
whats_action_(0)
{
hide();
setGuest(*browser_);
registerWidget(this);
browser_->setReadOnly(true);
}
HelpViewer::~HelpViewer()
{
#ifdef BALL_VIEW_DEBUG
Log.error() << "Destructing object " << this << " of class HelpViewer" << endl;
#endif
qApp->removeEventFilter(this);
}
void HelpViewer::initializeWidget(MainControl& main_control)
{
DockWidget::initializeWidget(main_control);
QAction* action = insertMenuEntry(MainControl::HELP, tr((project_ + " Documentation").c_str()), this, SLOT(showHelp()),
"Shortcut|MainControl|Documentation|Help", QKeySequence(),
tr(""), UIOperationMode::MODE_ADVANCED);
setIcon(action, "actions/help-hint", true);
qApp->installEventFilter(this);
}
void HelpViewer::showHelp()
{
showHelp(default_page_);
}
void HelpViewer::showHelp(const String& URL)
{
showHelp(URL, "");
}
void HelpViewer::showHelp(const String& org_url, String entry)
{
String url = org_url;
String fragment;
if (url.has('#'))
{
url = url.before("#");
fragment = org_url.after("#");
}
QUrl qurl = QUrl::fromLocalFile((base_dir_ + url).c_str());
if (fragment != "") qurl.setFragment(fragment.c_str());
browser_->setSource(qurl);
QTextCursor ct = browser_->textCursor();
if (!ct.atStart())
{
ct.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
browser_->setTextCursor(ct);
}
if (entry != "") browser_->find(entry.c_str(), QTextDocument::FindCaseSensitively);
if (!isVisible())
{
show();
setFloating(true);
showMaximized();
}
}
void HelpViewer::onNotify(Message *message)
{
#ifdef BALL_VIEW_DEBUG
Log.error() << "HelpViewer" << this << "onNotify " << message << std::endl;
#endif
if (RTTI::isKindOf<RegisterHelpSystemMessage>(message))
{
RegisterHelpSystemMessage* msg = RTTI::castTo<RegisterHelpSystemMessage>(*message);
if (msg->isRegister())
{
registerForHelpSystem(msg->getObject(), msg->getURL());
}
else
{
unregisterForHelpSystem(msg->getObject());
}
return;
}
if (!RTTI::isKindOf<ShowHelpMessage>(message)) return;
ShowHelpMessage* msg = RTTI::castTo<ShowHelpMessage>(*message);
bool classname = false;
String project = msg->getProject();
String url = msg->getURL();
if (project.hasSuffix(" class"))
{
classname = true;
project = project.before(" ");
}
if (project != project_) return;
if (classname)
{
showDocumentationFor(url, msg->getEntry());
return;
}
showHelp(url, msg->getEntry());
}
void HelpViewer::showDocumentationFor(const String& classname, const String& member)
{
if (classes_to_files_.size() == 0) collectClasses_();
String classn = classname;
if (!classes_to_files_.has(classn))
{
classn = String("T") + classn;
if (!classes_to_files_.has(classn)) return;
}
showHelp(classes_to_files_[classn], member);
}
void HelpViewer::collectClasses_()
{
}
void HelpViewer::setDefaultPage(const String& url)
{
default_page_ = url;
QUrl qurl = QUrl::fromLocalFile((base_dir_ + url).c_str());
browser_->setSource(qurl);
}
const String& HelpViewer::getDefaultPage() const
{
return default_page_;
}
const String& HelpViewer::getBaseDirectory() const
{
return base_dir_;
}
void HelpViewer::setBaseDirectory(const String& dir)
{
if (dir == "") return;
base_dir_ = dir;
QUrl qurl = QUrl::fromLocalFile((base_dir_ + default_page_).c_str());
browser_->setSource(qurl);
}
void HelpViewer::enterWhatsThisMode()
{
qApp->setOverrideCursor(Qt::WhatsThisCursor);
whats_this_mode_ = true;
}
void HelpViewer::exitWhatsThisMode()
{
if (!whats_this_mode_) return;
whats_this_mode_ = false;
qApp->restoreOverrideCursor();
}
bool HelpViewer::showDocumentationForObject()
{
QPoint point = QCursor::pos();
QWidget* widget = qApp->widgetAt(point);
if (widget == 0) return false;
/////////////////////////////////////////////
// show help for widget
/////////////////////////////////////////////
if (showHelpFor(widget))
{
exitWhatsThisMode();
return true;
}
/////////////////////////////////////////////
// show help for menu entry
/////////////////////////////////////////////
// catch block is needed on windows,
// otherwise we get a uncaught exception, no idea why
// maybe the library has a bug under windows
try
{
if (RTTI::isKindOf<QMenu>(widget))
{
ignore_event_ = true;
// nothing happens if we dont have a docu entry
QAction* id = getMainControl()->getLastHighLightedMenuEntry();
if (docu_entries_.has(id))
{
showHelp(docu_entries_[id]);
exitWhatsThisMode();
return true;
}
}
}
catch(...)
{
}
return false;
}
bool HelpViewer::eventFilter(QObject*, QEvent* e)
{
/////////////////////////////////////////////
// Prevent opening a menu entry when obtaining whats this info for a menu entry
/////////////////////////////////////////////
if (ignore_event_)
{
if (e->type() == QEvent::MouseButtonRelease)
{
ignore_event_ = false;
getMainControl()->menuBar()->hide();
getMainControl()->menuBar()->show();
return true;
}
return false;
}
/////////////////////////////////////////////
// Show Documentation if F1 is pressed
/////////////////////////////////////////////
if (e->type() == QEvent::KeyPress)
{
QKeyEvent* ke = (QKeyEvent*) e;
if (ke->key() != Qt::Key_F1 ||
ke->modifiers() != Qt::NoModifier)
{
return false;
}
if (ke->key() == Qt::Key_Escape)
{
if (whats_this_mode_)
{
exitWhatsThisMode();
return true;
}
}
showDocumentationForObject();
return true;
}
/////////////////////////////////////////////
// now react only in whats this mode and if a mouse button is pressed
/////////////////////////////////////////////
if (!whats_this_mode_ ||
e->type() != QEvent::MouseButtonPress)
{
return false;
}
/////////////////////////////////////////////
// exit whats this mode with right mouse click
/////////////////////////////////////////////
QMouseEvent* me = (QMouseEvent*) e;
if (me->button() != Qt::LeftButton)
{
exitWhatsThisMode();
return true;
}
if (me->button() != Qt::LeftButton) return false;
QPoint point = QCursor::pos();
QWidget* widget = qApp->widgetAt(point);
QMenu* menu = 0;
QMenuBar* menu_bar = 0;
if (widget != 0)
{
menu = dynamic_cast<QMenu*>(widget);
menu_bar = dynamic_cast<QMenuBar*>(widget);
}
if (menu_bar != 0) return false;
bool shown_docu = showDocumentationForObject();
if (menu != 0)
{
if (shown_docu)
{
menu->hide();
}
else
{
return false;
}
}
return true;
}
void HelpViewer::registerForHelpSystem(const QObject* object, const String& docu_entry)
{
docu_entries_[object] = docu_entry;
}
void HelpViewer::unregisterForHelpSystem(const QObject* object)
{
docu_entries_.erase(object);
}
bool HelpViewer::showHelpFor(const QObject* object)
{
if (object == 0) return false;
HashMap<const QObject*, String>::Iterator to_find;
QObject* object2 = (QObject*) object;
QWidget* widget = dynamic_cast<QWidget*>(object2);
if (widget && widget->parent() != 0)
{
QToolBar* tb = dynamic_cast<QToolBar*>(widget->parent());
if (tb != 0)
{
QList<QAction *> acs = widget->actions();
if (acs.size() == 1)
{
to_find = docu_entries_.find(*acs.begin());
if (to_find != docu_entries_.end())
{
showHelp((*to_find).second);
return true;
}
}
}
}
while (object2 != 0)
{
to_find = docu_entries_.find(object2);
if (to_find != docu_entries_.end()) break;
object2 = object2->parent();
}
if (object2 == 0)
{
setStatusbarText(tr("No documentation for this widget available!"), true);
return false;
}
showHelp((*to_find).second);
return true;
}
bool HelpViewer::hasHelpFor(const QObject* object) const
{
return docu_entries_.has(object);
}
String HelpViewer::getHelpEntryFor(const QObject* widget) const
{
if (!docu_entries_.has(widget)) return false;
return docu_entries_[widget];
}
} // VIEW
} // namespace BALL
|