/* * multilineinput.h - simple dialog to get MultiLine text from user * Copyright (C) 2005 Maciej Niedzielski * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include "multilineinput.h" #include #include #include #include /* Multiline edit control for MultiLineInputDialog class, introduced to control hotkeys. */ class MultiLineInputDialog::Mle : public QTextEdit { public: Mle(QWidget *parent) : QTextEdit(parent) {} void keyPressEvent(QKeyEvent *e) { if(e->key() == Key_Return && ((e->state() & ControlButton) || (e->state() & AltButton)) ) e->ignore(); else QTextEdit::keyPressEvent(e); } }; /*! \class MultiLineInputDialog multilineinput.h \brief The MultiLineInputDialog class provides a simple dialog to get a multiline string from the user. \mainclass MultiLineInputDialog class is a QInputDialog like class, providing a way to get a multiline string from the user. \code bool ok; QString text = MultiLineInputDialog::getMultiLineText( "New Fortune", "Please enter the new fortune:", true, QString::null, &ok, this); if ( ok && !text.isEmpty() ) { // user entered something and pressed OK } else { // user entered nothing or pressed Cancel } \endcode Pressing Ctrl+Tab will always input tab into the edit field. Ctrl+Enter or Alt+Enter will invoke Ok. \sa QInputDialog */ /*! Constructs the dialog. The \a caption is the text in dialog's caption, the \a label test is information for user what they are expected to enter. The \a parent is the dialog's parent widget. The widget is called \a name. If \a modal is TRUE (the default) the dialog will be modal. \sa getMultiLineText() */ MultiLineInputDialog::MultiLineInputDialog(const QString &caption, const QString &label, bool tabChangesFocus, const QString &text, QWidget *parent, const char *name, bool modal, WFlags f) : QDialog (parent, name, modal, f) { setCaption(caption); QVBoxLayout *vbox = new QVBoxLayout(this, 3); vbox->addWidget(new QLabel(label, this)); mle = new Mle(this); mle->setText(text); mle->setTabChangesFocus(tabChangesFocus); vbox->addWidget(mle); QHBoxLayout *hbox = new QHBoxLayout(6); vbox->addLayout(hbox, AlignRight); QPushButton *ok = new QPushButton(tr("OK"), this, "qt_ok_btn" ); ok->setDefault(TRUE); QPushButton *cancel = new QPushButton(tr("Cancel"), this, "qt_cancel_btn" ); QSize bs = ok->sizeHint().expandedTo(cancel->sizeHint()); ok->setFixedSize(bs); cancel->setFixedSize(bs); hbox->addStretch(); hbox->addWidget(ok); hbox->addWidget(cancel); connect( ok, SIGNAL( clicked() ), this, SLOT( accept() ) ); connect( cancel, SIGNAL( clicked() ), this,SLOT( reject() ) ); QSize sh = sizeHint().expandedTo(QSize(400, 10)); resize(sh.width(),vbox->heightForWidth(sh.width())); } /*! Static function to input multiline text from user. The \a caption is the text in dialog's caption, the \a label test is information for user what they are expected to enter. The \a parent is the dialog's parent widget. The widget is called \a name. If \a tabChangesFocus is TRUE, pressing Tab key while edit area is focused will change focused widget - otherwise it will input tab character. */ QString MultiLineInputDialog::getMultiLineText(const QString &caption, const QString &label, bool tabChangesFocus, const QString &text, bool *ok, QWidget *parent, const char *name) { MultiLineInputDialog *dlg = new MultiLineInputDialog(caption, label, tabChangesFocus, text, parent, name? name : "input_MultiLine_text_dlg", TRUE); QString result; bool isOk = ( dlg->exec() == QDialog::Accepted ); if ( ok ) { *ok = isOk; } if ( isOk ) { result = dlg->mle->text(); } delete dlg; return result; } /*! Keyboard events filter to enable desired hotkeys. */ void MultiLineInputDialog::keyPressEvent(QKeyEvent *e) { if ( e->key() == Key_Tab && e->state() & ControlButton ) mle->insert("\t"); else if ( e->key() == Key_Return && ((e->state() & ControlButton) || (e->state() & AltButton)) ) accept(); else e->ignore(); }