GNU Octave  4.0.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-2015 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 #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 #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 #ifdef 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 #ifdef 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  { // Info file does not exist
102  _search_check_box->setEnabled (false);
103  _search_line_edit->setEnabled (false);
104 
105  QTextBrowser *msg = addNewTab (tr ("Error"));
106  QString msg_text = QString (
107  "<html><body><br><br><center><b>%1</b></center></body></html>").
108  arg (tr ("The info file<p>%1<p>or compressed versions do not exist").
110  msg->setHtml (msg_text);
111  }
112 }
113 
114 bool
115 webinfo::set_info_path (const QString& info_path)
116 {
117  if (_parser.set_info_path (info_path))
118  {
119  load_node ("Top");
120  return true;
121  }
122  else
123  return false;
124 }
125 
126 void
127 webinfo::load_node (const QString& node_name)
128 {
129  // no XREF in the tabs
130  QString tab_text = node_name;
131  tab_text.replace("XREF","");
132 
133  //Check if node has been already opened.
134  for (int i = 0; i < _tab_bar->count (); i++)
135  {
136  if (tab_text == _tab_bar->tabText (i))
137  {
138  _tab_bar->setCurrentIndex (i);
139  return;
140  }
141  }
142 
143  QString text = _parser.search_node (node_name);
144  int i = _parser.is_ref (node_name);
145  _text_browser = addNewTab (tab_text);
146  _text_browser->setHtml (_parser.node_text_to_html (text, i - 1, "anchor"));
147 
148  if (i != -1)
149  {
150  _text_browser->scrollToAnchor ("anchor");
151  }
152 }
153 
154 void
156 {
157  QString node = link.toString ();
158  if (node.at (0) != '#')
159  load_node (node);
160  else
161  _text_browser->scrollToAnchor (node);
162 }
163 
164 void
166 {
167  QVariant tab_data = _tab_bar->tabData (index);
168  _text_browser = static_cast<QTextBrowser*> (tab_data.value<void*> ());
169 
170  _stacked_widget->setCurrentIndex (_stacked_widget->indexOf (_text_browser));
171 
172  if (_text_browser->font () != _font_web)
173  {
174  _text_browser->setFont (_font_web);
175  }
176 }
177 
178 QTextBrowser *
179 webinfo::addNewTab (const QString& name)
180 {
181  _text_browser = new QTextBrowser (this);
182  _text_browser->setOpenLinks (false);
183  _text_browser->show ();
184 
185  connect (_text_browser, SIGNAL (anchorClicked (const QUrl &)), this,
186  SLOT (link_clicked (const QUrl &)));
187  disconnect(_tab_bar, SIGNAL (currentChanged(int)), this,
188  SLOT (current_tab_changed (int)));
189 
190  int ns = _stacked_widget->addWidget (_text_browser);
191  _stacked_widget->setCurrentIndex (ns);
192 
193  int nt = _tab_bar->addTab (name);
194  _tab_bar->setCurrentIndex (nt);
195  QVariant tab_data;
196  tab_data.setValue (static_cast<void*> (_text_browser));
197  _tab_bar->setTabData (nt, tab_data);
198 
199  connect (_tab_bar, SIGNAL (currentChanged (int)), this,
200  SLOT (current_tab_changed (int)));
201 
202  if (_text_browser->font () != _font_web)
203  {
204  _text_browser->setFont (_font_web);
205  }
206  return _text_browser;
207 }
208 
209 void
211 {
212  if (_tab_bar->count () > 1)
213  {
214  QVariant tab_data = _tab_bar->tabData (index);
215  QWidget *w = static_cast<QWidget*> (tab_data.value<void*> ());
216  _stacked_widget->removeWidget (w);
217  delete w;
218 
219  _tab_bar->removeTab (index);
220  }
221 }
222 
223 void
224 webinfo::load_ref (const QString &ref_name)
225 {
226  QString text = _parser.find_ref (ref_name);
227  if (text.length () > 0)
228  {
229  load_node (text);
230  }
231  else
232  {
233  // not found
234  load_node("Top");
235  }
236 
237  if (_text_browser)
238  _text_browser->setFocus();
239 }
240 
241 void
243 {
244  if (_search_check_box->isChecked ())
245  {
246  // Global search
247  QString results = _parser.global_search (_search_line_edit->text (), 5);
248  _text_browser=addNewTab ("Results for: " + _search_line_edit->text ());
249  _text_browser->setHtml (results);
250  }
251  else
252  {
253  // Local search
254  _text_browser->find (_search_line_edit->text ());
255  }
256 }
257 
258 void
260 {
261  _font_web.setPointSize (_font_web.pointSize() + 1);
262  _text_browser->setFont (_font_web);
263 }
264 
265 void
267 {
268  _font_web.setPointSize (_font_web.pointSize() - 1);
269  _text_browser->setFont (_font_web);
270 }
271 
272 void
274 {
275  if (_search_line_edit->hasFocus () && _search_line_edit->hasSelectedText ())
276  {
277  QClipboard *clipboard = QApplication::clipboard ();
278 
279  clipboard->setText (_search_line_edit->selectedText ());
280  }
281  if (_text_browser->hasFocus ())
282  {
283  _text_browser->copy ();
284  }
285 }
286 
287 void
289 {
290  if (_search_line_edit->hasFocus ())
291  {
292  _search_line_edit->selectAll ();
293  }
294  if (_text_browser->hasFocus ())
295  {
296  _text_browser->selectAll ();
297  }
298 }
299 
300 
301 void
303 {
304  if (_search_line_edit->hasFocus ())
305  {
306  QClipboard *clipboard = QApplication::clipboard ();
307  QString str = clipboard->text ();
308  if (str.length () > 0)
309  _search_line_edit->insert (str);
310  }
311 }
312 
void selectAll()
Definition: webinfo.cc:288
QTabBar * _tab_bar
Definition: webinfo.h:60
std::string Vinfo_file
Definition: help.cc:82
void load_ref(const QString &ref_name)
Definition: webinfo.cc:224
void zoom_out()
Definition: webinfo.cc:266
QCheckBox * _search_check_box
Definition: webinfo.h:63
QString global_search(const QString &text, int maxFounds)
Definition: parser.cc:582
QTextBrowser * addNewTab(const QString &name)
Definition: webinfo.cc:179
void pasteClipboard()
Definition: webinfo.cc:302
QString fromStdString(const std::string &s)
bool set_info_path(const QString &info_path)
Definition: webinfo.cc:115
void link_clicked(const QUrl &link)
Definition: webinfo.cc:155
QTextBrowser * _text_browser
Definition: webinfo.h:59
QFont _font_web
Definition: webinfo.h:68
QStackedWidget * _stacked_widget
Definition: webinfo.h:61
static octave_idx_type link(octave_idx_type s, octave_idx_type t, octave_idx_type *pp)
Definition: colamd.cc:104
QToolButton * _zoom_out_button
Definition: webinfo.h:65
void zoom_in()
Definition: webinfo.cc:259
std::complex< double > w(std::complex< double > z, double relerr=0)
void copyClipboard()
Definition: webinfo.cc:273
double arg(double x)
Definition: lo-mappers.h:37
QLineEdit * _search_line_edit
Definition: webinfo.h:62
QString node_text_to_html(const QString &text, int anchorPos=-1, const QString &anchor=QString())
Translates text of node to Html.
Definition: parser.cc:389
void load_node(const QString &node_name)
Definition: webinfo.cc:127
void current_tab_changed(int index)
Definition: webinfo.cc:165
int is_ref(const QString &node)
Checks if this node is reference.
Definition: parser.cc:134
void search()
Definition: webinfo.cc:242
void close_tab(int index)
Definition: webinfo.cc:210
bool set_info_path(const QString &_info_path)
Definition: parser.cc:51
parser _parser
Definition: webinfo.h:67
static QIcon icon(const QString &icon_name, bool fallback=true)
QString find_ref(const QString &name)
Definition: parser.cc:671
QString search_node(const QString &node)
Definition: parser.cc:150
webinfo(QWidget *parent=0)
Definition: webinfo.cc:43
QToolButton * _zoom_in_button
Definition: webinfo.h:64