GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
QTerminal.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2018 Michael Goffioul.
4 Copyright (C) 2012-2018 Jacob Dawid.
5 
6 This file is part of QTerminal.
7 
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not,
20 see <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #include "QTerminal.h"
25 
26 #if defined (Q_OS_WIN32)
27 # include "win32/QWinTerminalImpl.h"
28 #else
29 # include "unix/QUnixTerminalImpl.h"
30 #endif
31 
32 QTerminal *
34 {
35 #if defined (Q_OS_WIN32)
36  return new QWinTerminalImpl (xparent);
37 #else
38  return new QUnixTerminalImpl (xparent);
39 #endif
40 }
41 
44 {
45  static QList<QColor> colors;
46 
47  if (colors.isEmpty ())
48  {
49  colors << QColor(0,0,0)
50  << QColor(255,255,255)
51  << QColor(192,192,192)
52  << QColor(128,128,128);
53  }
54 
55  return colors;
56 }
57 
58 QStringList
60 {
61  static QStringList names;
62 
63  if (names.isEmpty ())
64  {
65  names << QObject::tr ("foreground")
66  << QObject::tr ("background")
67  << QObject::tr ("selection")
68  << QObject::tr ("cursor");
69  }
70 
71  return names;
72 }
73 
74 // slot for disabling the interrupt action when terminal loses focus
75 void
77  {
78  if (focus_out)
79  {
80  _interrupt_action->setShortcut (QKeySequence ());
81  _nop_action->setShortcut (QKeySequence ());
82  }
83  else
84  {
85  _interrupt_action->setShortcut (
86  QKeySequence (Qt::ControlModifier | Qt::Key_C));
87  _nop_action->setShortcut (
88  QKeySequence (Qt::ControlModifier | Qt::Key_D));
89  }
90  }
91 
92 // slot for the terminal's context menu
93 void
95  {
96  QClipboard * cb = QApplication::clipboard ();
97  QString selected_text = selectedText();
98  bool has_selected_text = ! selected_text.isEmpty ();
99 
100  _edit_action->setVisible (false);
101 
102 #if defined (Q_OS_WIN32)
103  // include this when in windows because there is no filter for
104  // detecting links and error messages yet
105  if (has_selected_text)
106  {
107  QRegExp file ("(?:[ \\t]+)(\\S+) at line (\\d+) column (?:\\d+)");
108 
109  int pos = file.indexIn (selected_text);
110 
111  if (pos > -1)
112  {
113  QString file_name = file.cap (1);
114  QString line = file.cap (2);
115 
116  _edit_action->setVisible (true);
117  _edit_action->setText (tr ("Edit %1 at line %2")
118  .arg (file_name).arg (line));
119 
120  QStringList data;
121  data << file_name << line;
122  _edit_action->setData (data);
123  }
124  }
125 #endif
126 
127  _paste_action->setEnabled (cb->text().length() > 0);
128  _copy_action->setEnabled (has_selected_text);
129 
130  // Get the actions of any hotspots the filters may have found
132  if (actions.length ())
133  _contextMenu->addSeparator ();
134  for (int i = 0; i < actions.length (); i++)
135  _contextMenu->addAction (actions.at(i));
136 
137  // Finally, show the context menu
138  _contextMenu->exec (mapToGlobal (at));
139 
140  // Cleaning up, remove actions of the hotspot
141  for (int i = 0; i < actions.length (); i++)
142  _contextMenu->removeAction (actions.at(i));
143  }
144 
145 // slot for edit files in error messages
146 void
148 {
149  QString file = _edit_action->data ().toStringList ().at (0);
150  int line = _edit_action->data ().toStringList ().at (1).toInt ();
151 
153 }
154 
155 void
156 QTerminal::notice_settings (const QSettings *settings)
157 {
158  // QSettings pointer is checked before emitting.
159 
160  // Set terminal font:
161  QFont term_font = QFont ();
162  term_font.setStyleHint (QFont::TypeWriter);
163  term_font.setFamily
164  (settings->value ("terminal/fontName", "Courier New").toString ());
165  term_font.setPointSize (settings->value ("terminal/fontSize", 10).toInt ());
166  setTerminalFont (term_font);
167 
168  QString cursorType
169  = settings->value ("terminal/cursorType", "ibeam").toString ();
170 
171  bool cursorBlinking;
172  if (settings->contains ("cursor_blinking"))
173  cursorBlinking = settings->value ("cursor_blinking",true).toBool ();
174  else
175  cursorBlinking = settings->value ("terminal/cursorBlinking",true).toBool ();
176 
177  if (cursorType == "ibeam")
178  setCursorType (QTerminal::IBeamCursor, cursorBlinking);
179  else if (cursorType == "block")
180  setCursorType (QTerminal::BlockCursor, cursorBlinking);
181  else if (cursorType == "underline")
182  setCursorType (QTerminal::UnderlineCursor, cursorBlinking);
183 
184  bool cursorUseForegroundColor
185  = settings->value ("terminal/cursorUseForegroundColor", true).toBool ();
186 
187  QList<QColor> colors = default_colors ();
188 
190  (settings->value ("terminal/color_f",
191  QVariant (colors.at (0))).value<QColor> ());
192 
194  (settings->value ("terminal/color_b",
195  QVariant (colors.at (1))).value<QColor> ());
196 
198  (settings->value ("terminal/color_s",
199  QVariant (colors.at (2))).value<QColor> ());
200 
202  (cursorUseForegroundColor,
203  settings->value ("terminal/color_c",
204  QVariant (colors.at (3))).value<QColor> ());
205  setScrollBufferSize (settings->value ("terminal/history_buffer",1000).toInt () );
206 
207  // check whether Copy shortcut is Ctrl-C
208  QKeySequence sc;
209  sc = QKeySequence (settings->value ("shortcuts/main_edit:copy").toString ());
210 
211  // if sc is empty, shortcuts are not yet in the settings (take the default)
212  if (sc.isEmpty ()) // QKeySequence::Copy as second argument in
213  sc = QKeySequence::Copy; // settings->value () does not work!
214 
215  // dis- or enable extra interrupt action
216  bool extra_ir_action = (sc != QKeySequence (Qt::ControlModifier | Qt::Key_C));
217  _interrupt_action->setEnabled (extra_ir_action);
218  has_extra_interrupt (extra_ir_action);
219 
220  // check whether shortcut Ctrl-D is in use by the main-window
221  bool ctrld = settings->value ("shortcuts/main_ctrld",false).toBool ();
222  _nop_action->setEnabled (! ctrld);
223 }
void edit_mfile_request(const QString &, int)
void edit_file(void)
Definition: QTerminal.cc:147
For example cd octave end example noindent changes the current working directory to an error message is printed and the working directory is not changed sc
Definition: dirfns.cc:124
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
virtual void setScrollBufferSize(int value=1000)=0
virtual void setForegroundColor(const QColor &color)=0
static QTerminal * create(QWidget *xparent=nullptr)
Definition: QTerminal.cc:33
QAction * _interrupt_action
Definition: QTerminal.h:204
QAction * _nop_action
Definition: QTerminal.h:205
virtual void setSelectionColor(const QColor &color)=0
octave_value arg
Definition: pr-output.cc:3244
virtual void has_extra_interrupt(bool extra)=0
QMenu * _contextMenu
Definition: QTerminal.h:198
QAction * _edit_action
Definition: QTerminal.h:202
virtual QString selectedText()=0
void set_global_shortcuts(bool focus_out)
Definition: QTerminal.cc:76
QAction * _copy_action
Definition: QTerminal.h:199
QAction * _paste_action
Definition: QTerminal.h:200
virtual QList< QAction * > get_hotspot_actions(const QPoint &)
Definition: QTerminal.h:64
virtual void setCursorColor(bool useForegroundColor, const QColor &color)=0
OCTAVE_EXPORT octave_value_list at
Definition: time.cc:115
void notice_settings(const QSettings *settings)
Definition: QTerminal.cc:156
static QStringList color_names(void)
Definition: QTerminal.cc:59
virtual void setTerminalFont(const QFont &font)=0
static QList< QColor > default_colors(void)
Definition: QTerminal.cc:43
virtual void setBackgroundColor(const QColor &color)=0
for i
Definition: data.cc:5264
virtual void setCursorType(CursorType type, bool blinking)
Definition: QTerminal.h:74
virtual void handleCustomContextMenuRequested(const QPoint &at)
Definition: QTerminal.cc:94