My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
qucshelp.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qucshelp.cpp - description
3  -------------------
4  begin : Sun Jan 11 2004
5  copyright : (C) 2004 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include "qucshelp.h"
23 #include "htmldatafetcher.h"
24 
25 #include <qpushbutton.h>
26 #include <qaction.h>
27 #include <qpixmap.h>
28 #include <qfile.h>
29 #include <qtextstream.h>
30 #include <qpopupmenu.h>
31 #include <qmenubar.h>
32 #include <qapplication.h>
33 #include <qlistview.h>
34 
35 
36 
37 QucsHelp::QucsHelp(const QString& page)
38 {
39  currentIndex = 0;
40  dataFetcher = new HtmlDataFetcher();
41  links = dataFetcher->fetchLinksToFiles(QucsHelpDir.filePath("index.html"));
42  // set application icon
43  setIcon (QPixmap(QucsSettings.BitmapDir + "big.qucs.xpm"));
44  setCaption(tr("Qucs Help System"));
45 
46  textBrowser = new TextBrowser(this);
47  textBrowser->setMinimumSize(400,200);
48  setCentralWidget(textBrowser);
49  setupActions();
50  createSidebar();
51 
52  textBrowser->setSource(QucsHelpDir.filePath(links[0]));
53 
54  // .......................................
55  if(!page.isEmpty())
56  textBrowser->setSource(QucsHelpDir.filePath(page));
57 }
58 
60 {}
61 
62 void QucsHelp::setupActions()
63 {
64  QToolBar *toolbar = new QToolBar(this,"main_toolbar");
65  QMenuBar *bar = menuBar();
66  statusBar();
67 
68  const QKeySequence ks = QKeySequence();
69 
70  QAction *quitAction = new QAction(QIconSet(QPixmap(QucsSettings.BitmapDir + "quit.png")),
71  tr("&Quit"), CTRL+Key_Q, this);
72  QAction *backAction = new QAction(QIconSet(QPixmap(QucsSettings.BitmapDir + "back.png")),
73  tr("&Back"), ALT+Key_Left, this);
74  QAction *forwardAction = new QAction(QIconSet(QPixmap(QucsSettings.BitmapDir + "forward.png")),
75  tr("&Forward"), ALT+Key_Right, this);
76  QAction *homeAction = new QAction(QIconSet(QPixmap(QucsSettings.BitmapDir + "home.png")),
77  tr("&Home"),CTRL+Key_H,this);
78  previousAction = new QAction(QIconSet(QPixmap(QucsSettings.BitmapDir + "previous.png")),tr("&Previous"),
79  ks, this);
80  nextAction = new QAction(QIconSet(QPixmap(QucsSettings.BitmapDir + "next.png")),
81  tr("&Next"), ks, this);
82  viewBrowseDock = new QAction(tr("&Table of Contents"), 0, this);
83  viewBrowseDock->setToggleAction(true);
84  viewBrowseDock->setOn(true);
85  viewBrowseDock->setStatusTip(tr("Enables/disables the table of contents"));
86  viewBrowseDock->setWhatsThis(tr("Table of Contents\n\nEnables/disables the table of contents"));
87 
88  connect(quitAction,SIGNAL(activated()),qApp,SLOT(quit()));
89 
90  connect(backAction,SIGNAL(activated()),textBrowser,SLOT(backward()));
91  connect(textBrowser,SIGNAL(backwardAvailable(bool)),backAction,SLOT(setEnabled(bool)));
92 
93  connect(forwardAction,SIGNAL(activated()),textBrowser,SLOT(forward()));
94  connect(textBrowser,SIGNAL(forwardAvailable(bool)),forwardAction,SLOT(setEnabled(bool)));
95 
96  connect(homeAction,SIGNAL(activated()),textBrowser,SLOT(home()));
97 
98  connect(textBrowser,SIGNAL(sourceChanged(const QString &)),this,SLOT(slotSourceChanged(const QString&)));
99  connect(previousAction,SIGNAL(activated()),this,SLOT(previousLink()));
100  connect(nextAction,SIGNAL(activated()),this,SLOT(nextLink()));
101  connect(viewBrowseDock, SIGNAL(toggled(bool)), SLOT(slotToggleSidebar(bool)));
102 
103  backAction->addTo(toolbar);
104  forwardAction->addTo(toolbar);
105  toolbar->addSeparator();
106  homeAction->addTo(toolbar);
107  previousAction->addTo(toolbar);
108  nextAction->addTo(toolbar);
109  toolbar->addSeparator();
110  quitAction->addTo(toolbar);
111 
112  QPopupMenu *fileMenu = new QPopupMenu(this);
113  quitAction->addTo(fileMenu);
114 
115  QPopupMenu *viewMenu = new QPopupMenu(this);
116  backAction->addTo(viewMenu);
117  forwardAction->addTo(viewMenu);
118  homeAction->addTo(viewMenu);
119  previousAction->addTo(viewMenu);
120  nextAction->addTo(viewMenu);
121  viewMenu->insertSeparator();
122  viewBrowseDock->addTo(viewMenu);
123 
124  QPopupMenu *helpMenu = new QPopupMenu(this);
125  helpMenu->insertItem(tr("&About Qt"),qApp,SLOT(aboutQt()));
126 
127  bar->insertItem(tr("&File"), fileMenu );
128  bar->insertItem(tr("&View"),viewMenu);
129  bar->insertSeparator();
130  bar->insertItem(tr("&Help"),helpMenu);
131 
132 }
133 
134 
135 void QucsHelp::createSidebar()
136 {
137  dock = new QDockWindow(QDockWindow::InDock,this);
138  dock->setResizeEnabled(true);
139  dock->setCloseMode(QDockWindow::Always);
140  connect(dock,SIGNAL(visibilityChanged(bool)),this,SLOT(slotToggleSidebarAction(bool)));
141 
142  chaptersView = new QListView(dock,"chapters_view");
143  chaptersView->setRootIsDecorated(false);
144  chaptersView->addColumn(tr("Contents"));
145  chaptersView->setSorting(-1);
146  chaptersView->setSelectionMode(QListView::Single);
147 
148  dock->setWidget(chaptersView);
149  moveDockWindow(dock,QDockWindow::Left);
150 
151 
152  QStringList l = dataFetcher->fetchChapterTexts(QucsHelpDir.filePath("index.html"));
153  for(int i=l.count()-1;i>=0;i--)
154  chaptersView->insertItem(new QListViewItem(chaptersView,l[i],QString::number(i+1)));
155 
156  QListViewItem *curItem = new QListViewItem(chaptersView,tr("Home"),QString::number(0));
157  chaptersView->insertItem(curItem);
158  chaptersView->setSelected(curItem,true);
159 
160  connect(chaptersView,SIGNAL(selectionChanged()),this,SLOT(displaySelectedChapter()));
161 }
162 
163 void QucsHelp::displaySelectedChapter()
164 {
165  if(chaptersView->selectedItem() == 0)
166  return;
167  uint y = chaptersView->selectedItem()->text(1).toUInt();
168  Q_ASSERT(y < links.count());
169  textBrowser->setSource(QucsHelpDir.filePath(links[y]));
170  cachedSelectedText = chaptersView->selectedItem()->text(1);
171 }
172 //This slot updates next and previous actions i.e enabling/disabling
173 void QucsHelp::slotSourceChanged(const QString& _str)
174 {
175  QString str(_str);
176  // Remove '#*' chars in link since we don't check '#top,etc' while tracking previous actions
177  int hashPos = str.findRev('#');
178  if(hashPos != -1)
179  str = str.left(hashPos);
180  // Don't do anything if accessing same page
181  if(str == currentSource)
182  return;
183 
184  bool found = false;
185  for(unsigned int i=0;i < links.count(); i++)
186  {
187  if(str.endsWith(links[i]))
188  {
189  currentIndex = i;
190  previousAction->setEnabled(bool(i!=0));
191  nextAction->setEnabled(bool(i+1 != links.count()));
192  QString temp = cachedSelectedText;
193  if(chaptersView->selectedItem())
194  temp = chaptersView->selectedItem()->text(1);
195  if(temp.toUInt() != i)
196  {
197  QListViewItem *item = chaptersView->findItem(QString::number(i),1);
198  if(item != 0l)
199  {
200  chaptersView->blockSignals(true);
201  chaptersView->setSelected(item,true);
202  chaptersView->blockSignals(false);
203  }
204  }
205  found = true;
206  break;
207  }
208  }
209  if(found == false) // some error
210  {
211  textBrowser->setSource(QucsHelpDir.filePath(links[0]));
212  currentSource = QucsHelpDir.filePath(links[0]);
213  qDebug("QucsHelp::slotSourceChanged(): Link mismatch \n Link: %s",str.ascii());
214  }
215  else
216  currentSource = str;
217 }
218 
219 
220 void QucsHelp::previousLink()
221 {
222  if(currentIndex > 0)
223  --currentIndex;
224  textBrowser->setSource(QucsHelpDir.filePath(links[currentIndex]));
225 }
226 
227 void QucsHelp::nextLink()
228 {
229  ++currentIndex;
230  if(currentIndex >= links.count())
231  currentIndex = links.count();
232  textBrowser->setSource(QucsHelpDir.filePath(links[currentIndex]));
233 }
234 
235 void QucsHelp::slotToggleSidebar(bool b)
236 {
237  dock->setShown(b);
238 }
239 
240 void QucsHelp::slotToggleSidebarAction(bool b)
241 {
242  viewBrowseDock->blockSignals(true);
243  viewBrowseDock->setOn(b);
244  viewBrowseDock->blockSignals(false);
245 }