GNU Octave  4.2.1
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-2016 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@cybercatalyst.com>
26 
27 #if defined (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 #include "resource-manager.h"
41 
42 
44  : QWidget (p)
45 {
46  _font_web = font ();
47 
48  QVBoxLayout *vbox_layout = new QVBoxLayout ();
49  vbox_layout->setMargin (0);
50  setLayout (vbox_layout);
51 
52  QHBoxLayout *hbox_layout = new QHBoxLayout ();
53  hbox_layout->setMargin (0);
54  hbox_layout->setSpacing (0);
55  vbox_layout->addLayout (hbox_layout);
56 
57  _tab_bar = new QTabBar (this);
58  _tab_bar->setSizePolicy (QSizePolicy::Preferred,QSizePolicy::Preferred);
59  _tab_bar->setExpanding (false);
60  _tab_bar->setTabsClosable (true);
61 #if defined (HAVE_QTABWIDGET_SETMOVABLE)
62  _tab_bar->setMovable (true);
63 #endif
64  hbox_layout->addWidget (_tab_bar);
65 
66  _zoom_in_button = new QToolButton (this);
67  _zoom_in_button->setIcon (resource_manager::icon ("zoom-in"));
68  hbox_layout->addWidget (_zoom_in_button);
69 
70  _zoom_out_button = new QToolButton (this);
71  _zoom_out_button->setIcon (resource_manager::icon ("zoom-out"));
72  hbox_layout->addWidget (_zoom_out_button);
73 
74  _stacked_widget = new QStackedWidget (this);
75  vbox_layout->addWidget (_stacked_widget);
76 
77  hbox_layout = new QHBoxLayout ();
78  vbox_layout->addLayout (hbox_layout);
79 
80  _search_line_edit = new QLineEdit(this);
81 #if defined (HAVE_SETPLACEHOLDERTEXT)
82  _search_line_edit->setPlaceholderText (
83  tr ("Type here and press \'Return\' to search"));
84 #endif
85  hbox_layout->addWidget (_search_line_edit);
86 
87  _search_check_box = new QCheckBox (tr ("Global search"));
88  hbox_layout->addWidget (_search_check_box);
89 
90  connect (_tab_bar, SIGNAL (tabCloseRequested (int)), this,
91  SLOT (close_tab (int)));
92  connect (_tab_bar, SIGNAL (currentChanged (int)), this,
93  SLOT (current_tab_changed (int)));
94  connect (_zoom_in_button, SIGNAL (clicked ()), this, SLOT (zoom_in ()));
95  connect (_zoom_out_button, SIGNAL (clicked ()), this, SLOT (zoom_out ()));
96  connect (_search_line_edit, SIGNAL (returnPressed ()), this, SLOT (search ()));
97 
98  resize (500, 300);
99 
101  {
102  // Info file does not exist
103  _search_check_box->setEnabled (false);
104  _search_line_edit->setEnabled (false);
105 
106  QTextBrowser *msg = addNewTab (tr ("Error"));
107  QString msg_text = QString (
108  "<html><body><br><br><center><b>%1</b></center></body></html>").
109  arg (tr ("The info file<p>%1<p>or compressed versions do not exist").
111  msg->setHtml (msg_text);
112  }
113 }
114 
115 bool
116 webinfo::set_info_path (const QString& info_path)
117 {
118  if (_parser.set_info_path (info_path))
119  {
120  load_node ("Top");
121  return true;
122  }
123  else
124  return false;
125 }
126 
127 void
128 webinfo::load_node (const QString& node_name)
129 {
130  // no XREF in the tabs
131  QString tab_text = node_name;
132  tab_text.replace("XREF","");
133 
134  //Check if node has been already opened.
135  for (int i = 0; i < _tab_bar->count (); i++)
136  {
137  if (tab_text == _tab_bar->tabText (i))
138  {
139  _tab_bar->setCurrentIndex (i);
140  return;
141  }
142  }
143 
144  QString text = _parser.search_node (node_name);
145  int i = _parser.is_ref (node_name);
146  _text_browser = addNewTab (tab_text);
147  _text_browser->setHtml (_parser.node_text_to_html (text, i - 1, "anchor"));
148 
149  if (i != -1)
150  {
151  _text_browser->scrollToAnchor ("anchor");
152  }
153 }
154 
155 void
157 {
158  QString node = link.toString ();
159  if (node.at (0) != '#')
160  load_node (node);
161  else
162  _text_browser->scrollToAnchor (node);
163 }
164 
165 void
167 {
168  QVariant tab_data = _tab_bar->tabData (index);
169  _text_browser = static_cast<QTextBrowser*> (tab_data.value<void*> ());
170 
171  _stacked_widget->setCurrentIndex (_stacked_widget->indexOf (_text_browser));
172 
173  if (_text_browser->font () != _font_web)
174  {
175  _text_browser->setFont (_font_web);
176  }
177 }
178 
179 QTextBrowser *
180 webinfo::addNewTab (const QString& name)
181 {
182  _text_browser = new QTextBrowser (this);
183  _text_browser->setOpenLinks (false);
184  _text_browser->show ();
185 
186  connect (_text_browser, SIGNAL (anchorClicked (const QUrl &)), this,
187  SLOT (link_clicked (const QUrl &)));
188  disconnect(_tab_bar, SIGNAL (currentChanged(int)), this,
189  SLOT (current_tab_changed (int)));
190 
191  int ns = _stacked_widget->addWidget (_text_browser);
192  _stacked_widget->setCurrentIndex (ns);
193 
194  int nt = _tab_bar->addTab (name);
195  _tab_bar->setCurrentIndex (nt);
196  QVariant tab_data;
197  tab_data.setValue (static_cast<void*> (_text_browser));
198  _tab_bar->setTabData (nt, tab_data);
199 
200  connect (_tab_bar, SIGNAL (currentChanged (int)), this,
201  SLOT (current_tab_changed (int)));
202 
203  if (_text_browser->font () != _font_web)
204  {
205  _text_browser->setFont (_font_web);
206  }
207  return _text_browser;
208 }
209 
210 void
212 {
213  if (_tab_bar->count () > 1)
214  {
215  QVariant tab_data = _tab_bar->tabData (index);
216  QWidget *w = static_cast<QWidget*> (tab_data.value<void*> ());
217  _stacked_widget->removeWidget (w);
218  delete w;
219 
220  _tab_bar->removeTab (index);
221  }
222 }
223 
224 void
225 webinfo::load_ref (const QString &ref_name)
226 {
227  QString text = _parser.find_ref (ref_name);
228  if (text.length () > 0)
229  {
230  load_node (text);
231  }
232  else
233  {
234  // not found
235  load_node("Top");
236  }
237 
238  if (_text_browser)
239  _text_browser->setFocus();
240 }
241 
242 void
244 {
245  if (_search_line_edit->text ().trimmed ().isEmpty ())
246  return; // do nothing if search field is empty or only has whitespaces
247 
248  if (_search_check_box->isChecked ())
249  {
250  // Global search
251  QString results = _parser.global_search (_search_line_edit->text (), 5);
252  _text_browser=addNewTab ("Results for: " + _search_line_edit->text ());
253  _text_browser->setHtml (results);
254  }
255  else
256  {
257  // Local search
258  _text_browser->find (_search_line_edit->text ());
259  }
260 }
261 
262 void
264 {
265  _font_web.setPointSize (_font_web.pointSize() + 1);
266  _text_browser->setFont (_font_web);
267 }
268 
269 void
271 {
272  _font_web.setPointSize (_font_web.pointSize() - 1);
273  _text_browser->setFont (_font_web);
274 }
275 
276 void
278 {
279  if (_search_line_edit->hasFocus () && _search_line_edit->hasSelectedText ())
280  {
281  QClipboard *clipboard = QApplication::clipboard ();
282 
283  clipboard->setText (_search_line_edit->selectedText ());
284  }
285  if (_text_browser->hasFocus ())
286  {
287  _text_browser->copy ();
288  }
289 }
290 
291 void
293 {
294  if (_search_line_edit->hasFocus ())
295  {
296  _search_line_edit->selectAll ();
297  }
298  if (_text_browser->hasFocus ())
299  {
300  _text_browser->selectAll ();
301  }
302 }
303 
304 void
306 {
307  if (_search_line_edit->hasFocus ())
308  {
309  QClipboard *clipboard = QApplication::clipboard ();
310  QString str = clipboard->text ();
311  if (str.length () > 0)
312  _search_line_edit->insert (str);
313  }
314 }
void selectAll()
Definition: webinfo.cc:292
QTabBar * _tab_bar
Definition: webinfo.h:63
std::string Vinfo_file
Definition: help.cc:82
void load_ref(const QString &ref_name)
Definition: webinfo.cc:225
void zoom_out()
Definition: webinfo.cc:270
QCheckBox * _search_check_box
Definition: webinfo.h:66
QString global_search(const QString &text, int maxFounds)
Definition: parser.cc:577
QTextBrowser * addNewTab(const QString &name)
Definition: webinfo.cc:180
void pasteClipboard()
Definition: webinfo.cc:305
QString fromStdString(const std::string &s)
bool set_info_path(const QString &info_path)
Definition: webinfo.cc:116
void link_clicked(const QUrl &link)
Definition: webinfo.cc:156
QTextBrowser * _text_browser
Definition: webinfo.h:62
QFont _font_web
Definition: webinfo.h:71
QStackedWidget * _stacked_widget
Definition: webinfo.h:64
octave_value arg
Definition: pr-output.cc:3440
static octave_idx_type link(octave_idx_type s, octave_idx_type t, octave_idx_type *pp)
Definition: colamd.cc:105
QToolButton * _zoom_out_button
Definition: webinfo.h:68
void zoom_in()
Definition: webinfo.cc:263
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
std::complex< double > w(std::complex< double > z, double relerr=0)
std::string str
Definition: hash.cc:118
void copyClipboard()
Definition: webinfo.cc:277
QLineEdit * _search_line_edit
Definition: webinfo.h:65
QString node_text_to_html(const QString &text, int anchorPos=-1, const QString &anchor=QString())
Translates text of node to Html.
Definition: parser.cc:390
void load_node(const QString &node_name)
Definition: webinfo.cc:128
void current_tab_changed(int index)
Definition: webinfo.cc:166
int is_ref(const QString &node)
Checks if this node is reference.
Definition: parser.cc:136
void search()
Definition: webinfo.cc:243
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
void close_tab(int index)
Definition: webinfo.cc:211
bool set_info_path(const QString &_info_path)
Definition: parser.cc:51
p
Definition: lu.cc:138
parser _parser
Definition: webinfo.h:70
static QIcon icon(const QString &icon_name, bool fallback=true)
QString find_ref(const QString &name)
Definition: parser.cc:666
QString search_node(const QString &node)
Definition: parser.cc:152
webinfo(QWidget *parent=0)
Definition: webinfo.cc:43
QToolButton * _zoom_in_button
Definition: webinfo.h:67