[242] | 1 | #include "GWt_console.h" |
---|
[226] | 2 | |
---|
| 3 | #include <Wt/WApplication> |
---|
| 4 | #include <Wt/WText> |
---|
[401] | 5 | #include <Wt/WBorder> |
---|
| 6 | #include <Wt/WLayout> |
---|
[411] | 7 | #include <Wt/WVBoxLayout> |
---|
[401] | 8 | #include <Wt/WPanel> |
---|
[411] | 9 | #include <Wt/WLabel> |
---|
| 10 | #include <Wt/WLineEdit> |
---|
[401] | 11 | #include <Wt/WScrollArea> |
---|
[411] | 12 | #include <Wt/Utils> |
---|
[226] | 13 | |
---|
[411] | 14 | |
---|
[240] | 15 | GWt_console::GWt_console() |
---|
| 16 | :WContainerWidget() |
---|
| 17 | { |
---|
[272] | 18 | setObjectName("console"); |
---|
[401] | 19 | |
---|
[411] | 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); |
---|
[417] | 26 | searchLineEdit_->setToolTip("clic here to search a string in the output"); |
---|
[411] | 27 | |
---|
[401] | 28 | // le panel |
---|
| 29 | WPanel *panelConsole = new WPanel(this); |
---|
| 30 | panelConsole->setTitle(" output"); |
---|
| 31 | |
---|
[411] | 32 | WScrollArea* scrollContainer = new WScrollArea(); |
---|
| 33 | |
---|
[401] | 34 | |
---|
[411] | 35 | outputCurrent_ = new WText(this); |
---|
| 36 | outputCurrent_->setTextFormat(Wt::XHTMLText); |
---|
| 37 | outputCurrent_->setInline(false); |
---|
| 38 | outputCurrent_->setMaximumSize(750,400); |
---|
[401] | 39 | |
---|
[411] | 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); |
---|
[240] | 55 | } |
---|
[227] | 56 | |
---|
[240] | 57 | |
---|
| 58 | GWt_console::~GWt_console() |
---|
| 59 | { |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
[411] | 63 | void 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 | |
---|
[240] | 134 | void GWt_console::addConsoleMessage(WString msg) { |
---|
[272] | 135 | |
---|
[411] | 136 | // changes specials caracters |
---|
| 137 | outputOriginal_ += Wt::Utils::htmlEncode (msg.toUTF8(), Wt::Utils::EncodeNewLines); |
---|
| 138 | |
---|
| 139 | // apply filter, will modify outputCurrent |
---|
| 140 | searchFilter(); |
---|
[247] | 141 | /* |
---|
[272] | 142 | * Little javascript trick to make sure we scroll along with new content |
---|
| 143 | */ |
---|
[411] | 144 | wApp->doJavaScript(outputCurrent_->jsRef() + ".scrollTop += " |
---|
| 145 | + outputCurrent_->jsRef() + ".scrollHeight;"); |
---|
[272] | 146 | |
---|
[226] | 147 | } |
---|
[240] | 148 | |
---|