GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
webinfo.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009 P. L. Lucas
4 Copyright (C) 2012-2013 Jacob Dawid
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 // Author: P. L. Lucas
25 // Author: Jacob Dawid <jacob.dawid@gmail.com>
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 
31 #include "webinfo.h"
32 #include <QVBoxLayout>
33 #include <QHBoxLayout>
34 #include <QApplication>
35 #include <QClipboard>
36 
37 #include "file-ops.h"
38 #include "help.h"
39 #include "defaults.h"
40 
41 
43  : QWidget (p)
44 {
45  _font_web = font ();
46 
47  QVBoxLayout *vbox_layout = new QVBoxLayout ();
48  vbox_layout->setMargin (0);
49  setLayout (vbox_layout);
50 
51  QHBoxLayout *hbox_layout = new QHBoxLayout ();
52  hbox_layout->setMargin (0);
53  hbox_layout->setSpacing (0);
54  vbox_layout->addLayout (hbox_layout);
55 
56  _tab_bar = new QTabBar (this);
57  _tab_bar->setSizePolicy (QSizePolicy::Preferred,QSizePolicy::Preferred);
58  _tab_bar->setExpanding (false);
59  _tab_bar->setTabsClosable (true);
60  _tab_bar->setMovable (true);
61  hbox_layout->addWidget (_tab_bar);
62 
63  _zoom_in_button = new QToolButton (this);
64  _zoom_in_button->setIcon (QIcon (":/actions/icons/zoom-in.png"));
65  hbox_layout->addWidget (_zoom_in_button);
66 
67  _zoom_out_button = new QToolButton (this);
68  _zoom_out_button->setIcon (QIcon (":/actions/icons/zoom-out.png"));
69  hbox_layout->addWidget (_zoom_out_button);
70 
71  _stacked_widget = new QStackedWidget (this);
72  vbox_layout->addWidget (_stacked_widget);
73 
74  hbox_layout = new QHBoxLayout ();
75  vbox_layout->addLayout (hbox_layout);
76 
77  _search_line_edit = new QLineEdit(this);
78 #ifdef HAVE_SETPLACEHOLDERTEXT
79  _search_line_edit->setPlaceholderText (
80  tr ("Type here and press \'Return\' to search"));
81 #endif
82  hbox_layout->addWidget (_search_line_edit);
83 
84  _search_check_box = new QCheckBox (tr ("Global search"));
85  hbox_layout->addWidget (_search_check_box);
86 
87  connect (_tab_bar, SIGNAL (tabCloseRequested (int)), this,
88  SLOT (close_tab (int)));
89  connect (_tab_bar, SIGNAL (currentChanged (int)), this,
90  SLOT (current_tab_changed (int)));
91  connect (_zoom_in_button, SIGNAL (clicked ()), this, SLOT (zoom_in ()));
92  connect (_zoom_out_button, SIGNAL (clicked ()), this, SLOT (zoom_out ()));
93  connect (_search_line_edit, SIGNAL (returnPressed ()), this, SLOT (search ()));
94 
95  resize (500, 300);
96 
97  set_info_path (QString::fromStdString (Vinfo_file));
98 
99 }
100 
101 void
102 webinfo::set_info_path (const QString& info_path)
103 {
104  _parser.set_info_path (info_path);
105  load_node ("Top");
106 }
107 
108 void
109 webinfo::load_node (const QString& node_name)
110 {
111  // no XREF in the tabs
112  QString tab_text = node_name;
113  tab_text.replace("XREF","");
114 
115  //Check if node has been already opened.
116  for (int i = 0; i < _tab_bar->count (); i++)
117  {
118  if (tab_text == _tab_bar->tabText (i))
119  {
120  _tab_bar->setCurrentIndex (i);
121  return;
122  }
123  }
124 
125  QString text = _parser.search_node (node_name);
126  int i = _parser.is_ref (node_name);
127  _text_browser = addNewTab (tab_text);
128  _text_browser->setHtml (_parser.node_text_to_html (text, i - 1, "anchor"));
129 
130  if (i != -1)
131  {
132  _text_browser->scrollToAnchor ("anchor");
133  }
134 }
135 
136 void
138 {
139  QString node = link.toString ();
140  if (node.at (0) != '#')
141  load_node (node);
142  else
143  _text_browser->scrollToAnchor (node);
144 }
145 
146 void
148 {
149  QVariant tab_data = _tab_bar->tabData (index);
150  _text_browser = static_cast<QTextBrowser*> (tab_data.value<void*> ());
151 
152  _stacked_widget->setCurrentIndex (_stacked_widget->indexOf (_text_browser));
153 
154  if (_text_browser->font () != _font_web)
155  {
156  _text_browser->setFont (_font_web);
157  }
158 }
159 
160 QTextBrowser *
161 webinfo::addNewTab (const QString& name)
162 {
163  _text_browser = new QTextBrowser (this);
164  _text_browser->setOpenLinks (false);
165  _text_browser->show ();
166 
167  connect (_text_browser, SIGNAL (anchorClicked (const QUrl &)), this,
168  SLOT (link_clicked (const QUrl &)) );
169  disconnect(_tab_bar, SIGNAL (currentChanged(int)), this,
170  SLOT (current_tab_changed (int)));
171 
172  int ns = _stacked_widget->addWidget (_text_browser);
173  _stacked_widget->setCurrentIndex (ns);
174 
175  int nt = _tab_bar->addTab (name);
176  _tab_bar->setCurrentIndex (nt);
177  QVariant tab_data;
178  tab_data.setValue (static_cast<void*> (_text_browser));
179  _tab_bar->setTabData (nt, tab_data);
180 
181  connect (_tab_bar, SIGNAL (currentChanged (int)), this,
182  SLOT (current_tab_changed (int)));
183 
184  if (_text_browser->font () != _font_web)
185  {
186  _text_browser->setFont (_font_web);
187  }
188  return _text_browser;
189 }
190 
191 void
193 {
194  if (_tab_bar->count () > 1)
195  {
196  QVariant tab_data = _tab_bar->tabData (index);
197  QWidget *w = static_cast<QWidget*> (tab_data.value<void*> ());
198  _stacked_widget->removeWidget (w);
199  delete w;
200 
201  _tab_bar->removeTab (index);
202  }
203 }
204 
205 void
206 webinfo::load_ref (const QString &ref_name)
207 {
208  QString text = _parser.find_ref (ref_name);
209  if (text.length () > 0)
210  {
211  load_node (text);
212  }
213  else
214  {
215  // not found
216  load_node("Top");
217  }
218 
219  if (_text_browser)
220  _text_browser->setFocus();
221 }
222 
223 void
225 {
226  if (_search_check_box->isChecked ())
227  {
228  // Global search
229  QString results = _parser.global_search (_search_line_edit->text (), 5);
230  _text_browser=addNewTab ("Results for: " + _search_line_edit->text ());
231  _text_browser->setHtml (results);
232  }
233  else
234  {
235  // Local search
236  _text_browser->find (_search_line_edit->text ());
237  }
238 }
239 
240 void
242 {
243  _font_web.setPointSize (_font_web.pointSize() + 1);
244  _text_browser->setFont (_font_web);
245 }
246 
247 void
249 {
250  _font_web.setPointSize (_font_web.pointSize() - 1);
251  _text_browser->setFont (_font_web);
252 }
253 
254 void
256 {
257  if (_search_line_edit->hasFocus () && _search_line_edit->hasSelectedText ())
258  {
259  QClipboard *clipboard = QApplication::clipboard ();
260 
261  clipboard->setText (_search_line_edit->selectedText ());
262  }
263  if (_text_browser->hasFocus ())
264  {
265  _text_browser->copy ();
266  }
267 }
268 
269 void
271 {
272  if (_search_line_edit->hasFocus ())
273  {
274  QClipboard *clipboard = QApplication::clipboard ();
275  QString str = clipboard->text ();
276  if (str.length () > 0)
277  _search_line_edit->insert (str);
278  }
279 }
280