Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / genericgui / LogViewer.cxx
1 // Copyright (C) 2006-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "LogViewer.hxx"
21 #include <fstream>
22 #include <sstream>
23
24 using namespace std;
25
26 LogViewer::LogViewer(std::string label, QWidget *parent)
27 {
28   setupUi(this);
29   _label = label;
30   browser->setLineWrapMode(QTextEdit::NoWrap);
31   browser->setReadOnly(1);
32   connect(pushButton,SIGNAL(clicked()),this, SLOT(close()));
33 }
34
35 LogViewer::~LogViewer()
36 {
37 }
38
39 void LogViewer::setText(std::string text)
40 {
41   string atext = _label + "\n\n" + text;
42   browser->setText(atext.c_str());
43 }
44
45 void LogViewer::readFile(std::string fileName)
46 {
47   std::ifstream f(fileName.c_str());
48   std::stringstream hfile;
49   hfile << f.rdbuf();
50   string atext = _label + "\n" + fileName + "\n\n";
51   QString qtext = atext.c_str();
52   browser->setText(qtext + hfile.str().c_str());
53   f.close();
54 }
55
56 ContainerLogViewer::ContainerLogViewer(std::string label, QWidget *parent):LogViewer(label,parent)
57 {
58   gridLayout->removeWidget(browser);
59   gridLayout->addWidget(browser, 0, 0, 1, 3);
60
61   QPushButton* next = new QPushButton("next execution",this);
62   gridLayout->addWidget(next, 1, 1, 1, 1);
63   connect(next,SIGNAL(clicked()),this, SLOT(onNext()));
64   QPushButton* previous = new QPushButton("previous execution",this);
65   gridLayout->addWidget(previous, 1, 2, 1, 1);
66   connect(previous,SIGNAL(clicked()),this, SLOT(onPrevious()));
67 }
68
69 void ContainerLogViewer::readFile(std::string fileName)
70 {
71   LogViewer::readFile(fileName);
72   //move cursor to the end
73   QTextCursor cursor=browser->textCursor();
74   cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
75   browser->setTextCursor(cursor);
76 }
77
78 //go to next begin execution schema
79 void ContainerLogViewer::onNext()
80 {
81   QTextCursor orig = browser->textCursor();
82   QTextCursor find_result = browser->document()->find("**************************Begin schema execution", orig);
83
84   if(find_result.isNull())
85     {
86       //Try to find from the beginning
87       find_result = browser->document()->find("**************************Begin schema execution",0);
88     }
89
90   if(!find_result.isNull())
91     browser->setTextCursor(find_result);
92 }
93
94 //go to previous begin execution schema
95 void ContainerLogViewer::onPrevious()
96 {
97   QTextCursor orig = browser->textCursor();
98   QTextCursor find_result = browser->document()->find("**************************Begin schema execution",
99                                                       orig, QTextDocument::FindBackward);
100   if(find_result.isNull())
101     {
102       //Try to find from the end
103       find_result = browser->document()->find("**************************Begin schema execution",
104                                               browser->document()->characterCount()-1, QTextDocument::FindBackward);
105     }
106
107   if(!find_result.isNull())
108     browser->setTextCursor(find_result);
109 }