File indexing completed on 2025-01-19 06:54:50
0001 // 0002 // C++ Implementation: cOutput 0003 // 0004 // Description: the cOutput class 0005 // 0006 /* 0007 Copyright 2005-2011 Tomas Mecir <kmuddy@kmuddy.com> 0008 0009 This program is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU General Public License as 0011 published by the Free Software Foundation; either version 2 of 0012 the License, or (at your option) any later version. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include "coutput.h" 0024 0025 #include "cglobalsettings.h" 0026 #include "cconsole.h" 0027 #include "cprofilesettings.h" 0028 #include "ctextchunk.h" 0029 0030 #include <QRandomGenerator> 0031 #include <QScrollBar> 0032 #include <QVBoxLayout> 0033 0034 #include <KLocalizedString> 0035 0036 cOutput::cOutput (int sess, QWidget *parent) : QWidget(parent), cActionBase ("output", sess) 0037 { 0038 con = new cConsole (this); 0039 QVBoxLayout *layout = new QVBoxLayout (this); 0040 layout->setMargin (0); 0041 layout->addWidget (con); 0042 0043 con->setSession (sess); 0044 0045 echocolor = Qt::yellow; 0046 systemcolor = Qt::cyan; 0047 bgcolor = Qt::black; 0048 0049 setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); 0050 setFocusPolicy (Qt::NoFocus); 0051 0052 // connect cConsole to us ... 0053 connect (con, &cConsole::dimensionsChanged, this, &cOutput::dimensionsChanged); 0054 connect (con, &cConsole::sendCommand, this, &cOutput::sendCommand); 0055 connect (con, &cConsole::promptCommand, this, &cOutput::promptCommand); 0056 0057 // react on events 0058 addEventHandler ("display-line", 20, PT_TEXTCHUNK); 0059 addEventHandler ("display-prompt", 20, PT_TEXTCHUNK); 0060 addEventHandler ("command-sent", 50, PT_STRING); 0061 addEventHandler ("message", 50, PT_STRING); 0062 addEventHandler ("user-message", 50, PT_STRING); 0063 addGlobalEventHandler ("global-settings-changed", 50, PT_NOTHING); 0064 0065 aconsize = 25; 0066 con->setScrollTextSize (aconsize); 0067 } 0068 0069 0070 cOutput::~cOutput () 0071 { 0072 removeEventHandler ("display-line"); 0073 removeEventHandler ("display-prompt"); 0074 removeEventHandler ("display-sent"); 0075 removeEventHandler ("message"); 0076 removeEventHandler ("user-message"); 0077 removeGlobalEventHandler ("global-settings-changed"); 0078 } 0079 0080 void cOutput::eventNothingHandler (QString event, int /*session*/) 0081 { 0082 if (event == "global-settings-changed") { 0083 cGlobalSettings *gs = cGlobalSettings::self(); 0084 con->setHistorySize (gs->getInt ("history-size")); 0085 0086 con->setFont (gs->getFont ("console-font")); 0087 setEchoColor (gs->getColor ("color-" + QString::number (gs->getInt ("echo-color")))); 0088 setSystemColor (gs->getColor ("color-" + QString::number (gs->getInt ("system-color")))); 0089 setEnableEcho (gs->getBool ("command-echo")); 0090 setEnableMessages (gs->getBool ("show-messages")); 0091 con->setEnableBlinking (gs->getBool ("allow-blink")); 0092 con->setIndentation (gs->getInt ("indent")); 0093 0094 //changing font often causes view to move - move to the very bottom 0095 con->verticalScrollBar()->setValue (con->verticalScrollBar()->maximum()); 0096 } 0097 } 0098 0099 void cOutput::eventStringHandler (QString event, int, QString &par1, const QString &) 0100 { 0101 if (event == "command-sent") { 0102 if (cmdecho) 0103 addCommand (par1); 0104 } 0105 else if (event == "message") { 0106 if (messages) //only if messages are enabled 0107 systemMessage (par1); 0108 } 0109 else if (event == "user-message") { 0110 systemMessage (par1); 0111 } 0112 } 0113 0114 void cOutput::eventChunkHandler (QString event, int, cTextChunk *chunk) 0115 { 0116 if (event == "display-line") 0117 addLine (chunk); 0118 if (event == "display-prompt") 0119 addText (chunk); 0120 } 0121 0122 void cOutput::dimensionsChanged (int x, int y) 0123 { 0124 invokeEvent ("dimensions-changed", sess(), x, y); 0125 } 0126 0127 void cOutput::sendCommand (const QString &command) 0128 { 0129 // we send the command directly, with no parsing, because these commands 0130 // are coming from the server, and we want to keep some security ... 0131 invokeEvent ("send-command", sess(), command); 0132 } 0133 0134 void cOutput::promptCommand (const QString &command) 0135 { 0136 callAction ("inputline", "set-text", sess(), command); 0137 } 0138 0139 void cOutput::setDefaultBkColor (QColor color) 0140 { 0141 bgcolor = color; 0142 con->setDefaultBkColor (color); 0143 } 0144 0145 void cOutput::setEchoColor (QColor color) 0146 { 0147 echocolor = color; 0148 } 0149 0150 void cOutput::setSystemColor (QColor color) 0151 { 0152 systemcolor = color; 0153 } 0154 0155 void cOutput::getAllColors (QColor &_echo, QColor &_system) 0156 //this is used by cTranscript class to determine correct colors for command output 0157 { 0158 _echo = echocolor; 0159 _system = systemcolor; 0160 } 0161 0162 void cOutput::setEnableEcho (bool value) 0163 { 0164 cmdecho = value; 0165 } 0166 0167 void cOutput::setEnableMessages (bool value) 0168 { 0169 messages = value; 0170 } 0171 0172 void cOutput::addText (cTextChunk *chunk) 0173 { 0174 cProfileSettings *sett = settings (); 0175 //prompt displayed if enabled in profile prefs, or if it's a quick-connection 0176 if ((!sett) || sett->getBool ("prompt-console")) 0177 { 0178 con->addText (chunk); 0179 invokeEvent ("displayed-prompt", sess(), chunk); 0180 } 0181 } 0182 0183 void cOutput::addLine (cTextChunk *chunk) 0184 { 0185 con->addLine (chunk); 0186 invokeEvent ("displayed-line", sess(), chunk); 0187 } 0188 0189 void cOutput::addCommand (const QString &text) 0190 //addText plus some colouring... 0191 { 0192 //IMPORTANT: the signal emitted here is also captured by session transcript 0193 //so, if you disable cmd echo here, it won't work with session 0194 //transcript!!! 0195 0196 cTextChunk *chunk = cTextChunk::makeLine (text, echocolor, 0197 bgcolor, con); 0198 addLine (chunk); 0199 delete chunk; 0200 } 0201 0202 void cOutput::systemMessage (const QString &text) 0203 //addText plus some colouring... 0204 { 0205 con->forceBeginOfLine (); 0206 cTextChunk *chunk = cTextChunk::makeLine (text, systemcolor, 0207 bgcolor, con); 0208 addLine (chunk); 0209 delete chunk; 0210 } 0211 0212 void cOutput::decisionMessage (const QString &text) 0213 //addText plus some colouring... 0214 { 0215 con->forceBeginOfLine (); 0216 cTextChunk *chunk = cTextChunk::makeLine (text, systemcolor, 0217 bgcolor, con); 0218 addLine (chunk); 0219 delete chunk; 0220 } 0221 0222 void cOutput::makeDecision () 0223 { 0224 QString ss; 0225 //generate a random number in 0..9 range 0226 int which = QRandomGenerator::global()->bounded(0, 10); 0227 switch (which) { 0228 case 0: ss = i18n ("No, no, no!"); break; 0229 case 1: ss = i18n ("I don't agree with it."); break; 0230 case 2: ss = i18n ("Better not."); break; 0231 case 3: ss = i18n ("I'd probably suggest to reject."); break; 0232 case 4: ss = i18n ("Saying no seems a bit better."); break; 0233 case 5: ss = i18n ("Saying yes seems a bit better."); break; 0234 case 6: ss = i18n ("I'd probably suggest to accept."); break; 0235 case 7: ss = i18n ("Sounds good."); break; 0236 case 8: ss = i18n ("I agree with it."); break; 0237 case 9: ss = i18n ("Definitely yes!"); break; 0238 }; 0239 QString s = i18n ("My decision: %1", ss); 0240 //now display that decision 0241 decisionMessage (s); 0242 } 0243 0244 void cOutput::aconUp () 0245 { 0246 //85% is max size 0247 if (aconsize > 80) return; 0248 aconsize += 5; 0249 0250 con->setScrollTextSize (aconsize); 0251 } 0252 0253 void cOutput::aconDown () 0254 { 0255 //5% is min size 0256 if (aconsize < 10) return; 0257 aconsize -= 5; 0258 0259 con->setScrollTextSize (aconsize); 0260 } 0261 0262 #include "moc_coutput.cpp"