SMILX  1.01
milxQtConsole.cpp
1 /*=========================================================================
2  The Software is copyright (c) Commonwealth Scientific and Industrial Research Organisation (CSIRO)
3  ABN 41 687 119 230.
4  All rights reserved.
5 
6  Licensed under the CSIRO BSD 3-Clause License
7  You may not use this file except in compliance with the License.
8  You may obtain a copy of the License in the file LICENSE.md or at
9 
10  https://stash.csiro.au/projects/SMILI/repos/smili/browse/license.txt
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 =========================================================================*/
18 #include "milxQtConsole.h"
19 
20 #include <QDateTime>
21 
22 milxQtConsole::milxQtConsole(QWidget *theParent) : QTabWidget(theParent)
23 {
24  timestamps = true;
25 
26  dockArea = Qt::BottomDockWidgetArea;
27 
28  dock = new QDockWidget(tr("Console"));
29  dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
30  dock->setWidget(this);
31  dock->setObjectName("Console");
32 
33  logWindow = new QTextEdit;
34  logWindow->clear();
35  logWindow->setReadOnly(true);
36  logWindow->setWindowTitle("Log");
37  logWindow->setTextInteractionFlags(Qt::TextBrowserInteraction);
38  logWindow->ensureCursorVisible();
39 // logWindow->setWindowModified(false);
40  setTab(logWindow);
41  setCurrentWidget(logWindow); //Hierachy deletion
42 
43  createActions();
44 
46 }
47 
49 {
50  //dtor
51 }
52 
53 void milxQtConsole::setTab(QWidget *newWidget)
54 {
55  QTextEdit *editor = qobject_cast<QTextEdit *>(newWidget);
56  if(editor)
57  {
58  connect(editor, SIGNAL(textChanged()), this, SLOT(consoleWasModified()));
59  connect(this, SIGNAL(currentChanged(int)), this, SLOT(consoleSwitched(int)));
60  }
61 
62  addTab(newWidget, newWidget->windowTitle());
63 }
64 
65 void milxQtConsole::consoleMessage(const QString & message)
66 {
67  logWindow->append(QString());
68  logWindow->insertPlainText(message);
69 }
70 
71 void milxQtConsole::consoleHTMLMessage(const QString & message)
72 {
73  logWindow->append(QString());
74  logWindow->insertHtml(message);
75 }
76 
77 void milxQtConsole::printError(QString msg)
78 {
79  QDateTime currentTime = QDateTime::currentDateTime();
80  QString timeStr = "[" + currentTime.toString("ddd-d hh:mm:ss") + "] ";
81 
82  if(!timestamps)
83  timeStr = "";
84 
85  cerr << timeStr.toStdString() << msg.toStdString() << endl;
86  //<font color="red">This is some text!</font>
87  msg.prepend("Error: ");
88  msg.prepend(timeStr);
89  msg.prepend("<font color='red'>");
90  msg.append("</font>");
91  consoleHTMLMessage(msg);
92 }
93 
94 void milxQtConsole::printWarning(QString msg)
95 {
96  QDateTime currentTime = QDateTime::currentDateTime();
97  QString timeStr = "[" + currentTime.toString("ddd-d hh:mm:ss") + "] ";
98 
99  if(!timestamps)
100  timeStr = "";
101 
102  cerr << timeStr.toStdString() << msg.toStdString() << endl;
103  msg.prepend("Warning: ");
104  msg.prepend(timeStr);
105  msg.prepend("<font color='blue'>");
106  msg.append("</font>");
107  consoleHTMLMessage(msg);
108 }
109 
110 void milxQtConsole::printDebug(QString msg)
111 {
112  QDateTime currentTime = QDateTime::currentDateTime();
113  QString timeStr = "[" + currentTime.toString("ddd-d hh:mm:ss") + "] ";
114 
115  if(!timestamps)
116  timeStr = "";
117 
118  cerr << timeStr.toStdString() << msg.toStdString() << endl;
119  msg.prepend("Debug: ");
120  msg.prepend(timeStr);
121  msg.prepend("<font color='orange'>");
122  msg.append("</font>");
123  consoleHTMLMessage(msg);
124 }
125 
126 void milxQtConsole::printInfo(QString msg)
127 {
128  QDateTime currentTime = QDateTime::currentDateTime();
129  QString timeStr = "[" + currentTime.toString("ddd-d hh:mm:ss") + "] ";
130 
131  if(!timestamps)
132  timeStr = "";
133 
134  cout << timeStr.toStdString() << msg.toStdString() << endl;
135  msg.prepend(timeStr);
136  msg.prepend("<font color='black'>");
137  msg.append("</font>");
138  consoleHTMLMessage(msg);
139 }
140 
142 {
143  for(int j = 0; j < count(); j ++)
144  {
145  QTextEdit *editor = qobject_cast<QTextEdit *>(widget(j));
146  if(editor && j != currentIndex())
147  {
148 // editor->setWindowModified(editor->document()->isModified());
149  if(editor->document()->isModified())
150  setTabText(j, editor->windowTitle() + "*");
151  }
152  }
153 }
154 
156 {
157  QTextEdit *editor = qobject_cast<QTextEdit *>(widget(index));
158  if(editor)
159  {
160 // editor->setWindowModified(false);
161  editor->document()->setModified(false);
162  setTabText(index, editor->windowTitle());
163  }
164 }
165 
167 {
168  copyAct = new QAction(this);
169  copyAct->setIcon(QIcon(":/resources/toolbar/copy.png"));
170  copyAct->setText(tr("Console", "Copy", 0));
171  copyAct->setShortcut(tr("Ctrl+c"));
172 
173 // cutAct = new QAction(this);
174 // cutAct->setText(QApplication::translate("Console", "Cut", 0, QApplication::UnicodeUTF8));
175 // cutAct->setShortcut(tr("Ctrl+x"));
176 //
177 // pasteAct = new QAction(this);
178 // pasteAct->setText(QApplication::translate("Console", "Paste", 0, QApplication::UnicodeUTF8));
179 // pasteAct->setShortcut(tr("Ctrl+v"));
180 
181  clearAct = new QAction(this);
182  clearAct->setText(tr("Console", "Clear", 0));
183  clearAct->setShortcut(tr("Crtl+z"));
184 }
185 
187 {
188  //Actions
189  connect(copyAct, SIGNAL(triggered()), logWindow, SLOT(copy()));
190 // connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
191 // connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
192  connect(clearAct, SIGNAL(triggered()), logWindow, SLOT(clear()));
193 }
void consoleWasModified()
Change the window title to show that the tab has been modified.
void printDebug(QString msg)
Debug message wrapper for console.
virtual ~milxQtConsole()
QPointer< QTextEdit > logWindow
Log messages window.
void consoleHTMLMessage(const QString &message)
output message as HTML
void printError(QString msg)
Error message wrapper for console.
void createActions()
Create all the actions for the console.
QAction * copyAct
Copy action for text.
Definition: milxQtConsole.h:99
milxQtConsole(QWidget *theParent=0)
void consoleSwitched(int index)
Update member for when the tab changes.
void consoleMessage(const QString &message)
output message
void printWarning(QString msg)
Warning message wrapper for console.
QAction * clearAct
clear action for text
bool timestamps
Timestamp messages?
Definition: milxQtConsole.h:97
void printInfo(QString msg)
Info message wrapper for console.
void createConnections()
Create connections for the console.
QPointer< QDockWidget > dock
Dock widget.