messagebox.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2014 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13  ***************************************************************************/
14 
15 #include "messagebox.h"
16 #include "xnodeconnector.h"
17 #include <QMainWindow>
18 #include <QApplication>
19 #include <QDesktopWidget>
20 #include <QTimer>
21 
22 #include "ui_messageform.h"
23 
25 static FrmMessage *s_pFrmMessage = 0L;
26 static QTimer *s_timer = 0L;
27 
28 XMessageBox::XMessageBox(QWidget *parent) {
29  s_pFrmMessage = new FrmMessage((QWidget*)0, Qt::Tool | Qt::WindowStaysOnBottomHint);
30  s_pFrmMessage->show();
31 
32  s_pFrmMessage->m_widget->hide();
33 
34  QRect rect = QApplication::desktop()->availableGeometry(s_pFrmMessage);
35  int y = rect.bottom() - s_pFrmMessage->height();
36 #if defined __WIN32__ || defined WINDOWS || defined _WIN32
37  y -= 50; //for taskbar, due to a bug of availableGeometry.
38 #endif
39  s_pFrmMessage->move(0, y);
40 // s_pFrmMessage->m_list->setMouseTracking(true); //for statusTip.
41 
42  {
43  QFont font(s_pFrmMessage->font());
44  font.setPointSize(font.pointSize() * 4 / 5);
45  s_pFrmMessage->m_list->setFont(font);
46  }
47  {
48  QFont font(s_pFrmMessage->font());
49  font.setPointSize(font.pointSize() * 5 / 6);
50  s_pFrmMessage->m_label->setFont(font);
51  }
52 
53  s_timer = new QTimer(this);
54  connect(s_timer, SIGNAL(timeout()), this, SLOT(hide()));
55  s_timer->setSingleShot(true);
56 }
57 
58 QWidget*
59 XMessageBox::form() {
60  return s_pFrmMessage;
61 }
62 void
63 XMessageBox::hide() {
64  s_pFrmMessage->m_widget->hide();
65 }
66 void
67 XMessageBox::post(XString msg, const QIcon &icon, bool popup, int duration_ms, XString tooltip) {
68  if( !msg.length()) {
69  s_pFrmMessage->m_widget->hide();
70  return;
71  }
72  if(popup) {
73  s_pFrmMessage->m_label->setText(msg);
74  s_pFrmMessage->m_label->repaint();
75  s_pFrmMessage->m_btn->setIcon(icon);
76  if(duration_ms) {
77  s_pFrmMessage->m_widget->show();
78  s_pFrmMessage->showNormal();
79  s_pFrmMessage->raise();
80  }
81  else {
82  s_pFrmMessage->m_widget->hide();
83  }
84  s_timer->stop();
85  if(duration_ms > 0) {
86  s_timer->setInterval(duration_ms);
87  s_timer->start();
88  }
89 
90  }
91 
92  msg = XTime::now().getTimeFmtStr("%H:%M:%S ", false) + msg;
93  QListWidgetItem *item;
94  item = new QListWidgetItem(icon, msg, s_pFrmMessage->m_list);
95  item->setToolTip(tooltip);
96  if(s_pFrmMessage->m_list->count() > 100)
97  s_pFrmMessage->m_list->takeItem(0);
98  s_pFrmMessage->m_list->scrollToBottom();
99 }

Generated for KAME4 by  doxygen 1.8.3