GUIChatTextInput.cpp

IdĽ do dokumentacji tego pliku.
00001 #include <QEvent>
00002 #include <QKeyEvent>
00003 #include <QMessageBox> 
00004 #include <QTextCharFormat>
00005 #include "GUIChatTextInput.h"
00006 
00007 GUIChatTextInput::GUIChatTextInput (QWidget *parent, QToolBar *tb, QColor defFColor) : QTextEdit (parent)
00008 { 
00009     this->setAutoFormatting(QTextEdit::AutoNone);
00010         this->defFontColor = defFColor;
00011     this->setTextColor(defFontColor);
00012         createTextToolBar(tb);
00013     connect(this, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
00014             this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
00015 
00016     fontChanged(this->font());
00017     colorChanged(defFontColor);
00018 }
00019 
00020 void GUIChatTextInput::setDefFontColor(QColor def)
00021 {
00022         defFontColor = def;
00023         colorChanged(defFontColor);
00024 }
00025 
00026 
00027 
00028 
00029 
00030 bool GUIChatTextInput::event (QEvent * e)
00031 {
00032         if (e->type () == QEvent::KeyPress)
00033         {
00034                 QKeyEvent *key_event = (QKeyEvent*) e;
00035                 if (key_event->modifiers () == Qt::NoModifier && 
00036            (key_event->key () == Qt::Key_Enter || key_event->key () == Qt::Key_Return))
00037                 {
00038                         emit sendRequest ();
00039                         e->ignore ();
00040                         return false;
00041                 }
00042         }
00043         return QTextEdit::event (e);
00044 }
00045 
00046 void GUIChatTextInput::keyPressEvent( QKeyEvent *e )
00047 { 
00048         if(isEmpty() && (e->key() == Qt::Key_Left || e->key() == Qt::Key_Right || e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))
00049                 this->ensureCursorVisible ();
00050         else
00051                 QTextEdit::keyPressEvent (e);           
00052 }
00053 
00054 void GUIChatTextInput::mousePressEvent( QMouseEvent *e )
00055 { 
00056         this->ensureCursorVisible ();
00057         if(!isEmpty())
00058                 QTextEdit::mousePressEvent (e);
00059 }
00060 
00061 void GUIChatTextInput::createTextToolBar(QToolBar *tb)
00062 {
00063         //kroj czcionki
00064         comboFont = new QComboBox(tb);
00065     tb->addWidget(comboFont);
00066     comboFont->setEditable(false);
00067     QFontDatabase db;
00068     comboFont->addItems(db.families());
00069 
00070     connect(comboFont, SIGNAL(activated(const QString &)),
00071             this, SLOT(textFamily(const QString &)));
00072     comboFont->setCurrentIndex(comboFont->findText(QApplication::font().family()));
00073 
00074         //wielkosc czcioki
00075     comboSize = new QComboBox(tb);
00076     comboSize->setObjectName("comboSize");
00077     tb->addWidget(comboSize);
00078     comboSize->setEditable(false);
00079 
00080     foreach(int size, db.standardSizes())
00081                 comboSize->addItem(QString::number(size));
00082 
00083     connect(comboSize, SIGNAL(activated(const QString &)),
00084             this, SLOT(textSize(const QString &)));
00085 
00086     comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font().pointSize())));                                                            
00087         tb->addSeparator();
00088 
00089         //pogrubiona
00090         actionTextBold = new QAction(QIcon("icon/TextToolBar/textbold.png"), tr("Pogrubienie"),this);
00091     actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
00092     QFont bold;
00093         bold.setBold(true);
00094     actionTextBold->setFont(bold);
00095     connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
00096     tb->addAction(actionTextBold);
00097     actionTextBold->setCheckable(true);
00098 
00099         //pochylona
00100     actionTextItalic = new QAction(QIcon("icon/TextToolBar/textitalic.png"), tr("Kursywa"),tb);
00101     actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
00102     QFont italic;
00103     italic.setItalic(true);
00104     actionTextItalic->setFont(italic);
00105 
00106     connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
00107     tb->addAction(actionTextItalic);
00108     actionTextItalic->setCheckable(true);
00109 
00110         //podkreslona
00111     actionTextUnderline = new QAction(QIcon("icon/TextToolBar/textunder.png"), tr("Podkreślenie"),tb);
00112     actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
00113     QFont underline;
00114     underline.setUnderline(true);
00115     actionTextUnderline->setFont(underline);
00116     connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
00117     tb->addAction(actionTextUnderline);
00118     actionTextUnderline->setCheckable(true);
00119 
00120         //kolor czcionki
00121     QPixmap pix(32, 32);
00122     pix.fill(defFontColor);
00123     actionTextColor = new QAction(pix, tr("&Kolor..."), this);
00124     connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
00125     tb->addAction(actionTextColor);
00126 }
00127 
00128 void GUIChatTextInput::textBold()
00129 {
00130     this->setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
00131     this->ensureCursorVisible ();
00132 }
00133 
00134 void GUIChatTextInput::textUnderline()
00135 {
00136     this->setFontUnderline(actionTextUnderline->isChecked());
00137     this->ensureCursorVisible ();
00138 }
00139 
00140 void GUIChatTextInput::textItalic()
00141 {
00142     this->setFontItalic(actionTextItalic->isChecked());
00143     this->ensureCursorVisible ();
00144 }
00145 
00146 void GUIChatTextInput::textColor()
00147 {
00148         QColor col = QColorDialog::getColor(defFontColor,this);
00149     if (!col.isValid())
00150         return;
00151     this->setTextColor(col);
00152     colorChanged(col);
00153     this->ensureCursorVisible ();
00154 }
00155 
00156 void GUIChatTextInput::colorChanged(const QColor &c)
00157 {
00158     QPixmap pix(32, 32);
00159     pix.fill(c);
00160     actionTextColor->setIcon(pix);
00161     this->ensureCursorVisible ();
00162 }
00163 
00164 void GUIChatTextInput::fontChanged(const QFont &f)
00165 {
00166     comboFont->setCurrentIndex(comboFont->findText(f.family()));
00167     comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
00168     actionTextBold->setChecked(f.bold());
00169     actionTextItalic->setChecked(f.italic());
00170     actionTextUnderline->setChecked(f.underline());
00171     this->ensureCursorVisible ();
00172 }
00173 
00174 void GUIChatTextInput::currentCharFormatChanged(const QTextCharFormat &format)
00175 {
00176                 fontChanged(format.font());
00177         colorChanged(format.foreground().color());
00178         this->ensureCursorVisible ();
00179 }
00180 
00181 void GUIChatTextInput::textFamily(const QString &f)
00182 {
00183     this->setFontFamily(f);
00184     this->ensureCursorVisible ();
00185 }
00186 
00187 void GUIChatTextInput::textSize(const QString &p)
00188 {
00189     this->setFontPointSize(p.toFloat());
00190     this->ensureCursorVisible ();
00191 }
00192 
00193 bool GUIChatTextInput::isEmpty()
00194 {
00195         QString input = this->toPlainText().replace(" ","");
00196         if(input.isEmpty())
00197                 return true;
00198         else 
00199                 return false;
00200 }

Wygenerowano Sun Jun 11 12:55:08 2006 dla lanChat programem  doxygen 1.4.6