Source

ksokoban / ModalLabel.cpp

/*
 *  ksokoban - a Sokoban game for KDE
 *  Copyright (C) 1998  Anders Widell  <awl@hem.passagen.se>
 *
 *  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 program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <QLabel>
#include <QFont>
#include <KApplication>
#include <KGlobalSettings>
#include <QWidgetList>
#include <QString>

#include "ModalLabel.h"

#include "ModalLabel.moc"

ModalLabel::ModalLabel(const QString &text, QWidget *parent, Qt::WFlags f)
  : QLabel(text, parent, f) {
  QFont font(KGlobalSettings::generalFont().family(), 24, QFont::Bold);
  QFontMetrics fontMet(font);
  setAutoFillBackground(true);
  QString currentLine;
  QRect bounds;
  int lineLen, width=0, height=0;

  for (int linePos=0; linePos < (int) text.length(); linePos += lineLen+1) {

    lineLen = text.indexOf('\n', linePos);
    if (lineLen < 0) lineLen = text.length() - linePos;
    else lineLen -= linePos;

    currentLine = text.mid(linePos, lineLen);
    bounds = fontMet.boundingRect(currentLine);

    if (bounds.width() > width) width = bounds.width();
    height += bounds.height();
  }

  width += 32;
  height += 32;

  if (width < 300) width = 300;
  if (height < 75) height = 75;

  setAlignment (Qt::AlignCenter);
  setFrameStyle (QFrame::Panel | QFrame::Raised);
  setLineWidth (4);
  setFont (font);
  move (parent->width ()/2 - width/2, parent->height ()/2 - height/2);
  resize (width, height);
  show ();

  QWidgetList list = QApplication::allWidgets();
  for(QWidgetList::Iterator it=list.begin(); it!=list.end(); it++) {
	(*it)->installEventFilter (this);
  }
  
  completed_ = false;
  startTimer (1000);
}

void
ModalLabel::timerEvent (QTimerEvent *) {
  completed_ = true;
}

bool
ModalLabel::eventFilter (QObject *, QEvent *e) {
  switch (e->type()) {
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::MouseButtonDblClick:
    case QEvent::MouseMove:
    case QEvent::KeyPress:
    case QEvent::KeyRelease:
      //case QEvent::Accel:
      //case QEvent::DragEnter:
    case QEvent::DragMove:
    case QEvent::DragLeave:
    case QEvent::Drop:
      //case QEvent::DragResponse:

      //kdDebug << "Ate event" << endl;
    return true;
    break;
  default:
    return false;
  }
}

void
ModalLabel::message (const QString &text, QWidget *parent) {
  KApplication *app = KApplication::kApplication ();
  ModalLabel cl (text, parent);

  while (!cl.completed_) app->processEvents ();
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.