LogTextEdit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/qt/widgets/ceditor/LogTextEdit.h
24 
25  \brief A TextEdit that can be used to watch a log file.
26 */
27 
28 // TerraLib
29 #include "LogTextEdit.h"
30 
31 // STL
32 #include <functional>
33 
34 // QT
35 #include <QColor>
36 #include <QFile>
37 #include <QTextStream>
38 #if(QT_VERSION >= 0x050000)
39 #include <QStandardPaths>
40 #else
41 #include <QDesktopServices>
42 #endif
43 
45  const QString &fileName)
46  : QPlainTextEdit(parent), m_watcher(nullptr)
47 {
48  setReadOnly(true);
49  setTextInteractionFlags(Qt::TextSelectableByMouse |
50  Qt::TextSelectableByKeyboard);
51  setLineWrapMode(QPlainTextEdit::NoWrap);
52  QString tempName;
53  tempName = fileName;
54 
55  if (tempName.isEmpty())
56  {
57  QString userDataDir;
58 #if(QT_VERSION >= 0x050000)
59  userDataDir =
60  QStandardPaths::writableLocation(QStandardPaths::DataLocation);
61 #else
62  userDataDir =
63  QDesktopServices::storageLocation(QDesktopServices::DataLocation);
64 #endif
65  tempName = userDataDir + "/log/TerraLib.log";
66  }
67 
69  tempName.toUtf8().data(),
70  std::bind(&te::qt::widgets::LogTextEdit::textReady, this)));
71 
72  m_watcher->watch();
73 
74  m_file.setFileName(tempName);
75  QTextStream ifile(&m_file);
76 
77  m_file.open(QFile::ReadOnly);
78 
79  setPlainText(ifile.readAll());
80 
81  m_pos = ifile.pos();
82 
83  moveCursor(QTextCursor::End);
84 
85  connect(this, SIGNAL(textPending()), this, SLOT(read()));
86 }
87 
89 {
90  m_watcher->unwatch();
91  m_file.close();
92 }
93 
95 {
96  QTextStream ifile(&m_file);
97 
98  ifile.seek(m_pos);
99 
100  moveCursor(QTextCursor::End);
101 
102  while (!ifile.atEnd())
103  {
104  insertPlainText(ifile.readLine());
105  insertPlainText("\n");
106  }
107 
108  m_pos = ifile.pos();
109 }
110 
112 {
113  emit textPending();
114 }
115 
117 {
118  QList<QTextEdit::ExtraSelection> matches;
119 
120  moveCursor(QTextCursor::Start);
121 
122  auto bgColor = QColor(Qt::gray).lighter(135);
123 
124  while(find(str))
125  {
126  QTextEdit::ExtraSelection extra;
127 
128  extra.format.setBackground(bgColor);
129 
130  extra.cursor = textCursor();
131 
132  matches.append(extra);
133  }
134 
135  setExtraSelections(matches);
136 
137  moveCursor(QTextCursor::End);
138 }
std::unique_ptr< te::core::FileWatcher > m_watcher
Definition: LogTextEdit.h:67
void findAll(const QString &str)
LogTextEdit(QWidget *parent=0, const QString &fileName="")
Definition: LogTextEdit.cpp:44