Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
textdoc.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  textdoc.cpp
3  -------------
4 Copyright (C) 2006 by Michael Margraf <michael.margraf@alumni.tu-berlin.de>
5 Copyright (C) 2014 by Guilherme Brondani Torri <guitorri@gmail.com>
6 
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 #include <QtGui>
22 #include <QtCore>
23 
24 #include "main.h"
25 #include "qucs.h"
26 #include "textdoc.h"
27 #include "syntax.h"
28 #include "components/vhdlfile.h"
29 #include "components/verilogfile.h"
30 #include "components/vafile.h"
31 
42 TextDoc::TextDoc(QucsApp *App_, const QString& Name_) : QucsDoc(App_, Name_), QPlainTextEdit()
43 {
44  TextFont = QFont("Courier New");
45  TextFont.setPointSize(QucsSettings.font.pointSize()-1);
46  TextFont.setStyleHint(QFont::Courier);
47  TextFont.setFixedPitch(true);
48  document()->setDefaultFont(TextFont);
49 
50  simulation = true;
51  Library = "";
52  Libraries = "";
53  SetChanged = false;
54  devtype = DEV_DEF;
55 
56  tmpPosX = tmpPosY = 1; // set to 1 to trigger line highlighting
57  Scale = (float)TextFont.pointSize();
58  //TODO (not supported) setUndoDepth(QucsSettings.maxUndo);
59  setLanguage (Name_);
60  QFileInfo Info (Name_);
61 
62  if(App) {
63  if(Name_.isEmpty()) {
64  App->DocumentTab->addTab(this, QPixmap(empty_xpm), QObject::tr("untitled"));
65  }
66  else {
67  App->DocumentTab->addTab(this, QPixmap(empty_xpm), Info.fileName());
68  }
69  App->DocumentTab->setCurrentPage(App->DocumentTab->indexOf(this));
70 
71  viewport()->setFocus();
72 
73  setWordWrapMode(QTextOption::NoWrap);
74  setPaletteBackgroundColor(QucsSettings.BGColor);
75  connect(this, SIGNAL(textChanged()), SLOT(slotSetChanged()));
76  connect(this, SIGNAL(cursorPositionChanged()),
77  SLOT(slotCursorPosChanged()));
78 
81  syntaxHighlight->setDocument(document());
82 
83  connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
85  }
86 }
87 
92 {
93  if(App) {
94  delete syntaxHighlight;
95  App->DocumentTab->removePage(this); // delete tab in TabBar
96  }
97 }
98 
104 void TextDoc::setLanguage (const QString& FileName)
105 {
106  QFileInfo Info (FileName);
107  QString ext = Info.extension (false);
108  if (ext == "vhd" || ext == "vhdl")
110  else if (ext == "v")
112  else if (ext == "va")
114  else if (ext == "m" || ext == "oct")
116  else
118 }
119 
125 void TextDoc::setLanguage (int lang)
126 {
127  language = lang;
128 }
129 
135 {
136  QFile file (DocName + ".cfg");
137  if (!file.open (QIODevice::WriteOnly))
138  return false;
139 
140  QTextStream stream (&file);
141  stream << "Textfile settings file, Qucs " PACKAGE_VERSION "\n"
142  << "Simulation=" << simulation << "\n"
143  << "Duration=" << SimTime << "\n"
144  << "Module=" << (!simulation) << "\n"
145  << "Library=" << Library << "\n"
146  << "Libraries=" << Libraries << "\n"
147  << "ShortDesc=" << ShortDesc << "\n"
148  << "LongDesc=" << LongDesc << "\n"
149  << "Icon=" << Icon << "\n"
150  << "Recreate=" << recreate << "\n"
151  << "DeviceType=" << devtype << "\n";
152 
153  file.close ();
154  SetChanged = false;
155  return true;
156 }
157 
163 {
164  QFile file (DocName + ".cfg");
165  if (!file.open (QIODevice::ReadOnly))
166  return false;
167 
168  QTextStream stream (&file);
169  QString Line, Setting;
170 
171  bool ok;
172  while (!stream.atEnd ()) {
173  Line = stream.readLine ();
174  Setting = Line.section ('=', 0, 0);
175  Line = Line.section ('=', 1).stripWhiteSpace ();
176  if (Setting == "Simulation") {
177  simulation = Line.toInt (&ok);
178  } else if (Setting == "Duration") {
179  SimTime = Line;
180  } else if (Setting == "Module") {
181  } else if (Setting == "Library") {
182  Library = Line;
183  } else if (Setting == "Libraries") {
184  Libraries = Line;
185  } else if (Setting == "ShortDesc") {
186  ShortDesc = Line;
187  } else if (Setting == "LongDesc") {
188  LongDesc = Line;
189  } else if (Setting == "Icon") {
190  Icon = Line;
191  } else if (Setting == "Recreate") {
192  recreate = Line.toInt (&ok);
193  } else if (Setting == "DeviceType") {
194  devtype = Line.toInt (&ok);
195  }
196  }
197 
198  file.close ();
199  return true;
200 }
201 
206 void TextDoc::setName (const QString& Name_)
207 {
208  DocName = Name_;
210 
211  QFileInfo Info (DocName);
212  if (App)
213  App->DocumentTab->setTabLabel (this, Info.fileName ());
214 
215  DataSet = Info.baseName (true) + ".dat";
216  DataDisplay = Info.baseName (true) + ".dpl";
217  if(Info.extension(false) == "m" || Info.extension(false) == "oct")
218  SimTime = "1";
219 }
220 
227 {
228  int x, y;
230  viewport()->setFocus ();
231 
232  if (document()->isUndoAvailable())
233  App->undo->setEnabled (true);
234  else
235  App->undo->setEnabled (false);
236  if (document()->isRedoAvailable ())
237  App->redo->setEnabled (true);
238  else
239  App->redo->setEnabled (false);
240 
241  // update appropriate menu entries
242  App->symEdit->setMenuText (tr("Edit Text Symbol"));
243  App->symEdit->setStatusTip (tr("Edits the symbol for this text document"));
244  App->symEdit->setWhatsThis (
245  tr("Edit Text Symbol\n\nEdits the symbol for this text document"));
246 
247  if (language == LANG_VHDL) {
248  App->insEntity->setMenuText (tr("VHDL entity"));
249  App->insEntity->setStatusTip (tr("Inserts skeleton of VHDL entity"));
250  App->insEntity->setWhatsThis (
251  tr("VHDL entity\n\nInserts the skeleton of a VHDL entity"));
252  }
253  else if (language == LANG_VERILOG || language == LANG_VERILOGA) {
254  App->insEntity->setMenuText (tr("Verilog module"));
255  App->insEntity->setStatusTip (tr("Inserts skeleton of Verilog module"));
256  App->insEntity->setWhatsThis (
257  tr("Verilog module\n\nInserts the skeleton of a Verilog module"));
258  App->buildModule->setEnabled(true);
259  }
260  else if (language == LANG_OCTAVE) {
261  App->insEntity->setMenuText (tr("Octave function"));
262  App->insEntity->setStatusTip (tr("Inserts skeleton of Octave function"));
263  App->insEntity->setWhatsThis (
264  tr("Octave function\n\nInserts the skeleton of a Octave function"));
265  }
266  App->simulate->setEnabled (true);
267  App->editActivate->setEnabled (true);
268 }
269 
274 {
275  QTextCursor pos = textCursor();
276  int x = pos.blockNumber();
277  int y = pos.columnNumber();
278  App->printCursorPosition(x+1, y+1);
279  tmpPosX = x;
280  tmpPosY = y;
281 }
282 
287 {
288  if((document()->isModified() && !DocChanged) || SetChanged) {
289  App->DocumentTab->setTabIconSet(this, QPixmap(smallsave_xpm));
290  DocChanged = true;
291  }
292  else if((!document()->isModified() && DocChanged)) {
293  App->DocumentTab->setTabIconSet(this, QPixmap(empty_xpm));
294  DocChanged = false;
295  }
296 
297  App->undo->setEnabled(document()->isUndoAvailable());
298  App->redo->setEnabled(document()->isRedoAvailable());
299 }
300 
308 QMenu *TextDoc::createStandardContextMenu( const QPoint &pos )
309 {
310  QMenu *popup = QPlainTextEdit::createStandardContextMenu();
311 
312  if (language != LANG_OCTAVE) {
313  App->fileSettings->addTo(popup);
314  }
315  return popup;
316 }
317 
323 {
324  QFile file (DocName);
325  if (!file.open (QIODevice::ReadOnly))
326  return false;
328 
329  QTextStream stream (&file);
330  insertPlainText(stream.read ());
331  document()->setModified(false);
332  slotSetChanged ();
333  file.close ();
334  lastSaved = QDateTime::currentDateTime ();
335  loadSettings ();
336  SimOpenDpl = simulation ? true : false;
337  refreshLanguage();
338  return true;
339 }
340 
341 
347 {
348  saveSettings ();
349 
350  QFile file (DocName);
351  if (!file.open (QIODevice::WriteOnly))
352  return -1;
354 
355  QTextStream stream (&file);
356  stream << toPlainText();
357  document()->setModified (false);
358  slotSetChanged ();
359  file.close ();
360 
361  QFileInfo Info (DocName);
362  lastSaved = Info.lastModified ();
363 
365  QList<QTextEdit::ExtraSelection> extraSelections;
366  this->setExtraSelections(extraSelections);
367  refreshLanguage();
368 
369  return 0;
370 }
371 
379 float TextDoc::zoomBy(float s)
380 {
381  if(s == 2.0) {
382  QFont f = document()->defaultFont();
383  f.setPointSize(f.pointSize()*2);
384  document()->setDefaultFont(f);
385  }
386  else {
387  QFont f = document()->defaultFont();
388  f.setPointSize(f.pointSize()*s);
389  document()->setDefaultFont(f);
390  }
391  return Scale;
392 }
393 
398 {
399  TextFont = QFont("Courier New");
400  TextFont.setPointSize(QucsSettings.font.pointSize()-1);
401  TextFont.setStyleHint(QFont::Courier);
402  TextFont.setFixedPitch(true);
403  document()->setDefaultFont(TextFont);
404 }
405 
411 bool TextDoc::loadSimulationTime(QString& Time)
412 {
413  if(!SimTime.isEmpty()) {
414  Time = SimTime;
415  return true;
416  }
417  return false;
418 }
419 
425 {
426  QTextCursor cursor = this->textCursor();
427 
428  if(!cursor.hasSelection())
429  return; // No selection available
430 
431  // get range of selection
432  int start = cursor.selectionStart();
433  int end = cursor.selectionEnd();
434 
435  cursor.setPosition(start);
436  int firstLine = cursor.blockNumber();
437  cursor.setPosition(end, QTextCursor::KeepAnchor);
438  int lastLine = cursor.blockNumber();
439 
440  // use comment string indicator depending on language
441  QString co;
442 
443  switch (language) {
444  case LANG_VHDL:
445  co = "--";
446  break;
447  case LANG_VERILOG:
448  case LANG_VERILOGA:
449  co = "//";
450  break;
451  case LANG_OCTAVE:
452  co = "%";
453  break;
454  default:
455  co = "";
456  break;
457  }
458 
459  QStringList newlines;
460  for (int i=firstLine; i<=lastLine; i++) {
461  QString line = document()->findBlockByLineNumber(i).text();
462  if (line.startsWith(co)){
463  // uncomment
464  line.remove(0,co.length());
465  newlines << line;
466  }
467  else {
468  // comment
469  line = line.insert(0, co);
470  newlines << line;
471  }
472  }
473  insertPlainText(newlines.join("\n"));
474 }
475 
480 {
481  if (language == LANG_VHDL)
482  appendPlainText("entity is\n port ( : in bit);\nend;\n"
483  "architecture of is\n signal : bit;\nbegin\n\nend;\n\n");
484  else if (language == LANG_VERILOG)
485  appendPlainText ("module ( );\ninput ;\noutput ;\nbegin\n\nend\n"
486  "endmodule\n\n");
487  else if (language == LANG_OCTAVE)
488  appendPlainText ("function = ( )\n"
489  "endfunction\n\n");
490 }
491 
497 {
498  switch (language) {
499  case LANG_VHDL:
500  {
501  VHDL_File_Info VInfo (toPlainText());
502  return VInfo.EntityName;
503  }
504  case LANG_VERILOG:
505  {
506  Verilog_File_Info VInfo (toPlainText());
507  return VInfo.ModuleName;
508  }
509  case LANG_VERILOGA:
510  {
511  VerilogA_File_Info VInfo (toPlainText());
512  return VInfo.ModuleName;
513  }
514  case LANG_OCTAVE:
515  {
516  QFileInfo Info (DocName);
517  return Info.baseName (true);
518  }
519  default:
520  return "";
521  }
522 }
523 
528 {
529  QList<QTextEdit::ExtraSelection> extraSelections;
530 
531  if (!isReadOnly()) {
532  QTextEdit::ExtraSelection selection;
533 
534  QColor lineColor = QColor(Qt::blue).lighter(195);
535 
536  selection.format.setBackground(lineColor);
537  selection.format.setProperty(QTextFormat::FullWidthSelection, true);
538  selection.cursor = textCursor();
539  selection.cursor.clearSelection();
540  extraSelections.append(selection);
541  }
542 
543  setExtraSelections(extraSelections);
544 }
545 
547 {
548  this->setLanguage(DocName);
550  syntaxHighlight->setDocument(document());
551 }
QString EntityName
Definition: vhdlfile.h:59
void slotCursorPosChanged()
TextDoc::slotCursorPosChanged update status bar with line:column.
Definition: textdoc.cpp:273
int save()
TextDoc::save saves the current document and it settings.
Definition: textdoc.cpp:346
QString Libraries
Definition: textdoc.h:73
void becomeCurrent(bool)
TextDoc::becomeCurrent sets text document as current.
Definition: textdoc.cpp:226
~TextDoc()
TextDoc::~TextDoc Text document destructor.
Definition: textdoc.cpp:91
Definition of the TextDoc class.
const char * smallsave_xpm[]
Definition: qucsdoc.cpp:28
bool SimOpenDpl
Definition: qucsdoc.h:61
QFont font
Definition: main.h:46
float Scale
Definition: qucsdoc.h:58
float zoomBy(float)
TextDoc::zoomBy increases/decreases the text font size.
Definition: textdoc.cpp:379
QString Library
Definition: textdoc.h:72
tQucsSettings QucsSettings
Definition: main.cpp:52
QString ModuleName
Definition: verilogfile.h:54
bool saveSettings(void)
TextDoc::saveSettings saves the text document settings .cfg.
Definition: textdoc.cpp:134
bool recreate
Definition: textdoc.h:77
bool simulation
Definition: textdoc.h:71
#define DEV_DEF
Definition: textdoc.h:44
QAction * insEntity
Definition: qucs.h:292
bool DocChanged
Definition: qucsdoc.h:60
QString Icon
Definition: textdoc.h:76
QString ModuleName
Definition: vafile.h:29
void setLanguage(int)
TextDoc::setLanguage(int)
Definition: textdoc.cpp:125
int language
Definition: textdoc.h:81
Definitions and declarations for the main application.
QString getModuleName(void)
TextDoc::getModuleName parse the module name ou of the text file contents.
Definition: textdoc.cpp:496
QString LongDesc
Definition: textdoc.h:75
const char * empty_xpm[]
Definition: qucsdoc.cpp:65
void printCursorPosition(int, int)
Definition: qucs_init.cpp:980
QFont TextFont
Definition: textdoc.h:69
int devtype
Definition: textdoc.h:78
bool loadSimulationTime(QString &)
TextDoc::loadSimulationTime set SimTime member variable.
Definition: textdoc.cpp:411
bool load()
TextDoc::load loads a text document.
Definition: textdoc.cpp:322
SyntaxHighlighter * syntaxHighlight
Definition: textdoc.h:94
bool loadSettings(void)
TextDoc::loadSettings loads the text document settings.
Definition: textdoc.cpp:162
void slotSetChanged()
TextDoc::slotSetChanged togles tab icon to indicate unsaved changes.
Definition: textdoc.cpp:286
void refreshLanguage(void)
Definition: textdoc.cpp:546
QucsApp * App
Definition: qucsdoc.h:59
void setLanguage(int)
Definition: syntax.cpp:59
QAction * undo
Definition: qucs.h:174
QDateTime lastSaved
Definition: qucsdoc.h:56
QAction * redo
Definition: qucs.h:174
QAction * symEdit
Definition: qucs.h:174
Definition: element.h:48
TextDoc(QucsApp *, const QString &)
TextDoc::TextDoc Text document constructor.
Definition: textdoc.cpp:42
QAction * editActivate
Definition: qucs.h:292
QString SimTime
Definition: qucsdoc.h:55
QString DataSet
Definition: qucsdoc.h:52
void setName(const QString &)
TextDoc::setName sets the text file name on its tab.
Definition: textdoc.cpp:206
QTabWidget * DocumentTab
Definition: qucs.h:164
QAction * fileSettings
Definition: qucs.h:174
void insertSkeleton()
TextDoc::insertSkeleton adds a basic skeleton for type of text file.
Definition: textdoc.cpp:479
QString DocName
Definition: qucsdoc.h:51
int tmpPosY
Definition: qucsdoc.h:65
void commentSelected()
TextDoc::commentSelected toggles the comment of selected text See also QucsApp::slotEditActivate.
Definition: textdoc.cpp:424
Definition: qucs.h:61
QAction * buildModule
Definition: qucs.h:292
QAction * simulate
Definition: qucs.h:174
int tmpPosX
Definition: qucsdoc.h:65
void showNoZoom()
TextDoc::showNoZoom resets the font scaling.
Definition: textdoc.cpp:397
bool SetChanged
Definition: textdoc.h:80
QColor BGColor
Definition: main.h:48
QString ShortDesc
Definition: textdoc.h:74
QString DataDisplay
Definition: qucsdoc.h:53
void highlightCurrentLine()
TextDoc::highlightCurrentLine mark the current line.
Definition: textdoc.cpp:527
QMenu * createStandardContextMenu(const QPoint &)
TextDoc::createStandardContextMenu creates the standard context menu.
Definition: textdoc.cpp:308