source: PSPA/Interface_Web/trunk/pspaWT/sources/userInterface/src/GWt_console.cc @ 411

Last change on this file since 411 was 411, checked in by garnier, 11 years ago

ameliorations graphiques et ajout d'une fonction search dans l'output

File size: 4.1 KB
Line 
1#include "GWt_console.h"
2
3#include <Wt/WApplication>
4#include <Wt/WText>
5#include <Wt/WBorder>
6#include <Wt/WLayout>
7#include <Wt/WVBoxLayout>
8#include <Wt/WPanel>
9#include <Wt/WLabel>
10#include <Wt/WLineEdit>
11#include <Wt/WScrollArea>
12#include <Wt/Utils>
13
14
15GWt_console::GWt_console()
16:WContainerWidget()
17{
18    setObjectName("console");
19
20
21    // la ligne de search
22    Wt::WContainerWidget *searchContainer = new Wt::WContainerWidget();
23   
24    Wt::WLabel *label = new Wt::WLabel("Search ", searchContainer);
25    searchLineEdit_ = new Wt::WLineEdit("", searchContainer);
26    searchLineEdit_->setToolTip("clic here to search a string into the output");
27   
28    // le panel
29    WPanel *panelConsole = new WPanel(this);
30    panelConsole->setTitle(" output");
31
32    WScrollArea* scrollContainer = new  WScrollArea();
33   
34
35    outputCurrent_ = new WText(this);
36    outputCurrent_->setTextFormat(Wt::XHTMLText);
37    outputCurrent_->setInline(false);
38    outputCurrent_->setMaximumSize(750,400);
39
40    scrollContainer->setWidget(outputCurrent_);
41    scrollContainer->setStyleClass("scrollConsole");
42
43    Wt::WContainerWidget *panelContainer = new Wt::WContainerWidget();
44    Wt::WVBoxLayout* vContainerLayout = new Wt::WVBoxLayout();
45
46    vContainerLayout->addWidget(searchContainer);
47    vContainerLayout->addWidget(scrollContainer);
48
49
50    panelContainer->setLayout(vContainerLayout);
51    panelConsole->setCentralWidget(panelContainer);
52
53    // signals
54    searchLineEdit_->keyWentUp ().connect(this, &GWt_console::searchFilter);
55}
56
57
58GWt_console::~GWt_console()
59{
60}
61
62
63void GWt_console::searchFilter() {
64
65    std::string br = "<br />";
66   
67    std::string txt = searchLineEdit_->text().toUTF8 ();
68    if (searchLineEdit_->text() == "") {
69        outputCurrent_->setText(outputOriginal_);
70        outputCurrent_->setTextFormat(Wt::XHTMLText);
71        return;
72    }
73   
74    std::string outputStr = outputOriginal_.toUTF8();
75    std::string outputFinal = "";
76   
77    // look for lines that contains the seacrh string
78    bool end = false;
79    int nextPos = 0;
80    int nextPosTxt = 0;
81    int lineNumber = 0;
82    std::string beginStr;
83    std::string endStr;
84   
85    // loop on all lines
86    while (end == false) {
87        lineNumber++;
88       
89        nextPos = outputStr.find(br);
90       
91        // if this line contains the token
92        if (nextPos != std::string::npos) {
93            beginStr = outputStr.substr(0,nextPos);
94            endStr = outputStr.substr(nextPos+br.size());
95            outputStr = endStr;
96
97
98            // colorize for all token
99            bool lineEnd = false;
100            std::string colorizeTxt = "";
101            while (lineEnd == false) {
102                nextPosTxt = beginStr.find(txt);
103               
104                if (nextPosTxt != std::string::npos) {
105                    colorizeTxt += beginStr.substr(0,nextPosTxt)+
106                    std::string("<b><font style='background-color:yellow;'>")+
107                    txt+
108                    std::string("</font></b>");
109                    beginStr = beginStr.substr(nextPosTxt+txt.size());
110                   
111                } else if (colorizeTxt != "") {
112                    colorizeTxt += beginStr;
113                    lineEnd = true;
114                } else {
115                    lineEnd = true;
116                }
117            }
118            if (colorizeTxt != "") {
119                std::stringstream ss;
120                ss << lineNumber;
121                outputFinal += std::string("<font color='gray'>line ")+ss.str()+" : </font>"+colorizeTxt+br;
122            }
123        } else {
124            end = true;
125        }
126    }
127   
128    outputCurrent_->setText(outputFinal);
129    outputCurrent_->setTextFormat(Wt::XHTMLText);
130
131}
132
133
134void GWt_console::addConsoleMessage(WString msg) {
135   
136    // changes specials caracters
137    outputOriginal_ += Wt::Utils::htmlEncode (msg.toUTF8(),  Wt::Utils::EncodeNewLines);
138
139    // apply filter, will modify outputCurrent
140    searchFilter();
141    /*
142     * Little javascript trick to make sure we scroll along with new content
143     */
144    wApp->doJavaScript(outputCurrent_->jsRef() + ".scrollTop += "
145                       + outputCurrent_->jsRef() + ".scrollHeight;");
146   
147}
148
Note: See TracBrowser for help on using the repository browser.