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