File indexing completed on 2024-03-24 15:43:26

0001 //
0002 // C++ Implementation: cmultiinputline
0003 //
0004 // Description: 
0005 //
0006 /*
0007 Copyright 2004-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 "cmultiinputline.h"
0024 
0025 #include "cglobalsettings.h"
0026 #include "ccmdqueue.h"
0027 #include "ccmdqueues.h"
0028 
0029 #include <QKeyEvent>
0030 #include <QFontDatabase>
0031 #include <QTextBlock>
0032 
0033 //maximum number of lines ...
0034 #define MAXLINES 10
0035 
0036 cMultiInputLine::cMultiInputLine (int sess, QWidget *parent)
0037  : QTextEdit(parent), cActionBase ("multiinputline", sess)
0038 {
0039   setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Fixed);
0040 
0041   setAcceptRichText (false);
0042   setWordWrapMode (QTextOption::WrapAtWordBoundaryOrAnywhere);
0043 
0044   //height: 2 lines
0045   setLinesHeight (2);
0046 
0047   connect (this, &cMultiInputLine::textChanged, this, &cMultiInputLine::updateHeight);
0048 
0049   keeptext = true;
0050   selectkepttext = true;
0051   swapenters = false;
0052 
0053   addGlobalEventHandler ("global-settings-changed", 50, PT_NOTHING);
0054 }
0055 
0056 
0057 cMultiInputLine::~cMultiInputLine()
0058 {
0059   removeGlobalEventHandler ("global-settings-changed");
0060 }
0061 
0062 void cMultiInputLine::eventNothingHandler (QString event, int)
0063 {
0064   if (event == "global-settings-changed") {
0065     cGlobalSettings *gs = cGlobalSettings::self();
0066 
0067     setMyFont (gs->getFont ("input-font"));
0068     keepText (gs->getBool ("keep-text"));
0069     selectKeptText (gs->getBool ("keep-text") ? gs->getBool ("select-kept") : false);
0070     swapEnters (gs->getBool ("swap-enters"));
0071     QPalette p = palette ();
0072     p.setColor (QPalette::Base, gs->getColor("color-" + QString::number (gs->getInt ("input-bg-color"))));
0073     p.setColor (QPalette::Text, gs->getColor("color-" + QString::number (gs->getInt ("input-fg-color"))));
0074     setPalette (p);
0075   }
0076 }
0077 
0078 void cMultiInputLine::initialize ()
0079 {
0080   //change colors
0081   // TODO: full color support
0082   cGlobalSettings *gs = cGlobalSettings::self();
0083   QPalette p = palette ();
0084   p.setColor (QPalette::Base, gs->getColor ("color-0"));  // black
0085   p.setColor (QPalette::Text, gs->getColor ("color-14"));  // yellow
0086   setPalette (p);
0087 
0088   //scroll-bars
0089   setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
0090   setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
0091   
0092   //change font
0093   setMyFont (QFontDatabase::systemFont (QFontDatabase::FixedFont)); //default system fixed font
0094 }
0095 
0096 void cMultiInputLine::setMyFont (const QFont &font)
0097 {
0098   setCurrentFont (font);
0099   setLinesHeight (_lines);
0100 }
0101 
0102 void cMultiInputLine::keepText (bool value)
0103 {
0104   keeptext = value;
0105 }
0106 
0107 void cMultiInputLine::selectKeptText (bool value)
0108 {
0109   selectkepttext = value;
0110 }
0111 
0112 void cMultiInputLine::swapEnters (bool value)
0113 {
0114   swapenters = value;
0115 }
0116 
0117 void cMultiInputLine::setLinesHeight (int lines)
0118 {
0119   if (lines < 2) lines = 2;
0120   if (lines > MAXLINES) lines = MAXLINES;
0121   _lines = lines;
0122   QFontMetrics fm (font());
0123   //one pixel space between lines, also one pixel spaces at the top/bottom
0124   int newheight = lines * (fm.height() + 1) + 2;  
0125   int frameheight = rect().height() - contentsRect().height();
0126   setFixedHeight (frameheight + newheight + 1);
0127 }
0128 
0129 void cMultiInputLine::updateHeight()
0130 {
0131   setLinesHeight (document()->lineCount());
0132 }
0133 
0134 void cMultiInputLine::sendCommands ()
0135 {
0136   cCmdQueues *queues = (cCmdQueues *) object ("cmdqueues");
0137   if (!queues) return;
0138   // create a command queue with all the commands
0139   cCmdQueue *queue = new cCmdQueue (sess());
0140   for (QTextBlock block = document()->begin(); block.isValid(); block = block.next())
0141     queue->addCommand (block.text());
0142   queues->addQueue (queue);
0143 
0144   //delete text if not needed
0145   if (!keeptext)
0146     setText ("");
0147   //select everything if needed
0148   if (selectkepttext)
0149     selectAll ();
0150 }
0151 
0152 void cMultiInputLine::keyPressEvent (QKeyEvent *e)
0153 {
0154   bool ctrl = false;
0155   if (e->modifiers() & Qt::ControlModifier)
0156     ctrl = true;
0157   if ((e->key() == Qt::Key_Return) || (e->key() == Qt::Key_Enter))
0158   {
0159     //ENTER pressed, handle it !
0160     //we send sommands on plain ENTER, or on CTRL+ENTER if we have to swap them ...
0161     if (ctrl == swapenters)
0162       sendCommands ();
0163     else
0164     {
0165       // need to make a new QKeyEvent, as we need to drop the ctrl modifier
0166       QKeyEvent *enter = new QKeyEvent (e->type(), e->key(), Qt::NoModifier, QString());
0167       //standard behaviour otherwise
0168       QTextEdit::keyPressEvent (enter);
0169       delete enter;
0170     }
0171   }
0172   else
0173     QTextEdit::keyPressEvent (e);
0174 }
0175 
0176 #include "moc_cmultiinputline.cpp"