File indexing completed on 2021-12-21 12:50:22

0001 /*
0002  *  ksokoban - a Sokoban game by KDE
0003  *  Copyright (C) 1998  Anders Widell  <awl@hem.passagen.se>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License
0016  *  along with this program; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include <QLabel>
0021 #include <QFont>
0022 #include <QApplication>
0023 #include <QWidgetList>
0024 #include <QString>
0025 #include <QFontDatabase>
0026 
0027 #include "ModalLabel.h"
0028 
0029 ModalLabel::ModalLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
0030   : QLabel(text, parent, f) {
0031   QFont font(QFontDatabase::systemFont(QFontDatabase::GeneralFont).family(), 24, QFont::Bold);
0032   QFontMetrics fontMet(font);
0033   setAutoFillBackground(true);
0034   QString currentLine;
0035   QRect bounds;
0036   int lineLen, width=0, height=0;
0037 
0038   for (int linePos=0; linePos < text.length(); linePos += lineLen+1) {
0039 
0040     lineLen = text.indexOf('\n', linePos);
0041     if (lineLen < 0) lineLen = text.length() - linePos;
0042     else lineLen -= linePos;
0043 
0044     currentLine = text.mid(linePos, lineLen);
0045     bounds = fontMet.boundingRect(currentLine);
0046 
0047     if (bounds.width() > width) width = bounds.width();
0048     height += bounds.height();
0049   }
0050 
0051   width += 32;
0052   height += 32;
0053 
0054   if (width < 300) width = 300;
0055   if (height < 75) height = 75;
0056 
0057   setAlignment (Qt::AlignCenter);
0058   setFrameStyle (QFrame::Panel | QFrame::Raised);
0059   setLineWidth (4);
0060   setFont (font);
0061   move (parent->width ()/2 - width/2, parent->height ()/2 - height/2);
0062   resize (width, height);
0063   show ();
0064 
0065   QWidgetList list = QApplication::allWidgets();
0066   for(QWidgetList::Iterator it=list.begin(); it!=list.end(); it++) {
0067     (*it)->installEventFilter (this);
0068   }
0069   
0070   completed_ = false;
0071   startTimer (1000);
0072 }
0073 
0074 void
0075 ModalLabel::timerEvent (QTimerEvent *) {
0076   completed_ = true;
0077 }
0078 
0079 bool
0080 ModalLabel::eventFilter (QObject *, QEvent *e) {
0081   switch (e->type()) {
0082     case QEvent::MouseButtonPress:
0083     case QEvent::MouseButtonRelease:
0084     case QEvent::MouseButtonDblClick:
0085     case QEvent::MouseMove:
0086     case QEvent::KeyPress:
0087     case QEvent::KeyRelease:
0088       //case QEvent::Accel:
0089       //case QEvent::DragEnter:
0090     case QEvent::DragMove:
0091     case QEvent::DragLeave:
0092     case QEvent::Drop:
0093       //case QEvent::DragResponse:
0094 
0095       //kdDebug << "Ate event" << endl;
0096     return true;
0097   default:
0098     return false;
0099   }
0100 }
0101 
0102 void
0103 ModalLabel::message (const QString &text, QWidget *parent) {
0104   QApplication *app = qApp;
0105   ModalLabel cl (text, parent);
0106 
0107   while (!cl.completed_) app->processEvents ();
0108 }